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 102 103 104 105 106 107 108 109
|
/*
* Open Chinese Convert
*
* End-to-end validation of all configs against consolidated testcases.json.
*/
#ifndef BAZEL
// This test is Bazel-only; CMake builds should skip compiling it.
static_assert(false, "ConfigDictValidationTest is only supported under Bazel");
#else
#include <fstream>
#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>
#include "gtest/gtest.h"
#include "rapidjson/document.h"
#include "src/SimpleConverter.hpp"
#include "tools/cpp/runfiles/runfiles.h"
using bazel::tools::cpp::runfiles::Runfiles;
namespace opencc {
namespace {
class ConfigDictValidationTest : public ::testing::Test {
protected:
void SetUp() override {
#ifdef BAZEL
runfiles_.reset(Runfiles::CreateForTest());
ASSERT_NE(nullptr, runfiles_);
testcasesPath_ = runfiles_->Rlocation("_main/test/testcases/testcases.json");
configDir_ = runfiles_->Rlocation("_main/data/config");
dictDir_ = runfiles_->Rlocation("_main/data/dictionary");
#else
FAIL() << "This test expects Bazel runfiles.";
#endif
}
std::string ReadFile(const std::string& path) {
std::ifstream ifs(path);
EXPECT_TRUE(ifs.is_open()) << path;
std::stringstream buffer;
buffer << ifs.rdbuf();
return buffer.str();
}
SimpleConverter& GetConverter(const std::string& config) {
auto it = converters_.find(config);
if (it != converters_.end()) {
return *it->second;
}
const std::string configPath = configDir_ + "/" + config + ".json";
auto inserted = converters_.emplace(
config,
std::make_unique<SimpleConverter>(configPath,
std::vector<std::string>{
configDir_, dictDir_}));
return *inserted.first->second;
}
std::unique_ptr<Runfiles> runfiles_;
std::string testcasesPath_;
std::string configDir_;
std::string dictDir_;
std::unordered_map<std::string, std::unique_ptr<SimpleConverter>>
converters_;
};
TEST_F(ConfigDictValidationTest, ConvertExpectedOutputs) {
const std::string json = ReadFile(testcasesPath_);
rapidjson::Document doc;
doc.Parse(json.c_str());
ASSERT_FALSE(doc.HasParseError());
ASSERT_TRUE(doc.IsObject());
ASSERT_TRUE(doc.HasMember("cases"));
const auto& cases = doc["cases"];
ASSERT_TRUE(cases.IsArray());
for (auto& testcase : cases.GetArray()) {
ASSERT_TRUE(testcase.IsObject());
ASSERT_TRUE(testcase.HasMember("input"));
ASSERT_TRUE(testcase["input"].IsString());
const std::string input = testcase["input"].GetString();
const std::string id =
testcase.HasMember("id") && testcase["id"].IsString()
? testcase["id"].GetString()
: "(unknown id)";
ASSERT_TRUE(testcase.HasMember("expected"));
const auto& expectedObj = testcase["expected"];
ASSERT_TRUE(expectedObj.IsObject());
for (auto itr = expectedObj.MemberBegin(); itr != expectedObj.MemberEnd();
++itr) {
const std::string config = itr->name.GetString();
ASSERT_TRUE(itr->value.IsString());
const std::string expected = itr->value.GetString();
SimpleConverter& converter = GetConverter(config);
EXPECT_EQ(expected, converter.Convert(input))
<< "config=" << config << " case=" << id;
}
}
}
} // namespace
} // namespace opencc
#endif // BAZEL
|