-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass_Cat_test.erl
More file actions
291 lines (195 loc) · 7.96 KB
/
Copy pathclass_Cat_test.erl
File metadata and controls
291 lines (195 loc) · 7.96 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
% Copyright (C) 2007-2026 Olivier Boudeville
%
% This file is part of the Ceylan-WOOPER examples.
%
% It has been placed in the public domain.
%
% Author: Olivier Boudeville [olivier (dot) boudeville (at) esperide (dot) com]
% Creation date: 2007.
-module(class_Cat_test).
-moduledoc """
Unit tests for the **Cat class** implementation.
See the class_Cat.erl tested module.
""".
% For run/0 export and al:
-include_lib("myriad/include/test_facilities.hrl").
-export([ run/1 ]).
-spec run() -> no_return().
run() ->
run( class_Cat:is_wooper_debug() ).
-spec run( boolean() ) -> no_return().
run( IsDebug ) ->
test_facilities:start( ?MODULE ),
% Allows to support both OTP conventions and ad hoc, automatic ones:
wooper_utils:start_for_test(),
test_facilities:display( "Debug mode: ~ts.",
[ class_Cat:is_wooper_debug() ] ),
% General tests.
MyC = class_Cat:new_link( 3, female, sand, white ),
MyC ! { getClassname, [], self() },
receive
{ wooper_result, class_Cat } ->
test_facilities:display(
"After constructor, getClassname/1 returned 'class_Cat' "
"as expected." );
{ wooper_result, UnexpectedClass } ->
test_facilities:fail( "wrong class: ~p", [ UnexpectedClass ] )
end,
MyC ! { getSuperclasses, [], self() },
receive
{ wooper_result, Classes=[ class_Mammal, class_ViviparousBeing,
class_Serialisable ] } ->
test_facilities:display( "After constructor, getSuperclasses/1 "
"returned ~p as expected.", [ Classes ] );
{ wooper_result, UnexpectedSuperclasses } ->
test_facilities:fail( "wrong superclasses: ~p",
[ UnexpectedSuperclasses ] )
end,
% Tests related to Mammals and Creatures.
MyC ! { getAge, [], self() },
receive
{ wooper_result, 3 } ->
test_facilities:display(
"After constructor, getAge/1 returned 3 as expected." );
{ wooper_result, UnexpectedAge } ->
test_facilities:fail( "wrong age: ~p", [ UnexpectedAge ] )
end,
MyC ! { getGender, [], self() },
receive
{ wooper_result, female } ->
test_facilities:display(
"After constructor, getGender/1 returned female as expected." );
{ wooper_result, UnexpectedGender } ->
test_facilities:fail( "wrong gender: ~p", [ UnexpectedGender ] )
end,
% If wanting to test request/oneway mismatches: MyC ! { setAge, 5, self() },
MyC ! { setAge, 5 },
MyC ! { getAge, [], self() },
receive
{ wooper_result, 5 }->
test_facilities:display(
"After setAge, getAge/1 returned 5 as expected." );
{ wooper_result, UnexpectedNewAge } ->
test_facilities:fail( "wrong age: ~p", [ UnexpectedNewAge ] )
end,
MyC ! declareBirthday,
MyC ! { getAge, [], self() },
receive
{ wooper_result, 6 }->
test_facilities:display(
"After declareBirthday/1, getAge/1 returned 6 as expected." );
{ wooper_result, UnexpectedLastAge } ->
test_facilities:fail( "wrong age: ~p", [ UnexpectedLastAge ] )
end,
MyC ! declareBirthday,
MyC ! { isHotBlooded, [], self() },
receive
{ wooper_result, true }->
test_facilities:display(
"isHotBlooded/1 returned true as expected." );
{ wooper_result, UnexpectedBlood } ->
test_facilities:fail( "wrong blood type: ~p", [ UnexpectedBlood ] )
end,
MyC ! { getFurColor, [], self() },
receive
{ wooper_result, sand }->
test_facilities:display(
"getFurColor/1 returned sand as expected." );
{ wooper_result, UnexpectedFurColor } ->
test_facilities:fail( "wrong fur color: ~p",
[ UnexpectedFurColor ] )
end,
% Tests related to ViviparousBeings.
MyC ! { getMeanChildrenCount, [], self() },
receive
{ wooper_result, 4 } ->
test_facilities:display(
"After constructor, getMeanChildrenCount/1 returned 4 "
"as expected." );
{ wooper_result, UnexpectedMeanCount } ->
test_facilities:fail( "wrong mean children count: ~p",
[ UnexpectedMeanCount ] )
end,
MyC ! { getBirthGivenCount, [], self() },
receive
{ wooper_result, 0 } ->
test_facilities:display(
"After constructor, getBirthGivenCount/1 returned 0 "
"as expected." );
{ wooper_result, UnexpectedFirstCount } ->
test_facilities:fail( "wrong first children count: ~p",
[ UnexpectedFirstCount ] )
end,
MyC ! { giveBirth, 5 },
MyC ! { getBirthGivenCount, [], self() },
receive
{ wooper_result, 5 }->
test_facilities:display(
"After giveBirth/2, getBirthGivenCount/1 returned 5 "
"as expected." );
{ wooper_result, UnexpectedSecondCount } ->
test_facilities:fail( "wrong second children count: ~p",
[ UnexpectedSecondCount ] )
end,
% Tests related to Cats.
MyC ! { getTeatCount, [], self() },
receive
{ wooper_result, 6 }->
test_facilities:display( "getTeatCount/1 returned 6 as expected." );
{ wooper_result, UnexpectedTeatCount } ->
test_facilities:fail( "wrong teat count: ~p",
[ UnexpectedTeatCount ] )
end,
MyC ! { canEat, soup, self() },
receive
{ wooper_result, true }->
test_facilities:display( "This cat can eat soup, as expected." );
{ wooper_result, UnexpectedFoodPreference } ->
test_facilities:fail( "wrong food preference: ~p",
[ UnexpectedFoodPreference ] )
end,
MyC ! { canEat, tangerine, self() },
receive
{ wooper_result, false }->
test_facilities:display(
"This cat cannot eat tangerine, as expected." );
{ wooper_result, UnexpectedOtherFoodPreference } ->
test_facilities:fail( "wrong food preference: ~p",
[ UnexpectedOtherFoodPreference ] );
UnexpectedReturn ->
test_facilities:fail( "unexpected method return: ~p",
[ UnexpectedReturn ] )
end,
MyC ! { getWhiskerColor, [], self() },
receive
{ wooper_result, white }->
test_facilities:display(
"This cat has white whiskers, as expected." );
{ wooper_result, UnexpectedWhiskerColor } ->
test_facilities:fail( "wrong whisker color: ~p",
[ UnexpectedWhiskerColor ] );
AUnexpectedReturn ->
test_facilities:fail( "unexpected method return: ~p",
[ AUnexpectedReturn ] )
end,
18 = class_Cat:get_mean_life_expectancy(),
IsDebug andalso
begin
MyC ! { wooper_get_instance_description,[], self() },
receive
{ wooper_result, InspectString } ->
test_facilities:display( "Instance description: ~ts",
[ InspectString ] )
end
end,
% Some waiting could be needed in cases where the interpreter is to stop
% immediately afterwards, so that the actions performed in the destructor
% can be performed:
%
MyC ! delete,
MyOtherC = class_Cat:new_link( 3, male, black, white ),
% No race condition with the end of this test process:
wooper:delete_synchronously_instance( MyOtherC ),
test_facilities:display( "This cat could be created and "
"be synchronously deleted, as expected." ),
test_facilities:stop().