blob: 50ac9e6ff6cd0fc59be22a50a750ee9004ff0811 [file] [log] [blame]
Paweł Hajdan, Jr70fcead2014-11-24 15:58:511// 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
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"
13#include "base/values.h"
14#include "testing/gtest/include/gtest/gtest.h"
15
16namespace base {
17
phajdan.jrb4cf4a092015-07-29 09:49:5418TestIdentifier::TestIdentifier() {
19}
20
vmpstre65942b2016-02-25 00:50:3121TestIdentifier::TestIdentifier(const TestIdentifier& other) = default;
22
Paweł Hajdan19368c02015-01-23 13:33:4923std::string FormatFullTestName(const std::string& test_case_name,
24 const std::string& test_name) {
25 return test_case_name + "." + test_name;
26}
27
phajdan.jrb4cf4a092015-07-29 09:49:5428std::vector<TestIdentifier> GetCompiledInTests() {
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5129 testing::UnitTest* const unit_test = testing::UnitTest::GetInstance();
30
phajdan.jrb4cf4a092015-07-29 09:49:5431 std::vector<TestIdentifier> tests;
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5132 for (int i = 0; i < unit_test->total_test_case_count(); ++i) {
33 const testing::TestCase* test_case = unit_test->GetTestCase(i);
34 for (int j = 0; j < test_case->total_test_count(); ++j) {
35 const testing::TestInfo* test_info = test_case->GetTestInfo(j);
phajdan.jrb4cf4a092015-07-29 09:49:5436 TestIdentifier test_data;
37 test_data.test_case_name = test_case->name();
38 test_data.test_name = test_info->name();
39 test_data.file = test_info->file();
40 test_data.line = test_info->line();
41 tests.push_back(test_data);
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5142 }
43 }
44 return tests;
45}
46
47bool WriteCompiledInTestsToFile(const FilePath& path) {
phajdan.jrb4cf4a092015-07-29 09:49:5448 std::vector<TestIdentifier> tests(GetCompiledInTests());
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5149
50 ListValue root;
51 for (size_t i = 0; i < tests.size(); ++i) {
dcheng98e96a72016-06-11 03:41:4852 std::unique_ptr<class base::DictionaryValue> test_info(new DictionaryValue);
phajdan.jrb4cf4a092015-07-29 09:49:5453 test_info->SetString("test_case_name", tests[i].test_case_name);
54 test_info->SetString("test_name", tests[i].test_name);
55 test_info->SetString("file", tests[i].file);
56 test_info->SetInteger("line", tests[i].line);
dcheng98e96a72016-06-11 03:41:4857 root.Append(std::move(test_info));
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5158 }
59
60 JSONFileValueSerializer serializer(path);
61 return serializer.Serialize(root);
62}
63
Paweł Hajdan26bc4152015-01-15 13:32:0164bool ReadTestNamesFromFile(const FilePath& path,
phajdan.jrb4cf4a092015-07-29 09:49:5465 std::vector<TestIdentifier>* output) {
prashhir54a994502015-03-05 09:30:5766 JSONFileValueDeserializer deserializer(path);
Paweł Hajdan26bc4152015-01-15 13:32:0167 int error_code = 0;
68 std::string error_message;
dcheng093de9b2016-04-04 21:25:5169 std::unique_ptr<base::Value> value =
olli.raulaba045252015-10-16 06:16:4070 deserializer.Deserialize(&error_code, &error_message);
Paweł Hajdan26bc4152015-01-15 13:32:0171 if (!value.get())
72 return false;
73
74 base::ListValue* tests = nullptr;
75 if (!value->GetAsList(&tests))
76 return false;
77
phajdan.jrb4cf4a092015-07-29 09:49:5478 std::vector<base::TestIdentifier> result;
Paweł Hajdan26bc4152015-01-15 13:32:0179 for (base::ListValue::iterator i = tests->begin(); i != tests->end(); ++i) {
80 base::DictionaryValue* test = nullptr;
81 if (!(*i)->GetAsDictionary(&test))
82 return false;
83
phajdan.jrb4cf4a092015-07-29 09:49:5484 TestIdentifier test_data;
85
86 if (!test->GetStringASCII("test_case_name", &test_data.test_case_name))
Paweł Hajdan26bc4152015-01-15 13:32:0187 return false;
88
phajdan.jrb4cf4a092015-07-29 09:49:5489 if (!test->GetStringASCII("test_name", &test_data.test_name))
Paweł Hajdan26bc4152015-01-15 13:32:0190 return false;
91
phajdan.jrb4cf4a092015-07-29 09:49:5492 if (!test->GetStringASCII("file", &test_data.file))
93 return false;
94
95 if (!test->GetInteger("line", &test_data.line))
96 return false;
97
98 result.push_back(test_data);
Paweł Hajdan26bc4152015-01-15 13:32:0199 }
100
101 output->swap(result);
102 return true;
103}
104
prashhir54a994502015-03-05 09:30:57105} // namespace base