diff options
author | Ramon Fernandez <ramon@mongodb.com> | 2016-08-25 16:34:34 -0400 |
---|---|---|
committer | Ramon Fernandez <ramon@mongodb.com> | 2016-08-25 16:54:18 -0400 |
commit | c330c9991ab45e7d0685d53e699ef26dba065660 (patch) | |
tree | 3dc5cd06b5f6c7eaaa4cb20cbe763504c14a772b /src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples | |
parent | eb62b862d5ebf179a1bcd9f394070e69c30188ab (diff) | |
download | mongo-c330c9991ab45e7d0685d53e699ef26dba065660.tar.gz |
Import tools: 5b883d86fdb4df55036d5dba2ca6f9dfa0750b44 from branch v3.3
ref: 1ac1389bda..5b883d86fd
for: 3.3.12
SERVER-25814 Initial vendor import: tools
Diffstat (limited to 'src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples')
6 files changed, 333 insertions, 0 deletions
diff --git a/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/assertion_examples_test.go b/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/assertion_examples_test.go new file mode 100644 index 00000000000..a933292a2e7 --- /dev/null +++ b/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/assertion_examples_test.go @@ -0,0 +1,125 @@ +package examples + +import ( + "bytes" + "io" + "testing" + "time" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestAssertionsAreAvailableFromConveyPackage(t *testing.T) { + SetDefaultFailureMode(FailureContinues) + defer SetDefaultFailureMode(FailureHalts) + + Convey("Equality assertions should be accessible", t, func() { + thing1a := thing{a: "asdf"} + thing1b := thing{a: "asdf"} + thing2 := thing{a: "qwer"} + + So(1, ShouldEqual, 1) + So(1, ShouldNotEqual, 2) + So(1, ShouldAlmostEqual, 1.000000000000001) + So(1, ShouldNotAlmostEqual, 2, 0.5) + So(thing1a, ShouldResemble, thing1b) + So(thing1a, ShouldNotResemble, thing2) + So(&thing1a, ShouldPointTo, &thing1a) + So(&thing1a, ShouldNotPointTo, &thing1b) + So(nil, ShouldBeNil) + So(1, ShouldNotBeNil) + So(true, ShouldBeTrue) + So(false, ShouldBeFalse) + So(0, ShouldBeZeroValue) + }) + + Convey("Numeric comparison assertions should be accessible", t, func() { + So(1, ShouldBeGreaterThan, 0) + So(1, ShouldBeGreaterThanOrEqualTo, 1) + So(1, ShouldBeLessThan, 2) + So(1, ShouldBeLessThanOrEqualTo, 1) + So(1, ShouldBeBetween, 0, 2) + So(1, ShouldNotBeBetween, 2, 4) + So(1, ShouldBeBetweenOrEqual, 1, 2) + So(1, ShouldNotBeBetweenOrEqual, 2, 4) + }) + + Convey("Container assertions should be accessible", t, func() { + So([]int{1, 2, 3}, ShouldContain, 2) + So([]int{1, 2, 3}, ShouldNotContain, 4) + So(map[int]int{1: 1, 2: 2, 3: 3}, ShouldContainKey, 2) + So(map[int]int{1: 1, 2: 2, 3: 3}, ShouldNotContainKey, 4) + So(1, ShouldBeIn, []int{1, 2, 3}) + So(4, ShouldNotBeIn, []int{1, 2, 3}) + So([]int{}, ShouldBeEmpty) + So([]int{1}, ShouldNotBeEmpty) + So([]int{1, 2}, ShouldHaveLength, 2) + }) + + Convey("String assertions should be accessible", t, func() { + So("asdf", ShouldStartWith, "a") + So("asdf", ShouldNotStartWith, "z") + So("asdf", ShouldEndWith, "df") + So("asdf", ShouldNotEndWith, "as") + So("", ShouldBeBlank) + So("asdf", ShouldNotBeBlank) + So("asdf", ShouldContainSubstring, "sd") + So("asdf", ShouldNotContainSubstring, "af") + }) + + Convey("Panic recovery assertions should be accessible", t, func() { + So(panics, ShouldPanic) + So(func() {}, ShouldNotPanic) + So(panics, ShouldPanicWith, "Goofy Gophers!") + So(panics, ShouldNotPanicWith, "Guileless Gophers!") + }) + + Convey("Type-checking assertions should be accessible", t, func() { + + // NOTE: Values or pointers may be checked. If a value is passed, + // it will be cast as a pointer to the value to avoid cases where + // the struct being tested takes pointer receivers. Go allows values + // or pointers to be passed as receivers on methods with a value + // receiver, but only pointers on methods with pointer receivers. + // See: + // http://golang.org/doc/effective_go.html#pointers_vs_values + // http://golang.org/doc/effective_go.html#blank_implements + // http://blog.golang.org/laws-of-reflection + + So(1, ShouldHaveSameTypeAs, 0) + So(1, ShouldNotHaveSameTypeAs, "1") + + So(bytes.NewBufferString(""), ShouldImplement, (*io.Reader)(nil)) + So("string", ShouldNotImplement, (*io.Reader)(nil)) + }) + + Convey("Time assertions should be accessible", t, func() { + january1, _ := time.Parse(timeLayout, "2013-01-01 00:00") + january2, _ := time.Parse(timeLayout, "2013-01-02 00:00") + january3, _ := time.Parse(timeLayout, "2013-01-03 00:00") + january4, _ := time.Parse(timeLayout, "2013-01-04 00:00") + january5, _ := time.Parse(timeLayout, "2013-01-05 00:00") + oneDay, _ := time.ParseDuration("24h0m0s") + + So(january1, ShouldHappenBefore, january4) + So(january1, ShouldHappenOnOrBefore, january1) + So(january2, ShouldHappenAfter, january1) + So(january2, ShouldHappenOnOrAfter, january2) + So(january3, ShouldHappenBetween, january2, january5) + So(january3, ShouldHappenOnOrBetween, january3, january5) + So(january1, ShouldNotHappenOnOrBetween, january2, january5) + So(january2, ShouldHappenWithin, oneDay, january3) + So(january5, ShouldNotHappenWithin, oneDay, january1) + So([]time.Time{january1, january2}, ShouldBeChronological) + }) +} + +type thing struct { + a string +} + +func panics() { + panic("Goofy Gophers!") +} + +const timeLayout = "2006-01-02 15:04" diff --git a/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/bowling_game.go b/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/bowling_game.go new file mode 100644 index 00000000000..547bf93d1c3 --- /dev/null +++ b/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/bowling_game.go @@ -0,0 +1,75 @@ +package examples + +// Game contains the state of a bowling game. +type Game struct { + rolls []int + current int +} + +// NewGame allocates and starts a new game of bowling. +func NewGame() *Game { + game := new(Game) + game.rolls = make([]int, maxThrowsPerGame) + return game +} + +// Roll rolls the ball and knocks down the number of pins specified by pins. +func (self *Game) Roll(pins int) { + self.rolls[self.current] = pins + self.current++ +} + +// Score calculates and returns the player's current score. +func (self *Game) Score() (sum int) { + for throw, frame := 0, 0; frame < framesPerGame; frame++ { + if self.isStrike(throw) { + sum += self.strikeBonusFor(throw) + throw += 1 + } else if self.isSpare(throw) { + sum += self.spareBonusFor(throw) + throw += 2 + } else { + sum += self.framePointsAt(throw) + throw += 2 + } + } + return sum +} + +// isStrike determines if a given throw is a strike or not. A strike is knocking +// down all pins in one throw. +func (self *Game) isStrike(throw int) bool { + return self.rolls[throw] == allPins +} + +// strikeBonusFor calculates and returns the strike bonus for a throw. +func (self *Game) strikeBonusFor(throw int) int { + return allPins + self.framePointsAt(throw+1) +} + +// isSpare determines if a given frame is a spare or not. A spare is knocking +// down all pins in one frame with two throws. +func (self *Game) isSpare(throw int) bool { + return self.framePointsAt(throw) == allPins +} + +// spareBonusFor calculates and returns the spare bonus for a throw. +func (self *Game) spareBonusFor(throw int) int { + return allPins + self.rolls[throw+2] +} + +// framePointsAt computes and returns the score in a frame specified by throw. +func (self *Game) framePointsAt(throw int) int { + return self.rolls[throw] + self.rolls[throw+1] +} + +const ( + // allPins is the number of pins allocated per fresh throw. + allPins = 10 + + // framesPerGame is the number of frames per bowling game. + framesPerGame = 10 + + // maxThrowsPerGame is the maximum number of throws possible in a single game. + maxThrowsPerGame = 21 +) diff --git a/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/bowling_game_test.go b/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/bowling_game_test.go new file mode 100644 index 00000000000..18e997d44ae --- /dev/null +++ b/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/bowling_game_test.go @@ -0,0 +1,80 @@ +/* + +Reference: http://butunclebob.com/ArticleS.UncleBob.TheBowlingGameKata + +See the very first link (which happens to be the very first word of +the first paragraph) on the page for a tutorial. + +*/ + +package examples + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestBowlingGameScoring(t *testing.T) { + Convey("Given a fresh score card", t, func() { + game := NewGame() + + Convey("When all gutter balls are thrown", func() { + game.rollMany(20, 0) + + Convey("The score should be zero", func() { + So(game.Score(), ShouldEqual, 0) + }) + }) + + Convey("When all throws knock down only one pin", func() { + game.rollMany(20, 1) + + Convey("The score should be 20", func() { + So(game.Score(), ShouldEqual, 20) + }) + }) + + Convey("When a spare is thrown", func() { + game.rollSpare() + game.Roll(3) + game.rollMany(17, 0) + + Convey("The score should include a spare bonus.", func() { + So(game.Score(), ShouldEqual, 16) + }) + }) + + Convey("When a strike is thrown", func() { + game.rollStrike() + game.Roll(3) + game.Roll(4) + game.rollMany(16, 0) + + Convey("The score should include a strike bonus.", func() { + So(game.Score(), ShouldEqual, 24) + }) + }) + + Convey("When all strikes are thrown", func() { + game.rollMany(21, 10) + + Convey("The score should be 300.", func() { + So(game.Score(), ShouldEqual, 300) + }) + }) + }) +} + +func (self *Game) rollMany(times, pins int) { + for x := 0; x < times; x++ { + self.Roll(pins) + } +} +func (self *Game) rollSpare() { + self.Roll(5) + self.Roll(5) +} +func (self *Game) rollStrike() { + self.Roll(10) +} diff --git a/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/doc.go b/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/doc.go new file mode 100644 index 00000000000..dae661e18dc --- /dev/null +++ b/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/doc.go @@ -0,0 +1,5 @@ +// Package examples contains, well, examples of how to use goconvey to +// specify behavior of a system under test. It contains a well-known example +// by Robert C. Martin called "Bowling Game Kata" as well as another very +// trivial example that demonstrates Reset() and some of the assertions. +package examples diff --git a/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/examples.goconvey b/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/examples.goconvey new file mode 100644 index 00000000000..b5c805fbf4e --- /dev/null +++ b/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/examples.goconvey @@ -0,0 +1,12 @@ +// Uncomment the next line to disable the package when running the GoConvey UI: +//IGNORE + +// Uncomment the next line to limit testing to the specified test function name pattern: +//-run=TestAssertionsAreAvailableFromConveyPackage + +// Uncomment the next line to limit testing to those tests that don't bail when testing.Short() is true: +//-short + +// include any additional `go test` flags or application-specific flags below: + +-timeout=1s diff --git a/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/simple_example_test.go b/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/simple_example_test.go new file mode 100644 index 00000000000..dadfd8136a3 --- /dev/null +++ b/src/mongo/gotools/vendor/src/github.com/smartystreets/goconvey/examples/simple_example_test.go @@ -0,0 +1,36 @@ +package examples + +import ( + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestIntegerManipulation(t *testing.T) { + t.Parallel() + + Convey("Given a starting integer value", t, func() { + x := 42 + + Convey("When incremented", func() { + x++ + + Convey("The value should be greater by one", func() { + So(x, ShouldEqual, 43) + }) + Convey("The value should NOT be what it used to be", func() { + So(x, ShouldNotEqual, 42) + }) + }) + Convey("When decremented", func() { + x-- + + Convey("The value should be lesser by one", func() { + So(x, ShouldEqual, 41) + }) + Convey("The value should NOT be what it used to be", func() { + So(x, ShouldNotEqual, 42) + }) + }) + }) +} |