data_driven_test.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
1// Copyright 2013 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 "components/autofill/core/browser/data_driven_test.h" 6 7#include "base/file_util.h" 8#include "base/files/file_enumerator.h" 9#include "base/path_service.h" 10#include "base/strings/string_util.h" 11#include "chrome/common/chrome_paths.h" 12#include "testing/gtest/include/gtest/gtest.h" 13 14namespace autofill { 15namespace { 16 17// Reads |file| into |content|, and converts Windows line-endings to Unix ones. 18// Returns true on success. 19bool ReadFile(const base::FilePath& file, std::string* content) { 20 if (!file_util::ReadFileToString(file, content)) 21 return false; 22 23 ReplaceSubstringsAfterOffset(content, 0, "\r\n", "\n"); 24 return true; 25} 26 27// Write |content| to |file|. Returns true on success. 28bool WriteFile(const base::FilePath& file, const std::string& content) { 29 int write_size = file_util::WriteFile(file, content.c_str(), 30 content.length()); 31 return write_size == static_cast<int>(content.length()); 32} 33 34} // namespace 35 36void DataDrivenTest::RunDataDrivenTest( 37 const base::FilePath& input_directory, 38 const base::FilePath& output_directory, 39 const base::FilePath::StringType& file_name_pattern) { 40 base::FileEnumerator input_files(input_directory, 41 false, 42 base::FileEnumerator::FILES, 43 file_name_pattern); 44 45 for (base::FilePath input_file = input_files.Next(); 46 !input_file.empty(); 47 input_file = input_files.Next()) { 48 SCOPED_TRACE(input_file.BaseName().value()); 49 50 std::string input; 51 ASSERT_TRUE(ReadFile(input_file, &input)); 52 53 std::string output; 54 GenerateResults(input, &output); 55 56 base::FilePath output_file = output_directory.Append( 57 input_file.BaseName().StripTrailingSeparators().ReplaceExtension( 58 FILE_PATH_LITERAL(".out"))); 59 60 std::string output_file_contents; 61 if (ReadFile(output_file, &output_file_contents)) 62 EXPECT_EQ(output_file_contents, output); 63 else 64 ASSERT_TRUE(WriteFile(output_file, output)); 65 } 66} 67 68base::FilePath DataDrivenTest::GetInputDirectory( 69 const base::FilePath::StringType& test_name) { 70 base::FilePath test_data_dir_; 71 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_); 72 test_data_dir_ = test_data_dir_.AppendASCII("autofill") 73 .Append(test_name) 74 .AppendASCII("input"); 75 return test_data_dir_; 76} 77 78base::FilePath DataDrivenTest::GetOutputDirectory( 79 const base::FilePath::StringType& test_name) { 80 base::FilePath test_data_dir_; 81 PathService::Get(chrome::DIR_TEST_DATA, &test_data_dir_); 82 test_data_dir_ = test_data_dir_.AppendASCII("autofill") 83 .Append(test_name) 84 .AppendASCII("output"); 85 return test_data_dir_; 86} 87 88DataDrivenTest::DataDrivenTest() { 89} 90 91DataDrivenTest::~DataDrivenTest() { 92} 93 94} // namespace autofill 95