blob: 22b565ec99b11e2bf769868f1950b1c00e09a80d [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#ifndef TESTING_DATA_DRIVEN_TESTING_DATA_DRIVEN_TEST_H_
6#define TESTING_DATA_DRIVEN_TESTING_DATA_DRIVEN_TEST_H_
[email protected]7173edd02011-02-09 00:31:577
8#include <string>
9
[email protected]57999812013-02-24 05:40:5210#include "base/files/file_path.h"
[email protected]7173edd02011-02-09 00:31:5711
Gayane Petrosyan945148b2021-11-04 18:52:0312namespace testing {
[email protected]e217c5632013-04-12 19:11:4813
[email protected]7173edd02011-02-09 00:31:5714// 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 Petrosyan945148b2021-11-04 18:52:0317// gold output file is generated; for subsequent runs, the test output is
[email protected]7173edd02011-02-09 00:31:5718// compared to this gold output.
19class DataDrivenTest {
20 public:
Peter Boström9f667c382021-10-01 20:09:3121 DataDrivenTest(const DataDrivenTest&) = delete;
22 DataDrivenTest& operator=(const DataDrivenTest&) = delete;
23
[email protected]7173edd02011-02-09 00:31:5724 // 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]650b2d52013-02-10 03:41:4529 void RunDataDrivenTest(const base::FilePath& input_directory,
30 const base::FilePath& output_directory,
31 const base::FilePath::StringType& file_name_pattern);
[email protected]7173edd02011-02-09 00:31:5732
Evan Stadef11e8ceb22015-01-08 23:11:2633 // 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 McFarlanebf55e6a2017-11-28 18:55:2336 const base::FilePath& output_directory,
37 bool is_expected_to_pass);
Evan Stadef11e8ceb22015-01-08 23:11:2638
[email protected]7173edd02011-02-09 00:31:5739 // 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]650b2d52013-02-10 03:41:4546 // Return |base::FilePath|s to the test input and output subdirectories
Gayane Petrosyan945148b2021-11-04 18:52:0347 // ../|feature_dir|/|test_name|/input and ../|feature_dir|/|test_name|/output.
48 base::FilePath GetInputDirectory();
49 base::FilePath GetOutputDirectory();
[email protected]7173edd02011-02-09 00:31:5750
51 protected:
Gayane Petrosyan945148b2021-11-04 18:52:0352 DataDrivenTest(const base::FilePath& test_data_directory,
53 const base::FilePath::StringType& feature_name,
54 const base::FilePath::StringType& test_name);
[email protected]7173edd02011-02-09 00:31:5755 virtual ~DataDrivenTest();
56
57 private:
[email protected]0faac1d2013-07-19 23:51:0458 base::FilePath test_data_directory_;
Gayane Petrosyan945148b2021-11-04 18:52:0359 base::FilePath::StringType feature_directory_;
60 base::FilePath::StringType test_name_;
[email protected]7173edd02011-02-09 00:31:5761};
62
Gayane Petrosyan945148b2021-11-04 18:52:0363} // namespace testing
[email protected]e217c5632013-04-12 19:11:4864
Gayane Petrosyan945148b2021-11-04 18:52:0365#endif // TESTING_DATA_DRIVEN_TESTING_DATA_DRIVEN_TEST_H_