blob: b9ab4a3a1b55c36164b3ba7ab22afc98930fe9b8 [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
Paweł Hajdan, Jr70fcead2014-11-24 15:58:519#include "base/files/file_path.h"
10#include "base/json/json_file_value_serializer.h"
11#include "base/values.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14namespace base {
15
phajdan.jrb4cf4a092015-07-29 09:49:5416TestIdentifier::TestIdentifier() {
17}
18
Paweł Hajdan19368c02015-01-23 13:33:4919std::string FormatFullTestName(const std::string& test_case_name,
20 const std::string& test_name) {
21 return test_case_name + "." + test_name;
22}
23
phajdan.jrb4cf4a092015-07-29 09:49:5424std::vector<TestIdentifier> GetCompiledInTests() {
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5125 testing::UnitTest* const unit_test = testing::UnitTest::GetInstance();
26
phajdan.jrb4cf4a092015-07-29 09:49:5427 std::vector<TestIdentifier> tests;
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5128 for (int i = 0; i < unit_test->total_test_case_count(); ++i) {
29 const testing::TestCase* test_case = unit_test->GetTestCase(i);
30 for (int j = 0; j < test_case->total_test_count(); ++j) {
31 const testing::TestInfo* test_info = test_case->GetTestInfo(j);
phajdan.jrb4cf4a092015-07-29 09:49:5432 TestIdentifier test_data;
33 test_data.test_case_name = test_case->name();
34 test_data.test_name = test_info->name();
35 test_data.file = test_info->file();
36 test_data.line = test_info->line();
37 tests.push_back(test_data);
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5138 }
39 }
40 return tests;
41}
42
43bool WriteCompiledInTestsToFile(const FilePath& path) {
phajdan.jrb4cf4a092015-07-29 09:49:5444 std::vector<TestIdentifier> tests(GetCompiledInTests());
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5145
46 ListValue root;
47 for (size_t i = 0; i < tests.size(); ++i) {
48 DictionaryValue* test_info = new DictionaryValue;
phajdan.jrb4cf4a092015-07-29 09:49:5449 test_info->SetString("test_case_name", tests[i].test_case_name);
50 test_info->SetString("test_name", tests[i].test_name);
51 test_info->SetString("file", tests[i].file);
52 test_info->SetInteger("line", tests[i].line);
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5153 root.Append(test_info);
54 }
55
56 JSONFileValueSerializer serializer(path);
57 return serializer.Serialize(root);
58}
59
Paweł Hajdan26bc4152015-01-15 13:32:0160bool ReadTestNamesFromFile(const FilePath& path,
phajdan.jrb4cf4a092015-07-29 09:49:5461 std::vector<TestIdentifier>* output) {
prashhir54a994502015-03-05 09:30:5762 JSONFileValueDeserializer deserializer(path);
Paweł Hajdan26bc4152015-01-15 13:32:0163 int error_code = 0;
64 std::string error_message;
olli.raulaba045252015-10-16 06:16:4065 scoped_ptr<base::Value> value =
66 deserializer.Deserialize(&error_code, &error_message);
Paweł Hajdan26bc4152015-01-15 13:32:0167 if (!value.get())
68 return false;
69
70 base::ListValue* tests = nullptr;
71 if (!value->GetAsList(&tests))
72 return false;
73
phajdan.jrb4cf4a092015-07-29 09:49:5474 std::vector<base::TestIdentifier> result;
Paweł Hajdan26bc4152015-01-15 13:32:0175 for (base::ListValue::iterator i = tests->begin(); i != tests->end(); ++i) {
76 base::DictionaryValue* test = nullptr;
77 if (!(*i)->GetAsDictionary(&test))
78 return false;
79
phajdan.jrb4cf4a092015-07-29 09:49:5480 TestIdentifier test_data;
81
82 if (!test->GetStringASCII("test_case_name", &test_data.test_case_name))
Paweł Hajdan26bc4152015-01-15 13:32:0183 return false;
84
phajdan.jrb4cf4a092015-07-29 09:49:5485 if (!test->GetStringASCII("test_name", &test_data.test_name))
Paweł Hajdan26bc4152015-01-15 13:32:0186 return false;
87
phajdan.jrb4cf4a092015-07-29 09:49:5488 if (!test->GetStringASCII("file", &test_data.file))
89 return false;
90
91 if (!test->GetInteger("line", &test_data.line))
92 return false;
93
94 result.push_back(test_data);
Paweł Hajdan26bc4152015-01-15 13:32:0195 }
96
97 output->swap(result);
98 return true;
99}
100
prashhir54a994502015-03-05 09:30:57101} // namespace base