Chromium Code Reviews| Index: go/src/infra/gae/libs/wrapper/featurebreaker_test.go |
| diff --git a/go/src/infra/gae/libs/wrapper/featurebreaker_test.go b/go/src/infra/gae/libs/wrapper/featurebreaker_test.go |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3d245084ce8b3f4deddc7114523034b6516eb9bf |
| --- /dev/null |
| +++ b/go/src/infra/gae/libs/wrapper/featurebreaker_test.go |
| @@ -0,0 +1,91 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package wrapper |
| + |
| +import ( |
| + "fmt" |
| + "testing" |
| + |
| + . "github.com/smartystreets/goconvey/convey" |
| +) |
| + |
| +type foo struct { |
| + *BrokenFeatures |
|
M-A Ruel
2015/05/25 17:14:51
So this way you could embed the struct, not a poin
iannucci
2015/05/26 18:25:06
I won't be able to initialize the map on-demand th
M-A Ruel
2015/05/27 00:27:10
Yes, since you need to grab the lock in each metho
|
| +} |
| + |
| +func (f *foo) Foo() (string, error) { |
| + err := f.IsBroken() |
| + if err != nil { |
| + return "", err |
| + } |
| + return "foo", nil |
| +} |
| + |
| +func (f *foo) Bar() (string, error) { |
| + err := f.IsBroken() |
| + if err != nil { |
| + return "", err |
| + } |
| + return "bar", nil |
| +} |
| + |
| +type override struct { |
| + *BrokenFeatures |
| + totallyRekt bool |
| +} |
| + |
| +func (o *override) IsBroken() error { |
| + if o.totallyRekt { |
| + return fmt.Errorf("totallyRekt") |
| + } |
| + return o.BrokenFeatures.IsBroken() |
| +} |
| + |
| +func (o *override) Foo() error { |
| + return o.IsBroken() |
| +} |
| + |
| +func TestTestable(t *testing.T) { |
| + e := fmt.Errorf("sup") |
| + f := foo{NewBrokenFeatures(e)} |
| + |
| + Convey("Testable", t, func() { |
| + Convey("can break functions", func() { |
| + s, err := f.Foo() |
| + So(s, ShouldEqual, "foo") |
| + So(err, ShouldBeNil) |
| + |
| + f.SetBrokenFeatures("Foo") |
| + _, err = f.Foo() |
| + So(err, ShouldEqual, e) |
| + |
| + Convey("and unbreak them", func() { |
| + f.UnsetBrokenFeatures("Foo") |
| + s, err = f.Foo() |
| + So(s, ShouldEqual, "foo") |
| + So(err, ShouldBeNil) |
| + }) |
| + |
| + Convey("and breaking features doesn't break unrelated ones", func() { |
| + s, err := f.Bar() |
| + So(s, ShouldEqual, "bar") |
| + So(err, ShouldBeNil) |
| + }) |
| + }) |
| + |
| + Convey("Can override IsBroken too", func() { |
| + o := &override{NewBrokenFeatures(e), false} |
| + Convey("Can break functions as normal", func() { |
| + o.SetBrokenFeatures("Foo") |
| + So(o.Foo(), ShouldEqual, e) |
| + |
| + Convey("but can also break them in a user defined way", func() { |
| + o.totallyRekt = true |
| + So(o.Foo().Error(), ShouldContainSubstring, "totallyRekt") |
| + }) |
| + }) |
| + }) |
| + }) |
| +} |