blob: 4b1ddb456330e67b92ad73939520ef1011658667 [file] [log] [blame]
Avi Drissmandfd880852022-09-15 20:11:091// Copyright 2013 The Chromium Authors
[email protected]7173edd02011-02-09 00:31:572// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Gayane Petrosyan945148b2021-11-04 18:52:035#include "testing/data_driven_testing/data_driven_test.h"
[email protected]7173edd02011-02-09 00:31:576
[email protected]25a4c1c2013-06-08 04:53:367#include "base/files/file_enumerator.h"
thestig819adcc82014-09-10 22:24:538#include "base/files/file_util.h"
[email protected]4cc42fe2013-06-11 04:17:109#include "base/strings/string_util.h"
jam3f2d3932017-04-26 20:28:5110#include "base/threading/thread_restrictions.h"
[email protected]7173edd02011-02-09 00:31:5711#include "testing/gtest/include/gtest/gtest.h"
Dominic Battre50103492019-09-19 08:38:1112#include "third_party/re2/src/re2/re2.h"
[email protected]7173edd02011-02-09 00:31:5713
Gayane Petrosyan945148b2021-11-04 18:52:0314namespace testing {
[email protected]7173edd02011-02-09 00:31:5715namespace {
16
17// Reads |file| into |content|, and converts Windows line-endings to Unix ones.
18// Returns true on success.
[email protected]650b2d52013-02-10 03:41:4519bool ReadFile(const base::FilePath& file, std::string* content) {
[email protected]82f84b92013-08-30 18:23:5020 if (!base::ReadFileToString(file, content))
[email protected]7173edd02011-02-09 00:31:5721 return false;
22
brettwe6dae462015-06-24 20:54:4523 base::ReplaceSubstringsAfterOffset(content, 0, "\r\n", "\n");
[email protected]7173edd02011-02-09 00:31:5724 return true;
25}
26
27// Write |content| to |file|. Returns true on success.
[email protected]650b2d52013-02-10 03:41:4528bool WriteFile(const base::FilePath& file, const std::string& content) {
[email protected]e5c2a22e2014-03-06 20:42:3029 int write_size = base::WriteFile(file, content.c_str(),
30 static_cast<int>(content.length()));
[email protected]7173edd02011-02-09 00:31:5731 return write_size == static_cast<int>(content.length());
32}
33
Dominic Battre50103492019-09-19 08:38:1134// Removes lines starting with (optional) whitespace and a #.
35void StripComments(std::string* content) {
36 RE2::GlobalReplace(
37 content,
38 // Enable multi-line mode, ^ and $ match begin/end line in addition to
39 // begin/end text.
40 "(?m)"
41 // Search for start of lines (^), ignore spaces (\\s*), and then look for
42 // '#'.
43 "^\\s*#"
44 // Consume all characters (.*) until end of line ($).
45 ".*$"
46 // Consume the line wrapping so that the entire line is gone.
47 "[\\r\\n]*",
48 // Replace entire line with empty string.
49 "");
50}
51
[email protected]7173edd02011-02-09 00:31:5752} // namespace
53
54void DataDrivenTest::RunDataDrivenTest(
[email protected]650b2d52013-02-10 03:41:4555 const base::FilePath& input_directory,
56 const base::FilePath& output_directory,
57 const base::FilePath::StringType& file_name_pattern) {
Francois Doraye6fb2d02017-10-18 21:29:1358 base::ScopedAllowBlockingForTesting allow_blocking;
[email protected]0faac1d2013-07-19 23:51:0459 ASSERT_TRUE(base::DirectoryExists(input_directory));
60 ASSERT_TRUE(base::DirectoryExists(output_directory));
Gayane Petrosyan945148b2021-11-04 18:52:0361 base::FileEnumerator input_files(
62 input_directory, false, base::FileEnumerator::FILES, file_name_pattern);
Roger McFarlanebf55e6a2017-11-28 18:55:2363 const bool kIsExpectedToPass = true;
Gayane Petrosyan945148b2021-11-04 18:52:0364 for (base::FilePath input_file = input_files.Next(); !input_file.empty();
[email protected]7173edd02011-02-09 00:31:5765 input_file = input_files.Next()) {
Roger McFarlanebf55e6a2017-11-28 18:55:2366 RunOneDataDrivenTest(input_file, output_directory, kIsExpectedToPass);
[email protected]7173edd02011-02-09 00:31:5767 }
68}
69
Evan Stadef11e8ceb22015-01-08 23:11:2670void DataDrivenTest::RunOneDataDrivenTest(
71 const base::FilePath& test_file_name,
Roger McFarlanebf55e6a2017-11-28 18:55:2372 const base::FilePath& output_directory,
73 bool is_expected_to_pass) {
Francois Doraye6fb2d02017-10-18 21:29:1374 base::ScopedAllowBlockingForTesting allow_blocking;
estade431dfb722015-06-05 22:30:0175 // iOS doesn't get rid of removed test files. TODO(estade): remove this after
76 // all iOS bots are clobbered.
77 if (test_file_name.BaseName().value() == FILE_PATH_LITERAL("multimerge.in"))
78 return;
79
Evan Stadef11e8ceb22015-01-08 23:11:2680 ASSERT_TRUE(base::DirectoryExists(output_directory));
81 SCOPED_TRACE(test_file_name.BaseName().value());
82
83 std::string input;
84 ReadFile(test_file_name, &input);
85
86 std::string output;
Gabriel Charettead330422021-07-21 00:49:2887 {
88 base::ScopedDisallowBlocking disallow_blocking;
89 GenerateResults(input, &output);
90 }
Evan Stadef11e8ceb22015-01-08 23:11:2691
92 base::FilePath output_file = output_directory.Append(
93 test_file_name.BaseName().StripTrailingSeparators().ReplaceExtension(
94 FILE_PATH_LITERAL(".out")));
95
96 std::string output_file_contents;
Roger McFarlanebf55e6a2017-11-28 18:55:2397 if (!ReadFile(output_file, &output_file_contents)) {
Evan Stadef11e8ceb22015-01-08 23:11:2698 ASSERT_TRUE(WriteFile(output_file, output));
Roger McFarlanebf55e6a2017-11-28 18:55:2399 return;
100 }
Dominic Battre50103492019-09-19 08:38:11101 // Remove comment lines (lead by '#' character).
102 StripComments(&output_file_contents);
Roger McFarlanebf55e6a2017-11-28 18:55:23103
104 if (is_expected_to_pass) {
105 EXPECT_EQ(output_file_contents, output);
106 } else {
107 EXPECT_NE(output_file_contents, output);
108 }
Evan Stadef11e8ceb22015-01-08 23:11:26109}
110
Gayane Petrosyan945148b2021-11-04 18:52:03111base::FilePath DataDrivenTest::GetInputDirectory() {
112 return test_data_directory_.Append(feature_directory_)
113 .Append(test_name_)
Roger McFarlanebf55e6a2017-11-28 18:55:23114 .AppendASCII("input");
[email protected]7173edd02011-02-09 00:31:57115}
116
Gayane Petrosyan945148b2021-11-04 18:52:03117base::FilePath DataDrivenTest::GetOutputDirectory() {
118 return test_data_directory_.Append(feature_directory_)
119 .Append(test_name_)
Roger McFarlanebf55e6a2017-11-28 18:55:23120 .AppendASCII("output");
[email protected]7173edd02011-02-09 00:31:57121}
122
Gayane Petrosyan945148b2021-11-04 18:52:03123DataDrivenTest::DataDrivenTest(
124 const base::FilePath& test_data_directory,
125 const base::FilePath::StringType& feature_directory,
126 const base::FilePath::StringType& test_name)
127 : test_data_directory_(test_data_directory),
128 feature_directory_(feature_directory),
129 test_name_(test_name) {}
[email protected]7173edd02011-02-09 00:31:57130
Gayane Petrosyan945148b2021-11-04 18:52:03131DataDrivenTest::~DataDrivenTest() {}
[email protected]e217c5632013-04-12 19:11:48132
Gayane Petrosyan945148b2021-11-04 18:52:03133} // namespace testing