xref: /aosp_15_r20/external/libchrome/base/test/gtest_util.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright 2014 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "base/test/gtest_util.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include <stddef.h>
8*635a8641SAndroid Build Coastguard Worker 
9*635a8641SAndroid Build Coastguard Worker #include <memory>
10*635a8641SAndroid Build Coastguard Worker 
11*635a8641SAndroid Build Coastguard Worker #include "base/files/file_path.h"
12*635a8641SAndroid Build Coastguard Worker #include "base/json/json_file_value_serializer.h"
13*635a8641SAndroid Build Coastguard Worker #include "base/strings/string_util.h"
14*635a8641SAndroid Build Coastguard Worker #include "base/values.h"
15*635a8641SAndroid Build Coastguard Worker #include "testing/gtest/include/gtest/gtest.h"
16*635a8641SAndroid Build Coastguard Worker 
17*635a8641SAndroid Build Coastguard Worker namespace base {
18*635a8641SAndroid Build Coastguard Worker 
19*635a8641SAndroid Build Coastguard Worker TestIdentifier::TestIdentifier() = default;
20*635a8641SAndroid Build Coastguard Worker 
21*635a8641SAndroid Build Coastguard Worker TestIdentifier::TestIdentifier(const TestIdentifier& other) = default;
22*635a8641SAndroid Build Coastguard Worker 
FormatFullTestName(const std::string & test_case_name,const std::string & test_name)23*635a8641SAndroid Build Coastguard Worker std::string FormatFullTestName(const std::string& test_case_name,
24*635a8641SAndroid Build Coastguard Worker                                const std::string& test_name) {
25*635a8641SAndroid Build Coastguard Worker   return test_case_name + "." + test_name;
26*635a8641SAndroid Build Coastguard Worker }
27*635a8641SAndroid Build Coastguard Worker 
TestNameWithoutDisabledPrefix(const std::string & full_test_name)28*635a8641SAndroid Build Coastguard Worker std::string TestNameWithoutDisabledPrefix(const std::string& full_test_name) {
29*635a8641SAndroid Build Coastguard Worker   std::string test_name_no_disabled(full_test_name);
30*635a8641SAndroid Build Coastguard Worker   ReplaceSubstringsAfterOffset(&test_name_no_disabled, 0, "DISABLED_", "");
31*635a8641SAndroid Build Coastguard Worker   return test_name_no_disabled;
32*635a8641SAndroid Build Coastguard Worker }
33*635a8641SAndroid Build Coastguard Worker 
GetCompiledInTests()34*635a8641SAndroid Build Coastguard Worker std::vector<TestIdentifier> GetCompiledInTests() {
35*635a8641SAndroid Build Coastguard Worker   testing::UnitTest* const unit_test = testing::UnitTest::GetInstance();
36*635a8641SAndroid Build Coastguard Worker 
37*635a8641SAndroid Build Coastguard Worker   std::vector<TestIdentifier> tests;
38*635a8641SAndroid Build Coastguard Worker   for (int i = 0; i < unit_test->total_test_case_count(); ++i) {
39*635a8641SAndroid Build Coastguard Worker     const testing::TestCase* test_case = unit_test->GetTestCase(i);
40*635a8641SAndroid Build Coastguard Worker     for (int j = 0; j < test_case->total_test_count(); ++j) {
41*635a8641SAndroid Build Coastguard Worker       const testing::TestInfo* test_info = test_case->GetTestInfo(j);
42*635a8641SAndroid Build Coastguard Worker       TestIdentifier test_data;
43*635a8641SAndroid Build Coastguard Worker       test_data.test_case_name = test_case->name();
44*635a8641SAndroid Build Coastguard Worker       test_data.test_name = test_info->name();
45*635a8641SAndroid Build Coastguard Worker       test_data.file = test_info->file();
46*635a8641SAndroid Build Coastguard Worker       test_data.line = test_info->line();
47*635a8641SAndroid Build Coastguard Worker       tests.push_back(test_data);
48*635a8641SAndroid Build Coastguard Worker     }
49*635a8641SAndroid Build Coastguard Worker   }
50*635a8641SAndroid Build Coastguard Worker   return tests;
51*635a8641SAndroid Build Coastguard Worker }
52*635a8641SAndroid Build Coastguard Worker 
WriteCompiledInTestsToFile(const FilePath & path)53*635a8641SAndroid Build Coastguard Worker bool WriteCompiledInTestsToFile(const FilePath& path) {
54*635a8641SAndroid Build Coastguard Worker   std::vector<TestIdentifier> tests(GetCompiledInTests());
55*635a8641SAndroid Build Coastguard Worker 
56*635a8641SAndroid Build Coastguard Worker   ListValue root;
57*635a8641SAndroid Build Coastguard Worker   for (size_t i = 0; i < tests.size(); ++i) {
58*635a8641SAndroid Build Coastguard Worker     std::unique_ptr<DictionaryValue> test_info(new DictionaryValue);
59*635a8641SAndroid Build Coastguard Worker     test_info->SetString("test_case_name", tests[i].test_case_name);
60*635a8641SAndroid Build Coastguard Worker     test_info->SetString("test_name", tests[i].test_name);
61*635a8641SAndroid Build Coastguard Worker     test_info->SetString("file", tests[i].file);
62*635a8641SAndroid Build Coastguard Worker     test_info->SetInteger("line", tests[i].line);
63*635a8641SAndroid Build Coastguard Worker     root.Append(std::move(test_info));
64*635a8641SAndroid Build Coastguard Worker   }
65*635a8641SAndroid Build Coastguard Worker 
66*635a8641SAndroid Build Coastguard Worker   JSONFileValueSerializer serializer(path);
67*635a8641SAndroid Build Coastguard Worker   return serializer.Serialize(root);
68*635a8641SAndroid Build Coastguard Worker }
69*635a8641SAndroid Build Coastguard Worker 
ReadTestNamesFromFile(const FilePath & path,std::vector<TestIdentifier> * output)70*635a8641SAndroid Build Coastguard Worker bool ReadTestNamesFromFile(const FilePath& path,
71*635a8641SAndroid Build Coastguard Worker                            std::vector<TestIdentifier>* output) {
72*635a8641SAndroid Build Coastguard Worker   JSONFileValueDeserializer deserializer(path);
73*635a8641SAndroid Build Coastguard Worker   int error_code = 0;
74*635a8641SAndroid Build Coastguard Worker   std::string error_message;
75*635a8641SAndroid Build Coastguard Worker   std::unique_ptr<base::Value> value =
76*635a8641SAndroid Build Coastguard Worker       deserializer.Deserialize(&error_code, &error_message);
77*635a8641SAndroid Build Coastguard Worker   if (!value.get())
78*635a8641SAndroid Build Coastguard Worker     return false;
79*635a8641SAndroid Build Coastguard Worker 
80*635a8641SAndroid Build Coastguard Worker   base::ListValue* tests = nullptr;
81*635a8641SAndroid Build Coastguard Worker   if (!value->GetAsList(&tests))
82*635a8641SAndroid Build Coastguard Worker     return false;
83*635a8641SAndroid Build Coastguard Worker 
84*635a8641SAndroid Build Coastguard Worker   std::vector<base::TestIdentifier> result;
85*635a8641SAndroid Build Coastguard Worker   for (base::ListValue::iterator i = tests->begin(); i != tests->end(); ++i) {
86*635a8641SAndroid Build Coastguard Worker     base::DictionaryValue* test = nullptr;
87*635a8641SAndroid Build Coastguard Worker     if (!i->GetAsDictionary(&test))
88*635a8641SAndroid Build Coastguard Worker       return false;
89*635a8641SAndroid Build Coastguard Worker 
90*635a8641SAndroid Build Coastguard Worker     TestIdentifier test_data;
91*635a8641SAndroid Build Coastguard Worker 
92*635a8641SAndroid Build Coastguard Worker     if (!test->GetStringASCII("test_case_name", &test_data.test_case_name))
93*635a8641SAndroid Build Coastguard Worker       return false;
94*635a8641SAndroid Build Coastguard Worker 
95*635a8641SAndroid Build Coastguard Worker     if (!test->GetStringASCII("test_name", &test_data.test_name))
96*635a8641SAndroid Build Coastguard Worker       return false;
97*635a8641SAndroid Build Coastguard Worker 
98*635a8641SAndroid Build Coastguard Worker     if (!test->GetStringASCII("file", &test_data.file))
99*635a8641SAndroid Build Coastguard Worker       return false;
100*635a8641SAndroid Build Coastguard Worker 
101*635a8641SAndroid Build Coastguard Worker     if (!test->GetInteger("line", &test_data.line))
102*635a8641SAndroid Build Coastguard Worker       return false;
103*635a8641SAndroid Build Coastguard Worker 
104*635a8641SAndroid Build Coastguard Worker     result.push_back(test_data);
105*635a8641SAndroid Build Coastguard Worker   }
106*635a8641SAndroid Build Coastguard Worker 
107*635a8641SAndroid Build Coastguard Worker   output->swap(result);
108*635a8641SAndroid Build Coastguard Worker   return true;
109*635a8641SAndroid Build Coastguard Worker }
110*635a8641SAndroid Build Coastguard Worker 
111*635a8641SAndroid Build Coastguard Worker }  // namespace base
112