Skip to content

Commit 26f6786

Browse files
committed
✨ (roman-numerals): add second test
1 parent 4ca9d77 commit 26f6786

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

property-base-test/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# property-base-test
2+
3+
使用 [Roman Numberals Kata](https://codingdojo.org/kata/RomanNumerals/) 這個題目來展示如何在 TDD 開發流使用 property base test 來讓測試更穩固。
4+
5+
## 講述順序
6+
7+
1. 先用原本 TDD 的方式來逐步拆解任務
8+
2. 介紹 property base test
9+
3. 使用 property base test 來讓測試更加穩固
10+
4. 分析測試的邊界條件
11+
12+
## [Roman Numberals Kata](https://codingdojo.org/kata/RomanNumerals/)
13+
14+
羅馬數字表達式是一種表達數值的方式
15+
16+
舉例來說
17+
18+
| 數值 | 羅馬數字表達式 |
19+
| ---- | -------------- |
20+
| 1 | I |
21+
| 2 | II |
22+
| 3 | III |
23+
| 4 | IV |
24+
| 5 | V |
25+
| 6 | VI |
26+
| 7 | VII |
27+
| 8 | VIII |
28+
| 9 | IX |
29+
| 10 | X |
30+
| 50 | L |
31+
| 100 | C |
32+
| 500 | D |
33+
| 1000 | M |
34+
35+
MCMLXXXIV is 1984
36+
37+
這邊我們需要實做兩個功能
38+
39+
1. ConvertToRoman : 從一般數值換成羅馬數字表達式
40+
2. ConvertToArabic : 從羅馬數字表達式一般數值換成
41+
42+

property-base-test/roman_number.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package roman_numerals
2+
3+
func ConvertToRoman(arabic int) string {
4+
if arabic == 2 {
5+
return "II"
6+
}
7+
return "I"
8+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package roman_numerals
2+
3+
import "testing"
4+
5+
func TestRomanNumerals(t *testing.T) {
6+
cases := []struct {
7+
Description string
8+
Arabic int
9+
Want string
10+
}{
11+
{"1 gets converted to I", 1, "I"},
12+
{"2 gets converted to II", 2, "II"},
13+
}
14+
15+
for _, test := range cases {
16+
t.Run(test.Description, func(t *testing.T) {
17+
got := ConvertToRoman(test.Arabic)
18+
if got != test.Want {
19+
t.Errorf("got %q, want %q", got, test.Want)
20+
}
21+
})
22+
}
23+
}

0 commit comments

Comments
 (0)