summaryrefslogtreecommitdiffstats
path: root/base/test/gtest_util.cc
blob: b9ab4a3a1b55c36164b3ba7ab22afc98930fe9b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/test/gtest_util.h"

#include <stddef.h>

#include "base/files/file_path.h"
#include "base/json/json_file_value_serializer.h"
#include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

TestIdentifier::TestIdentifier() {
}

std::string FormatFullTestName(const std::string& test_case_name,
                               const std::string& test_name) {
  return test_case_name + "." + test_name;
}

std::vector<TestIdentifier> GetCompiledInTests() {
  testing::UnitTest* const unit_test = testing::UnitTest::GetInstance();

  std::vector<TestIdentifier> tests;
  for (int i = 0; i < unit_test->total_test_case_count(); ++i) {
    const testing::TestCase* test_case = unit_test->GetTestCase(i);
    for (int j = 0; j < test_case->total_test_count(); ++j) {
      const testing::TestInfo* test_info = test_case->GetTestInfo(j);
      TestIdentifier test_data;
      test_data.test_case_name = test_case->name();
      test_data.test_name = test_info->name();
      test_data.file = test_info->file();
      test_data.line = test_info->line();
      tests.push_back(test_data);
    }
  }
  return tests;
}

bool WriteCompiledInTestsToFile(const FilePath& path) {
  std::vector<TestIdentifier> tests(GetCompiledInTests());

  ListValue root;
  for (size_t i = 0; i < tests.size(); ++i) {
    DictionaryValue* test_info = new DictionaryValue;
    test_info->SetString("test_case_name", tests[i].test_case_name);
    test_info->SetString("test_name", tests[i].test_name);
    test_info->SetString("file", tests[i].file);
    test_info->SetInteger("line", tests[i].line);
    root.Append(test_info);
  }

  JSONFileValueSerializer serializer(path);
  return serializer.Serialize(root);
}

bool ReadTestNamesFromFile(const FilePath& path,
                           std::vector<TestIdentifier>* output) {
  JSONFileValueDeserializer deserializer(path);
  int error_code = 0;
  std::string error_message;
  scoped_ptr<base::Value> value =
      deserializer.Deserialize(&error_code, &error_message);
  if (!value.get())
    return false;

  base::ListValue* tests = nullptr;
  if (!value->GetAsList(&tests))
    return false;

  std::vector<base::TestIdentifier> result;
  for (base::ListValue::iterator i = tests->begin(); i != tests->end(); ++i) {
    base::DictionaryValue* test = nullptr;
    if (!(*i)->GetAsDictionary(&test))
      return false;

    TestIdentifier test_data;

    if (!test->GetStringASCII("test_case_name", &test_data.test_case_name))
      return false;

    if (!test->GetStringASCII("test_name", &test_data.test_name))
      return false;

    if (!test->GetStringASCII("file", &test_data.file))
      return false;

    if (!test->GetInteger("line", &test_data.line))
      return false;

    result.push_back(test_data);
  }

  output->swap(result);
  return true;
}

}  // namespace base