Skip to content

Commit

Permalink
test(source): increase test coverage for FuncMap
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Aug 21, 2024
1 parent d8da228 commit 87bda1e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 9 deletions.
9 changes: 3 additions & 6 deletions internal/source/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,7 @@ func embedFSFilepaths(dir string, fileSet *token.FileSet, files []*ast.File, exp
}
var comment strings.Builder
commentNode := readComments(&comment, decl.Doc, spec.Doc)
templateNames, err := parseTemplateNames(comment.String())
if err != nil {
return nil, err
}
templateNames := parseTemplateNames(comment.String())
absMat, err := embeddedFilesMatchingTemplateNameList(dir, fileSet, commentNode, templateNames, embeddedFiles)
if err != nil {
return nil, err
Expand Down Expand Up @@ -263,7 +260,7 @@ func readComments(s *strings.Builder, groups ...*ast.CommentGroup) ast.Node {
return n
}

func parseTemplateNames(input string) ([]string, error) {
func parseTemplateNames(input string) []string {
// todo: refactor to use strconv.QuotedPrefix
var (
templateNames []string
Expand Down Expand Up @@ -306,7 +303,7 @@ func parseTemplateNames(input string) ([]string, error) {
templateNames = append(templateNames, currentTemplateName.String())
}

return templateNames, nil
return templateNames
}

func contextError(workingDirectory string, set *token.FileSet, pos token.Pos, err error) error {
Expand Down
18 changes: 15 additions & 3 deletions internal/source/template_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_parseTemplateNames(t *testing.T) {
Expand Down Expand Up @@ -40,9 +39,22 @@ func Test_parseTemplateNames(t *testing.T) {
},
} {
t.Run(tt.name, func(t *testing.T) {
result, err := parseTemplateNames(tt.input)
require.NoError(t, err)
result := parseTemplateNames(tt.input)
assert.EqualValues(t, tt.expected, result)
})
}

t.Run("mismatched backtick", func(t *testing.T) {
// TODO: make parseTemplateNames return an error
assert.NotPanics(t, func() {
_ = parseTemplateNames("`x\"")
})
})

t.Run("mismatched double quote", func(t *testing.T) {
// TODO: make parseTemplateNames return an error
assert.NotPanics(t, func() {
_ = parseTemplateNames("\"x`")
})
})
}
73 changes: 73 additions & 0 deletions internal/source/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,79 @@ func TestTemplates(t *testing.T) {
})
require.ErrorContains(t, err, `missing_func.gohtml:1: function "enemy" not defined`)
})
t.Run("Func wrong parameter kind", func(t *testing.T) {
dir := createTestDir(t, filepath.FromSlash("testdata/funcs.txtar"))
goFiles, fileSet := parseGo(t, dir)
_, err := source.Templates(dir, "templatesWrongArg", fileSet, goFiles, []string{
filepath.Join(dir, "missing_func.gohtml"),
filepath.Join(dir, "greet.gohtml"),
})
require.ErrorContains(t, err, `expected a composite literal with type template.FuncMap got wrong`)
})

