Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 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 | |
avi | d351e5a | 2015-12-24 03:28:02 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 9 | #include <memory> |
| 10 | |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
| 12 | #include "base/json/json_file_value_serializer.h" |
stgao | 9537aa9 | 2016-10-31 21:38:44 | [diff] [blame] | 13 | #include "base/strings/string_util.h" |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 14 | #include "base/values.h" |
| 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
| 17 | namespace base { |
| 18 | |
Chris Watkins | bb7211c | 2017-11-29 07:16:38 | [diff] [blame^] | 19 | TestIdentifier::TestIdentifier() = default; |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 20 | |
vmpstr | e65942b | 2016-02-25 00:50:31 | [diff] [blame] | 21 | TestIdentifier::TestIdentifier(const TestIdentifier& other) = default; |
| 22 | |
Paweł Hajdan | 19368c0 | 2015-01-23 13:33:49 | [diff] [blame] | 23 | std::string FormatFullTestName(const std::string& test_case_name, |
| 24 | const std::string& test_name) { |
| 25 | return test_case_name + "." + test_name; |
| 26 | } |
| 27 | |
stgao | 9537aa9 | 2016-10-31 21:38:44 | [diff] [blame] | 28 | std::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.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 34 | std::vector<TestIdentifier> GetCompiledInTests() { |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 35 | testing::UnitTest* const unit_test = testing::UnitTest::GetInstance(); |
| 36 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 37 | std::vector<TestIdentifier> tests; |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 38 | 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.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 42 | 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, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | return tests; |
| 51 | } |
| 52 | |
| 53 | bool WriteCompiledInTestsToFile(const FilePath& path) { |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 54 | std::vector<TestIdentifier> tests(GetCompiledInTests()); |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 55 | |
| 56 | ListValue root; |
| 57 | for (size_t i = 0; i < tests.size(); ++i) { |
dcheng | 031a8f8 | 2016-09-08 21:04:33 | [diff] [blame] | 58 | std::unique_ptr<DictionaryValue> test_info(new DictionaryValue); |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 59 | 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); |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame] | 63 | root.Append(std::move(test_info)); |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | JSONFileValueSerializer serializer(path); |
| 67 | return serializer.Serialize(root); |
| 68 | } |
| 69 | |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 70 | bool ReadTestNamesFromFile(const FilePath& path, |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 71 | std::vector<TestIdentifier>* output) { |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 72 | JSONFileValueDeserializer deserializer(path); |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 73 | int error_code = 0; |
| 74 | std::string error_message; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 75 | std::unique_ptr<base::Value> value = |
olli.raula | ba04525 | 2015-10-16 06:16:40 | [diff] [blame] | 76 | deserializer.Deserialize(&error_code, &error_message); |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 77 | if (!value.get()) |
| 78 | return false; |
| 79 | |
| 80 | base::ListValue* tests = nullptr; |
| 81 | if (!value->GetAsList(&tests)) |
| 82 | return false; |
| 83 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 84 | std::vector<base::TestIdentifier> result; |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 85 | for (base::ListValue::iterator i = tests->begin(); i != tests->end(); ++i) { |
| 86 | base::DictionaryValue* test = nullptr; |
jdoerrie | a5676c6 | 2017-04-11 18:09:14 | [diff] [blame] | 87 | if (!i->GetAsDictionary(&test)) |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 88 | return false; |
| 89 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 90 | TestIdentifier test_data; |
| 91 | |
| 92 | if (!test->GetStringASCII("test_case_name", &test_data.test_case_name)) |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 93 | return false; |
| 94 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 95 | if (!test->GetStringASCII("test_name", &test_data.test_name)) |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 96 | return false; |
| 97 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 98 | 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ł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | output->swap(result); |
| 108 | return true; |
| 109 | } |
| 110 | |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 111 | } // namespace base |