Use UintToString() for unsigned values.
[chromium-blink-merge.git] / base / test / gtest_util.cc
blob19cec516b1469792404898e24109c9746154089f
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.
5 #include "base/test/gtest_util.h"
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"
12 namespace base {
14 TestIdentifier::TestIdentifier() {
17 std::string FormatFullTestName(const std::string& test_case_name,
18 const std::string& test_name) {
19 return test_case_name + "." + test_name;
22 std::vector<TestIdentifier> GetCompiledInTests() {
23 testing::UnitTest* const unit_test = testing::UnitTest::GetInstance();
25 std::vector<TestIdentifier> tests;
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);
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);
38 return tests;
41 bool WriteCompiledInTestsToFile(const FilePath& path) {
42 std::vector<TestIdentifier> tests(GetCompiledInTests());
44 ListValue root;
45 for (size_t i = 0; i < tests.size(); ++i) {
46 DictionaryValue* test_info = new DictionaryValue;
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);
51 root.Append(test_info);
54 JSONFileValueSerializer serializer(path);
55 return serializer.Serialize(root);
58 bool ReadTestNamesFromFile(const FilePath& path,
59 std::vector<TestIdentifier>* output) {
60 JSONFileValueDeserializer deserializer(path);
61 int error_code = 0;
62 std::string error_message;
63 scoped_ptr<base::Value> value(
64 deserializer.Deserialize(&error_code, &error_message));
65 if (!value.get())
66 return false;
68 base::ListValue* tests = nullptr;
69 if (!value->GetAsList(&tests))
70 return false;
72 std::vector<base::TestIdentifier> result;
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;
78 TestIdentifier test_data;
80 if (!test->GetStringASCII("test_case_name", &test_data.test_case_name))
81 return false;
83 if (!test->GetStringASCII("test_name", &test_data.test_name))
84 return false;
86 if (!test->GetStringASCII("file", &test_data.file))
87 return false;
89 if (!test->GetInteger("line", &test_data.line))
90 return false;
92 result.push_back(test_data);
95 output->swap(result);
96 return true;
99 } // namespace base