github.com/solo-io/cue@v0.4.7/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  		`"2019-01-02T15:04:05.01234567890-08:00"`,
    75  	}
    76  
    77  	for _, tc := range invalidTimes {
    78  		t.Run(tc, func(t *testing.T) {
    79  			// Test JSON unmarshaling
    80  			var tm time.Time
    81  
    82  			if err := json.Unmarshal([]byte(tc), &tm); err == nil {
    83  				t.Errorf("unmarshal JSON succeeded unexpectedly: %v", err)
    84  			}
    85  
    86  			str, _ := strconv.Unquote(tc)
    87  
    88  			if _, err := Time(str); err == nil {
    89  				t.Errorf("CUE eval succeeded unexpectedly")
    90  			}
    91  
    92  			if _, err := Parse(RFC3339Nano, str); err == nil {
    93  				t.Errorf("CUE eval succeeded unexpectedly")
    94  			}
    95  		})
    96  	}
    97  }
    98  
    99  func TestUnix(t *testing.T) {
   100  	valid := []struct {
   101  		sec  int64
   102  		nano int64
   103  		want string
   104  	}{
   105  		{0, 0, "1970-01-01T00:00:00Z"},
   106  		{1500000000, 123456, "2017-07-14T02:40:00.000123456Z"},
   107  	}
   108  
   109  	for _, tc := range valid {
   110  		t.Run(tc.want, func(t *testing.T) {
   111  			got := Unix(tc.sec, tc.nano)
   112  			if got != tc.want {
   113  				t.Errorf("got %v; want %s", got, tc.want)
   114  			}
   115  		})
   116  	}
   117  }