Avi Drissman | dfd88085 | 2022-09-15 20:11:09 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Gayane Petrosyan | 945148b | 2021-11-04 18:52:03 | [diff] [blame] | 5 | #ifndef TESTING_DATA_DRIVEN_TESTING_DATA_DRIVEN_TEST_H_ |
| 6 | #define TESTING_DATA_DRIVEN_TESTING_DATA_DRIVEN_TEST_H_ |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 7 | |
| 8 | #include <string> |
| 9 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 10 | #include "base/files/file_path.h" |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 11 | |
Gayane Petrosyan | 945148b | 2021-11-04 18:52:03 | [diff] [blame] | 12 | namespace testing { |
[email protected] | e217c563 | 2013-04-12 19:11:48 | [diff] [blame] | 13 | |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 14 | // A convenience class for implementing data-driven tests. Subclassers need only |
| 15 | // implement the conversion of serialized input data to serialized output data |
| 16 | // and provide a set of input files. For each input file, on the first run, a |
Gayane Petrosyan | 945148b | 2021-11-04 18:52:03 | [diff] [blame] | 17 | // gold output file is generated; for subsequent runs, the test output is |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 18 | // compared to this gold output. |
| 19 | class DataDrivenTest { |
| 20 | public: |
Peter Boström | 9f667c38 | 2021-10-01 20:09:31 | [diff] [blame] | 21 | DataDrivenTest(const DataDrivenTest&) = delete; |
| 22 | DataDrivenTest& operator=(const DataDrivenTest&) = delete; |
| 23 | |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 24 | // For each file in |input_directory| whose filename matches |
| 25 | // |file_name_pattern|, slurps in the file contents and calls into |
| 26 | // |GenerateResults()|. If the corresponding output file already exists in |
| 27 | // the |output_directory|, verifies that the results match the file contents; |
| 28 | // otherwise, writes a gold result file to the |output_directory|. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 29 | void RunDataDrivenTest(const base::FilePath& input_directory, |
| 30 | const base::FilePath& output_directory, |
| 31 | const base::FilePath::StringType& file_name_pattern); |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 32 | |
Evan Stade | f11e8ceb2 | 2015-01-08 23:11:26 | [diff] [blame] | 33 | // As above, but runs a test for a single file, the full path of which is |
| 34 | // given by |test_file_name|. |
| 35 | void RunOneDataDrivenTest(const base::FilePath& test_file_name, |
Roger McFarlane | bf55e6a | 2017-11-28 18:55:23 | [diff] [blame] | 36 | const base::FilePath& output_directory, |
| 37 | bool is_expected_to_pass); |
Evan Stade | f11e8ceb2 | 2015-01-08 23:11:26 | [diff] [blame] | 38 | |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 39 | // Given the |input| data, generates the |output| results. The output results |
| 40 | // must be stable across runs. |
| 41 | // Note: The return type is |void| so that googletest |ASSERT_*| macros will |
| 42 | // compile. |
| 43 | virtual void GenerateResults(const std::string& input, |
| 44 | std::string* output) = 0; |
| 45 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 46 | // Return |base::FilePath|s to the test input and output subdirectories |
Gayane Petrosyan | 945148b | 2021-11-04 18:52:03 | [diff] [blame] | 47 | // ../|feature_dir|/|test_name|/input and ../|feature_dir|/|test_name|/output. |
| 48 | base::FilePath GetInputDirectory(); |
| 49 | base::FilePath GetOutputDirectory(); |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 50 | |
| 51 | protected: |
Gayane Petrosyan | 945148b | 2021-11-04 18:52:03 | [diff] [blame] | 52 | DataDrivenTest(const base::FilePath& test_data_directory, |
| 53 | const base::FilePath::StringType& feature_name, |
| 54 | const base::FilePath::StringType& test_name); |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 55 | virtual ~DataDrivenTest(); |
| 56 | |
| 57 | private: |
[email protected] | 0faac1d | 2013-07-19 23:51:04 | [diff] [blame] | 58 | base::FilePath test_data_directory_; |
Gayane Petrosyan | 945148b | 2021-11-04 18:52:03 | [diff] [blame] | 59 | base::FilePath::StringType feature_directory_; |
| 60 | base::FilePath::StringType test_name_; |
[email protected] | 7173edd0 | 2011-02-09 00:31:57 | [diff] [blame] | 61 | }; |
| 62 | |
Gayane Petrosyan | 945148b | 2021-11-04 18:52:03 | [diff] [blame] | 63 | } // namespace testing |
[email protected] | e217c563 | 2013-04-12 19:11:48 | [diff] [blame] | 64 | |
Gayane Petrosyan | 945148b | 2021-11-04 18:52:03 | [diff] [blame] | 65 | #endif // TESTING_DATA_DRIVEN_TESTING_DATA_DRIVEN_TEST_H_ |