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 Convey(`A Time flag`, t, func() { |
| 18 fs := flag.NewFlagSet("test", flag.ContinueOnError) |
| 19 var d Time |
| 20 fs.Var(&d, "time", "Test time parameter.") |
| 21 |
| 22 Convey(`Parses a 10-second Time from "2015-05-05T23:47:17+00:00"
.`, func() { |
| 23 err := fs.Parse([]string{"-time", "2015-05-05T23:47:17+0
0:00"}) |
| 24 So(err, ShouldBeNil) |
| 25 So(d.Time().Equal(time.Unix(1430869637, 0)), ShouldBeTru
e) |
| 26 }) |
| 27 |
| 28 Convey(`Returns an error when parsing "asdf".`, func() { |
| 29 err := fs.Parse([]string{"-time", "asdf"}) |
| 30 So(err, ShouldNotBeNil) |
| 31 }) |
| 32 |
| 33 Convey(`When treated as a JSON field`, func() { |
| 34 var s struct { |
| 35 T Time `json:"time"` |
| 36 } |
| 37 |
| 38 testJSON := `{"time":"asdf"}` |
| 39 Convey(`Fails to unmarshal from `+testJSON+`.`, func() { |
| 40 testJSON := testJSON |
| 41 err := json.Unmarshal([]byte(testJSON), &s) |
| 42 So(err, ShouldNotBeNil) |
| 43 }) |
| 44 |
| 45 Convey(`Marshals correctly to RFC3339 time string.`, fun
c() { |
| 46 s.T = Time(time.Unix(1430869637, 0)) |
| 47 testJSON, err := json.Marshal(&s) |
| 48 So(err, ShouldBeNil) |
| 49 So(string(testJSON), ShouldEqual, `{"time":"2015
-05-05T23:47:17Z"}`) |
| 50 |
| 51 Convey(`And Unmarshals correctly.`, func() { |
| 52 s.T = Time{} |
| 53 err := json.Unmarshal([]byte(testJSON),
&s) |
| 54 So(err, ShouldBeNil) |
| 55 So(s.T.Time().Equal(time.Unix(1430869637
, 0)), ShouldBeTrue) |
| 56 }) |
| 57 }) |
| 58 }) |
| 59 }) |
| 60 } |
OLD | NEW |