data_driven_test.cc revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/autofill/data_driven_test.h"
6
7#include "base/file_util.h"
8#include "base/path_service.h"
9#include "base/string_util.h"
10#include "chrome/common/chrome_paths.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13namespace {
14
15// Reads |file| into |content|, and converts Windows line-endings to Unix ones.
16// Returns true on success.
17bool ReadFile(const FilePath& file, std::string* content) {
18  if (!file_util::ReadFileToString(file, content))
19    return false;
20
21  ReplaceSubstringsAfterOffset(content, 0, "\r\n", "\n");
22  return true;
23}
24
25// Write |content| to |file|. Returns true on success.
26bool WriteFile(const FilePath& file, const std::string& content) {
27  int write_size = file_util::WriteFile(file, content.c_str(),
28                                        content.length());
29  return write_size == static_cast<int>(content.length());
30}
31
32}  // namespace
33
34void DataDrivenTest::RunDataDrivenTest(
35    const FilePath& input_directory,
36    const FilePath& output_directory,
37    const FilePath::StringType& file_name_pattern) {
38  file_util::FileEnumerator input_files(input_directory,
39                                        false,
40                                        file_util::FileEnumerator::FILES,
41                                        file_name_pattern);
42
43  for (FilePath input_file = input_files.Next();
44       !input_file.empty();
45       input_file = input_files.Next()) {
46    std::string input;
47    ASSERT_TRUE(ReadFile(input_file, &input));
48
49    std::string output;
50    GenerateResults(input, &output);
51
52    FilePath output_file = output_directory.Append(
53        input_file.BaseName().StripTrailingSeparators().ReplaceExtension(
54            FILE_PATH_LITERAL(".out")));
55
56    std::string output_file_contents;
57    if (ReadFile(output_file, &output_file_contents))
58      EXPECT_EQ(output_file_contents, output);
59    else
60      ASSERT_TRUE(WriteFile(output_file, output));
61  }
62}
63
64FilePath DataDrivenTest::GetInputDirectory(
65    const FilePath::StringType& test_name) {
66  FilePath test_data_dir_;
67  PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
68  test_data_dir_ = test_data_dir_.AppendASCII("autofill")
69                                 .Append(test_name)
70                                 .AppendASCII("input");
71  return test_data_dir_;
72}
73
74FilePath DataDrivenTest::GetOutputDirectory(
75    const FilePath::StringType& test_name) {
76  FilePath test_data_dir_;
77  PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
78  test_data_dir_ = test_data_dir_.AppendASCII("autofill")
79                                 .Append(test_name)
80                                 .AppendASCII("output");
81  return test_data_dir_;
82}
83
84DataDrivenTest::DataDrivenTest() {
85}
86
87DataDrivenTest::~DataDrivenTest() {
88}
89