blob: 3f44d748df3dbfddc1e936a84174ce957f932da2 [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
7#include "base/files/file_path.h"
8#include "base/json/json_file_value_serializer.h"
9#include "base/values.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12namespace base {
13
phajdan.jrb4cf4a092015-07-29 09:49:5414TestIdentifier::TestIdentifier() {
15}
16
Paweł Hajdan19368c02015-01-23 13:33:4917std::string FormatFullTestName(const std::string& test_case_name,
18 const std::string& test_name) {
19 return test_case_name + "." + test_name;
20}
21
phajdan.jrb4cf4a092015-07-29 09:49:5422std::vector<TestIdentifier> GetCompiledInTests() {
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5123 testing::UnitTest* const unit_test = testing::UnitTest::GetInstance();
24
phajdan.jrb4cf4a092015-07-29 09:49:5425 std::vector<TestIdentifier> tests;
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5126 for (int i = 0; i < unit_test->total_test_case_count(); ++i) {
27 const testing::TestCase* test_case = unit_test->GetTestCase(i);
28 for (int j = 0; j < test_case->total_test_count(); ++j) {
29 const testing::TestInfo* test_info = test_case->GetTestInfo(j);
phajdan.jrb4cf4a092015-07-29 09:49:5430 TestIdentifier test_data;
31 test_data.test_case_name = test_case->name();
32 test_data.test_name = test_info->name();
33 test_data.file = test_info->file();
34 test_data.line = test_info->line();
35 tests.push_back(test_data);
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5136 }
37 }
38 return tests;
39}
40
41bool WriteCompiledInTestsToFile(const FilePath& path) {
phajdan.jrb4cf4a092015-07-29 09:49:5442 std::vector<TestIdentifier> tests(GetCompiledInTests());
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5143
44 ListValue root;
45 for (size_t i = 0; i < tests.size(); ++i) {
46 DictionaryValue* test_info = new DictionaryValue;
phajdan.jrb4cf4a092015-07-29 09:49:5447 test_info->SetString("test_case_name", tests[i].test_case_name);
48 test_info->SetString("test_name", tests[i].test_name);
49 test_info->SetString("file", tests[i].file);
50 test_info->SetInteger("line", tests[i].line);
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5151 root.Append(test_info);
52 }
53
54 JSONFileValueSerializer serializer(path);
55 return serializer.Serialize(root);
56}
57
Paweł Hajdan26bc4152015-01-15 13:32:0158bool ReadTestNamesFromFile(const FilePath& path,
phajdan.jrb4cf4a092015-07-29 09:49:5459 std::vector<TestIdentifier>* output) {
prashhir54a994502015-03-05 09:30:5760 JSONFileValueDeserializer deserializer(path);
Paweł Hajdan26bc4152015-01-15 13:32:0161 int error_code = 0;
62 std::string error_message;
olli.raulaba045252015-10-16 06:16:4063 scoped_ptr<base::Value> value =
64 deserializer.Deserialize(&error_code, &error_message);
Paweł Hajdan26bc4152015-01-15 13:32:0165 if (!value.get())
66 return false;
67
68 base::ListValue* tests = nullptr;
69 if (!value->GetAsList(&tests))
70 return false;
71
phajdan.jrb4cf4a092015-07-29 09:49:5472 std::vector<base::TestIdentifier> result;
Paweł Hajdan26bc4152015-01-15 13:32:0173 for (base::ListValue::iterator i = tests->begin(); i != tests->end(); ++i) {
74 base::DictionaryValue* test = nullptr;
75 if (!(*i)->GetAsDictionary(&test))
76 return false;
77
phajdan.jrb4cf4a092015-07-29 09:49:5478 TestIdentifier test_data;
79
80 if (!test->GetStringASCII("test_case_name", &test_data.test_case_name))
Paweł Hajdan26bc4152015-01-15 13:32:0181 return false;
82
phajdan.jrb4cf4a092015-07-29 09:49:5483 if (!test->GetStringASCII("test_name", &test_data.test_name))
Paweł Hajdan26bc4152015-01-15 13:32:0184 return false;
85
phajdan.jrb4cf4a092015-07-29 09:49:5486 if (!test->GetStringASCII("file", &test_data.file))
87 return false;
88
89 if (!test->GetInteger("line", &test_data.line))
90 return false;
91
92 result.push_back(test_data);
Paweł Hajdan26bc4152015-01-15 13:32:0193 }
94
95 output->swap(result);
96 return true;
97}
98
prashhir54a994502015-03-05 09:30:5799} // namespace base