xref: /aosp_15_r20/external/cronet/testing/data_driven_testing/data_driven_test.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef TESTING_DATA_DRIVEN_TESTING_DATA_DRIVEN_TEST_H_
6 #define TESTING_DATA_DRIVEN_TESTING_DATA_DRIVEN_TEST_H_
7 
8 #include <string>
9 
10 #include "base/files/file_path.h"
11 
12 namespace testing {
13 
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
17 // gold output file is generated; for subsequent runs, the test output is
18 // compared to this gold output.
19 class DataDrivenTest {
20  public:
21   DataDrivenTest(const DataDrivenTest&) = delete;
22   DataDrivenTest& operator=(const DataDrivenTest&) = delete;
23 
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|.
29   void RunDataDrivenTest(const base::FilePath& input_directory,
30                          const base::FilePath& output_directory,
31                          const base::FilePath::StringType& file_name_pattern);
32 
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,
36                             const base::FilePath& output_directory,
37                             bool is_expected_to_pass);
38 
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 
46   // Return |base::FilePath|s to the test input and output subdirectories
47   // ../|feature_dir|/|test_name|/input and ../|feature_dir|/|test_name|/output.
48   base::FilePath GetInputDirectory();
49   base::FilePath GetOutputDirectory();
50 
51  protected:
52   DataDrivenTest(const base::FilePath& test_data_directory,
53                  const base::FilePath::StringType& feature_name,
54                  const base::FilePath::StringType& test_name);
55   virtual ~DataDrivenTest();
56 
57  private:
58   base::FilePath test_data_directory_;
59   base::FilePath::StringType feature_directory_;
60   base::FilePath::StringType test_name_;
61 };
62 
63 }  // namespace testing
64 
65 #endif  // TESTING_DATA_DRIVEN_TESTING_DATA_DRIVEN_TEST_H_
66