Skip to content

Commit

Permalink
test: slice of structs
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Jul 21, 2023
1 parent ee41d23 commit f9ada36
Showing 1 changed file with 99 additions and 6 deletions.
105 changes: 99 additions & 6 deletions template_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gomplate

import (
"reflect"
"testing"
"time"

Expand All @@ -10,22 +11,28 @@ import (
"github.com/stretchr/testify/assert"
)

type NoStructTag struct {
Name string
UPPER string
}

type Address struct {
City string `json:"city_name"`
}

type Person struct {
Name string `json:"name"`
Address Address
MetaData map[string]any
Codes []string
Name string `json:"name"`
Address *Address `json:",omitempty"`
MetaData map[string]any `json:",omitempty"`
Codes []string `json:",omitempty"`
Addresses []Address `json:"addresses,omitempty"`
}

// A shared test data for all template test
var structEnv = map[string]any{
"results": Person{
Name: "Aditya",
Address: Address{
Address: &Address{
City: "Kathmandu",
},
},
Expand Down Expand Up @@ -58,6 +65,7 @@ var junitEnv = JunitTestSuites{
Totals: Totals{
Passed: 1,
},
Suites: []JunitTestSuite{{Name: "hi", Totals: Totals{Failed: 2}}},
}

type SQLDetails struct {
Expand Down Expand Up @@ -112,7 +120,16 @@ func TestGomplate(t *testing.T) {
{map[string]interface{}{"old": "1.2.3", "new": "1.2.3"}, "{{ .old | semverCompare .new }}", "true"},
{map[string]interface{}{"old": "1.2.3", "new": "1.2.4"}, "{{ .old | semverCompare .new }}", "false"},
{structEnv, `{{.results.name}} {{.results.Address.city_name}}`, "Aditya Kathmandu"},
{map[string]any{"results": junitEnv}, `{{.results.passed}}`, "1"},
{
map[string]any{"results": junitEnv},
`
{{.results.passed}}
{{ range $r := .results.suites}}
{{$r.name}} ✅ {{$r.passed}} ❌ {{$r.failed}} in 🕑 {{$r.duration}}
{{end}}
`,
"1",
},
{
map[string]any{
"results": SQLDetails{
Expand Down Expand Up @@ -184,3 +201,79 @@ func TestCel(t *testing.T) {
})
}
}

func Test_serialize(t *testing.T) {
tests := []struct {
name string
in map[string]any
want map[string]any
wantErr bool
}{
{name: "nil", in: nil, want: nil, wantErr: false},
{name: "empty", in: map[string]any{}, want: map[string]any{}, wantErr: false},
{
name: "simple - no struct tags",
in: map[string]any{"r": NoStructTag{Name: "Kathmandu", UPPER: "u"}},
want: map[string]any{"r": map[string]any{"Name": "Kathmandu", "UPPER": "u"}},
wantErr: false,
},
{name: "simple - struct tags", in: map[string]any{"r": Address{City: "Kathmandu"}}, want: map[string]any{"r": map[string]any{"city_name": "Kathmandu"}}, wantErr: false},
{
name: "nested struct",
in: map[string]any{"r": Person{Name: "Aditya", Address: &Address{City: "Kathmandu"}}},
want: map[string]any{"r": map[string]any{"name": "Aditya", "Address": map[string]any{"city_name": "Kathmandu"}}},
wantErr: false,
},
{
name: "slice of struct",
in: map[string]any{
"r": []Address{
{City: "Kathmandu"},
{City: "Lalitpur"},
},
},
want: map[string]any{
"r": []any{
map[string]any{"city_name": "Kathmandu"},
map[string]any{"city_name": "Lalitpur"},
},
},
wantErr: false,
},
{
name: "nested slice of struct",
in: map[string]any{
"r": Person{
Name: "Aditya",
Addresses: []Address{
{City: "Kathmandu"},
{City: "Lalitpur"},
},
},
},
want: map[string]any{
"r": map[string]any{
"name": "Aditya",
"addresses": []map[string]any{
{"city_name": "Kathmandu"},
{"city_name": "Lalitpur"},
},
},
},
wantErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := serialize(tt.in)
if (err != nil) != tt.wantErr {
t.Errorf("serialize() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("serialize() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit f9ada36

Please sign in to comment.