data_driven_test.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright 2013 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. #ifndef TESTING_DATA_DRIVEN_TESTING_DATA_DRIVEN_TEST_H_
  5. #define TESTING_DATA_DRIVEN_TESTING_DATA_DRIVEN_TEST_H_
  6. #include <string>
  7. #include "base/files/file_path.h"
  8. namespace testing {
  9. // A convenience class for implementing data-driven tests. Subclassers need only
  10. // implement the conversion of serialized input data to serialized output data
  11. // and provide a set of input files. For each input file, on the first run, a
  12. // gold output file is generated; for subsequent runs, the test output is
  13. // compared to this gold output.
  14. class DataDrivenTest {
  15. public:
  16. DataDrivenTest(const DataDrivenTest&) = delete;
  17. DataDrivenTest& operator=(const DataDrivenTest&) = delete;
  18. // For each file in |input_directory| whose filename matches
  19. // |file_name_pattern|, slurps in the file contents and calls into
  20. // |GenerateResults()|. If the corresponding output file already exists in
  21. // the |output_directory|, verifies that the results match the file contents;
  22. // otherwise, writes a gold result file to the |output_directory|.
  23. void RunDataDrivenTest(const base::FilePath& input_directory,
  24. const base::FilePath& output_directory,
  25. const base::FilePath::StringType& file_name_pattern);
  26. // As above, but runs a test for a single file, the full path of which is
  27. // given by |test_file_name|.
  28. void RunOneDataDrivenTest(const base::FilePath& test_file_name,
  29. const base::FilePath& output_directory,
  30. bool is_expected_to_pass);
  31. // Given the |input| data, generates the |output| results. The output results
  32. // must be stable across runs.
  33. // Note: The return type is |void| so that googletest |ASSERT_*| macros will
  34. // compile.
  35. virtual void GenerateResults(const std::string& input,
  36. std::string* output) = 0;
  37. // Return |base::FilePath|s to the test input and output subdirectories
  38. // ../|feature_dir|/|test_name|/input and ../|feature_dir|/|test_name|/output.
  39. base::FilePath GetInputDirectory();
  40. base::FilePath GetOutputDirectory();
  41. protected:
  42. DataDrivenTest(const base::FilePath& test_data_directory,
  43. const base::FilePath::StringType& feature_name,
  44. const base::FilePath::StringType& test_name);
  45. virtual ~DataDrivenTest();
  46. private:
  47. base::FilePath test_data_directory_;
  48. base::FilePath::StringType feature_directory_;
  49. base::FilePath::StringType test_name_;
  50. };
  51. } // namespace testing
  52. #endif // TESTING_DATA_DRIVEN_TESTING_DATA_DRIVEN_TEST_H_