1 // Copyright 2014 The Chromium Authors. All rights reserved. 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/values.h" 15 #include "testing/gtest/include/gtest/gtest.h" 16 17 namespace base { 18 19 TestIdentifier::TestIdentifier() { 20 } 21 22 TestIdentifier::TestIdentifier(const TestIdentifier& other) = default; 23 24 std::string FormatFullTestName(const std::string& test_case_name, 25 const std::string& test_name) { 26 return test_case_name + "." + test_name; 27 } 28 29 std::string TestNameWithoutDisabledPrefix(const std::string& full_test_name) { 30 std::string test_name_no_disabled(full_test_name); 31 ReplaceSubstringsAfterOffset(&test_name_no_disabled, 0, "DISABLED_", ""); 32 return test_name_no_disabled; 33 } 34 35 std::vector<TestIdentifier> GetCompiledInTests() { 36 testing::UnitTest* const unit_test = testing::UnitTest::GetInstance(); 37 38 std::vector<TestIdentifier> tests; 39 for (int i = 0; i < unit_test->total_test_case_count(); ++i) { 40 const testing::TestCase* test_case = unit_test->GetTestCase(i); 41 for (int j = 0; j < test_case->total_test_count(); ++j) { 42 const testing::TestInfo* test_info = test_case->GetTestInfo(j); 43 TestIdentifier test_data; 44 test_data.test_case_name = test_case->name(); 45 test_data.test_name = test_info->name(); 46 test_data.file = test_info->file(); 47 test_data.line = test_info->line(); 48 tests.push_back(test_data); 49 } 50 } 51 return tests; 52 } 53 54 bool WriteCompiledInTestsToFile(const FilePath& path) { 55 std::vector<TestIdentifier> tests(GetCompiledInTests()); 56 57 ListValue root; 58 for (size_t i = 0; i < tests.size(); ++i) { 59 std::unique_ptr<DictionaryValue> test_info(new DictionaryValue); 60 test_info->SetString("test_case_name", tests[i].test_case_name); 61 test_info->SetString("test_name", tests[i].test_name); 62 test_info->SetString("file", tests[i].file); 63 test_info->SetInteger("line", tests[i].line); 64 root.Append(std::move(test_info)); 65 } 66 67 JSONFileValueSerializer serializer(path); 68 return serializer.Serialize(root); 69 } 70 71 bool ReadTestNamesFromFile(const FilePath& path, 72 std::vector<TestIdentifier>* output) { 73 JSONFileValueDeserializer deserializer(path); 74 int error_code = 0; 75 std::string error_message; 76 std::unique_ptr<base::Value> value = 77 deserializer.Deserialize(&error_code, &error_message); 78 if (!value.get()) 79 return false; 80 81 base::ListValue* tests = nullptr; 82 if (!value->GetAsList(&tests)) 83 return false; 84 85 std::vector<base::TestIdentifier> result; 86 for (base::ListValue::iterator i = tests->begin(); i != tests->end(); ++i) { 87 base::DictionaryValue* test = nullptr; 88 if (!(*i)->GetAsDictionary(&test)) 89 return false; 90 91 TestIdentifier test_data; 92 93 if (!test->GetStringASCII("test_case_name", &test_data.test_case_name)) 94 return false; 95 96 if (!test->GetStringASCII("test_name", &test_data.test_name)) 97 return false; 98 99 if (!test->GetStringASCII("file", &test_data.file)) 100 return false; 101 102 if (!test->GetInteger("line", &test_data.line)) 103 return false; 104 105 result.push_back(test_data); 106 } 107 108 output->swap(result); 109 return true; 110 } 111 112 } // namespace base 113