github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/datetime/format_test.go (about) 1 package datetime_test 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/MontFerret/ferret/pkg/runtime/core" 8 "github.com/MontFerret/ferret/pkg/runtime/values" 9 "github.com/MontFerret/ferret/pkg/stdlib/datetime" 10 ) 11 12 func TestDateFormat(t *testing.T) { 13 tcs := []*testCase{ 14 &testCase{ 15 Name: "When more than 2 arguments", 16 Expected: values.None, 17 Args: []core.Value{ 18 values.NewString("string"), 19 values.NewInt(0), 20 values.NewArray(0), 21 }, 22 ShouldErr: true, 23 }, 24 &testCase{ 25 Name: "When less than 2 arguments", 26 Expected: values.None, 27 Args: []core.Value{ 28 values.NewInt(0), 29 }, 30 ShouldErr: true, 31 }, 32 &testCase{ 33 Name: "When first argument is wrong", 34 Expected: values.None, 35 Args: []core.Value{ 36 values.NewInt(0), 37 values.NewString(time.RFC822), 38 }, 39 ShouldErr: true, 40 }, 41 &testCase{ 42 Name: "When second argument is wrong", 43 Expected: values.None, 44 Args: []core.Value{ 45 values.NewCurrentDateTime(), 46 values.NewInt(0), 47 }, 48 ShouldErr: true, 49 }, 50 &testCase{ 51 Name: "When DefaultTimeLayout", 52 Expected: values.NewString("1999-02-07T15:04:05Z"), 53 Args: []core.Value{ 54 mustDefaultLayoutDt("1999-02-07T15:04:05Z"), 55 values.NewString(values.DefaultTimeLayout), 56 }, 57 }, 58 &testCase{ 59 Name: "When RFC3339Nano", 60 Expected: values.NewString( 61 time.Date(2018, time.November, 5, 0, 54, 15, 5125, time.Local). 62 Format(time.RFC3339Nano), 63 ), 64 Args: []core.Value{ 65 values.NewDateTime( 66 time.Date(2018, time.November, 5, 0, 54, 15, 5125, time.Local), 67 ), 68 values.NewString(time.RFC3339Nano), 69 }, 70 }, 71 &testCase{ 72 Name: "When custom format", 73 Expected: values.NewString( 74 time.Date(2018, time.November, 5, 0, 54, 15, 5125, time.Local). 75 Format("2006-01-02"), 76 ), 77 Args: []core.Value{ 78 values.NewDateTime( 79 time.Date(2018, time.November, 5, 0, 54, 15, 5125, time.Local), 80 ), 81 values.NewString("2006-01-02"), 82 }, 83 }, 84 &testCase{ 85 Name: "When empty string", 86 Expected: values.NewString(""), 87 Args: []core.Value{ 88 values.NewCurrentDateTime(), 89 values.NewString(""), 90 }, 91 }, 92 &testCase{ 93 Name: "When random string without numbers", 94 Expected: values.NewString("qwerty"), 95 Args: []core.Value{ 96 values.NewCurrentDateTime(), 97 values.NewString("qwerty"), 98 }, 99 }, 100 &testCase{ 101 Name: "When random string with numbers", 102 Expected: values.NewString("qwerty2018uio"), 103 Args: []core.Value{ 104 values.NewDateTime( 105 time.Date(2018, time.November, 5, 0, 54, 15, 5125, time.Local), 106 ), 107 values.NewString("qwerty2006uio"), 108 }, 109 }, 110 } 111 112 for _, tc := range tcs { 113 tc.Do(t, datetime.DateFormat) 114 } 115 }