#include "suites/test_suites.hpp" #include "support/dom_helpers.hpp" namespace { /* ** Схема с пространством имён, наследованием, simpleContent, xs:all, xs:any, группами и ID. ** Группа с xs:all — вся модель элемента opts: внутри xs:sequence она недопустима (cos-all-limited). ** ** A schema with a namespace, derivation, simpleContent, xs:all, xs:any, groups and IDs. ** The group with xs:all is the whole model of the opts element: inside xs:sequence it is not allowed (cos-all-limited). */ const char* const ADVANCED_XSD = "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""; // Число ошибок для документа в пространстве urn:t // The number of errors for a document in the urn:t namespace std::size_t errorsFor(const std::string& body) { static const std::shared_ptr SCHEMA = beresta::xsd::Schema::fromString(ADVANCED_XSD); beresta::Document document = beresta::XmlParser().parse( "" + body + ""); return beresta::xsd::SchemaValidator(SCHEMA).validate(document).size(); } /* ** Корректный документ: расширение типа, simpleContent, xs:all в любом ** порядке, xs:any, ID/IDREF, xsi:nil. Затем по одному нарушению ** каждого правила: атрибут simpleContent, xs:all, IDREF, повтор ID, nil, ##other. ** ** A valid document: a type extension, simpleContent, xs:all in any ** order, xs:any, ID/IDREF, xsi:nil. Then one violation of ** each rule: a simpleContent attribute, xs:all, IDREF, a repeated ID, nil, ##other. */ void testAdvancedRules() { TEST_ASSERT(errorsFor("i110.5" "i21i121" "") == 0); TEST_ASSERT(errorsFor("i110") == 1); TEST_ASSERT(errorsFor("i111") == 1); TEST_ASSERT(errorsFor("i11none") == 1); TEST_ASSERT(errorsFor("i11" "i12") == 1); TEST_ASSERT(errorsFor("x") == 1); TEST_ASSERT(errorsFor("i11") == 1); } // Проверка схемой прямо при разборе: ошибки со строками приходят в ErrorHandler // Schema validation right during parsing: errors with lines arrive in the ErrorHandler void testParserIntegration() { beresta::CollectingErrorHandler handler; beresta::ParserOptions options; options.schema = beresta::xsd::Schema::fromString(ADVANCED_XSD); options.errorHandler = &handler; beresta::XmlParser(options).parse("\n\n1x\n" "abc\n\n"); TEST_ASSERT(handler.getDiagnostics().size() == 2); TEST_ASSERT(handler.getDiagnostics()[0].position.line == 3); beresta::ParserOptions strict; strict.schema = options.schema; TEST_ASSERT_THROWS(beresta::XmlParser(strict).parse(""), beresta::ParseException); } // xs:include через FileResourceResolver // xs:include through FileResourceResolver void testSchemaInclude() { const support::TestDirectory temporary("xsd_include"); const auto& root = temporary.path; support::writeTestFile(root / "types.xsd", "" "" ""); support::writeTestFile(root / "main.xsd", "" ""); beresta::FileResourceResolver resolver(root); beresta::xsd::SchemaLoadOptions options; options.resolver = &resolver; auto schema = beresta::xsd::Schema::fromFile(root / "main.xsd", options); beresta::xsd::SchemaValidator validator(schema); TEST_ASSERT(validator.isValid(beresta::XmlParser().parse("ABC"))); TEST_ASSERT(!validator.isValid(beresta::XmlParser().parse("ABCD"))); } } namespace suites { // Регистрация расширенных тестов XSD // Registers the advanced XSD tests void registerXsdAdvancedTests(test::TestRunner& runner) { runner.add("XSD: наследование, all, any, ID и их нарушения", testAdvancedRules); runner.add("XSD: проверка при разборе", testParserIntegration); runner.add("XSD: xs:include", testSchemaInclude); } }