github.com/joomcode/cue@v0.4.4-0.20221111115225-539fe3512047/pkg/time/time_test.go (about) 1 // Copyright 2019 CUE Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package time 16 17 import ( 18 "encoding/json" 19 "strconv" 20 "testing" 21 "time" 22 ) 23 24 func TestTimestamp(t *testing.T) { 25 // Valid go times (for JSON marshaling) are represented as is 26 validTimes := []string{ 27 // valid Go times 28 "null", 29 `"2019-01-02T15:04:05Z"`, 30 `"2019-01-02T15:04:05-08:00"`, 31 `"2019-01-02T15:04:05.0-08:00"`, 32 `"2019-01-02T15:04:05.01-08:00"`, 33 `"2019-01-02T15:04:05.012345678-08:00"`, 34 `"2019-02-28T15:04:59Z"`, 35 36 // TODO: allow leap seconds? This is allowed by the RFC 3339 spec. 37 // `"2019-06-30T23:59:60Z"`, // leap seconds 38 } 39 40 for _, tc := range validTimes { 41 t.Run(tc, func(t *testing.T) { 42 // Test JSON unmarshaling 43 var tm time.Time 44 45 if err := json.Unmarshal([]byte(tc), &tm); err != nil { 46 t.Errorf("unmarshal JSON failed unexpectedly: %v", err) 47 } 48 49 if tc == "null" { 50 return 51 } 52 str, _ := strconv.Unquote(tc) 53 54 if b, err := Time(str); !b || err != nil { 55 t.Errorf("Time failed unexpectedly: %v", err) 56 } 57 if _, err := Parse(RFC3339Nano, str); err != nil { 58 t.Errorf("Parse failed unexpectedly") 59 } 60 }) 61 } 62 63 invalidTimes := []string{ 64 `"2019-01-02T15:04:05"`, // missing time zone 65 `"2019-01-02T15:04:61Z"`, // seconds out of range 66 `"2019-01-02T15:60:00Z"`, // minute out of range 67 `"2019-01-02T24:00:00Z"`, // hour out of range 68 `"2019-01-32T23:00:00Z"`, // day out of range 69 `"2019-01-00T23:00:00Z"`, // day out of range 70 `"2019-00-15T23:00:00Z"`, // month out of range 71 `"2019-13-15T23:00:00Z"`, // month out of range 72 `"2019-01-02T15:04:05Z+08:00"`, // double time zone 73 `"2019-01-02T15:04:05+08"`, // partial time zone 74 75 // TODO: Go 1.17 rejected the extra digits, 76 // and Go 1.18 started accepting them while discarding them. 77 // We want CUE to be consistent across Go versions, 78 // so we should probably fork Go's time package to behave exactly the 79 // way we want and in a consistent way across Go versions. 80 // In the meantime, having newer Go versions accept more inputs is not a 81 // terrible state of affairs, so for now we disable the test case. 82 // `"2019-01-02T15:04:05.01234567890-08:00"`, 83 } 84 85 for _, tc := range invalidTimes { 86 t.Run(tc, func(t *testing.T) { 87 // Test JSON unmarshaling 88 var tm time.Time 89 90 if err := json.Unmarshal([]byte(tc), &tm); err == nil { 91 t.Errorf("unmarshal JSON succeeded unexpectedly: %v", err) 92 } 93 94 str, _ := strconv.Unquote(tc) 95 96 if _, err := Time(str); err == nil { 97 t.Errorf("CUE eval succeeded unexpectedly") 98 } 99 100 if _, err := Parse(RFC3339Nano, str); err == nil { 101 t.Errorf("CUE eval succeeded unexpectedly") 102 } 103 }) 104 } 105 } 106 107 func TestUnix(t *testing.T) { 108 valid := []struct { 109 sec int64 110 nano int64 111 want string 112 }{ 113 {0, 0, "1970-01-01T00:00:00Z"}, 114 {1500000000, 123456, "2017-07-14T02:40:00.000123456Z"}, 115 } 116 117 for _, tc := range valid { 118 t.Run(tc.want, func(t *testing.T) { 119 got := Unix(tc.sec, tc.nano) 120 if got != tc.want { 121 t.Errorf("got %v; want %s", got, tc.want) 122 } 123 }) 124 } 125 }