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 "errors" |
| 9 "fmt" |
| 10 "sync" |
| 11 "testing" |
| 12 |
| 13 . "github.com/smartystreets/goconvey/convey" |
| 14 ) |
| 15 |
| 16 type foo struct { |
| 17 BrokenFeatures |
| 18 } |
| 19 |
| 20 func (f *foo) halp() error { // test the ability to call IsBroken from an intern
al helper |
| 21 return f.IsBroken() |
| 22 } |
| 23 |
| 24 func (f *foo) Foo() (string, error) { |
| 25 err := f.halp() |
| 26 if err != nil { |
| 27 return "", err |
| 28 } |
| 29 return "foo", nil |
| 30 } |
| 31 |
| 32 func (f *foo) Bar() (string, error) { |
| 33 err := f.halp() |
| 34 if err != nil { |
| 35 return "", err |
| 36 } |
| 37 return "bar", nil |
| 38 } |
| 39 |
| 40 type override struct { |
| 41 BrokenFeatures |
| 42 totallyRekt bool |
| 43 } |
| 44 |
| 45 func (o *override) IsBroken() error { |
| 46 if o.totallyRekt { |
| 47 return fmt.Errorf("totallyRekt") |
| 48 } |
| 49 return o.BrokenFeatures.IsBroken() |
| 50 } |
| 51 |
| 52 func (o *override) Foo() error { |
| 53 return o.IsBroken() |
| 54 } |
| 55 |
| 56 func TestBrokenFeatures(t *testing.T) { |
| 57 e := errors.New("sup") |
| 58 eCustom := fmt.Errorf("bad stuff happened") |
| 59 f := foo{BrokenFeatures{DefaultError: e}} |
| 60 |
| 61 Convey("BrokenFeatures", t, func() { |
| 62 Convey("can break functions", func() { |
| 63 s, err := f.Foo() |
| 64 So(s, ShouldEqual, "foo") |
| 65 So(err, ShouldBeNil) |
| 66 |
| 67 f.BreakFeatures(nil, "Foo") |
| 68 _, err = f.Foo() |
| 69 So(err, ShouldEqual, e) |
| 70 |
| 71 Convey("and unbreak them", func() { |
| 72 f.UnbreakFeatures("Foo") |
| 73 s, err = f.Foo() |
| 74 So(s, ShouldEqual, "foo") |
| 75 So(err, ShouldBeNil) |
| 76 }) |
| 77 |
| 78 Convey("and breaking features doesn't break unrelated on
es", func() { |
| 79 s, err := f.Bar() |
| 80 So(s, ShouldEqual, "bar") |
| 81 So(err, ShouldBeNil) |
| 82 }) |
| 83 }) |
| 84 |
| 85 Convey("Can override IsBroken too", func() { |
| 86 o := &override{BrokenFeatures{DefaultError: e}, false} |
| 87 Convey("Can break functions as normal", func() { |
| 88 o.BreakFeatures(nil, "Foo") |
| 89 So(o.Foo(), ShouldEqual, e) |
| 90 |
| 91 Convey("but can also break them in a user define
d way", func() { |
| 92 o.totallyRekt = true |
| 93 So(o.Foo().Error(), ShouldContainSubstri
ng, "totallyRekt") |
| 94 }) |
| 95 }) |
| 96 }) |
| 97 |
| 98 Convey("Not specifying a default gets you a generic error", func
() { |
| 99 f.BrokenFeatures.DefaultError = nil |
| 100 f.BreakFeatures(nil, "Foo") |
| 101 _, err := f.Foo() |
| 102 So(err.Error(), ShouldContainSubstring, `"Foo"`) |
| 103 }) |
| 104 |
| 105 Convey("Can override the error returned", func() { |
| 106 f.BreakFeatures(eCustom, "Foo") |
| 107 v, err := f.Foo() |
| 108 So(v, ShouldEqual, "") |
| 109 So(err, ShouldEqual, eCustom) |
| 110 }) |
| 111 |
| 112 Convey("Can be broken if not embedded", func(c C) { |
| 113 var wg sync.WaitGroup |
| 114 wg.Add(1) |
| 115 go func() { |
| 116 defer wg.Done() |
| 117 bf := BrokenFeatures{DefaultError: e} |
| 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 c.So(bf.IsBroken(), ShouldEqual, ErrBrokenFeatur
esBroken) |
| 122 }() |
| 123 wg.Wait() |
| 124 }) |
| 125 }) |
| 126 } |
OLD | NEW |