Guest User

Untitled

a guest
Nov 17th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. import spock.lang.Specification
  2. import spock.lang.Unroll
  3. /**
  4. * Created by IntelliJ IDEA.
  5. * User: kyon
  6. * Date: 12/06/24
  7. * Time: 14:53
  8. * To change this template use File | Settings | File Templates.
  9. */
  10. class VendingSpec extends Specification {
  11. def sut
  12. def "setup"(){
  13. sut = new VendingMachine()
  14.  
  15. List.metaClass.everyWithIndex = {Closure closure ->
  16. def result = true
  17. delegate.eachWithIndex{it, i -> result &= closure.call(it, i)}
  18. result
  19. }
  20.  
  21. }
  22.  
  23. @Unroll
  24. def "insert #money is accepted #expect"(){
  25.  
  26. expect:
  27. money.everyWithIndex{it, i ->
  28. sut.insert(it) == expect[i]
  29. }
  30.  
  31. where:
  32. money |expect
  33. [10] |[10]
  34. [10,50] |[10,60]
  35. [10, 5] |[10, 10]
  36. [5000] |[0]
  37. }
  38. @Unroll
  39. def "show #change when insert #money"(){
  40.  
  41. expect:
  42. money.each {
  43. sut.insert(it)
  44. }
  45. sut.showChange() == change
  46. where:
  47. money |change
  48. [10,100] |0
  49. [5000] |5000
  50. [1,10,100,5000] |5001
  51. }
  52.  
  53. @Unroll
  54. def "show stock #goodsName : #goodsPrice has #goodsCount"(){
  55.  
  56. expect:
  57. goodsName.every{
  58. def goods = sut.getStock(it)
  59. [goods.name, goods.price, goods.count] == [it, goodsPrice, goodsCount]
  60. }
  61.  
  62. where:
  63. goodsName |goodsPrice |goodsCount
  64. ["Cola"] |120 |5
  65.  
  66.  
  67. }
  68.  
  69.  
  70. @Unroll
  71. def "do cancel insert #totalPrice "(){
  72.  
  73. expect:
  74. money.each{
  75. sut.insert(it)
  76. }
  77.  
  78. sut.cancel() == 0
  79. sut.showChange() == totalPrice
  80.  
  81. where:
  82. money |totalPrice
  83. [100,100] |200
  84. [5,50,1000] |1055
  85. }
  86.  
  87. def "check goods stock"(){
  88.  
  89. }
  90.  
  91. def "can Buy"(){
  92. expect:
  93.  
  94. money.everyWithIndex{it, i ->
  95. sut.insert(it);
  96. sut.canBuy() == expect[i]
  97. }
  98.  
  99. where:
  100. money |expect
  101. [10,10,100] |[false,false,true]
  102. [500,10] |[true,true]
  103.  
  104. }
  105.  
  106.  
  107. class VendingMachine {
  108. def total = 0
  109. def change = 0
  110. def acceptMoney = [10,50,100,500,1000]
  111. def goods = [name:"Cola",price:120,count:5]
  112. def int insert(money){
  113. if (acceptMoney.contains(money)) {
  114. total +=money
  115. } else {
  116. change += money
  117. }
  118. total
  119. }
  120.  
  121. def showChange(){
  122. change
  123.  
  124. }
  125.  
  126. def getStock(goods){
  127. this.goods
  128. }
  129.  
  130. def cancel(){
  131. change += total
  132. total = 0
  133. 0
  134. }
  135.  
  136. def canBuy(){
  137. total >= goods.price
  138.  
  139. }
  140. }
  141.  
  142. }
Add Comment
Please, Sign In to add comment