OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package clockflag |
| 6 |
| 7 import ( |
| 8 "encoding/json" |
| 9 "flag" |
| 10 "testing" |
| 11 "time" |
| 12 |
| 13 . "github.com/smartystreets/goconvey/convey" |
| 14 ) |
| 15 |
| 16 func TestTime(t *testing.T) { |
| 17 t.Parallel() |
| 18 |
| 19 Convey(`A Time flag`, t, func() { |
| 20 fs := flag.NewFlagSet("test", flag.ContinueOnError) |
| 21 var d Time |
| 22 fs.Var(&d, "time", "Test time parameter.") |
| 23 |
| 24 Convey(`Parses a 10-second Time from "2015-05-05T23:47:17+00:00"
.`, func() { |
| 25 err := fs.Parse([]string{"-time", "2015-05-05T23:47:17+0
0:00"}) |
| 26 So(err, ShouldBeNil) |
| 27 So(d.Time().Equal(time.Unix(1430869637, 0)), ShouldBeTru
e) |
| 28 }) |
| 29 |
| 30 Convey(`Returns an error when parsing "asdf".`, func() { |
| 31 err := fs.Parse([]string{"-time", "asdf"}) |
| 32 So(err, ShouldNotBeNil) |
| 33 }) |
| 34 |
| 35 Convey(`When treated as a JSON field`, func() { |
| 36 var s struct { |
| 37 T Time `json:"time"` |
| 38 } |
| 39 |
| 40 testJSON := `{"time":"asdf"}` |
| 41 Convey(`Fails to unmarshal from `+testJSON+`.`, func() { |
| 42 testJSON := testJSON |
| 43 err := json.Unmarshal([]byte(testJSON), &s) |
| 44 So(err, ShouldNotBeNil) |
| 45 }) |
| 46 |
| 47 Convey(`Marshals correctly to RFC3339 time string.`, fun
c() { |
| 48 s.T = Time(time.Unix(1430869637, 0)) |
| 49 testJSON, err := json.Marshal(&s) |
| 50 So(err, ShouldBeNil) |
| 51 So(string(testJSON), ShouldEqual, `{"time":"2015
-05-05T23:47:17Z"}`) |
| 52 |
| 53 Convey(`And Unmarshals correctly.`, func() { |
| 54 s.T = Time{} |
| 55 err := json.Unmarshal([]byte(testJSON),
&s) |
| 56 So(err, ShouldBeNil) |
| 57 So(s.T.Time().Equal(time.Unix(1430869637
, 0)), ShouldBeTrue) |
| 58 }) |
| 59 }) |
| 60 }) |
| 61 }) |
| 62 } |
OLD | NEW |