Last active
May 30, 2023 16:03
-
-
Save deitrix/58e7331069b8ce61ff88cd9962c68300 to your computer and use it in GitHub Desktop.
Test cases for Luna
This file contains hidden or 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
package main | |
import ( | |
"bytes" | |
"strings" | |
"testing" | |
"text/template" | |
) | |
type Data struct { | |
Type string | |
Origin *Origin | |
} | |
type Origin struct { | |
Type string | |
} | |
const tmpl = ` | |
{{if or (not .Origin) (and .Origin (ne .Origin.Type "scheduler"))}} | |
{{if eq .Type "x"}} | |
a | |
{{else if eq .Type "y"}} | |
b | |
{{else}} | |
c | |
{{end}} | |
{{else}} | |
d | |
{{end}} | |
` | |
func TestCombinations(t *testing.T) { | |
type test struct { | |
data Data | |
expect string | |
} | |
tests := map[string]test{ | |
"origin is nil and type is x": { | |
data: Data{ | |
Type: "x", | |
Origin: nil, | |
}, | |
expect: "a", | |
}, | |
"origin is nil and type is y": { | |
data: Data{ | |
Type: "y", | |
Origin: nil, | |
}, | |
expect: "b", | |
}, | |
"origin is nil and type is unknown": { | |
data: Data{ | |
Type: "z", | |
Origin: nil, | |
}, | |
expect: "c", | |
}, | |
"origin is scheduler and type is x": { | |
data: Data{ | |
Type: "x", | |
Origin: &Origin{ | |
Type: "scheduler", | |
}, | |
}, | |
expect: "d", | |
}, | |
"origin is scheduler and type is y": { | |
data: Data{ | |
Type: "y", | |
Origin: &Origin{ | |
Type: "scheduler", | |
}, | |
}, | |
expect: "d", | |
}, | |
"origin is scheduler and type is unknown": { | |
data: Data{ | |
Type: "z", | |
Origin: &Origin{ | |
Type: "scheduler", | |
}, | |
}, | |
expect: "d", | |
}, | |
"origin is not scheduler and type is x": { | |
data: Data{ | |
Type: "x", | |
Origin: &Origin{ | |
Type: "not-scheduler", | |
}, | |
}, | |
expect: "a", | |
}, | |
"origin is not scheduler and type is y": { | |
data: Data{ | |
Type: "y", | |
Origin: &Origin{ | |
Type: "not-scheduler", | |
}, | |
}, | |
expect: "b", | |
}, | |
"origin is not scheduler and type is unknown": { | |
data: Data{ | |
Type: "z", | |
Origin: &Origin{ | |
Type: "not-scheduler", | |
}, | |
}, | |
expect: "c", | |
}, | |
} | |
templ, _ := template.New("tmpl").Parse(tmpl) | |
for name, test := range tests { | |
t.Run(name, func(t *testing.T) { | |
buf := new(bytes.Buffer) | |
if err := templ.Execute(buf, test.data); err != nil { | |
t.Fatal(err) | |
} | |
got := strings.TrimSpace(buf.String()) | |
if test.expect != got { | |
t.Errorf("expect %s but got %s", test.expect, got) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment