diff --git a/internal/source/template.go b/internal/source/template.go index fef78a2..3942db8 100644 --- a/internal/source/template.go +++ b/internal/source/template.go @@ -88,6 +88,15 @@ func evaluateTemplateSelector(ts *template.Template, expression ast.Expr, workin return nil, err } return up.Delims(list[0], list[1]), nil + case "Parse": + if len(call.Args) != 1 { + return nil, contextError(workingDirectory, fileSet, call.Lparen, fmt.Errorf("expected exactly one string literal argument")) + } + sl, err := evaluateStringLiteralExpression(workingDirectory, fileSet, call.Args[0]) + if err != nil { + return nil, err + } + return up.Parse(sl) case "New": if len(call.Args) != 1 { return nil, contextError(workingDirectory, fileSet, call.Lparen, fmt.Errorf("expected exactly one string literal argument")) diff --git a/internal/source/template_test.go b/internal/source/template_test.go index 27ec984..e478c87 100644 --- a/internal/source/template_test.go +++ b/internal/source/template_test.go @@ -519,6 +519,33 @@ func TestTemplates(t *testing.T) { }) require.ErrorContains(t, err, `expected string literal got wrong`) }) + t.Run("Parse template name from new", func(t *testing.T) { + dir := createTestDir(t, filepath.FromSlash("testdata/parse.txtar")) + goFiles, fileSet := parseGo(t, dir) + ts, err := source.Templates(dir, "templates", fileSet, goFiles, nil) + require.NoError(t, err) + assert.NotNil(t, ts.Lookup("GET /")) + }) + t.Run("Parse string has multiple routes", func(t *testing.T) { + dir := createTestDir(t, filepath.FromSlash("testdata/parse.txtar")) + goFiles, fileSet := parseGo(t, dir) + ts, err := source.Templates(dir, "multiple", fileSet, goFiles, nil) + require.NoError(t, err) + assert.NotNil(t, ts.Lookup("GET /")) + assert.NotNil(t, ts.Lookup("GET /{name}")) + }) + t.Run("Parse is missing argument", func(t *testing.T) { + dir := createTestDir(t, filepath.FromSlash("testdata/parse.txtar")) + goFiles, fileSet := parseGo(t, dir) + _, err := source.Templates(dir, "noArg", fileSet, goFiles, nil) + require.ErrorContains(t, err, "run template noArg failed at parse.go:12:35: expected exactly one string literal argument") + }) + t.Run("Parse gets wrong argument type", func(t *testing.T) { + dir := createTestDir(t, filepath.FromSlash("testdata/parse.txtar")) + goFiles, fileSet := parseGo(t, dir) + _, err := source.Templates(dir, "wrongArg", fileSet, goFiles, nil) + require.ErrorContains(t, err, "run template wrongArg failed at parse.go:14:40: expected string literal got 500") + }) } func createTestDir(t *testing.T, filename string) string { diff --git a/internal/source/testdata/parse.txtar b/internal/source/testdata/parse.txtar new file mode 100644 index 0000000..7157867 --- /dev/null +++ b/internal/source/testdata/parse.txtar @@ -0,0 +1,15 @@ +-- parse.go -- +package main + +import "html/template" + +var templates = template.New("GET /").Parse(`

Hello, world!

`) + +var multiple = template.New("").Parse(` +{{define "GET /"}}

Hello, world!

{{end}} +{{define "GET /{name}"}}

Hello, {{.PathValue "name"}}!

{{end}} +`) + +var noArg = template.New("").Parse() + +var wrongArg = template.New("").Parse(500) \ No newline at end of file