TestSuite.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Andrew Kaster <akaster@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/ByteString.h>
  9. #include <AK/Function.h>
  10. #include <AK/Vector.h>
  11. #include <LibTest/Macros.h>
  12. #include <LibTest/Randomized/RandomnessSource.h>
  13. #include <LibTest/TestCase.h>
  14. #include <LibTest/TestResult.h>
  15. namespace Test {
  16. class TestSuite {
  17. public:
  18. static TestSuite& the()
  19. {
  20. if (s_global == nullptr)
  21. s_global = new TestSuite();
  22. return *s_global;
  23. }
  24. static void release()
  25. {
  26. if (s_global)
  27. delete s_global;
  28. s_global = nullptr;
  29. }
  30. int run(Vector<NonnullRefPtr<TestCase>> const&);
  31. int main(ByteString const& suite_name, Span<StringView> arguments);
  32. Vector<NonnullRefPtr<TestCase>> find_cases(ByteString const& search, bool find_tests, bool find_benchmarks);
  33. void add_case(NonnullRefPtr<TestCase> const& test_case)
  34. {
  35. m_cases.append(test_case);
  36. }
  37. TestResult current_test_result() const { return m_current_test_result; }
  38. void set_current_test_result(TestResult result) { m_current_test_result = result; }
  39. void set_suite_setup(Function<void()> setup) { m_setup = move(setup); }
  40. // The RandomnessSource is where generators record / replay random data
  41. // from. Initially a live "truly random" RandomnessSource is used, and when
  42. // a failure is found, a set of hardcoded RandomnessSources is used during
  43. // shrinking.
  44. void set_randomness_source(Randomized::RandomnessSource source) { m_randomness_source = move(source); }
  45. Randomized::RandomnessSource& randomness_source() { return m_randomness_source; }
  46. // Dictates whether FAIL(), EXPECT() and similar macros in LibTest/Macros.h
  47. // print messages or not. This is important for randomized tests because
  48. // they run the test function many times in a row, and we only want to
  49. // report the _minimal_ (shrunk) failure to the user, not all of them.
  50. bool is_reporting_enabled() { return m_reporting_enabled; }
  51. void enable_reporting() { m_reporting_enabled = true; }
  52. void disable_reporting() { m_reporting_enabled = false; }
  53. u64 randomized_runs() { return m_randomized_runs; }
  54. private:
  55. static TestSuite* s_global;
  56. Vector<NonnullRefPtr<TestCase>> m_cases;
  57. u64 m_testtime = 0;
  58. u64 m_benchtime = 0;
  59. ByteString m_suite_name;
  60. u64 m_benchmark_repetitions = 1;
  61. u64 m_randomized_runs = 100;
  62. Function<void()> m_setup;
  63. TestResult m_current_test_result = TestResult::NotRun;
  64. Randomized::RandomnessSource m_randomness_source = Randomized::RandomnessSource::live();
  65. bool m_reporting_enabled = true;
  66. };
  67. }