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 | |
| 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 | |
| 12 | namespace base { |
| 13 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 14 | TestIdentifier::TestIdentifier() { |
| 15 | } |
| 16 | |
Paweł Hajdan | 19368c0 | 2015-01-23 13:33:49 | [diff] [blame] | 17 | std::string FormatFullTestName(const std::string& test_case_name, |
| 18 | const std::string& test_name) { |
| 19 | return test_case_name + "." + test_name; |
| 20 | } |
| 21 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 22 | std::vector<TestIdentifier> GetCompiledInTests() { |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 23 | testing::UnitTest* const unit_test = testing::UnitTest::GetInstance(); |
| 24 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 25 | std::vector<TestIdentifier> tests; |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 26 | 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.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 30 | 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, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | return tests; |
| 39 | } |
| 40 | |
| 41 | bool WriteCompiledInTestsToFile(const FilePath& path) { |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 42 | std::vector<TestIdentifier> tests(GetCompiledInTests()); |
Paweł Hajdan, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 43 | |
| 44 | ListValue root; |
| 45 | for (size_t i = 0; i < tests.size(); ++i) { |
| 46 | DictionaryValue* test_info = new DictionaryValue; |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 47 | 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, Jr | 70fcead | 2014-11-24 15:58:51 | [diff] [blame] | 51 | root.Append(test_info); |
| 52 | } |
| 53 | |
| 54 | JSONFileValueSerializer serializer(path); |
| 55 | return serializer.Serialize(root); |
| 56 | } |
| 57 | |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 58 | bool ReadTestNamesFromFile(const FilePath& path, |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 59 | std::vector<TestIdentifier>* output) { |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 60 | JSONFileValueDeserializer deserializer(path); |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 61 | int error_code = 0; |
| 62 | std::string error_message; |
olli.raula | ba04525 | 2015-10-16 06:16:40 | [diff] [blame^] | 63 | scoped_ptr<base::Value> value = |
| 64 | deserializer.Deserialize(&error_code, &error_message); |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 65 | if (!value.get()) |
| 66 | return false; |
| 67 | |
| 68 | base::ListValue* tests = nullptr; |
| 69 | if (!value->GetAsList(&tests)) |
| 70 | return false; |
| 71 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 72 | std::vector<base::TestIdentifier> result; |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 73 | 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.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 78 | TestIdentifier test_data; |
| 79 | |
| 80 | if (!test->GetStringASCII("test_case_name", &test_data.test_case_name)) |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 81 | return false; |
| 82 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 83 | if (!test->GetStringASCII("test_name", &test_data.test_name)) |
Paweł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 84 | return false; |
| 85 | |
phajdan.jr | b4cf4a09 | 2015-07-29 09:49:54 | [diff] [blame] | 86 | 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ł Hajdan | 26bc415 | 2015-01-15 13:32:01 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | output->swap(result); |
| 96 | return true; |
| 97 | } |
| 98 | |
prashhir | 54a99450 | 2015-03-05 09:30:57 | [diff] [blame] | 99 | } // namespace base |