This commit is contained in:
Megan Marsh 2021-03-17 10:48:55 -07:00
parent f12c89bd84
commit a6f907c688
1 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,64 @@
package function
import (
"fmt"
"regexp"
"testing"
"time"
"github.com/zclconf/go-cty/cty"
)
// HCL template usage example:
//
// locals {
// emptyformat = legacy_isotime()
// golangformat = legacy_isotime("01-02-2006")
// }
func TestLegacyIsotime_empty(t *testing.T) {
got, err := LegacyIsotimeFunc.Call([]cty.Value{})
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
_, err = time.Parse(time.RFC3339, got.AsString())
if err != nil {
t.Fatalf("Didn't get an RFC3339 string from empty case: %s", err)
}
}
func TestLegacyIsotime_inputs(t *testing.T) {
tests := []struct {
Value cty.Value
Want string
}{
{
cty.StringVal("01-02-2006"),
`^\d{2}-\d{2}-\d{4}$`,
},
{
cty.StringVal("Mon Jan 02, 2006"),
`^(Mon|Tues|Wed|Thu|Fri|Sat|Sun){1} (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec){1} \d{2}, \d{4}$`,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("legacy_isotime(%#v)", test.Value), func(t *testing.T) {
got, err := LegacyIsotime(test.Value)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
re, err := regexp.Compile(test.Want)
if err != nil {
t.Fatalf("Bad regular expression test string: %#v", err)
}
if !re.MatchString(got.AsString()) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}