Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import spock.lang.Specification
- import spock.lang.Unroll
- /**
- * Created by IntelliJ IDEA.
- * User: kyon
- * Date: 12/06/24
- * Time: 14:53
- * To change this template use File | Settings | File Templates.
- */
- class VendingSpec extends Specification {
- def sut
- def "setup"(){
- sut = new VendingMachine()
- List.metaClass.everyWithIndex = {Closure closure ->
- def result = true
- delegate.eachWithIndex{it, i -> result &= closure.call(it, i)}
- result
- }
- }
- @Unroll
- def "insert #money is accepted #expect"(){
- expect:
- money.everyWithIndex{it, i ->
- sut.insert(it) == expect[i]
- }
- where:
- money |expect
- [10] |[10]
- [10,50] |[10,60]
- [10, 5] |[10, 10]
- [5000] |[0]
- }
- @Unroll
- def "show #change when insert #money"(){
- expect:
- money.each {
- sut.insert(it)
- }
- sut.showChange() == change
- where:
- money |change
- [10,100] |0
- [5000] |5000
- [1,10,100,5000] |5001
- }
- @Unroll
- def "show stock #goodsName : #goodsPrice has #goodsCount"(){
- expect:
- goodsName.every{
- def goods = sut.getStock(it)
- [goods.name, goods.price, goods.count] == [it, goodsPrice, goodsCount]
- }
- where:
- goodsName |goodsPrice |goodsCount
- ["Cola"] |120 |5
- }
- @Unroll
- def "do cancel insert #totalPrice "(){
- expect:
- money.each{
- sut.insert(it)
- }
- sut.cancel() == 0
- sut.showChange() == totalPrice
- where:
- money |totalPrice
- [100,100] |200
- [5,50,1000] |1055
- }
- def "check goods stock"(){
- }
- def "can Buy"(){
- expect:
- money.everyWithIndex{it, i ->
- sut.insert(it);
- sut.canBuy() == expect[i]
- }
- where:
- money |expect
- [10,10,100] |[false,false,true]
- [500,10] |[true,true]
- }
- class VendingMachine {
- def total = 0
- def change = 0
- def acceptMoney = [10,50,100,500,1000]
- def goods = [name:"Cola",price:120,count:5]
- def int insert(money){
- if (acceptMoney.contains(money)) {
- total +=money
- } else {
- change += money
- }
- total
- }
- def showChange(){
- change
- }
- def getStock(goods){
- this.goods
- }
- def cancel(){
- change += total
- total = 0
- 0
- }
- def canBuy(){
- total >= goods.price
- }
- }
- }
Add Comment
Please, Sign In to add comment