Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using NSpec;
- class describe_VendingMachine : nspec
- {
- void given_an_empty_VendingMachine()
- {
- before = () => machine = new VendingMachine();
- should_not_be_in_stock("A1");
- context["doritos are stocked in A1, with a price of $1"] = () =>
- {
- stock("doritos", "A1", 1m);
- should_be_in_stock("A1");
- context["mountain dew is stocked in A2, with a price of $1"] = () =>
- {
- stock("mountain dew", "A2", 1m);
- should_be_in_stock("A2");
- should_be_in_stock("A1");
- };
- context["doritos are vended"] = () =>
- {
- before = () => machine.VendItem("A1");
- should_not_be_in_stock("A1");
- };
- specify = () => machine.CanVend("A1").should_be_false();
- new[] { 1m, 5m }.Do(amount =>
- {
- context["{0} dollar is put in the machine".With(amount)] = () =>
- {
- insert_money(amount);
- specify = () => machine.CanVend("A1").should_be_true();
- };
- });
- };
- }
- void insert_money(decimal amount)
- {
- before = () => machine.Insert(amount);
- }
- void should_be_in_stock(string slot)
- {
- specify = () => machine.InStock(slot).should_be_true();
- }
- void should_not_be_in_stock(string slot)
- {
- specify = () => machine.InStock(slot).should_be_false();
- }
- void stock(string name, string slot, decimal price)
- {
- before = () => machine.StockItem(name, slot, price);
- }
- VendingMachine machine;
- }
- public class VendingMachine
- {
- public void StockItem(string name, string slot, decimal price)
- {
- stocked.Add(slot);
- this.price = price;
- }
- public bool InStock(string slot)
- {
- return stocked.Contains(slot);
- }
- public VendingMachine()
- {
- stocked = new List<string>();
- }
- public void VendItem(string slot)
- {
- stocked.Remove(slot);
- }
- public void Insert(decimal money)
- {
- this.money = money;
- }
- public bool CanVend(string slot)
- {
- return money >= price;
- }
- IList<string> stocked;
- decimal money;
- private decimal price;
- }
Add Comment
Please, Sign In to add comment