-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Atanas Dinov <[email protected]>
- Loading branch information
1 parent
009afe5
commit e7ba299
Showing
6 changed files
with
112 additions
and
126 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
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
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
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
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,25 @@ | ||
package template | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"text/template" | ||
) | ||
|
||
func Parse(name string, contents string, templateData any) (string, error) { | ||
if templateData == nil { | ||
return "", fmt.Errorf("template data not provided") | ||
} | ||
|
||
tmpl, err := template.New(name).Parse(contents) | ||
if err != nil { | ||
return "", fmt.Errorf("parsing contents: %w", err) | ||
} | ||
|
||
var buff bytes.Buffer | ||
if err = tmpl.Execute(&buff, templateData); err != nil { | ||
return "", fmt.Errorf("applying template: %w", err) | ||
} | ||
|
||
return buff.String(), 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,72 @@ | ||
package template | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestParse(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
templateName string | ||
contents string | ||
templateData any | ||
expectedOutput string | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "Template is successfully processed", | ||
templateName: "valid-template", | ||
contents: "{{.Foo}} and {{.Bar}}", | ||
templateData: struct { | ||
Foo string | ||
Bar string | ||
}{ | ||
Foo: "ooF", | ||
Bar: "raB", | ||
}, | ||
expectedOutput: "ooF and raB", | ||
}, | ||
{ | ||
name: "Templating fails due to missing data", | ||
templateName: "missing-data", | ||
contents: "{{.Foo}} and {{.Bar}}", | ||
expectedErr: "template data not provided", | ||
}, | ||
{ | ||
name: "Templating fails due to invalid syntax", | ||
templateName: "invalid-syntax", | ||
contents: "{{.Foo and ", | ||
templateData: struct{}{}, | ||
expectedErr: "parsing contents: template: invalid-syntax:1: unclosed action", | ||
}, | ||
{ | ||
name: "Templating fails due to missing field", | ||
templateName: "invalid-data", | ||
contents: "{{.Foo}} and {{.Bar}}", | ||
templateData: struct { | ||
Foo string | ||
}{ | ||
Foo: "ooF", | ||
}, | ||
expectedErr: "applying template: template: invalid-data:1:15: " + | ||
"executing \"invalid-data\" at <.Bar>: can't evaluate field Bar in type struct { Foo string }", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
data, err := Parse(test.templateName, test.contents, test.templateData) | ||
|
||
if test.expectedErr != "" { | ||
assert.EqualError(t, err, test.expectedErr) | ||
assert.Equal(t, "", data) | ||
} else { | ||
require.Nil(t, err) | ||
assert.Equal(t, test.expectedOutput, data) | ||
} | ||
}) | ||
} | ||
} |