File: parser_test_inputs_test.go

package info (click to toggle)
golang-github-smartystreets-gunit 1.2.0%2Bgit20180314.6f0d627-2.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 200 kB
  • sloc: makefile: 5
file content (162 lines) | stat: -rwxr-xr-x 4,093 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package scan

const comprehensiveTestCode = `package parse

import (
	"github.com/smartystreets/assertions/should"
	"github.com/smartystreets/gunit"
)

type BowlingGameScoringTests struct {
	*gunit.Fixture

	game *Game
}

func (self *BowlingGameScoringTests) SetupTheGame() {
	self.game = NewGame()
}

func (self *BowlingGameScoringTests) TeardownTheGame() {
	self.game = nil
}

func (self *BowlingGameScoringTests) TestAfterAllGutterBallsTheScoreShouldBeZero() {
	self.rollMany(20, 0)
	self.So(self.game.Score(), should.Equal, 0)
}

func (self *BowlingGameScoringTests) TestAfterAllOnesTheScoreShouldBeTwenty() {
	self.rollMany(20, 1)
	self.So(self.game.Score(), should.Equal, 20)
}

func (self *BowlingGameScoringTests) SkipTestASpareDeservesABonus()      {}

func (self *BowlingGameScoringTests) LongTestPerfectGame() {
	self.rollMany(12, 10)
	self.So(self.game.Score(), should.Equal, 300)
}

func (self *BowlingGameScoringTests) SkipLongTestPerfectGame() {
	self.rollMany(12, 10)
	self.So(self.game.Score(), should.Equal, 300)
}

func (self *BowlingGameScoringTests) rollMany(times, pins int) {
	for x := 0; x < times; x++ {
		self.game.Roll(pins)
	}
}
func (self *BowlingGameScoringTests) rollSpare() {
	self.game.Roll(5)
	self.game.Roll(5)
}
func (self *BowlingGameScoringTests) rollStrike() {
	self.game.Roll(10)
}

func (self *BowlingGameScoringTests) TestNotNiladic_ShouldNotBeCollected(a int) {
	// This should not be collected (it's not niladic)
}
func (self *BowlingGameScoringTests) TestNotVoid_ShouldNOTBeCollected() int {
	return -1
	// This should not be collected (it's not void)
}

//////////////////////////////////////////////////////////////////////////////

// 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
)

//////////////////////////////////////////////////////////////////////////////
// These types shouldn't be parsed as fixtures:

type TestFixtureWrongTestCase struct {
	*blah.Fixture
}
type TestFixtureWrongPackage struct {
	*gunit.Fixture2
}

type Hah interface {
	Hi() string
}

type BlahFixture struct {
	blah int
}

//////////////////////////////////////////////////////////////////////////////
`