blob: 7717cf7cd751b3ee44380f3d311633072463f5a6 [file] [log] [blame]
[email protected]7173edd02011-02-09 00:31:571// 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
[email protected]e4b2fa32013-03-09 22:56:565#include "components/autofill/browser/data_driven_test.h"
[email protected]7173edd02011-02-09 00:31:576
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
[email protected]e217c5632013-04-12 19:11:4813namespace autofill {
[email protected]7173edd02011-02-09 00:31:5714namespace {
15
16// Reads |file| into |content|, and converts Windows line-endings to Unix ones.
17// Returns true on success.
[email protected]650b2d52013-02-10 03:41:4518bool ReadFile(const base::FilePath& file, std::string* content) {
[email protected]7173edd02011-02-09 00:31:5719 if (!file_util::ReadFileToString(file, content))
20 return false;
21
22 ReplaceSubstringsAfterOffset(content, 0, "\r\n", "\n");
23 return true;
24}
25
26// Write |content| to |file|. Returns true on success.
[email protected]650b2d52013-02-10 03:41:4527bool WriteFile(const base::FilePath& file, const std::string& content) {
[email protected]7173edd02011-02-09 00:31:5728 int write_size = file_util::WriteFile(file, content.c_str(),
29 content.length());
30 return write_size == static_cast<int>(content.length());
31}
32
33} // namespace
34
35void DataDrivenTest::RunDataDrivenTest(
[email protected]650b2d52013-02-10 03:41:4536 const base::FilePath& input_directory,
37 const base::FilePath& output_directory,
38 const base::FilePath::StringType& file_name_pattern) {
[email protected]7173edd02011-02-09 00:31:5739 file_util::FileEnumerator input_files(input_directory,
40 false,
41 file_util::FileEnumerator::FILES,
42 file_name_pattern);
43
[email protected]650b2d52013-02-10 03:41:4544 for (base::FilePath input_file = input_files.Next();
[email protected]7173edd02011-02-09 00:31:5745 !input_file.empty();
46 input_file = input_files.Next()) {
[email protected]1ea35dc32011-03-27 11:42:1647 SCOPED_TRACE(input_file.BaseName().value());
48
[email protected]7173edd02011-02-09 00:31:5749 std::string input;
50 ASSERT_TRUE(ReadFile(input_file, &input));
51
52 std::string output;
53 GenerateResults(input, &output);
54
[email protected]650b2d52013-02-10 03:41:4555 base::FilePath output_file = output_directory.Append(
[email protected]7173edd02011-02-09 00:31:5756 input_file.BaseName().StripTrailingSeparators().ReplaceExtension(
57 FILE_PATH_LITERAL(".out")));
58
59 std::string output_file_contents;
60 if (ReadFile(output_file, &output_file_contents))
61 EXPECT_EQ(output_file_contents, output);
62 else
63 ASSERT_TRUE(WriteFile(output_file, output));
64 }
65}
66
[email protected]650b2d52013-02-10 03:41:4567base::FilePath DataDrivenTest::GetInputDirectory(
68 const base::FilePath::StringType& test_name) {
69 base::FilePath test_data_dir_;
[email protected]7173edd02011-02-09 00:31:5770 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
71 test_data_dir_ = test_data_dir_.AppendASCII("autofill")
72 .Append(test_name)
73 .AppendASCII("input");
74 return test_data_dir_;
75}
76
[email protected]650b2d52013-02-10 03:41:4577base::FilePath DataDrivenTest::GetOutputDirectory(
78 const base::FilePath::StringType& test_name) {
79 base::FilePath test_data_dir_;
[email protected]7173edd02011-02-09 00:31:5780 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_);
81 test_data_dir_ = test_data_dir_.AppendASCII("autofill")
82 .Append(test_name)
83 .AppendASCII("output");
84 return test_data_dir_;
85}
86
87DataDrivenTest::DataDrivenTest() {
88}
89
90DataDrivenTest::~DataDrivenTest() {
91}
[email protected]e217c5632013-04-12 19:11:4892
93} // namespace autofill