Guest User

Untitled

a guest
Dec 10th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using NSpec;
  3.  
  4. class describe_VendingMachine : nspec
  5. {
  6. void given_an_empty_VendingMachine()
  7. {
  8. before = () => machine = new VendingMachine();
  9.  
  10. should_not_be_in_stock("A1");
  11.  
  12. context["doritos are stocked in A1, with a price of $1"] = () =>
  13. {
  14. stock("doritos", "A1", 1m);
  15.  
  16. should_be_in_stock("A1");
  17.  
  18. context["mountain dew is stocked in A2, with a price of $1"] = () =>
  19. {
  20. stock("mountain dew", "A2", 1m);
  21.  
  22. should_be_in_stock("A2");
  23.  
  24. should_be_in_stock("A1");
  25. };
  26.  
  27. context["doritos are vended"] = () =>
  28. {
  29. before = () => machine.VendItem("A1");
  30.  
  31. should_not_be_in_stock("A1");
  32. };
  33.  
  34. specify = () => machine.CanVend("A1").should_be_false();
  35.  
  36. new[] { 1m, 5m }.Do(amount =>
  37. {
  38. context["{0} dollar is put in the machine".With(amount)] = () =>
  39. {
  40. insert_money(amount);
  41.  
  42. specify = () => machine.CanVend("A1").should_be_true();
  43. };
  44. });
  45. };
  46. }
  47.  
  48. void insert_money(decimal amount)
  49. {
  50. before = () => machine.Insert(amount);
  51. }
  52.  
  53. void should_be_in_stock(string slot)
  54. {
  55. specify = () => machine.InStock(slot).should_be_true();
  56. }
  57.  
  58. void should_not_be_in_stock(string slot)
  59. {
  60. specify = () => machine.InStock(slot).should_be_false();
  61. }
  62.  
  63. void stock(string name, string slot, decimal price)
  64. {
  65. before = () => machine.StockItem(name, slot, price);
  66. }
  67.  
  68. VendingMachine machine;
  69. }
  70.  
  71. public class VendingMachine
  72. {
  73. public void StockItem(string name, string slot, decimal price)
  74. {
  75. stocked.Add(slot);
  76.  
  77. this.price = price;
  78. }
  79.  
  80. public bool InStock(string slot)
  81. {
  82. return stocked.Contains(slot);
  83. }
  84.  
  85. public VendingMachine()
  86. {
  87. stocked = new List<string>();
  88. }
  89.  
  90. public void VendItem(string slot)
  91. {
  92. stocked.Remove(slot);
  93. }
  94.  
  95. public void Insert(decimal money)
  96. {
  97. this.money = money;
  98. }
  99.  
  100. public bool CanVend(string slot)
  101. {
  102. return money >= price;
  103. }
  104. IList<string> stocked;
  105. decimal money;
  106. private decimal price;
  107. }
Add Comment
Please, Sign In to add comment