Skip to content

Commit

Permalink
test: parse testcases with gotemplate
Browse files Browse the repository at this point in the history
this ensures all testcases obey the gotemplate grammar
and we are compatible with the official implementation
  • Loading branch information
qvalentin committed Jan 14, 2025
1 parent 161e38c commit 3063143
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
122 changes: 122 additions & 0 deletions test/corpus_test.go
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
}
3 changes: 3 additions & 0 deletions test/go.mod
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

0 comments on commit 3063143

Please sign in to comment.