Home | History | Annotate | Download | only in test
      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 <stddef.h>
      8 
      9 #include <memory>
     10 
     11 #include "base/files/file_path.h"
     12 #include "base/json/json_file_value_serializer.h"
     13 #include "base/strings/string_util.h"
     14 #include "base/values.h"
     15 #include "testing/gtest/include/gtest/gtest.h"
     16 
     17 namespace base {
     18 
     19 TestIdentifier::TestIdentifier() = default;
     20 
     21 TestIdentifier::TestIdentifier(const TestIdentifier& other) = default;
     22 
     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 
     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 
     34 std::vector<TestIdentifier> GetCompiledInTests() {
     35   testing::UnitTest* const unit_test = testing::UnitTest::GetInstance();
     36 
     37   std::vector<TestIdentifier> tests;
     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);
     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);
     48     }
     49   }
     50   return tests;
     51 }
     52 
     53 bool WriteCompiledInTestsToFile(const FilePath& path) {
     54   std::vector<TestIdentifier> tests(GetCompiledInTests());
     55 
     56   ListValue root;
     57   for (size_t i = 0; i < tests.size(); ++i) {
     58     std::unique_ptr<DictionaryValue> test_info(new DictionaryValue);
     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);
     63     root.Append(std::move(test_info));
     64   }
     65 
     66   JSONFileValueSerializer serializer(path);
     67   return serializer.Serialize(root);
     68 }
     69 
     70 bool ReadTestNamesFromFile(const FilePath& path,
     71                            std::vector<TestIdentifier>* output) {
     72   JSONFileValueDeserializer deserializer(path);
     73   int error_code = 0;
     74   std::string error_message;
     75   std::unique_ptr<base::Value> value =
     76       deserializer.Deserialize(&error_code, &error_message);
     77   if (!value.get())
     78     return false;
     79 
     80   base::ListValue* tests = nullptr;
     81   if (!value->GetAsList(&tests))
     82     return false;
     83 
     84   std::vector<base::TestIdentifier> result;
     85   for (base::ListValue::iterator i = tests->begin(); i != tests->end(); ++i) {
     86     base::DictionaryValue* test = nullptr;
     87     if (!i->GetAsDictionary(&test))
     88       return false;
     89 
     90     TestIdentifier test_data;
     91 
     92     if (!test->GetStringASCII("test_case_name", &test_data.test_case_name))
     93       return false;
     94 
     95     if (!test->GetStringASCII("test_name", &test_data.test_name))
     96       return false;
     97 
     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);
    105   }
    106 
    107   output->swap(result);
    108   return true;
    109 }
    110 
    111 }  // namespace base
    112