blob: 5955446c9982e8d82c56c6509a12a86b10b0ae18 [file] [log] [blame]
Marcel van Lohuizen57333362019-04-01 14:35:09 +02001// 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
15package time
16
17import (
18 "encoding/json"
19 "testing"
20 "time"
21
22 "cuelang.org/go/cue"
23 "cuelang.org/go/cue/load"
24 "cuelang.org/go/cue/parser"
Marcel van Lohuizen57333362019-04-01 14:35:09 +020025)
26
27func TestTime(t *testing.T) {
28 inst := cue.Build(load.Instances([]string{"."}, nil))[0]
29 if inst.Err != nil {
30 t.Fatal(inst.Err)
31 }
32
33 parseCUE := func(t *testing.T, time string) error {
Marcel van Lohuizen3c7c40b2019-05-22 11:31:27 -040034 expr, err := parser.ParseExpr("test", "Time&"+time)
Marcel van Lohuizen57333362019-04-01 14:35:09 +020035 if err != nil {
36 t.Fatal(err)
37 }
38 v := inst.Eval(expr)
39 return v.Err()
40 }
41
42 // Valid go times (for JSON marshaling) are represented as is
43 validTimes := []string{
44 // valid Go times
45 "null",
46 `"2019-01-02T15:04:05Z"`,
47 `"2019-01-02T15:04:05-08:00"`,
48 `"2019-01-02T15:04:05.0-08:00"`,
49 `"2019-01-02T15:04:05.01-08:00"`,
50 `"2019-01-02T15:04:05.0123456789-08:00"`, // Is this a Go bug?
51 `"2019-02-28T15:04:59Z"`,
52
53 // TODO: allow leap seconds? This is allowed by the RFC 3339 spec.
54 // `"2019-06-30T23:59:60Z"`, // leap seconds
55 }
56
57 for _, tc := range validTimes {
58 t.Run(tc, func(t *testing.T) {
59 // Test JSON unmarshaling
60 var tm time.Time
61
62 if err := json.Unmarshal([]byte(tc), &tm); err != nil {
63 t.Errorf("unmarshal JSON failed unexpectedly: %v", err)
64 }
65
66 if err := parseCUE(t, tc); err != nil {
67 t.Errorf("CUE eval failed unexpectedly: %v", err)
68 }
69 })
70 }
71
72 invalidTimes := []string{
73 `"2019-01-02T15:04:05"`, // missing time zone
74 `"2019-01-02T15:04:61Z"`, // seconds out of range
75 `"2019-01-02T15:60:00Z"`, // minute out of range
76 `"2019-01-02T24:00:00Z"`, // hour out of range
77 `"2019-01-32T23:00:00Z"`, // day out of range
78 `"2019-01-00T23:00:00Z"`, // day out of range
79 `"2019-00-15T23:00:00Z"`, // month out of range
80 `"2019-13-15T23:00:00Z"`, // month out of range
81 `"2019-01-02T15:04:05Z+08:00"`, // double time zone
82 `"2019-01-02T15:04:05+08"`, // partial time zone
83 `"2019-01-02T15:04:05.01234567890-08:00"`,
84 }
85
86 for _, tc := range invalidTimes {
87 t.Run(tc, func(t *testing.T) {
88 // Test JSON unmarshaling
89 var tm time.Time
90
91 if err := json.Unmarshal([]byte(tc), &tm); err == nil {
92 t.Errorf("unmarshal JSON succeeded unexpectedly: %v", err)
93 }
94
95 if err := parseCUE(t, tc); err == nil {
96 t.Errorf("CUE eval succeeded unexpectedly: %v", err)
97 }
98 })
99 }
100}