Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(336)

Side by Side Diff: go/src/infra/libs/clock/duration_test.go

Issue 1154213012: Add context-aware "time" library wrapper. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 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 clock
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 Convey(`A DurationFlag flag`, t, func() {
18 fs := flag.NewFlagSet("test", flag.ContinueOnError)
19 var d DurationFlag
20 fs.Var(&d, "duration", "Test duration parameter.")
21
22 Convey(`Parses a 10-second Duration from "10s".`, func() {
23 err := fs.Parse([]string{"-duration", "10s"})
24 So(err, ShouldBeNil)
25 So(d.Duration(), ShouldEqual, time.Second*10)
26 So(d.IsZero(), ShouldBeFalse)
27 })
28
29 Convey(`Returns an error when parsing "10z".`, func() {
30 err := fs.Parse([]string{"-duration", "10z"})
31 So(err, ShouldNotBeNil)
32 })
33
34 Convey(`When treated as a JSON field`, func() {
35 var s struct {
36 D DurationFlag `json:"duration"`
37 }
38
39 testJSON := `{"duration":10}`
40 Convey(`Fails to unmarshal from `+testJSON+`.`, func() {
41 testJSON := testJSON
42 err := json.Unmarshal([]byte(testJSON), &s)
43 So(err, ShouldNotBeNil)
44 })
45
46 Convey(`Marshals correctly to duration string.`, func() {
47 testDuration := (5 * time.Second) + (2 * time.Mi nute)
48 s.D = DurationFlag(testDuration)
49 testJSON, err := json.Marshal(&s)
50 So(err, ShouldBeNil)
51 So(string(testJSON), ShouldEqual, `{"duration":" 2m5s"}`)
52
53 Convey(`And Unmarshals correctly.`, func() {
54 s.D = DurationFlag(0)
55 err := json.Unmarshal([]byte(testJSON), &s)
56 So(err, ShouldBeNil)
57 So(s.D.Duration(), ShouldEqual, testDura tion)
58 })
59 })
60 })
61 })
62 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698