Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsogarciacaro committed Jun 15, 2022
1 parent 5d4a3d7 commit f3f9d34
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Compiler/Service/ServiceLexing.fs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ module internal LexerStateEncoding =
match stringNest with
| [] -> false, 0, 0, false, []
| (i1, kind1, b1, _)::rest -> true, i1, encodeStringStyle kind1, b1, rest

let tag2, i2, kind2, b2 =
match rest with
| [] -> false, 0, 0, false
Expand Down
21 changes: 21 additions & 0 deletions tests/fsharp/Compiler/Language/StringInterpolation.fs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,16 @@ check "vcewweh228d" "{{" "{{"
check "vcewweh229f" "}}" "}}"
"""

[<Test>]
let ``String interpolation using escaped braces - double dollar`` () =
SimpleCheckTest
"""
check "vcewweh226i" $$"{" "{"
check "vcewweh226f" $$"{}" "{}"
check "vcewweh226p" $$"{ {{1}} }" "{ 1 }"
check "vcewweh227a" $$"}}" "}"
"""

[<Test>]
let ``String interpolation using verbatim strings`` () =
SimpleCheckTest
Expand All @@ -487,11 +497,19 @@ type R2 = { X : int ; Y: int }
// Check record expression (parenthesized)
check "vcewweh18" $"abc{({contents=1}.contents)}def" "abc1def"
// Check record expression (parenthesized) - double dollar
check "vcewweh18b" $$"abc{{({contents=1}.contents)}}def" "abc1def"
// Check record expression (unparenthesized, spaced)
check "vcewweh19a" $"abc{ {X=1} }def" "abc{ X = 1 }def"
check "vcewweh19b" $"abc{ {X=1} }def" "abc{ X = 1 }def"
// Check record expression (unparenthesized, spaced) - double dollar
check "vcewweh19c" $$"abc{{ {X=1} }}def" "abc{ X = 1 }def"
check "vcewweh19d" $$"abc{{ {X=1} }}def" "abc{ X = 1 }def"
// Check record expression (unparenthesized, spaced ending in token brace then string hole brace)
check "vcewweh19v" $"abc{ {X=1}}def" "abc{ X = 1 }def"
Expand All @@ -501,6 +519,9 @@ check "vcewweh20" $"abc{{X=1}}def" "abc{X=1}def"
// Check thing that is not really a record expression (braces are escaped)
check "vcewweh20b" $"abc{{quack=1}}def" "abc{quack=1}def"
// Check thing that is not really a record expression (double dollar)
check "vcewweh20c" $$"abc{X=1}def" "abc{X=1}def"
// Check thing that is not really a record expression (braces are escaped)
check "vcewweh21" $"abc{{X=1; Y=2}}def" "abc{X=1; Y=2}def"
Expand Down
105 changes: 100 additions & 5 deletions tests/service/TokenizerTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ let ``Tokenizer test 2 - single line non-nested string interpolation``() =
"let hello0 = $\"\""
"let hello1 = $\"Hello world\" "
"let hello2 = $\"Hello world {1+1} = {2}\" "
"let hello3 = $$\"Hello world {{1+1}} = {{2}}\" "
"let hello0v = @$\"\""
"let hello1v = @$\"Hello world\" "
"let hello2v = @$\"Hello world {1+1} = {2}\" " |]
"let hello2v = @$\"Hello world {1+1} = {2}\" "
"let hello3v = @$\"Hello world {{1+1}} = {{2}}\" " |]

let actual =
[ for lineNo, lineToks in tokenizedLines do
Expand Down Expand Up @@ -98,16 +100,25 @@ let ``Tokenizer test 2 - single line non-nested string interpolation``() =
("INTERP_STRING_PART", "{"); ("INT32", "2"); ("STRING_TEXT", "}");
("INTERP_STRING_END", "\""); ("WHITESPACE", " ")]);
(4,
[("LET", "let"); ("WHITESPACE", " "); ("IDENT", "hello3"); ("WHITESPACE", " ");
("EQUALS", "="); ("WHITESPACE", " "); ("STRING_TEXT", "$$\"");
("STRING_TEXT", "Hello"); ("STRING_TEXT", " "); ("STRING_TEXT", "world");
("STRING_TEXT", " "); ("INTERP_STRING_BEGIN_PART", "{{"); ("INT32", "1");
("PLUS_MINUS_OP", "+"); ("INT32", "1"); ("STRING_TEXT", "}}");
("STRING_TEXT", " "); ("STRING_TEXT", "="); ("STRING_TEXT", " ");
("INTERP_STRING_PART", "{{"); ("INT32", "2"); ("STRING_TEXT", "}}");
("INTERP_STRING_END", "\""); ("WHITESPACE", " ")]);
(5,
[("LET", "let"); ("WHITESPACE", " "); ("IDENT", "hello0v");
("WHITESPACE", " "); ("EQUALS", "="); ("WHITESPACE", " ");
("STRING_TEXT", "@$\""); ("INTERP_STRING_BEGIN_END", "\"")]);
(5,
("STRING_TEXT", "@$$\""); ("INTERP_STRING_BEGIN_END", "\"")]);
(6,
[("LET", "let"); ("WHITESPACE", " "); ("IDENT", "hello1v");
("WHITESPACE", " "); ("EQUALS", "="); ("WHITESPACE", " ");
("STRING_TEXT", "@$\""); ("STRING_TEXT", "Hello"); ("STRING_TEXT", " ");
("STRING_TEXT", "world"); ("INTERP_STRING_BEGIN_END", "\"");
("WHITESPACE", " ")]);
(6,
(7,
[("LET", "let"); ("WHITESPACE", " "); ("IDENT", "hello2v");
("WHITESPACE", " "); ("EQUALS", "="); ("WHITESPACE", " ");
("STRING_TEXT", "@$\""); ("STRING_TEXT", "Hello"); ("STRING_TEXT", " ");
Expand All @@ -116,6 +127,16 @@ let ``Tokenizer test 2 - single line non-nested string interpolation``() =
("INT32", "1"); ("STRING_TEXT", "}"); ("STRING_TEXT", " ");
("STRING_TEXT", "="); ("STRING_TEXT", " "); ("INTERP_STRING_PART", "{");
("INT32", "2"); ("STRING_TEXT", "}"); ("INTERP_STRING_END", "\"");
("WHITESPACE", " ")]);
(8,
[("LET", "let"); ("WHITESPACE", " "); ("IDENT", "hello3v");
("WHITESPACE", " "); ("EQUALS", "="); ("WHITESPACE", " ");
("STRING_TEXT", "@$$\""); ("STRING_TEXT", "Hello"); ("STRING_TEXT", " ");
("STRING_TEXT", "world"); ("STRING_TEXT", " ");
("INTERP_STRING_BEGIN_PART", "{{"); ("INT32", "1"); ("PLUS_MINUS_OP", "+");
("INT32", "1"); ("STRING_TEXT", "}}"); ("STRING_TEXT", " ");
("STRING_TEXT", "="); ("STRING_TEXT", " "); ("INTERP_STRING_PART", "{{");
("INT32", "2"); ("STRING_TEXT", "}}"); ("INTERP_STRING_END", "\"");
("WHITESPACE", " ")]);]

if actual <> expected then
Expand All @@ -129,7 +150,9 @@ let ``Tokenizer test - multiline non-nested string interpolation``() =
let tokenizedLines =
tokenizeLines
[| "let hello1t = $\"\"\"abc {1+"
" 1} def\"\"\"" |]
" 1} def\"\"\""
"let hello2t = $$\"\"\"abc {{1+"
" 1}} def\"\"\"" |]

let actual =
[ for lineNo, lineToks in tokenizedLines do
Expand All @@ -142,6 +165,14 @@ let ``Tokenizer test - multiline non-nested string interpolation``() =
("INTERP_STRING_BEGIN_PART", "{"); ("INT32", "1"); ("PLUS_MINUS_OP", "+")]);
(1,
[("WHITESPACE", " "); ("INT32", "1"); ("STRING_TEXT", "}");
("STRING_TEXT", " "); ("STRING_TEXT", "def"); ("INTERP_STRING_END", "\"\"\"")])
(2,
[("LET", "let"); ("WHITESPACE", " "); ("IDENT", "hello1t");
("WHITESPACE", " "); ("EQUALS", "="); ("WHITESPACE", " ");
("STRING_TEXT", "$$\"\"\""); ("STRING_TEXT", "abc"); ("STRING_TEXT", " ");
("INTERP_STRING_BEGIN_PART", "{{"); ("INT32", "1"); ("PLUS_MINUS_OP", "+")]);
(3,
[("WHITESPACE", " "); ("INT32", "1"); ("STRING_TEXT", "}}");
("STRING_TEXT", " "); ("STRING_TEXT", "def"); ("INTERP_STRING_END", "\"\"\"")])]

if actual <> expected then
Expand Down Expand Up @@ -191,6 +222,48 @@ let ``Tokenizer test - multi-line nested string interpolation``() =
printfn "expected = %A" expected
Assert.Fail(sprintf "actual and expected did not match,actual =\n%A\nexpected=\n%A\n" actual expected)

[<Test>]
// checks nested '{' and nested single-quote strings
let ``Tokenizer test - multi-line nested string interpolation - double dollar``() =
let tokenizedLines =
tokenizeLines
[| "let hello1t = $$\"\"\"abc {{\"a\" + "
" { "
" contents = \"b\" "
" }.contents "
" }} def\"\"\"" |]

let actual =
[ for lineNo, lineToks in tokenizedLines do
yield lineNo, [ for str, info in lineToks do yield info.TokenName, str ] ]
let expected =
[(0,
[("LET", "let"); ("WHITESPACE", " "); ("IDENT", "hello1t");
("WHITESPACE", " "); ("EQUALS", "="); ("WHITESPACE", " ");
("STRING_TEXT", "$$\"\"\""); ("STRING_TEXT", "abc"); ("STRING_TEXT", " ");
("INTERP_STRING_BEGIN_PART", "{{"); ("STRING_TEXT", "\""); ("STRING_TEXT", "a");
("STRING", "\""); ("WHITESPACE", " "); ("PLUS_MINUS_OP", "+");
("WHITESPACE", " ")]);
(1,
[("WHITESPACE", " "); ("LBRACE", "{");
("WHITESPACE", " ")]);
(2,
[("WHITESPACE", " "); ("IDENT", "contents");
("WHITESPACE", " "); ("EQUALS", "="); ("WHITESPACE", " ");
("STRING_TEXT", "\""); ("STRING_TEXT", "b"); ("STRING", "\"");
("WHITESPACE", " ")]);
(3,
[("WHITESPACE", " "); ("RBRACE", "}"); ("DOT", ".");
("IDENT", "contents"); ("WHITESPACE", " ")]);
(4,
[("WHITESPACE", " "); ("STRING_TEXT", "}}");
("STRING_TEXT", " "); ("STRING_TEXT", "def"); ("INTERP_STRING_END", "\"\"\"")])]

if actual <> expected then
printfn "actual = %A" actual
printfn "expected = %A" expected
Assert.Fail(sprintf "actual and expected did not match,actual =\n%A\nexpected=\n%A\n" actual expected)

[<Test>]
let ``Tokenizer test - single-line nested string interpolation``() =
let tokenizedLines =
Expand All @@ -214,3 +287,25 @@ let ``Tokenizer test - single-line nested string interpolation``() =
printfn "expected = %A" expected
Assert.Fail(sprintf "actual and expected did not match,actual =\n%A\nexpected=\n%A\n" actual expected)

[<Test>]
let ``Tokenizer test - single-line nested string interpolation - double dollar``() =
let tokenizedLines =
tokenizeLines
[| " $$\"abc {{ { contents = 1 } }}\" " |]

let actual =
[ for lineNo, lineToks in tokenizedLines do
yield lineNo, [ for str, info in lineToks do yield info.TokenName, str ] ]
let expected =
[(0,
[("WHITESPACE", " "); ("STRING_TEXT", "$$\""); ("STRING_TEXT", "abc");
("STRING_TEXT", " "); ("INTERP_STRING_BEGIN_PART", "{{"); ("WHITESPACE", " ");
("LBRACE", "{"); ("WHITESPACE", " "); ("IDENT", "contents");
("WHITESPACE", " "); ("EQUALS", "="); ("WHITESPACE", " "); ("INT32", "1");
("WHITESPACE", " "); ("RBRACE", "}"); ("WHITESPACE", " ");
("STRING_TEXT", "}}"); ("INTERP_STRING_END", "\""); ("WHITESPACE", " ")])]

if actual <> expected then
printfn "actual = %A" actual
printfn "expected = %A" expected
Assert.Fail(sprintf "actual and expected did not match,actual =\n%A\nexpected=\n%A\n" actual expected)

0 comments on commit f3f9d34

Please sign in to comment.