Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relax NG schema validation not working when DOCTYPE declaration missing
with file association Fixes redhat-developer/vscode-xml#831 Signed-off-by: azerr <azerr@redhat.com>
- Loading branch information
Showing
4 changed files
with
183 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...s/relaxng/xml/completion/XMLFileAssociationCompletionBasedOnRelaxNGCompactSyntaxTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.extensions.relaxng.xml.completion; | ||
|
||
import static org.eclipse.lemminx.XMLAssert.c; | ||
import static org.eclipse.lemminx.XMLAssert.te; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import org.eclipse.lemminx.AbstractCacheBasedTest; | ||
import org.eclipse.lemminx.XMLAssert; | ||
import org.eclipse.lemminx.commons.BadLocationException; | ||
import org.eclipse.lemminx.extensions.contentmodel.model.ContentModelManager; | ||
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLFileAssociation; | ||
import org.eclipse.lemminx.services.XMLLanguageService; | ||
import org.eclipse.lsp4j.CompletionItem; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* XML file associations completion tests with RelaxNG compact syntax. | ||
*/ | ||
public class XMLFileAssociationCompletionBasedOnRelaxNGCompactSyntaxTest extends AbstractCacheBasedTest { | ||
|
||
@Test | ||
public void completionInRoot() throws BadLocationException { | ||
// completion on <| | ||
String xml = "<|"; | ||
testCompletionFor(xml, // | ||
c("addressBook", te(0, 0, 0, 1, "<addressBook></addressBook>"), "<addressBook")); | ||
} | ||
|
||
@Test | ||
public void completionForElements() throws BadLocationException { | ||
// completion on <| | ||
String xml = "<addressBook>\r\n" + // | ||
"<|\r\n" + // | ||
"</addressBook>"; | ||
testCompletionFor(xml, // | ||
c("card", te(1, 0, 1, 1, "<card></card>"), "<card")); | ||
} | ||
|
||
@Test | ||
public void completionForAttributeNames() throws BadLocationException { | ||
// completion on <| | ||
String xml = "<addressBook>\r\n" + // | ||
"<card |></card>\r\n" + // | ||
"</addressBook>"; | ||
testCompletionFor(xml, // | ||
c("id", te(1, 6, 1, 6, "id=\"\""), "id")); | ||
} | ||
|
||
private static void testCompletionFor(String value, CompletionItem... expectedItems) throws BadLocationException { | ||
Consumer<XMLLanguageService> configuration = ls -> { | ||
ContentModelManager contentModelManager = ls.getComponent(ContentModelManager.class); | ||
contentModelManager | ||
.setFileAssociations(createXMLFileAssociation("src/test/resources/relaxng/")); | ||
}; | ||
XMLAssert.testCompletionFor(new XMLLanguageService(), value, null, configuration, | ||
"file:///test/addressBook.xml", | ||
null, true, expectedItems); | ||
} | ||
|
||
private static XMLFileAssociation[] createXMLFileAssociation(String baseSystemId) { | ||
XMLFileAssociation addressBook = new XMLFileAssociation(); | ||
addressBook.setPattern("**/addressBook.xml"); | ||
addressBook.setSystemId(baseSystemId + "addressBook.rnc"); | ||
return new XMLFileAssociation[] { addressBook }; | ||
} | ||
|
||
} |
85 changes: 85 additions & 0 deletions
85
...nsions/relaxng/xml/diagnostics/XMLFileAssociationRelaxNGCompactSyntaxDiagnosticsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Red Hat Inc. and others. | ||
* All rights reserved. This program and the accompanying materials | ||
* which accompanies this distribution, and is available at | ||
* http://www.eclipse.org/legal/epl-v20.html | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.lemminx.extensions.relaxng.xml.diagnostics; | ||
|
||
import static org.eclipse.lemminx.XMLAssert.d; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import org.eclipse.lemminx.AbstractCacheBasedTest; | ||
import org.eclipse.lemminx.XMLAssert; | ||
import org.eclipse.lemminx.extensions.contentmodel.model.ContentModelManager; | ||
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLFileAssociation; | ||
import org.eclipse.lemminx.extensions.relaxng.xml.validator.RelaxNGErrorCode; | ||
import org.eclipse.lemminx.services.XMLLanguageService; | ||
import org.eclipse.lsp4j.Diagnostic; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* XML Validation tests with RelaxNG compact syntax by using XML file | ||
* association. | ||
* | ||
*/ | ||
public class XMLFileAssociationRelaxNGCompactSyntaxDiagnosticsTest extends AbstractCacheBasedTest { | ||
|
||
@Test | ||
public void valid() throws Exception { | ||
String xml = "<addressBook>\r\n" + // | ||
" <card>\r\n" + // | ||
" <name>John Smith</name>\r\n" + // | ||
" <email>js@example.com</email>\r\n" + // | ||
" </card>\r\n" + // | ||
" <card>\r\n" + // | ||
" <name>Fred Bloggs</name>\r\n" + // | ||
" <email>fb@example.net</email>\r\n" + // | ||
" </card>\r\n" + // | ||
"</addressBook>"; | ||
testDiagnosticsFor(xml); | ||
} | ||
|
||
@Test | ||
public void unkwown_element() throws Exception { | ||
String xml = "<addressBook>\r\n" + // | ||
" <card>\r\n" + // | ||
" <nameXXX>John Smith</nameXXX>\r\n" + // unknown_element -> element "nameXXX" not allowed anywhere; | ||
// expected element "name" | ||
" <email>js@example.com</email>\r\n" + // unexpected_element_required_element_missing -> "element | ||
// "email" not allowed yet; missing required element "name"" | ||
" </card>\r\n" + // | ||
" <card>\r\n" + // | ||
" <name>Fred Bloggs</name>\r\n" + // | ||
" <email>fb@example.net</email>\r\n" + // | ||
" </card>\r\n" + // | ||
"</addressBook>"; | ||
testDiagnosticsFor(xml, // | ||
d(2, 5, 12, RelaxNGErrorCode.unknown_element), // | ||
d(3, 5, 10, RelaxNGErrorCode.unexpected_element_required_element_missing)); | ||
} | ||
|
||
private static void testDiagnosticsFor(String xml, Diagnostic... expected) { | ||
Consumer<XMLLanguageService> configuration = ls -> { | ||
ContentModelManager contentModelManager = ls.getComponent(ContentModelManager.class); | ||
contentModelManager | ||
.setFileAssociations(createXMLFileAssociation("src/test/resources/relaxng/")); | ||
}; | ||
|
||
XMLAssert.testDiagnosticsFor(xml, null, configuration, "file:///test/addressBook.xml", expected); | ||
} | ||
|
||
private static XMLFileAssociation[] createXMLFileAssociation(String baseSystemId) { | ||
XMLFileAssociation addressBook = new XMLFileAssociation(); | ||
addressBook.setPattern("**/addressBook.xml"); | ||
addressBook.setSystemId(baseSystemId + "addressBook.rnc"); | ||
return new XMLFileAssociation[] { addressBook }; | ||
} | ||
|
||
} |