Created
April 30, 2012 07:40
-
-
Save OdaShinsuke/2556306 to your computer and use it in GitHub Desktop.
TDD Pre Camp 大阪
This file contains hidden or 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
using System; | |
using System.Linq; | |
namespace TDDPre { | |
public class 自動販売機 { | |
private static int[] 正規のお金 = new int[] { 10, 50, 100, 500, 1000 }; | |
public int 投入金額 { get; private set; } | |
/// <summary> | |
/// Command | |
/// </summary> | |
/// <param name="お金"></param> | |
public void お金投入(int お金) { | |
if (!正規のお金.Contains(お金)) { | |
throw new ArgumentOutOfRangeException(); | |
} | |
if (投入金額 + お金 > 10000) { | |
throw new InvalidOperationException(); | |
} | |
投入金額 += お金; | |
} | |
public int 在庫数 { get; private set; } | |
public 自動販売機() { | |
在庫数 = 5; | |
} | |
public int 売上合計金額 { get; private set; } | |
private const int コーラの価格 = 120; | |
/// <summary> | |
/// Query | |
/// </summary> | |
/// <returns></returns> | |
public string 購入可能商品表示() { | |
return 在庫数 > 0 && 投入金額 >= コーラの価格 ? | |
"コーラ (120円)" : null; | |
} | |
/// <summary> | |
/// Command | |
/// </summary> | |
public void 購入() { | |
if (string.IsNullOrEmpty(購入可能商品表示())) { | |
throw new InvalidOperationException(); | |
} | |
投入金額 = 0; | |
在庫数 -= 1; | |
売上合計金額 += コーラの価格; | |
} | |
} | |
} |
This file contains hidden or 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
using System; | |
using System.Linq; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace TDDPre.Tests { | |
[TestClass] | |
public class 自動販売機Test { | |
public TestContext TestContext { get; set; } | |
[TestMethod] | |
public void 投入金額を取得する() { | |
var instance = new 自動販売機(); | |
instance.投入金額.Is(0); | |
} | |
[TestMethod] | |
public void 百円投入() { | |
var instance = new 自動販売機(); | |
instance.お金投入(100); | |
instance.投入金額.Is(100); | |
} | |
[TestMethod] | |
public void 百円を2回投入() { | |
var instance = new 自動販売機(); | |
instance.お金投入(100); | |
instance.お金投入(100); | |
instance.投入金額.Is(200); | |
} | |
[TestMethod] | |
[TestCase(10)] | |
[TestCase(50)] | |
[TestCase(100)] | |
[TestCase(500)] | |
[TestCase(1000)] | |
public void 正式なお金を投入() { | |
TestContext.Run((int お金) => { | |
var instance = new 自動販売機(); | |
instance.お金投入(お金); | |
instance.投入金額.Is(お金); | |
}); | |
} | |
[TestMethod] | |
[TestCase(1, 9, "1~9")] | |
[TestCase(11, 39, "11~49")] | |
[TestCase(51, 49, "51~99")] | |
[TestCase(101, 399, "101~499")] | |
[TestCase(501, 499, "501~999")] | |
[TestCase(1001, 9999, "1001~9999")] | |
public void 偽お金を投入() { | |
TestContext.Run((int min, int count, string msg) => { | |
var instance = new 自動販売機(); | |
foreach (var お金 in Enumerable.Range(min, count)) { | |
AssertEx.Throws<ArgumentOutOfRangeException>(() => instance.お金投入(お金), msg + ":" + お金); | |
} | |
}); | |
} | |
[TestMethod] | |
public void 一万十円投入() { | |
var instance = new 自動販売機(); | |
instance.お金投入(1000); | |
instance.お金投入(1000); | |
instance.お金投入(1000); | |
instance.お金投入(1000); | |
instance.お金投入(1000); | |
instance.お金投入(1000); | |
instance.お金投入(1000); | |
instance.お金投入(1000); | |
instance.お金投入(1000); | |
instance.お金投入(1000); | |
AssertEx.Throws<InvalidOperationException>(() => instance.お金投入(10)); | |
} | |
[TestMethod] | |
public void 在庫数を取得する() { | |
var instance = new 自動販売機(); | |
instance.在庫数.Is(5); | |
} | |
[TestMethod] | |
public void 売上金額合計を取得する() { | |
var instance = new 自動販売機(); | |
instance.売上合計金額.Is(0); | |
} | |
[TestMethod] | |
public void お金をいれずに購入可能商品を表示() { | |
var instance = new 自動販売機(); | |
instance.購入可能商品表示().IsNull(); | |
} | |
[TestMethod] | |
public void 百円入れて購入可能商品を表示() { | |
var instance = new 自動販売機(); | |
instance.お金投入(100); | |
instance.購入可能商品表示().IsNull(); | |
} | |
[TestMethod] | |
public void 百二十円入れて購入可能商品を表示() { | |
var instance = new 自動販売機(); | |
instance.お金投入(100); | |
instance.お金投入(10); | |
instance.お金投入(10); | |
instance.購入可能商品表示().Is("コーラ (120円)"); | |
} | |
[TestMethod] | |
public void 千円入れて購入可能商品を表示() { | |
var instance = new 自動販売機(); | |
instance.お金投入(1000); | |
instance.購入可能商品表示().Is("コーラ (120円)"); | |
} | |
[TestMethod] | |
public void 購入出来る商品がないのに購入() { | |
var instance = new 自動販売機(); | |
var 商品 = instance.購入可能商品表示(); | |
商品.IsNull(); | |
AssertEx.Throws<InvalidOperationException>(() => instance.購入()); | |
} | |
[TestMethod] | |
public void 購入出来る商品がある状態で購入すると投入金額が0になる() { | |
var instance = new 自動販売機(); | |
instance.お金投入(1000); | |
var 商品 = instance.購入可能商品表示(); | |
商品.IsNotNull(); | |
instance.購入(); | |
instance.投入金額.Is(0); | |
} | |
[TestMethod] | |
public void 購入出来る商品がある状態で購入すると在庫が減る() { | |
var instance = new 自動販売機(); | |
instance.お金投入(1000); | |
var 商品 = instance.購入可能商品表示(); | |
商品.IsNotNull(); | |
instance.購入(); | |
instance.在庫数.Is(4); | |
} | |
[TestMethod] | |
public void 購入出来る商品がある状態で購入すると売上金額が加算される() { | |
var instance = new 自動販売機(); | |
instance.お金投入(1000); | |
var 商品 = instance.購入可能商品表示(); | |
商品.IsNotNull(); | |
instance.購入(); | |
instance.売上合計金額.Is(120); | |
} | |
[TestMethod] | |
public void 在庫が無くなった状態で千円入れて購入可能商品を表示() { | |
var instance = new 自動販売機(); | |
instance.テスト用在庫を無くす処理(); | |
instance.お金投入(1000); | |
instance.購入可能商品表示().IsNull(); | |
} | |
[TestMethod] | |
public void 在庫が無くなった状態で購入する() { | |
var instance = new 自動販売機(); | |
instance.テスト用在庫を無くす処理(); | |
instance.お金投入(1000); | |
AssertEx.Throws<InvalidOperationException>(() => instance.購入()); | |
} | |
} | |
static class 自動販売機Ex { | |
public static void テスト用在庫を無くす処理(this 自動販売機 source) { | |
Action action = (() => { | |
source.お金投入(1000); | |
source.購入(); | |
}); | |
action(); | |
action(); | |
action(); | |
action(); | |
action(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment