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" |
| 13 | #include "base/values.h" |
| 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
| 16 | namespace base { |
| 17 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 18 | TestIdentifier::TestIdentifier() { |
| 19 | } |
| 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 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 28 | std::vector<TestIdentifier> GetCompiledInTests() { |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 29 | testing::UnitTest* const unit_test = testing::UnitTest::GetInstance(); |
| 30 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 31 | std::vector<TestIdentifier> tests; |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 32 | 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.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 36 | 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, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | return tests; |
| 45 | } |
| 46 | |
| 47 | bool WriteCompiledInTestsToFile(const FilePath& path) { |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 48 | std::vector<TestIdentifier> tests(GetCompiledInTests()); |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 49 | |
| 50 | ListValue root; |
| 51 | for (size_t i = 0; i < tests.size(); ++i) { |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame^] | 52 | std::unique_ptr<class base::DictionaryValue> test_info(new DictionaryValue); |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 53 | 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); |
dcheng | 98e96a7 | 2016-06-11 03:41:48 | [diff] [blame^] | 57 | root.Append(std::move(test_info)); |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | JSONFileValueSerializer serializer(path); |
| 61 | return serializer.Serialize(root); |
| 62 | } |
| 63 | |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 64 | bool ReadTestNamesFromFile(const FilePath& path, |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 65 | std::vector<TestIdentifier>* output) { |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 66 | JSONFileValueDeserializer deserializer(path); |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 67 | int error_code = 0; |
| 68 | std::string error_message; |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 69 | std::unique_ptr<base::Value> value = |
olli.raula | ba04525 | 2015-10-16 06:16:40 | [diff] [blame] | 70 | deserializer.Deserialize(&error_code, &error_message); |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 71 | if (!value.get()) |
| 72 | return false; |
| 73 | |
| 74 | base::ListValue* tests = nullptr; |
| 75 | if (!value->GetAsList(&tests)) |
| 76 | return false; |
| 77 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 78 | std::vector<base::TestIdentifier> result; |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 79 | 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.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 84 | TestIdentifier test_data; |
| 85 | |
| 86 | if (!test->GetStringASCII("test_case_name", &test_data.test_case_name)) |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 87 | return false; |
| 88 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 89 | if (!test->GetStringASCII("test_name", &test_data.test_name)) |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 90 | return false; |
| 91 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 92 | 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ł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | output->swap(result); |
| 102 | return true; |
| 103 | } |
| 104 | |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 105 | } // namespace base |