Skip to content

Commit

Permalink
Add negative tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LakshanWeerasinghe committed Jun 16, 2024
1 parent 79e825c commit 8fe8114
Show file tree
Hide file tree
Showing 11 changed files with 42 additions and 14 deletions.
10 changes: 8 additions & 2 deletions ballerina/tests/parse_string_negative.bal
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ function negativeDataProvider() returns [string, string][] => [
"'invalid token 'DIRECTIVE_MARKER' inside the single-quoted scalar'" +
" at line: '3' column: '3'"
],
["negative_test_19.yaml", "'invalid block header' at line: '1' column: '10'"]
["negative_test_19.yaml", "'invalid block header' at line: '1' column: '10'"],
["negative_test_20.yaml", "'token cannot start in the same line as the document marker' at line: '2' column: '7'"],
[
"negative_test_21.yaml",
"'block collection token cannot start in the same line as the directive marker' at line: '2' column: '7'"
]

];

Expand All @@ -91,7 +96,8 @@ function tagHandleNegativeDataProvider() returns [string, string][] => [
["tag_handle_negative_6.yaml", "'invalid digit character' at line: '1' column: '9'"],
["tag_handle_negative_7.yaml", "'invalid directive document' at line: '2' column: '1'"],
["tag_handle_negative_8.yaml", "'invalid document' at line: '1' column: '8'"],
["tag_handle_negative_9.yaml", "'directives are not allowed in a bare document' at line: '3' column: '5'"]
["tag_handle_negative_9.yaml", "'directives are not allowed in a bare document' at line: '3' column: '5'"],
["tag_handle_negative_10.yaml", "'tag schema not supported' at line: '3' column: '7'"]
];

@test:Config {
Expand Down
5 changes: 4 additions & 1 deletion ballerina/tests/parser_tests.bal
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ function tagHandleData() returns [string, TestCase][] => [
["tag_handle_5.yaml", {case: "yaml_version"}],
["tag_handle_6.yaml", {case: "reserved directive"}],
["tag_handle_7.yaml", {case: "secondary tag handle"}],
["tag_handle_8.yaml", {case: "value"}]
["tag_handle_8.yaml", {case: "value"}],
["tag_handle_9.yaml", {case: "uri_scanner"}],
["tag_handle_10.yaml", {case: "value"}],
["tag_handle_11.yaml", {case: "value"}]
];

type TestCase record {|
Expand Down
2 changes: 2 additions & 0 deletions ballerina/tests/resources/negative/negative_test_20.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"END OF document"
... key: value
2 changes: 2 additions & 0 deletions ballerina/tests/resources/negative/negative_test_21.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
"END OF document"
--- key: value
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%TAG !yaml! tag:yaml.org,2002:
---
case: !
3 changes: 3 additions & 0 deletions ballerina/tests/resources/parser/tag_handle_10.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%TAG ! tag:yaml.org,2002:
---
case: !str value
3 changes: 3 additions & 0 deletions ballerina/tests/resources/parser/tag_handle_11.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%TAG ! tag:yaml.org,2002:
---
case: !st%72 value
3 changes: 3 additions & 0 deletions ballerina/tests/resources/parser/tag_handle_9.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
%TAG !yaml! tag:yaml.org,2002:
---
case: !yaml!str uri_scanner
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ public State transition(LexerState lexerState) throws Error.YamlParserException
}
default -> { // Check for primary and name tag handles
lexerState.lexeme = "!";
lexerState.forward();
Scanner.iterate(lexerState, Scanner.DIFF_TAG_HANDLE_SCANNER, TAG_HANDLE, true);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,14 @@ public boolean scan(LexerState sm) throws Error.YamlParserException {
sm.setLexeme("!");
return true;

Check warning on line 316 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#L314-L316

Added lines #L314 - L316 were not covered by tests
}

// 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)) {
sm.setLexemeBuffer(sm.getLexeme().substring(1));
sm.setLexeme("!");
return true;
}

return false;
}

Expand All @@ -324,13 +332,6 @@ public boolean scan(LexerState sm) throws Error.YamlParserException {
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), 0)) {
sm.setLexemeBuffer(sm.getLexeme().substring(1));
sm.setLexeme("!");
return true;
}

// Store the complete primary tag if a hexadecimal escape is detected.
if (differentiate && sm.peek() == '%') {
scanUnicodeEscapedCharacters(sm, '%', 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1222,14 +1222,14 @@ private static YamlEvent parse(ParserState state, ParserUtils.ParserOption optio

// There cannot be nodes next to the document marker.
if (bufferedTokenType != EOL && bufferedTokenType != COMMENT && !explicit) {
throw new Error.YamlParserException("'${state.tokenBuffer.token}' token cannot " +
"start in the same line as the document marker", state.getLine(), state.getColumn());
throw new Error.YamlParserException("token cannot start in the same line as " +
"the document marker", state.getLine(), state.getColumn());
}

// Block collection nodes cannot be next to the directive marker.
if (explicit && (bufferedTokenType == PLANAR_CHAR && bufferedToken.getIndentation() != null
|| bufferedTokenType == SEQUENCE_ENTRY)) {
throw new Error.YamlParserException("'${state.tokenBuffer.token}' token cannot start " +
throw new Error.YamlParserException("block collection token cannot start " +
"in the same line as the directive marker", state.getLine(), state.getColumn());
}
}
Expand Down Expand Up @@ -1778,7 +1778,8 @@ private static YamlEvent constructEvent(YamlEvent yamlEvent, TagStructure newNod
return event;
}

/** Extract the data for the given node.
/**
* Extract the data for the given node.
*
* @param state - Current parser state
* @param peeked - If the expected token is already in the state
Expand Down

0 comments on commit 8fe8114

Please sign in to comment.