xref: /aosp_15_r20/external/cronet/base/test/gtest_util.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 The Chromium Authors
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 
7 #include <stddef.h>
8 
9 #include <memory>
10 
11 #include "base/files/file_path.h"
12 #include "base/json/json_file_value_serializer.h"
13 #include "base/strings/string_util.h"
14 #include "base/test/values_test_util.h"
15 #include "base/values.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 
18 namespace base {
19 
20 TestIdentifier::TestIdentifier() = default;
21 
22 TestIdentifier::TestIdentifier(const TestIdentifier& other) = default;
23 
24 TestIdentifier& TestIdentifier::operator=(const TestIdentifier& other) =
25     default;
26 
FormatFullTestName(const std::string & test_case_name,const std::string & test_name)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 
TestNameWithoutDisabledPrefix(const std::string & full_test_name)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 
GetCompiledInTests()38 std::vector<TestIdentifier> GetCompiledInTests() {
39   testing::UnitTest* const unit_test = testing::UnitTest::GetInstance();
40 
41   std::vector<TestIdentifier> tests;
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);
46       TestIdentifier test_data;
47       test_data.test_case_name = test_suite->name();
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);
52     }
53   }
54   return tests;
55 }
56 
WriteCompiledInTestsToFile(const FilePath & path)57 bool WriteCompiledInTestsToFile(const FilePath& path) {
58   std::vector<TestIdentifier> tests(GetCompiledInTests());
59 
60   Value::List storage;
61   for (const TestIdentifier& i : tests) {
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));
68   }
69 
70   return base::test::WriteJsonFile(path, storage).has_value();
71 }
72 
ReadTestNamesFromFile(const FilePath & path,std::vector<TestIdentifier> * output)73 bool ReadTestNamesFromFile(const FilePath& path,
74                            std::vector<TestIdentifier>* output) {
75   JSONFileValueDeserializer deserializer(path);
76   int error_code = 0;
77   std::string error_message;
78   std::unique_ptr<Value> value =
79       deserializer.Deserialize(&error_code, &error_message);
80   if (!value.get())
81     return false;
82 
83   if (!value->is_list())
84     return false;
85 
86   std::vector<TestIdentifier> result;
87   for (const Value& item : value->GetList()) {
88     if (!item.is_dict())
89       return false;
90 
91     const Value::Dict& dict = item.GetDict();
92     const std::string* test_case_name = dict.FindString("test_case_name");
93     if (!test_case_name || !IsStringASCII(*test_case_name))
94       return false;
95 
96     const std::string* test_name = dict.FindString("test_name");
97     if (!test_name || !IsStringASCII(*test_name))
98       return false;
99 
100     const std::string* file = dict.FindString("file");
101     if (!file || !IsStringASCII(*file))
102       return false;
103 
104     std::optional<int> line = dict.FindInt("line");
105     if (!line.has_value())
106       return false;
107 
108     TestIdentifier test_data;
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;
113     result.push_back(test_data);
114   }
115 
116   output->swap(result);
117   return true;
118 }
119 
120 }  // namespace base
121