| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Validate bundled JSON configurations against opencc_config.schema.json. |
| 5 | */ |
| 6 | |
| 7 | #ifndef BAZEL |
| 8 | // This test is Bazel-only; CMake builds should skip compiling it. |
| 9 | static_assert(false, "ConfigSchemaValidationTest is only supported under Bazel"); |
| 10 | #else |
| 11 | |
| 12 | #include <fstream> |
| 13 | #include <memory> |
| 14 | #include <sstream> |
| 15 | #include <string> |
| 16 | |
| 17 | #include "gtest/gtest.h" |
| 18 | #include "rapidjson/document.h" |
| 19 | #include "rapidjson/error/en.h" |
| 20 | #include "rapidjson/schema.h" |
| 21 | #include "rapidjson/stringbuffer.h" |
| 22 | |
| 23 | #include "tools/cpp/runfiles/runfiles.h" |
| 24 | using bazel::tools::cpp::runfiles::Runfiles; |
| 25 | |
| 26 | namespace opencc { |
| 27 | namespace { |
| 28 | |
| 29 | #ifndef OPENCC_CONFIG_SCHEMA_TEST_FILE |
| 30 | #error "OPENCC_CONFIG_SCHEMA_TEST_FILE must be defined" |
| 31 | #endif |
| 32 | |
| 33 | std::string ReadFile(const std::string& path) { |
| 34 | std::ifstream ifs(path); |
| 35 | EXPECT_TRUE(ifs.is_open()) << path; |
| 36 | std::stringstream buffer; |
| 37 | buffer << ifs.rdbuf(); |
| 38 | return buffer.str(); |
| 39 | } |
| 40 | |
| 41 | std::string StringifyPointer(const rapidjson::Pointer& pointer) { |
| 42 | rapidjson::StringBuffer buffer; |
| 43 | pointer.Stringify(buffer); |
| 44 | return buffer.GetString(); |
| 45 | } |
| 46 | |
| 47 | TEST(ConfigSchemaValidationTest, BundledConfigsMatchSchema) { |
| 48 | std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest()); |
| 49 | ASSERT_NE(nullptr, runfiles); |
| 50 | |
| 51 | // OPENCC_CONFIG_SCHEMA_TEST_FILE must be a workspace-relative path, |
| 52 | // e.g. "data/config/s2t.json" or "test/config_test/config_test.json". |
| 53 | const std::string configFile = OPENCC_CONFIG_SCHEMA_TEST_FILE; |
| 54 | const std::string schemaPath = |
| 55 | runfiles->Rlocation("_main/data/config/opencc_config.schema.json"); |
| 56 | const std::string schemaJson = ReadFile(schemaPath); |
| 57 | rapidjson::Document schemaDoc; |
| 58 | schemaDoc.Parse(schemaJson.c_str()); |
| 59 | ASSERT_FALSE(schemaDoc.HasParseError()) |
| 60 | << "schema parse error at offset " << schemaDoc.GetErrorOffset() << ": " |
| 61 | << rapidjson::GetParseError_En(schemaDoc.GetParseError()); |
| 62 | ASSERT_TRUE(schemaDoc.IsObject()); |
| 63 | rapidjson::SchemaDocument schema(schemaDoc); |
| 64 | |
| 65 | const std::string configPath = runfiles->Rlocation("_main/" + configFile); |
| 66 | const std::string configJson = ReadFile(configPath); |
| 67 | rapidjson::Document configDoc; |
| 68 | configDoc.Parse(configJson.c_str()); |
| 69 | ASSERT_FALSE(configDoc.HasParseError()) |
| 70 | << configFile << " parse error at offset " << configDoc.GetErrorOffset() |
| 71 | << ": " << rapidjson::GetParseError_En(configDoc.GetParseError()); |
| 72 | |
| 73 | rapidjson::SchemaValidator validator(schema); |
| 74 | EXPECT_TRUE(configDoc.Accept(validator)) |
| 75 | << configFile << " violates schema at document path " |
| 76 | << StringifyPointer(validator.GetInvalidDocumentPointer()) |
| 77 | << ", schema path " |
| 78 | << StringifyPointer(validator.GetInvalidSchemaPointer()) << ", keyword " |
| 79 | << validator.GetInvalidSchemaKeyword(); |
| 80 | } |
| 81 | |
| 82 | } // namespace |
| 83 | } // namespace opencc |
| 84 | |
| 85 | #endif // BAZEL |