-
Notifications
You must be signed in to change notification settings - Fork 1
/
caseWhen.rb
121 lines (106 loc) · 2.94 KB
/
caseWhen.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/ruby
# coding: utf-8
require 'test/unit'
class Animal
attr_reader :count
def initialize
@count = 0
end
# 本来の===の使い方とは違うが...
def ===(other)
# 呼び出された回数を後で返す
@count += 1
# 派生クラスは、派生クラスであるかどうかを返す
instance_of?(other.class)
end
def introduceSelf
"けものだよ"
end
end
class Cat < Animal
def introduceSelf
"ネコ科だよ"
end
end
class Serval < Cat
def introduceSelf
"私はサーバルキャットのサーバル!"
end
end
class WhiteFacedScopsOwl
def initialize
@base = Animal.new
@sub = Cat.new
@subsub = Serval.new
end
def matchUpto(obj)
# objと一致する型を見つけて、結果を文字列で返す
case obj
when @base then
"けものです"
when @sub then
"ネコ科です"
when @subsub then
"サーバルです"
else
"教えてやるので、おかわりをよこすのです"
end
end
def matchDownto(obj)
case obj
when @subsub then
"Leptailurus serval"
when @sub then
"Felidae"
when @base then
"Animalia"
else
"Give me a second helping, please"
end
end
def getBaseCount
@base.count
end
def getSubCount
@sub.count
end
def getSubSubCount
@subsub.count
end
end
class TestCaseWhen < Test::Unit::TestCase
data(
'base' => [:Animal, "けものだよ"],
'sub' => [:Cat, "ネコ科だよ"],
'subsub' => [:Serval, "私はサーバルキャットのサーバル!"])
def test_create(data)
classNameSym, expected = data
assert_equal(expected, Object.const_get(classNameSym).new.introduceSelf)
end
data(
'base' => [:Animal, 1, 0, 0, "けものです"],
'sub' => [:Cat, 1, 1, 0, "ネコ科です"],
'subsub' => [:Serval, 1, 1, 1, "サーバルです"],
'other' => [:String, 1, 1, 1, "教えてやるので、おかわりをよこすのです"])
def test_matchUpto(data)
classNameSym, baseCount, subCount, subSubCount, expected = data
konoha = WhiteFacedScopsOwl.new
assert_equal(expected, konoha.matchUpto(Object.const_get(classNameSym).new))
assert_equal(baseCount, konoha.getBaseCount)
assert_equal(subCount, konoha.getSubCount)
assert_equal(subSubCount, konoha.getSubSubCount)
end
data(
'base' => [:Animal, 1, 1, 1, "Animalia"],
'sub' => [:Cat, 0, 1, 1, "Felidae"],
'subsub' => [:Serval, 0, 0, 1, "Leptailurus serval"],
'other' => [:String, 1, 1, 1, "Give me a second helping, please"])
def test_matchDownto(data)
classNameSym, baseCount, subCount, subSubCount, expected = data
konoha = WhiteFacedScopsOwl.new
assert_equal(expected, konoha.matchDownto(Object.const_get(classNameSym).new))
assert_equal(baseCount, konoha.getBaseCount)
assert_equal(subCount, konoha.getSubCount)
assert_equal(subSubCount, konoha.getSubSubCount)
end
end