| 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 |
| 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 TestTestable(t *testing.T) { |
| 56 e := fmt.Errorf("sup") |
| 57 eCustom := fmt.Errorf("bad stuff happened") |
| 58 f := foo{NewBrokenFeatures(e)} |
| 59 |
| 60 Convey("Testable", t, func() { |
| 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{NewBrokenFeatures(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("Can override the error returned", func() { |
| 98 f.BreakFeatures(eCustom, "Foo") |
| 99 v, err := f.Foo() |
| 100 So(v, ShouldEqual, "") |
| 101 So(err, ShouldEqual, eCustom) |
| 102 }) |
| 103 |
| 104 Convey("Can be broken if not embedded", func() { |
| 105 err := error(nil) |
| 106 wg := sync.WaitGroup{} |
| 107 wg.Add(1) |
| 108 go func() { |
| 109 defer wg.Done() |
| 110 bf := NewBrokenFeatures(e) |
| 111 // break some feature so we're forced to crawl t
he stack. |
| 112 bf.BreakFeatures(nil, "Nerds") |
| 113 // should break because there's no exported func
tions on the stack. |
| 114 err = bf.IsBroken() |
| 115 }() |
| 116 wg.Wait() |
| 117 So(err, ShouldEqual, ErrBrokenFeaturesBroken) |
| 118 }) |
| 119 }) |
| 120 } |
| OLD | NEW |