Skip to content

Commit

Permalink
chore: add test case with time.Time in struct
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Nov 29, 2023
1 parent 3694961 commit 329f9fe
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
51 changes: 51 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package gomplate_test

import (
"testing"

"github.com/flanksource/gomplate/v3"
)

// var x = map[string]any {
// "sslAge": 7010490606167092,
// "json": map[string]any{},
// "content": "",
// "headers": map[string]interface {} ["Content-Type": *(*interface {})(0xc01d21a508),
// "Content-Length": *(*interface {})(0xc01d21a518),
// "Access-Control-Allow-Origin": *(*interface {})(0xc01d21a528),
// "Access-Control-Allow-Credentials": *(*interface {})(0xc01d21a538),
// "Strict-Transport-Security": *(*interface {})(0xc01d21a548),
// "Date": *(*interface {})(0xc01d21a558),
// },
// "canary": map[string]interface {} ["name": *(*interface {})(0xc01d21a1a8), "namespace": *(*interface {})(0xc01d21a1b8), "labels": *(*interface {})(0xc01d21a1c8), "id": *(*interface {})(0xc01d21a1d8), ],
// "check": 7010490606167092,
// "duration": 702,
// "results": map[string]any{"code": 200, "elapsed": 702296127}

func TestMa(t *testing.T) {
env := map[string]any{
"code": "200",
"sslAge": "true",
"check": map[string]any{
"name": "http-expr-tests",
"id": "018c0096-81c2-7e7d-b324-157d2172c547",
"description": "",
"labels": map[string]any{},
"endpoint": "https://httpbin.demo.aws.flanksource.com/status/200",
},
"duration": 702,
"results": map[string]any{"code": 200, "elapsed": 702296127},
}

for i := 0; i < 10000; i++ {
_, err := gomplate.RunTemplate(env, gomplate.Template{Expression: `string(code) + " should be 500"`})
if err != nil {
t.Fatal(err)
}

if _, ok := env["check"].(map[string]any); !ok {
t.Logf("Of type %T", env["check"])
t.Fail()
}
}
}
27 changes: 27 additions & 0 deletions tests/serialize_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests

import (
"fmt"
"testing"
"time"

Expand All @@ -11,12 +12,18 @@ import (
_ "github.com/robertkrimen/otto/underscore"
)

type myTime struct {
Time time.Time `json:"time"`
Duration time.Duration `json:"duration"`
}

func Test_serialize(t *testing.T) {
t.Parallel()

tests := []struct {
name string
in map[string]any
skip bool
want map[string]any
wantErr bool
}{
Expand Down Expand Up @@ -159,6 +166,22 @@ func Test_serialize(t *testing.T) {
},
},
},
{
name: "nested time.Duration",
skip: true, // TODO:
in: map[string]any{
"a": &myTime{
Time: time.Now(),
Duration: time.Second,
},
},
want: map[string]any{
"a": map[string]any{
"time": time.Now(),
"duration": time.Second,
},
},
},
{
name: "canary checker ctx.Environment",
in: map[string]any{
Expand Down Expand Up @@ -234,6 +257,10 @@ func Test_serialize(t *testing.T) {

for i := range tests {
tt := tests[i]
if tt.skip {
fmt.Printf("Skipping %s\n", tt.name)
continue
}

t.Run(tt.name, func(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit 329f9fe

Please sign in to comment.