Created
June 24, 2012 13:21
-
-
Save Gab-km/2983219 to your computer and use it in GitHub Desktop.
Cafe.Spockの成果物
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import spock.lang.* | |
class DropAndRefundTest extends spock.lang.Specification { | |
def setup(){ | |
} | |
@Unroll | |
def "自販機に #money を投入できる"() { | |
setup: | |
def vender = new VendingMachine() | |
expect: | |
vender.dropIn(money) | |
notThrown(IllegalArgumentException) | |
where: | |
money << [Money.ten(), Money.fifty(), Money.hundred(), Money.fiveHundreds(), Money.thousand()] | |
} | |
@Unroll | |
def "総計を取得できる"() { | |
setup: | |
def vender = new VendingMachine() | |
expect: | |
vender.dropIn(money) | |
vender.totalValue() == expected | |
where: | |
money | expected | |
Money.ten() | 10 | |
Money.fifty() | 50 | |
Money.hundred() | 100 | |
Money.fiveHundreds() | 500 | |
Money.thousand() | 1000 | |
} | |
@Unroll | |
def "複数枚のお金を投入した時に、累計を取得できる"() { | |
setup: | |
def vender = new VendingMachine() | |
expect: | |
money.each {m -> vender.dropIn(m)} | |
vender.totalValue() == expected | |
where: | |
money | expected | |
[Money.ten(), Money.hundred()] | 110 | |
[Money.fifty(), Money.thousand(), Money.fiveHundreds()] | 1550 | |
} | |
@Unroll | |
def "払い戻し操作を行うと、投入金額の総計を釣り銭として出力する"() { | |
setup: | |
def vender = new VendingMachine() | |
expect: | |
money.each {m -> vender.dropIn(m)} | |
vender.refund() == expected | |
vender.totalValue() == 0 | |
where: | |
money | expected | |
[Money.hundred()] | 100 | |
[Money.fifty()] | 50 | |
[Money.ten(), Money.fiveHundreds()] | 510 | |
} | |
def "#money when bar #expected"(){ | |
setup: | |
def vender = new VendingMachine() | |
expect: | |
vender.dropIn(money) == expected | |
where: | |
money | expected | |
Money.fiveThousands() | 5000 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Money { | |
int value | |
static Money ten() { | |
new Money(value: 10) | |
} | |
static Money fifty() { | |
new Money(value: 50) | |
} | |
static Money hundred() { | |
new Money(value: 100) | |
} | |
static Money fiveHundreds() { | |
new Money(value: 500) | |
} | |
static Money thousand() { | |
new Money(value: 1000) | |
} | |
static Money fiveThousands() { | |
new Money(value: 5000) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class VendingMachine { | |
int amount | |
def dropIn(Money money) { | |
if (money.value == 5000) { | |
5000 | |
} else { | |
amount += money.value | |
0 | |
} | |
} | |
def totalValue() { | |
amount | |
} | |
int refund() { | |
int ret = amount | |
amount = 0 | |
println ret | |
ret | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment