Skip to content

Commit

Permalink
Add more tests for expressions in string templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
RZhang05 committed Oct 30, 2024
1 parent ba580e2 commit d93bb65
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
69 changes: 68 additions & 1 deletion parser/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/onflow/cadence/common"
"github.com/onflow/cadence/errors"
"github.com/onflow/cadence/parser/lexer"
"github.com/onflow/cadence/runtime/tests/utils"

Check failure on line 37 in parser/expression_test.go

View workflow job for this annotation

GitHub Actions / Performance regression check

no required module provides package github.com/onflow/cadence/runtime/tests/utils; to add it:

Check failure on line 37 in parser/expression_test.go

View workflow job for this annotation

GitHub Actions / Test

no required module provides package github.com/onflow/cadence/runtime/tests/utils; to add it:
. "github.com/onflow/cadence/test_utils/common_utils"
)

Expand Down Expand Up @@ -6287,7 +6288,7 @@ func TestParseStringTemplate(t *testing.T) {
require.NoError(t, err)
})

t.Run("invalid, unbalanced paren", func(t *testing.T) {
t.Run("invalid, missing paren", func(t *testing.T) {

t.Parallel()

Expand All @@ -6314,6 +6315,33 @@ func TestParseStringTemplate(t *testing.T) {
)
})

t.Run("invalid, nested expression paren", func(t *testing.T) {

t.Parallel()

_, errs := testParseExpression(`
"\((2+2)/2()"
`)

var err error
if len(errs) > 0 {
err = Error{
Errors: errs,
}
}

require.Error(t, err)
utils.AssertEqualWithDiff(t,
[]error{
&SyntaxError{
Message: "expected token ')'",
Pos: ast.Position{Offset: 16, Line: 2, Column: 15},
},
},
errs,
)
})

t.Run("invalid, nested templates", func(t *testing.T) {

t.Parallel()
Expand Down Expand Up @@ -6478,6 +6506,45 @@ func TestParseStringTemplate(t *testing.T) {

AssertEqualWithDiff(t, expected, actual)
})

t.Run("valid, extra closing paren", func(t *testing.T) {

t.Parallel()

actual, errs := testParseExpression(`
"\(a))"
`)

var err error
if len(errs) > 0 {
err = Error{
Errors: errs,
}
}

require.NoError(t, err)

expected := &ast.StringTemplateExpression{
Values: []string{
"",
")",
},
Expressions: []ast.Expression{
&ast.IdentifierExpression{
Identifier: ast.Identifier{
Identifier: "a",
Pos: ast.Position{Offset: 7, Line: 2, Column: 6},
},
},
},
Range: ast.Range{
StartPos: ast.Position{Offset: 4, Line: 2, Column: 3},
EndPos: ast.Position{Offset: 10, Line: 2, Column: 9},
},
}

utils.AssertEqualWithDiff(t, expected, actual)
})
}

func TestParseNilCoalescing(t *testing.T) {
Expand Down
35 changes: 35 additions & 0 deletions sema/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,27 @@ func TestCheckStringTemplate(t *testing.T) {
assert.IsType(t, &sema.TypeMismatchWithDescriptionError{}, errs[0])
})

t.Run("invalid, struct with tostring", func(t *testing.T) {

t.Parallel()

_, err := ParseAndCheck(t, `
access(all)
struct SomeStruct {
access(all)
view fun toString(): String {
return "SomeStruct"
}
}
let a = SomeStruct()
let x: String = "\(a)"
`)

errs := RequireCheckerErrors(t, err, 1)

assert.IsType(t, &sema.TypeMismatchWithDescriptionError{}, errs[0])
})

t.Run("invalid, array", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -788,4 +809,18 @@ func TestCheckStringTemplate(t *testing.T) {

assert.IsType(t, &sema.TypeMismatchWithDescriptionError{}, errs[0])
})

t.Run("invalid, expression type", func(t *testing.T) {

t.Parallel()

_, err := ParseAndCheck(t, `
let y: Int = 0
let x: String = "\(y > 0 ? "String" : true)"
`)

errs := RequireCheckerErrors(t, err, 1)

assert.IsType(t, &sema.TypeMismatchWithDescriptionError{}, errs[0])
})
}

0 comments on commit d93bb65

Please sign in to comment.