Skip to content

Commit

Permalink
Add tests for handling quoted scalars
Browse files Browse the repository at this point in the history
  • Loading branch information
LakshanWeerasinghe committed Jun 12, 2024
1 parent 4248895 commit 59f4ff4
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 14 deletions.
2 changes: 1 addition & 1 deletion ballerina/tests/parse_string.bal
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ isolated function testParsingEmptyLines() returns error? {
map<string> value = check parseString(content);
map<string> expectedResult = {
"FoldingDoubleQuote": "Empty line\nas a line feed",
"FoldingSingleQuote": "Empty line\nas a line feed",
"FoldingSingleQuote": "Empty line\n'as a line 'feed",
"Chomping": "Clipped empty lines\n"
};
test:assertEquals(value, expectedResult);
Expand Down
24 changes: 23 additions & 1 deletion ballerina/tests/parse_string_negative.bal
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,29 @@ function negativeDataProvider() returns [string, string][] => [
["negative_test_11.yaml", "'unexpected event' at line: '1' column: '8'"],
["negative_test_12.yaml", "'unexpected event error' at line: '1' column: '5'"],
["negative_test_13.yaml", "'cannot have block sequence under flow collection' at line: '2' column: '3'"],
["negative_test_14.yaml", "''-' cannot be defined after tag properties' at line: '1' column: '7'"]
["negative_test_14.yaml", "''-' cannot be defined after tag properties' at line: '1' column: '7'"],
[
"negative_test_15.yaml",
"'invalid token 'DOCUMENT_MARKER' inside the double-quoted scalar'" +
" at line: '3' column: '3'"
],
[
"negative_test_16.yaml",
"'invalid token 'DIRECTIVE_MARKER' inside the double-quoted scalar'" +
" at line: '3' column: '3'"
],
[
"negative_test_17.yaml",
"'invalid token 'DOCUMENT_MARKER' inside the single-quoted scalar'" +
" at line: '3' column: '3'"
],
[
"negative_test_18.yaml",
"'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'"]

];

@test:Config {
Expand Down
4 changes: 4 additions & 0 deletions ballerina/tests/resources/negative/negative_test_15.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
invalid-block-scalar:
"invalid double quoted
...
bloack scalar"
4 changes: 4 additions & 0 deletions ballerina/tests/resources/negative/negative_test_16.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
invalid-block-scalar:
"invalid double quoted
---
bloack scalar"
4 changes: 4 additions & 0 deletions ballerina/tests/resources/negative/negative_test_17.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
invalid-block-scalar:
'invalid singlw quoted
...
bloack scalar'
4 changes: 4 additions & 0 deletions ballerina/tests/resources/negative/negative_test_18.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
invalid-block-scalar:
'invalid single quoted
---
bloack scalar'
3 changes: 3 additions & 0 deletions ballerina/tests/resources/negative/negative_test_19.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
invalid: |*
foo
2 changes: 1 addition & 1 deletion ballerina/tests/resources/nested_20.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ FoldingDoubleQuote:
FoldingSingleQuote:
'Empty line
as a line feed'
''as a line ''feed'
Chomping: |
Clipped empty lines
Original file line number Diff line number Diff line change
Expand Up @@ -754,9 +754,7 @@ public State transition(LexerState lexerState) throws Error.YamlParserException
// Check for empty lines
if (WHITE_SPACE_PATTERN.pattern(lexerState.peek())) {
String whitespace = getWhitespace(lexerState);
if (lexerState.peek() == -1) {
lexerState.forward();
lexerState.tokenize(EMPTY_LINE);
if (Scanner.scanAndTokenizeEOL(lexerState, EMPTY_LINE)) {
return this;
}
if (lexerState.firstLine) {
Expand Down Expand Up @@ -803,9 +801,7 @@ public State transition(LexerState lexerState) throws Error.YamlParserException
// Check for empty lines
if (WHITE_SPACE_PATTERN.pattern(lexerState.peek())) {
String whitespace = getWhitespace(lexerState);
if (lexerState.peek() == -1) {
lexerState.forward();
lexerState.tokenize(EMPTY_LINE);
if (Scanner.scanAndTokenizeEOL(lexerState, EMPTY_LINE)) {
return this;
}
if (lexerState.firstLine) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2183,8 +2183,11 @@ private static String doubleQuoteScalar(ParserState state) throws Error.YamlPars
}
state.getLexerState().setFirstLine(false);
}
default -> throw new Error.YamlParserException("invalid double quote scalar",
state.getLine(), state.getColumn());
default -> {
String errorMsg = "invalid token '" + state.getCurrentToken().getType()
+ "' inside the double-quoted scalar";
throw new Error.YamlParserException(errorMsg, state.getLine(), state.getColumn());
}
}
getNextToken(state);
}
Expand Down Expand Up @@ -2220,7 +2223,8 @@ private static void checkEmptyKey(ParserState state) throws Error.YamlParserExce
}
}

/** Parse the string of a single-quoted scalar.
/**
* Parse the string of a single-quoted scalar.
*
* @param state - Current parser state
* @return - Parsed single-quoted scalar value
Expand Down Expand Up @@ -2279,8 +2283,11 @@ private static String singleQuoteScalar(ParserState state) throws Error.YamlPars
}
state.getLexerState().setFirstLine(false);
}
default -> throw new Error.YamlParserException("invalid single quote character",
state.getLine(), state.getColumn());
default -> {
String errorMsg = "invalid token '" + state.getCurrentToken().getType()
+ "' inside the single-quoted scalar";
throw new Error.YamlParserException(errorMsg, state.getLine(), state.getColumn());
}
}
getNextToken(state);
}
Expand Down

0 comments on commit 59f4ff4

Please sign in to comment.