blob: 8b50af5f13307b42fbc557e3765a255af2547c66 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2014 The Chromium Authors
Paweł Hajdan, Jr70fcead2014-11-24 15:58:512// 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
avid351e5a2015-12-24 03:28:027#include <stddef.h>
8
dcheng98e96a72016-06-11 03:41:489#include <memory>
10
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5111#include "base/files/file_path.h"
12#include "base/json/json_file_value_serializer.h"
stgao9537aa92016-10-31 21:38:4413#include "base/strings/string_util.h"
Claudio DeSouza65ccd2b2e2023-03-09 17:35:2914#include "base/test/values_test_util.h"
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5115#include "base/values.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18namespace base {
19
Chris Watkinsbb7211c2017-11-29 07:16:3820TestIdentifier::TestIdentifier() = default;
phajdan.jrb4cf4a092015-07-29 09:49:5421
vmpstre65942b2016-02-25 00:50:3122TestIdentifier::TestIdentifier(const TestIdentifier& other) = default;
23
Peter Kastinge08ca13342021-07-08 02:46:5724TestIdentifier& TestIdentifier::operator=(const TestIdentifier& other) =
25 default;
26
Paweł Hajdan19368c02015-01-23 13:33:4927std::string FormatFullTestName(const std::string& test_case_name,
28 const std::string& test_name) {
29 return test_case_name + "." + test_name;
30}
31
stgao9537aa92016-10-31 21:38:4432std::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.jrb4cf4a092015-07-29 09:49:5438std::vector<TestIdentifier> GetCompiledInTests() {
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5139 testing::UnitTest* const unit_test = testing::UnitTest::GetInstance();
40
phajdan.jrb4cf4a092015-07-29 09:49:5441 std::vector<TestIdentifier> tests;
Mike Frysinger6ba1ed12023-12-08 21:59:0042 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.jrb4cf4a092015-07-29 09:49:5446 TestIdentifier test_data;
Mike Frysinger6ba1ed12023-12-08 21:59:0047 test_data.test_case_name = test_suite->name();
phajdan.jrb4cf4a092015-07-29 09:49:5448 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, Jr70fcead2014-11-24 15:58:5152 }
53 }
54 return tests;
55}
56
57bool WriteCompiledInTestsToFile(const FilePath& path) {
phajdan.jrb4cf4a092015-07-29 09:49:5458 std::vector<TestIdentifier> tests(GetCompiledInTests());
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5159
Matt Menkee671a4a2022-06-25 05:25:2860 Value::List storage;
Sylvain Defresneb8ec0cd2021-06-23 16:47:0661 for (const TestIdentifier& i : tests) {
Matt Menkee671a4a2022-06-25 05:25:2862 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, Jr70fcead2014-11-24 15:58:5168 }
69
Claudio DeSouza65ccd2b2e2023-03-09 17:35:2970 return base::test::WriteJsonFile(path, storage).has_value();
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5171}
72
Paweł Hajdan26bc4152015-01-15 13:32:0173bool ReadTestNamesFromFile(const FilePath& path,
phajdan.jrb4cf4a092015-07-29 09:49:5474 std::vector<TestIdentifier>* output) {
prashhir54a994502015-03-05 09:30:5775 JSONFileValueDeserializer deserializer(path);
Paweł Hajdan26bc4152015-01-15 13:32:0176 int error_code = 0;
77 std::string error_message;
Sylvain Defresneb8ec0cd2021-06-23 16:47:0678 std::unique_ptr<Value> value =
olli.raulaba045252015-10-16 06:16:4079 deserializer.Deserialize(&error_code, &error_message);
Paweł Hajdan26bc4152015-01-15 13:32:0180 if (!value.get())
81 return false;
82
Sylvain Defresneb8ec0cd2021-06-23 16:47:0683 if (!value->is_list())
Paweł Hajdan26bc4152015-01-15 13:32:0184 return false;
85
Sylvain Defresneb8ec0cd2021-06-23 16:47:0686 std::vector<TestIdentifier> result;
Matt Menkee671a4a2022-06-25 05:25:2887 for (const Value& item : value->GetList()) {
Sylvain Defresneb8ec0cd2021-06-23 16:47:0688 if (!item.is_dict())
89 return false;
90
Matt Menkee671a4a2022-06-25 05:25:2891 const Value::Dict& dict = item.GetDict();
92 const std::string* test_case_name = dict.FindString("test_case_name");
Sylvain Defresneb8ec0cd2021-06-23 16:47:0693 if (!test_case_name || !IsStringASCII(*test_case_name))
94 return false;
95
Matt Menkee671a4a2022-06-25 05:25:2896 const std::string* test_name = dict.FindString("test_name");
Sylvain Defresneb8ec0cd2021-06-23 16:47:0697 if (!test_name || !IsStringASCII(*test_name))
98 return false;
99
Matt Menkee671a4a2022-06-25 05:25:28100 const std::string* file = dict.FindString("file");
Sylvain Defresneb8ec0cd2021-06-23 16:47:06101 if (!file || !IsStringASCII(*file))
102 return false;
103
Matt Menkee671a4a2022-06-25 05:25:28104 absl::optional<int> line = dict.FindInt("line");
Sylvain Defresneb8ec0cd2021-06-23 16:47:06105 if (!line.has_value())
Paweł Hajdan26bc4152015-01-15 13:32:01106 return false;
107
phajdan.jrb4cf4a092015-07-29 09:49:54108 TestIdentifier test_data;
Sylvain Defresneb8ec0cd2021-06-23 16:47:06109 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.jrb4cf4a092015-07-29 09:49:54113 result.push_back(test_data);
Paweł Hajdan26bc4152015-01-15 13:32:01114 }
115
116 output->swap(result);
117 return true;
118}
119
prashhir54a994502015-03-05 09:30:57120} // namespace base