blob: e5d38f44ae8a308c3f225d129fd5d1f0c8501977 [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"
stgao9537aa92016-10-31 21:38:4413#include "base/strings/string_util.h"
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5114#include "base/values.h"
15#include "testing/gtest/include/gtest/gtest.h"
16
17namespace base {
18
Chris Watkinsbb7211c2017-11-29 07:16:3819TestIdentifier::TestIdentifier() = default;
phajdan.jrb4cf4a092015-07-29 09:49:5420
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
stgao9537aa92016-10-31 21:38:4428std::string TestNameWithoutDisabledPrefix(const std::string& full_test_name) {
29 std::string test_name_no_disabled(full_test_name);
30 ReplaceSubstringsAfterOffset(&test_name_no_disabled, 0, "DISABLED_", "");
31 return test_name_no_disabled;
32}
33
phajdan.jrb4cf4a092015-07-29 09:49:5434std::vector<TestIdentifier> GetCompiledInTests() {
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5135 testing::UnitTest* const unit_test = testing::UnitTest::GetInstance();
36
phajdan.jrb4cf4a092015-07-29 09:49:5437 std::vector<TestIdentifier> tests;
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5138 for (int i = 0; i < unit_test->total_test_case_count(); ++i) {
39 const testing::TestCase* test_case = unit_test->GetTestCase(i);
40 for (int j = 0; j < test_case->total_test_count(); ++j) {
41 const testing::TestInfo* test_info = test_case->GetTestInfo(j);
phajdan.jrb4cf4a092015-07-29 09:49:5442 TestIdentifier test_data;
43 test_data.test_case_name = test_case->name();
44 test_data.test_name = test_info->name();
45 test_data.file = test_info->file();
46 test_data.line = test_info->line();
47 tests.push_back(test_data);
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5148 }
49 }
50 return tests;
51}
52
53bool WriteCompiledInTestsToFile(const FilePath& path) {
phajdan.jrb4cf4a092015-07-29 09:49:5454 std::vector<TestIdentifier> tests(GetCompiledInTests());
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5155
56 ListValue root;
57 for (size_t i = 0; i < tests.size(); ++i) {
dcheng031a8f82016-09-08 21:04:3358 std::unique_ptr<DictionaryValue> test_info(new DictionaryValue);
phajdan.jrb4cf4a092015-07-29 09:49:5459 test_info->SetString("test_case_name", tests[i].test_case_name);
60 test_info->SetString("test_name", tests[i].test_name);
61 test_info->SetString("file", tests[i].file);
62 test_info->SetInteger("line", tests[i].line);
dcheng98e96a72016-06-11 03:41:4863 root.Append(std::move(test_info));
Paweł Hajdan, Jr70fcead2014-11-24 15:58:5164 }
65
66 JSONFileValueSerializer serializer(path);
67 return serializer.Serialize(root);
68}
69
Paweł Hajdan26bc4152015-01-15 13:32:0170bool ReadTestNamesFromFile(const FilePath& path,
phajdan.jrb4cf4a092015-07-29 09:49:5471 std::vector<TestIdentifier>* output) {
prashhir54a994502015-03-05 09:30:5772 JSONFileValueDeserializer deserializer(path);
Paweł Hajdan26bc4152015-01-15 13:32:0173 int error_code = 0;
74 std::string error_message;
dcheng093de9b2016-04-04 21:25:5175 std::unique_ptr<base::Value> value =
olli.raulaba045252015-10-16 06:16:4076 deserializer.Deserialize(&error_code, &error_message);
Paweł Hajdan26bc4152015-01-15 13:32:0177 if (!value.get())
78 return false;
79
80 base::ListValue* tests = nullptr;
81 if (!value->GetAsList(&tests))
82 return false;
83
phajdan.jrb4cf4a092015-07-29 09:49:5484 std::vector<base::TestIdentifier> result;
Paweł Hajdan26bc4152015-01-15 13:32:0185 for (base::ListValue::iterator i = tests->begin(); i != tests->end(); ++i) {
86 base::DictionaryValue* test = nullptr;
jdoerriea5676c62017-04-11 18:09:1487 if (!i->GetAsDictionary(&test))
Paweł Hajdan26bc4152015-01-15 13:32:0188 return false;
89
phajdan.jrb4cf4a092015-07-29 09:49:5490 TestIdentifier test_data;
91
92 if (!test->GetStringASCII("test_case_name", &test_data.test_case_name))
Paweł Hajdan26bc4152015-01-15 13:32:0193 return false;
94
phajdan.jrb4cf4a092015-07-29 09:49:5495 if (!test->GetStringASCII("test_name", &test_data.test_name))
Paweł Hajdan26bc4152015-01-15 13:32:0196 return false;
97
phajdan.jrb4cf4a092015-07-29 09:49:5498 if (!test->GetStringASCII("file", &test_data.file))
99 return false;
100
101 if (!test->GetInteger("line", &test_data.line))
102 return false;
103
104 result.push_back(test_data);
Paweł Hajdan26bc4152015-01-15 13:32:01105 }
106
107 output->swap(result);
108 return true;
109}
110
prashhir54a994502015-03-05 09:30:57111} // namespace base