t.Run("Func wrong too many args", func(t *testing.T) {
dir := createTestDir(t, filepath.FromSlash("testdata/funcs.txtar"))
goFiles, fileSet := parseGo(t, dir)
_, err := source.Templates(dir, "templatesTwoArgs", fileSet, goFiles, []string{
filepath.Join(dir, "missing_func.gohtml"),
filepath.Join(dir, "greet.gohtml"),
})
require.ErrorContains(t, err, `expected exactly 1 template.FuncMap composite literal argument`)
})
t.Run("Func wrong too no args", func(t *testing.T) {
dir := createTestDir(t, filepath.FromSlash("testdata/funcs.txtar"))
goFiles, fileSet := parseGo(t, dir)
_, err := source.Templates(dir, "templatesNoArgs", fileSet, goFiles, []string{
filepath.Join(dir, "missing_func.gohtml"),
filepath.Join(dir, "greet.gohtml"),
})
require.ErrorContains(t, err, `expected exactly 1 template.FuncMap composite literal argument`)
})
t.Run("Func wrong package ident", func(t *testing.T) {
dir := createTestDir(t, filepath.FromSlash("testdata/funcs.txtar"))
goFiles, fileSet := parseGo(t, dir)
_, err := source.Templates(dir, "templatesWrongTypePackageName", fileSet, goFiles, []string{
filepath.Join(dir, "missing_func.gohtml"),
filepath.Join(dir, "greet.gohtml"),
})
require.ErrorContains(t, err, `expected a composite literal with type template.FuncMap got wrong.FuncMap{}`)
})
t.Run("Func wrong Type ident", func(t *testing.T) {
dir := createTestDir(t, filepath.FromSlash("testdata/funcs.txtar"))
goFiles, fileSet := parseGo(t, dir)
_, err := source.Templates(dir, "templatesWrongTypeName", fileSet, goFiles, []string{
filepath.Join(dir, "missing_func.gohtml"),
filepath.Join(dir, "greet.gohtml"),
})
require.ErrorContains(t, err, `expected a composite literal with type template.FuncMap got template.Wrong{}`)
})
t.Run("Func wrong Type", func(t *testing.T) {
dir := createTestDir(t, filepath.FromSlash("testdata/funcs.txtar"))
goFiles, fileSet := parseGo(t, dir)
_, err := source.Templates(dir, "templatesWrongTypeExpression", fileSet, goFiles, []string{
filepath.Join(dir, "missing_func.gohtml"),
filepath.Join(dir, "greet.gohtml"),
})
require.ErrorContains(t, err, `expected a composite literal with type template.FuncMap got wrong{}`)
})
t.Run("Func wrong elem", func(t *testing.T) {
dir := createTestDir(t, filepath.FromSlash("testdata/funcs.txtar"))
goFiles, fileSet := parseGo(t, dir)
_, err := source.Templates(dir, "templatesWrongTypeElem", fileSet, goFiles, []string{
filepath.Join(dir, "missing_func.gohtml"),
filepath.Join(dir, "greet.gohtml"),
})
require.ErrorContains(t, err, `expected element at index 0 to be a key value pair got wrong`)
})
t.Run("Func wrong elem key", func(t *testing.T) {
dir := createTestDir(t, filepath.FromSlash("testdata/funcs.txtar"))
goFiles, fileSet := parseGo(t, dir)
_, err := source.Templates(dir, "templatesWrongElemKey", fileSet, goFiles, []string{
filepath.Join(dir, "missing_func.gohtml"),
filepath.Join(dir, "greet.gohtml"),
})
require.ErrorContains(t, err, `expected string literal got wrong`)
})
}

func createTestDir(t *testing.T, filename string) string {
Expand Down
16 changes: 16 additions & 0 deletions internal/source/testdata/funcs.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,22 @@ var (
templatesFuncNotDefined = template.New("x").Funcs(template.FuncMap{
"greet": func() string { return "Hello" },
}).ParseFS(src, "missing_func.gohtml")

templatesWrongArg = template.New("x").Funcs(wrong)

templatesTwoArgs = template.New("x").Funcs(wrong, fail)

templatesNoArgs = template.New("x").Funcs()

templatesWrongTypePackageName = template.New("x").Funcs(wrong.FuncMap{})

templatesWrongTypeName = template.New("x").Funcs(template.Wrong{})

templatesWrongTypeExpression = template.New("x").Funcs(wrong{})

templatesWrongTypeElem = template.New("x").Funcs(template.FuncMap{wrong})

templatesWrongElemKey = template.New("x").Funcs(template.FuncMap{wrong: func() string { return "" }})
)
-- greet.gohtml --
{{greet}}, world!
Expand Down

0 comments on commit 87bda1e

Please sign in to comment.