154 lines
3.2 KiB
Go
154 lines
3.2 KiB
Go
package parser
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
)
|
|
|
|
func TestDiagnose(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
template string
|
|
want []Diagnostic
|
|
}{
|
|
{
|
|
name: "no diagnostics",
|
|
template: `
|
|
package main
|
|
|
|
templ template () {
|
|
<p>Hello, World!</p>
|
|
}`,
|
|
want: nil,
|
|
},
|
|
|
|
// useOfLegacyCallSyntaxDiagnoser
|
|
|
|
{
|
|
name: "useOfLegacyCallSyntaxDiagnoser: template root",
|
|
template: `
|
|
package main
|
|
|
|
templ template () {
|
|
{! templ.Raw("foo") }
|
|
}`,
|
|
want: []Diagnostic{{
|
|
Message: "`{! foo }` syntax is deprecated. Use `@foo` syntax instead. Run `templ fmt .` to fix all instances.",
|
|
Range: Range{Position{39, 4, 4}, Position{55, 4, 20}},
|
|
}},
|
|
},
|
|
{
|
|
name: "useOfLegacyCallSyntaxDiagnoser: in div",
|
|
template: `
|
|
package main
|
|
|
|
templ template () {
|
|
<div>
|
|
{! templ.Raw("foo") }
|
|
</div>
|
|
}`,
|
|
want: []Diagnostic{{
|
|
Message: "`{! foo }` syntax is deprecated. Use `@foo` syntax instead. Run `templ fmt .` to fix all instances.",
|
|
Range: Range{Position{47, 5, 5}, Position{63, 5, 21}},
|
|
}},
|
|
},
|
|
{
|
|
name: "useOfLegacyCallSyntaxDiagnoser: in if",
|
|
template: `
|
|
package main
|
|
|
|
templ template () {
|
|
if true {
|
|
{! templ.Raw("foo") }
|
|
}
|
|
}`,
|
|
want: []Diagnostic{{
|
|
Message: "`{! foo }` syntax is deprecated. Use `@foo` syntax instead. Run `templ fmt .` to fix all instances.",
|
|
Range: Range{Position{51, 5, 5}, Position{67, 5, 21}},
|
|
}},
|
|
},
|
|
{
|
|
name: "useOfLegacyCallSyntaxDiagnoser: in for",
|
|
template: `
|
|
package main
|
|
|
|
templ template () {
|
|
for i := range x {
|
|
{! templ.Raw("foo") }
|
|
}
|
|
}`,
|
|
want: []Diagnostic{{
|
|
Message: "`{! foo }` syntax is deprecated. Use `@foo` syntax instead. Run `templ fmt .` to fix all instances.",
|
|
Range: Range{Position{60, 5, 5}, Position{76, 5, 21}},
|
|
}},
|
|
},
|
|
{
|
|
name: "useOfLegacyCallSyntaxDiagnoser: in switch",
|
|
template: `
|
|
package main
|
|
|
|
templ template () {
|
|
switch x {
|
|
case 1:
|
|
{! templ.Raw("foo") }
|
|
default:
|
|
{! x }
|
|
}
|
|
}`,
|
|
want: []Diagnostic{
|
|
{
|
|
Message: "`{! foo }` syntax is deprecated. Use `@foo` syntax instead. Run `templ fmt .` to fix all instances.",
|
|
Range: Range{Position{61, 6, 5}, Position{77, 6, 21}},
|
|
},
|
|
{
|
|
Message: "`{! foo }` syntax is deprecated. Use `@foo` syntax instead. Run `templ fmt .` to fix all instances.",
|
|
Range: Range{Position{95, 8, 5}, Position{96, 8, 6}},
|
|
},
|
|
},
|
|
},
|
|
{
|
|
name: "useOfLegacyCallSyntaxDiagnoser: in block",
|
|
template: `
|
|
package main
|
|
|
|
templ template () {
|
|
@layout("Home") {
|
|
{! templ.Raw("foo") }
|
|
}
|
|
}`,
|
|
want: []Diagnostic{{
|
|
Message: "`{! foo }` syntax is deprecated. Use `@foo` syntax instead. Run `templ fmt .` to fix all instances.",
|
|
Range: Range{Position{59, 5, 5}, Position{75, 5, 21}},
|
|
}},
|
|
},
|
|
{
|
|
name: "voidElementWithChildrenDiagnoser: no diagnostics",
|
|
template: `
|
|
package main
|
|
|
|
templ template () {
|
|
<div>
|
|
<input/>
|
|
</div>
|
|
}`,
|
|
want: nil,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
tf, err := ParseString(tt.template)
|
|
if err != nil {
|
|
t.Fatalf("ParseTemplateFile() error = %v", err)
|
|
}
|
|
got, err := Diagnose(tf)
|
|
if err != nil {
|
|
t.Fatalf("Diagnose() error = %v", err)
|
|
}
|
|
if diff := cmp.Diff(tt.want, got); diff != "" {
|
|
t.Errorf("Diagnose() mismatch (-got +want):\n%s", diff)
|
|
}
|
|
})
|
|
}
|
|
}
|