-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: parse testcases with gotemplate
this ensures all testcases obey the gotemplate grammar and we are compatible with the official implementation
- Loading branch information
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
"strings" | ||
"testing" | ||
"text/template" | ||
) | ||
|
||
var ( | ||
TESTCASE_SEPERATOR = regexp.MustCompile("\n(=+)\n") | ||
INPUT_OUTPUT_SEPERATOR = regexp.MustCompile("\n(---)\n") | ||
TESTS_DIR = "./corpus" | ||
) | ||
|
||
func TestCorpus(t *testing.T) { | ||
testCases, err := getTestCases() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
fmt.Printf("Got %d test cases\n", len(testCases)) | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
testTemplate(t, testCase.input, testCase.isError) | ||
}) | ||
} | ||
} | ||
|
||
func testTemplate(t *testing.T, input string, isError bool) { | ||
t.Helper() | ||
|
||
input = fmt.Sprintf("{{ $var := 1}} \n %s", input) | ||
_, err := template.New("template").Funcs( | ||
template.FuncMap{ | ||
"pipeline": func() string { return "" }, | ||
"functionName": func() string { return "" }, | ||
"condition": func() string { return "" }, | ||
}, | ||
).Parse(input) | ||
|
||
if err != nil && !isError { | ||
t.Fatal(err) | ||
} else if err == nil && isError { | ||
t.Fatal("expected error") | ||
} | ||
} | ||
|
||
type TestCase struct { | ||
file string | ||
name string | ||
input string | ||
sExpression string | ||
isError bool | ||
} | ||
|
||
func NewTestCase(file, name, input, sExpression string) TestCase { | ||
return TestCase{ | ||
name: fmt.Sprintf("%s: %s", file, name), | ||
input: input, | ||
sExpression: sExpression, | ||
isError: strings.Contains(sExpression, "(ERROR"), | ||
} | ||
} | ||
|
||
func getTestCasesForFile(filename string, content string) []TestCase { | ||
testCases := []TestCase{} | ||
parts := TESTCASE_SEPERATOR.Split(content, -1) | ||
|
||
for i := 0; i < len(parts); i++ { | ||
if i%2 == 0 && len(parts) > i+1 { | ||
|
||
testName := strings.TrimSpace(parts[i]) | ||
testContent := parts[i+1] | ||
testParts := INPUT_OUTPUT_SEPERATOR.Split(testContent, -1) | ||
|
||
if len(testParts) != 2 { | ||
continue | ||
} | ||
|
||
testCases = append(testCases, | ||
NewTestCase( | ||
filename, | ||
testName, | ||
testParts[0], | ||
testParts[1], | ||
), | ||
) | ||
} | ||
} | ||
|
||
return testCases | ||
} | ||
|
||
func getTestCases() ([]TestCase, error) { | ||
var testCases []TestCase | ||
|
||
err := filepath.Walk(TESTS_DIR, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if !info.IsDir() && strings.HasSuffix(info.Name(), ".txt") { | ||
content, err := os.ReadFile(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
testCases = append(testCases, getTestCasesForFile(path, string(content))...) | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
fmt.Println("Error walking the path:", err) | ||
return nil, err | ||
} | ||
|
||
return testCases, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module github.com/ngalaiko/tree-sitter-go-template/test | ||
|
||
go 1.23.4 |