Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [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 | |
| 5 | #include "base/test/gtest_util.h" |
| 6 | |
avi | d351e5a | 2015-12-24 03:28:02 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 9 | #include <memory> |
| 10 | |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
| 12 | #include "base/json/json_file_value_serializer.h" |
stgao | 9537aa9 | 2016-10-31 21:38:44 | [diff] [blame] | 13 | #include "base/strings/string_util.h" |
Claudio DeSouza | 65ccd2b2e | 2023-03-09 17:35:29 | [diff] [blame] | 14 | #include "base/test/values_test_util.h" |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 15 | #include "base/values.h" |
| 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
| 18 | namespace base { |
| 19 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame] | 20 | TestIdentifier::TestIdentifier() = default; |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 21 | |
vmpstr | e65942b | 2016-02-25 00:50:31 | [diff] [blame] | 22 | TestIdentifier::TestIdentifier(const TestIdentifier& other) = default; |
| 23 | |
Peter Kasting | e08ca1334 | 2021-07-08 02:46:57 | [diff] [blame] | 24 | TestIdentifier& TestIdentifier::operator=(const TestIdentifier& other) = |
| 25 | default; |
| 26 | |
Paweł Hajdan | 19368c0 | 2015-01-23 13:33:49 | [diff] [blame] | 27 | std::string FormatFullTestName(const std::string& test_case_name, |
| 28 | const std::string& test_name) { |
| 29 | return test_case_name + "." + test_name; |
| 30 | } |
| 31 | |
stgao | 9537aa9 | 2016-10-31 21:38:44 | [diff] [blame] | 32 | std::string TestNameWithoutDisabledPrefix(const std::string& full_test_name) { |
| 33 | std::string test_name_no_disabled(full_test_name); |
| 34 | ReplaceSubstringsAfterOffset(&test_name_no_disabled, 0, "DISABLED_", ""); |
| 35 | return test_name_no_disabled; |
| 36 | } |
| 37 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 38 | std::vector<TestIdentifier> GetCompiledInTests() { |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 39 | testing::UnitTest* const unit_test = testing::UnitTest::GetInstance(); |
| 40 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 41 | std::vector<TestIdentifier> tests; |
Mike Frysinger | 6ba1ed1 | 2023-12-08 21:59:00 | [diff] [blame] | 42 | for (int i = 0; i < unit_test->total_test_suite_count(); ++i) { |
| 43 | const testing::TestSuite* test_suite = unit_test->GetTestSuite(i); |
| 44 | for (int j = 0; j < test_suite->total_test_count(); ++j) { |
| 45 | const testing::TestInfo* test_info = test_suite->GetTestInfo(j); |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 46 | TestIdentifier test_data; |
Mike Frysinger | 6ba1ed1 | 2023-12-08 21:59:00 | [diff] [blame] | 47 | test_data.test_case_name = test_suite->name(); |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 48 | test_data.test_name = test_info->name(); |
| 49 | test_data.file = test_info->file(); |
| 50 | test_data.line = test_info->line(); |
| 51 | tests.push_back(test_data); |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | return tests; |
| 55 | } |
| 56 | |
| 57 | bool WriteCompiledInTestsToFile(const FilePath& path) { |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 58 | std::vector<TestIdentifier> tests(GetCompiledInTests()); |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 59 | |
Matt Menke | e671a4a | 2022-06-25 05:25:28 | [diff] [blame] | 60 | Value::List storage; |
Sylvain Defresne | b8ec0cd | 2021-06-23 16:47:06 | [diff] [blame] | 61 | for (const TestIdentifier& i : tests) { |
Matt Menke | e671a4a | 2022-06-25 05:25:28 | [diff] [blame] | 62 | Value::Dict test_info; |
| 63 | test_info.Set("test_case_name", i.test_case_name); |
| 64 | test_info.Set("test_name", i.test_name); |
| 65 | test_info.Set("file", i.file); |
| 66 | test_info.Set("line", i.line); |
| 67 | storage.Append(std::move(test_info)); |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 68 | } |
| 69 | |
Claudio DeSouza | 65ccd2b2e | 2023-03-09 17:35:29 | [diff] [blame] | 70 | return base::test::WriteJsonFile(path, storage).has_value(); |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 71 | } |
| 72 | |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 73 | bool ReadTestNamesFromFile(const FilePath& path, |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 74 | std::vector<TestIdentifier>* output) { |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 75 | JSONFileValueDeserializer deserializer(path); |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 76 | int error_code = 0; |
| 77 | std::string error_message; |
Sylvain Defresne | b8ec0cd | 2021-06-23 16:47:06 | [diff] [blame] | 78 | std::unique_ptr<Value> value = |
olli.raula | ba04525 | 2015-10-16 06:16:40 | [diff] [blame] | 79 | deserializer.Deserialize(&error_code, &error_message); |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 80 | if (!value.get()) |
| 81 | return false; |
| 82 | |
Sylvain Defresne | b8ec0cd | 2021-06-23 16:47:06 | [diff] [blame] | 83 | if (!value->is_list()) |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 84 | return false; |
| 85 | |
Sylvain Defresne | b8ec0cd | 2021-06-23 16:47:06 | [diff] [blame] | 86 | std::vector<TestIdentifier> result; |
Matt Menke | e671a4a | 2022-06-25 05:25:28 | [diff] [blame] | 87 | for (const Value& item : value->GetList()) { |
Sylvain Defresne | b8ec0cd | 2021-06-23 16:47:06 | [diff] [blame] | 88 | if (!item.is_dict()) |
| 89 | return false; |
| 90 | |
Matt Menke | e671a4a | 2022-06-25 05:25:28 | [diff] [blame] | 91 | const Value::Dict& dict = item.GetDict(); |
| 92 | const std::string* test_case_name = dict.FindString("test_case_name"); |
Sylvain Defresne | b8ec0cd | 2021-06-23 16:47:06 | [diff] [blame] | 93 | if (!test_case_name || !IsStringASCII(*test_case_name)) |
| 94 | return false; |
| 95 | |
Matt Menke | e671a4a | 2022-06-25 05:25:28 | [diff] [blame] | 96 | const std::string* test_name = dict.FindString("test_name"); |
Sylvain Defresne | b8ec0cd | 2021-06-23 16:47:06 | [diff] [blame] | 97 | if (!test_name || !IsStringASCII(*test_name)) |
| 98 | return false; |
| 99 | |
Matt Menke | e671a4a | 2022-06-25 05:25:28 | [diff] [blame] | 100 | const std::string* file = dict.FindString("file"); |
Sylvain Defresne | b8ec0cd | 2021-06-23 16:47:06 | [diff] [blame] | 101 | if (!file || !IsStringASCII(*file)) |
| 102 | return false; |
| 103 | |
Matt Menke | e671a4a | 2022-06-25 05:25:28 | [diff] [blame] | 104 | absl::optional<int> line = dict.FindInt("line"); |
Sylvain Defresne | b8ec0cd | 2021-06-23 16:47:06 | [diff] [blame] | 105 | if (!line.has_value()) |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 106 | return false; |
| 107 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 108 | TestIdentifier test_data; |
Sylvain Defresne | b8ec0cd | 2021-06-23 16:47:06 | [diff] [blame] | 109 | test_data.test_case_name = *test_case_name; |
| 110 | test_data.test_name = *test_name; |
| 111 | test_data.file = *file; |
| 112 | test_data.line = *line; |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 113 | result.push_back(test_data); |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | output->swap(result); |
| 117 | return true; |
| 118 | } |
| 119 | |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 120 | } // namespace base |