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 wrapper | |
6 | |
7 import ( | |
8 "fmt" | |
9 "sync" | |
10 "testing" | |
11 | |
12 . "github.com/smartystreets/goconvey/convey" | |
13 ) | |
14 | |
15 type foo struct { | |
16 *BrokenFeatures | |
M-A Ruel
2015/05/27 12:31:46
It doesn't need to be a pointer anymore.
Everywher
iannucci
2015/05/27 17:06:55
yep done
| |
17 } | |
18 | |
19 func (f *foo) halp() error { // test the ability to call IsBroken from an intern al helper | |
20 return f.IsBroken() | |
21 } | |
22 | |
23 func (f *foo) Foo() (string, error) { | |
24 err := f.halp() | |
25 if err != nil { | |
26 return "", err | |
27 } | |
28 return "foo", nil | |
29 } | |
30 | |
31 func (f *foo) Bar() (string, error) { | |
32 err := f.halp() | |
33 if err != nil { | |
34 return "", err | |
35 } | |
36 return "bar", nil | |
37 } | |
38 | |
39 type override struct { | |
40 *BrokenFeatures | |
41 totallyRekt bool | |
42 } | |
43 | |
44 func (o *override) IsBroken() error { | |
45 if o.totallyRekt { | |
46 return fmt.Errorf("totallyRekt") | |
47 } | |
48 return o.BrokenFeatures.IsBroken() | |
49 } | |
50 | |
51 func (o *override) Foo() error { | |
52 return o.IsBroken() | |
53 } | |
54 | |
55 func TestBrokenFeatures(t *testing.T) { | |
56 e := fmt.Errorf("sup") | |
M-A Ruel
2015/05/27 12:31:46
errors.New
iannucci
2015/05/27 17:06:55
done
| |
57 eCustom := fmt.Errorf("bad stuff happened") | |
58 f := foo{&BrokenFeatures{DefaultError: e}} | |
59 | |
60 Convey("BrokenFeatures", t, func() { | |
M-A Ruel
2015/05/27 12:31:46
Frankly, each could be a small test case and it'd
iannucci
2015/05/27 17:06:55
I actually disagree, and I'm a fan of Convey (now
| |
61 Convey("can break functions", func() { | |
62 s, err := f.Foo() | |
63 So(s, ShouldEqual, "foo") | |
64 So(err, ShouldBeNil) | |
65 | |
66 f.BreakFeatures(nil, "Foo") | |
67 _, err = f.Foo() | |
68 So(err, ShouldEqual, e) | |
69 | |
70 Convey("and unbreak them", func() { | |
71 f.UnbreakFeatures("Foo") | |
72 s, err = f.Foo() | |
73 So(s, ShouldEqual, "foo") | |
74 So(err, ShouldBeNil) | |
75 }) | |
76 | |
77 Convey("and breaking features doesn't break unrelated on es", func() { | |
78 s, err := f.Bar() | |
79 So(s, ShouldEqual, "bar") | |
80 So(err, ShouldBeNil) | |
81 }) | |
82 }) | |
83 | |
84 Convey("Can override IsBroken too", func() { | |
85 o := &override{&BrokenFeatures{DefaultError: e}, false} | |
86 Convey("Can break functions as normal", func() { | |
87 o.BreakFeatures(nil, "Foo") | |
88 So(o.Foo(), ShouldEqual, e) | |
89 | |
90 Convey("but can also break them in a user define d way", func() { | |
91 o.totallyRekt = true | |
92 So(o.Foo().Error(), ShouldContainSubstri ng, "totallyRekt") | |
93 }) | |
94 }) | |
95 }) | |
96 | |
97 Convey("Not specifying a default gets you a generic error", func () { | |
98 f.BrokenFeatures.DefaultError = nil | |
99 f.BreakFeatures(nil, "Foo") | |
100 _, err := f.Foo() | |
101 So(err.Error(), ShouldContainSubstring, `"Foo"`) | |
102 }) | |
103 | |
104 Convey("Can override the error returned", func() { | |
105 f.BreakFeatures(eCustom, "Foo") | |
106 v, err := f.Foo() | |
107 So(v, ShouldEqual, "") | |
108 So(err, ShouldEqual, eCustom) | |
109 }) | |
110 | |
111 Convey("Can be broken if not embedded", func() { | |
112 err := error(nil) | |
M-A Ruel
2015/05/27 12:31:46
I often use
var err error
var wg sync.WaitGroup
f
iannucci
2015/05/27 17:06:55
done
| |
113 wg := sync.WaitGroup{} | |
114 wg.Add(1) | |
115 go func() { | |
116 defer wg.Done() | |
117 bf := &BrokenFeatures{DefaultError: e} | |
M-A Ruel
2015/05/27 12:31:46
doesn't need to be a pointer.
iannucci
2015/05/27 17:06:55
done
| |
118 // break some feature so we're forced to crawl t he stack. | |
119 bf.BreakFeatures(nil, "Nerds") | |
120 // should break because there's no exported func tions on the stack. | |
121 err = bf.IsBroken() | |
M-A Ruel
2015/05/27 12:31:46
With Convey, you can do checks in other goroutines
iannucci
2015/05/27 17:06:55
done, I just didn't do it before
| |
122 }() | |
123 wg.Wait() | |
124 So(err, ShouldEqual, ErrBrokenFeaturesBroken) | |
125 }) | |
126 }) | |
127 } | |
OLD | NEW |