Skip to content

Commit

Permalink
Add tag directive tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LakshanWeerasinghe committed Jun 10, 2024
1 parent cf25f61 commit df8e2b6
Show file tree
Hide file tree
Showing 14 changed files with 63 additions and 12 deletions.
19 changes: 19 additions & 0 deletions ballerina/tests/parse_string_negative.bal
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,22 @@ function negativeDataProvider() returns [string, string][] => [
"'block mapping cannot have the same indent as a block sequence' at line: '3' column: '10'"
]
];

@test:Config {
dataProvider: tagHandleNegativeDataProvider
}
isolated function tagHandleNegativeTests(string path, string expectedErrMsg) returns io:Error? {
string fullPath = NEGATIVE_TEST_PATH + "tag_handle_negative/" + path;
string content = check io:fileReadString(fullPath);
anydata|Error result = parseString(content);
test:assertTrue(result is Error);
test:assertEquals((<Error>result).message(), expectedErrMsg);
}

function tagHandleNegativeDataProvider() returns [string, string][] => [
["tag_handle_negative_1.yaml", "'incompatible yaml version for the 1.2 parser' at line: '1' column: '9'"],
["tag_handle_negative_2.yaml", "'incompatible yaml version for the 1.2 parser' at line: '1' column: '9'"],
["tag_handle_negative_3.yaml", "'YAML document version is already defined' at line: '2' column: '5'"],
["tag_handle_negative_4.yaml", "'duplicate tag handle' at line: '2' column: '12'"],
["tag_handle_negative_5.yaml", "'custom tags not supported' at line: '1' column: '28'"]
];
6 changes: 5 additions & 1 deletion ballerina/tests/parser_tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ isolated function testTagHandles(string inputPath, TestCase expectedValue) retur
function tagHandleData() returns [string, TestCase][] => [
["tag_handle_1.yaml", {case: "uri_scanner"}],
["tag_handle_2.yaml", {case: "yaml_version"}],
["tag_handle_3.yaml", {case: "verbitam"}]
["tag_handle_3.yaml", {case: "verbitam"}],
["tag_handle_4.yaml", {case: "yaml_version"}],
["tag_handle_5.yaml", {case: "yaml_version"}],
["tag_handle_6.yaml", {case: "reserved directive"}],
["tag_handle_7.yaml", {case: "secondary tag handle"}]
];

type TestCase record {|
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%YAML 2.1
---
case: "yaml_version"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%YAML 0.1
---
case: "yaml_version"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
%YAML 1.2
%YAML 1.1
---
case: "yaml_version"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
%TAG !yaml! tag:yaml.org,2002:
%TAG !yaml! tag:yaml.org,2002:
---
name: !yaml!str user
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%TAG ! tag:example.com,2000:
---
name: !!str user
2 changes: 1 addition & 1 deletion ballerina/tests/resources/parser/tag_handle_1.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
%TAG ! tag:example.com,2002:
%TAG ! tag:yaml.org,2002:
---
case: uri_scanner
3 changes: 3 additions & 0 deletions ballerina/tests/resources/parser/tag_handle_4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%YAML 1.1
---
case: "yaml_version"
3 changes: 3 additions & 0 deletions ballerina/tests/resources/parser/tag_handle_5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%YAML 1.3
---
case: "yaml_version"
3 changes: 3 additions & 0 deletions ballerina/tests/resources/parser/tag_handle_6.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%FOO bar baz
---
case: reserved directive
3 changes: 3 additions & 0 deletions ballerina/tests/resources/parser/tag_handle_7.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%TAG !! tag:yaml.org,2002:
---
case: !!str secondary tag handle
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ public boolean scan(LexerState sm) throws Error.YamlParserException {

// Scan the word of the name tag.
if (matchPattern(sm, List.of(WORD_PATTERN, URI_PATTERN),
List.of(new Utils.CharPattern('!'), FLOW_INDICATOR_PATTERN), 1)) {
sm.forward();
List.of(new Utils.CharPattern('!'), FLOW_INDICATOR_PATTERN), 0)) {
sm.appendToLexeme(Character.toString(sm.peek()));
// Store the complete primary tag if another '!' cannot be detected.
if (differentiate && sm.peek(1) == -1) {
Expand All @@ -304,22 +303,20 @@ public boolean scan(LexerState sm) throws Error.YamlParserException {
}

// Scan the end delimiter of the tag.
if (sm.peek(1) == '!') {
sm.forward();
if (sm.peek() == '!') {
sm.appendToLexeme("!");
return true;
}

// Store the complete primary tag if a white space or a flow indicator is detected.
if (differentiate && matchPattern(sm, List.of(FLOW_INDICATOR_PATTERN, WHITE_SPACE_PATTERN), 1)) {
if (differentiate && matchPattern(sm, List.of(FLOW_INDICATOR_PATTERN, WHITE_SPACE_PATTERN), 0)) {
sm.setLexemeBuffer(sm.getLexeme().substring(1));
sm.setLexeme("!");
return true;

Check warning on line 315 in native/src/main/java/io/ballerina/lib/data/yaml/lexer/Scanner.java

View check run for this annotation

Codecov / codecov/patch

native/src/main/java/io/ballerina/lib/data/yaml/lexer/Scanner.java#L313-L315

Added lines #L313 - L315 were not covered by tests
}

// Store the complete primary tag if a hexadecimal escape is detected.
if (differentiate && sm.peek(1) == '%') {
sm.forward();
if (differentiate && sm.peek() == '%') {
scanUnicodeEscapedCharacters(sm, '%', 2);
sm.setLexemeBuffer(sm.getLexeme().substring(1));
sm.setLexeme("!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import io.ballerina.lib.data.yaml.lexer.LexerState;
import io.ballerina.lib.data.yaml.lexer.Token;
import io.ballerina.lib.data.yaml.utils.Constants;
import io.ballerina.lib.data.yaml.utils.Error;

import java.util.List;
Expand Down Expand Up @@ -90,6 +91,10 @@ public static void tagDirective(ParserState state) throws Error.YamlParserExcept
YamlParser.getNextToken(state, List.of(Token.TokenType.TAG_PREFIX));
String tagPrefix = state.getCurrentToken().getValue();

if (!tagPrefix.equals(Constants.DEFAULT_GLOBAL_TAG_HANDLE)) {
throw new Error.YamlParserException("custom tags not supported", state.getLine(), state.getColumn());
}

state.getCustomTagHandles().put(tagHandle, tagPrefix);
}

Expand All @@ -108,9 +113,6 @@ public static void reservedDirective(ParserState state) throws Error.YamlParserE
while (state.getBufferedToken().getType() == Token.TokenType.SEPARATION_IN_LINE) {
YamlParser.getNextToken(state);
YamlParser.getNextToken(state, true);
if (state.getBufferedToken().getType() != Token.TokenType.PRINTABLE_CHAR) {
break;
}
YamlParser.getNextToken(state);
reservedDirective.append(" ").append(state.getCurrentToken().getValue());
YamlParser.getNextToken(state, true);
Expand Down

0 comments on commit df8e2b6

Please sign in to comment.