| 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 TestDuration(t *testing.T) { | |
| 17 t.Parallel() | |
| 18 | |
| 19 Convey(`A Duration flag`, t, func() { | |
| 20 fs := flag.NewFlagSet("test", flag.ContinueOnError) | |
| 21 var d Duration | |
| 22 fs.Var(&d, "duration", "Test duration parameter.") | |
| 23 | |
| 24 Convey(`Parses a 10-second Duration from "10s".`, func() { | |
| 25 err := fs.Parse([]string{"-duration", "10s"}) | |
| 26 So(err, ShouldBeNil) | |
| 27 So(d.Duration(), ShouldEqual, time.Second*10) | |
| 28 So(d.IsZero(), ShouldBeFalse) | |
| 29 }) | |
| 30 | |
| 31 Convey(`Returns an error when parsing "10z".`, func() { | |
| 32 err := fs.Parse([]string{"-duration", "10z"}) | |
| 33 So(err, ShouldNotBeNil) | |
| 34 }) | |
| 35 | |
| 36 Convey(`When treated as a JSON field`, func() { | |
| 37 var s struct { | |
| 38 D Duration `json:"duration"` | |
| 39 } | |
| 40 | |
| 41 testJSON := `{"duration":10}` | |
| 42 Convey(`Fails to unmarshal from `+testJSON+`.`, func() { | |
| 43 testJSON := testJSON | |
| 44 err := json.Unmarshal([]byte(testJSON), &s) | |
| 45 So(err, ShouldNotBeNil) | |
| 46 }) | |
| 47 | |
| 48 Convey(`Marshals correctly to duration string.`, func()
{ | |
| 49 testDuration := (5 * time.Second) + (2 * time.Mi
nute) | |
| 50 s.D = Duration(testDuration) | |
| 51 testJSON, err := json.Marshal(&s) | |
| 52 So(err, ShouldBeNil) | |
| 53 So(string(testJSON), ShouldEqual, `{"duration":"
2m5s"}`) | |
| 54 | |
| 55 Convey(`And Unmarshals correctly.`, func() { | |
| 56 s.D = Duration(0) | |
| 57 err := json.Unmarshal([]byte(testJSON),
&s) | |
| 58 So(err, ShouldBeNil) | |
| 59 So(s.D.Duration(), ShouldEqual, testDura
tion) | |
| 60 }) | |
| 61 }) | |
| 62 }) | |
| 63 }) | |
| 64 } | |
| OLD | NEW |