Skip to content

Commit

Permalink
feat: support template.Template.Parse function
Browse files Browse the repository at this point in the history
resolves: #11
  • Loading branch information
crhntr committed Aug 30, 2024
1 parent daadcdf commit d6c9856
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
9 changes: 9 additions & 0 deletions internal/source/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
27 changes: 27 additions & 0 deletions internal/source/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions internal/source/testdata/parse.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- parse.go --
package main

import "html/template"

var templates = template.New("GET /").Parse(`<h1>Hello, world!</h1>`)

var multiple = template.New("").Parse(`
{{define "GET /"}}<h1>Hello, world!</h1>{{end}}
{{define "GET /{name}"}}<h1>Hello, {{.PathValue "name"}}!</h1>{{end}}
`)

var noArg = template.New("").Parse()

var wrongArg = template.New("").Parse(500)

0 comments on commit d6c9856

Please sign in to comment.