/
tests.erl
227 lines (193 loc) · 7.05 KB
/
tests.erl
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
-module(tests).
-include_lib("epitest/include/epitest.hrl").
-include_lib("eunit/include/eunit.hrl").
-include("records.hrl").
test("Empty recmod should have record_fields/0 returning record names") ->
[{f,
fun () ->
?assertEqual([data],emptyrecmod:record_fields())
end}
];
test("Empty recmod should have readers") ->
[{f,
fun () ->
?assertEqual("test", (#emptyrecmod{data="test"}):data())
end}
];
test("Empty recmod should have default object construction") ->
[{f,
fun () ->
?assertEqual(#emptyrecmod{}, (emptyrecmod:new()))
end}
];
test("Base recmod should have record_fields/0 returning record names") ->
[{f,
fun () ->
?assertEqual([field1,field2,field3],baserecmod:record_fields())
end}
];
test("Base recmod should have readers") ->
[{f,
fun () ->
Mod = #baserecmod{field1="test1",field2="test2",field3="test3"},
?assertEqual("test1", Mod:field1()),
?assertEqual("test2", Mod:field2()),
?assertEqual("test3", Mod:field3())
end}
];
test("Base recmod should have default object construction") ->
[{f,
fun () ->
?assertEqual(#baserecmod{}, (baserecmod:new()))
end}
];
test("Argumentless function should have THIS assigned") ->
[{f,
fun () ->
Mod = #baserecmod{field1="test1",field2="test2",field3="test3"},
?assertEqual(Mod, proplists:get_value(this, Mod:argless()))
end}];
test("Argumentless function should have all parameters assigned") ->
[{f,
fun () ->
Mod = #baserecmod{field1="test1",field2="test2",field3="test3"},
?assertEqual("test1", proplists:get_value(field1, Mod:argless())),
?assertEqual("test2", proplists:get_value(field2, Mod:argless())),
?assertEqual("test3", proplists:get_value(field3, Mod:argless()))
end}];
test("In base recmod, argumentless function should have SELF == THIS") ->
[{f,
fun () ->
Mod = #baserecmod{field1="test1",field2="test2",field3="test3"},
?assertEqual(proplists:get_value(this, Mod:somefun(somearg)), proplists:get_value(self, Mod:somefun(somearg)))
end}];
test("Function should have THIS assigned") ->
[{f,
fun () ->
Mod = #baserecmod{field1="test1",field2="test2",field3="test3"},
?assertEqual(Mod, proplists:get_value(this, Mod:somefun(somearg)))
end}];
test("Function should have all parameters assigned") ->
[{f,
fun () ->
Mod = #baserecmod{field1="test1",field2="test2",field3="test3"},
?assertEqual("test1", proplists:get_value(field1, Mod:somefun(somearg))),
?assertEqual("test2", proplists:get_value(field2, Mod:somefun(somearg))),
?assertEqual("test3", proplists:get_value(field3, Mod:somefun(somearg)))
end}];
test("Function should have all arguments assigned") ->
[{f,
fun () ->
Mod = #baserecmod{field1="test1",field2="test2",field3="test3"},
?assertEqual(somearg, proplists:get_value(arg, Mod:somefun(somearg)))
end}];
test("In base recmod, function should have SELF == THIS") ->
[{f,
fun () ->
Mod = #baserecmod{field1="test1",field2="test2",field3="test3"},
?assertEqual(proplists:get_value(this, Mod:somefun(somearg)), proplists:get_value(self, Mod:somefun(somearg)))
end}];
test("Local call to a function should succeed") ->
[{f,
fun () ->
Mod = #baserecmod{field1="test1",field2="test2",field3="test3"},
?assertEqual(otherfun, proplists:get_value(arg, Mod:someotherfun()))
end}];
test("Local call to a function should pass THIS") ->
[{f,
fun () ->
Mod = #baserecmod{field1="test1",field2="test2",field3="test3"},
?assertEqual(Mod, proplists:get_value(this, Mod:someotherfun()))
end}];
test("Local call to a function should pass SELF") ->
[{f,
fun () ->
Mod = #baserecmod{field1="test1",field2="test2",field3="test3"},
?assertEqual(Mod, proplists:get_value(self, Mod:someotherfun()))
end}];
test("Extending recmod should have record_fields/0 returning record names") ->
[{f,
fun () ->
?assertEqual([field1,field2,otherfield],extrecmod:record_fields())
end}
];
test("Extending recmod should have readers") ->
[{f,
fun () ->
Mod = #extrecmod{field1="test1",field2="test2",otherfield="test3"},
?assertEqual("test1", Mod:field1()),
?assertEqual("test2", Mod:field2()),
?assertEqual("test3", Mod:otherfield())
end}
];
test("Extending recmod should have default object construction") ->
[{f,
fun () ->
?assertEqual(#extrecmod{}, (extrecmod:new()))
end}
];
test("Calling undefined function in extending recmod should call base module's implementation with record coerced to base record") ->
[{f,
fun () ->
BaseMod = #baserecmod{field1="test1",field2="test2"},
Mod = #extrecmod{field1="test1",field2="test2",otherfield="test3"},
?assertEqual(BaseMod, proplists:get_value(this, Mod:argless()))
end}
];
test("Calling undefined function in extending recmod should call pass its record as SELF to the base") ->
[{f,
fun () ->
Mod = #extrecmod{field1="test1",field2="test2",otherfield="test3"},
?assertEqual(Mod, proplists:get_value(self, Mod:argless()))
end}
];
test("Calling non-matching function in extending recmod should call base module's implementation with record coerced to base record") ->
[{f,
fun () ->
BaseMod = #baserecmod{field1="test1",field2="test2"},
Mod = #extrecmod{field1="test1",field2="test2",otherfield="test3"},
?assertEqual(BaseMod, proplists:get_value(this, Mod:somefun(something)))
end}
];
test("Calling non-matching function in extending recmod should call pass its record as SELF to the base") ->
[{f,
fun () ->
Mod = #extrecmod{field1="test1",field2="test2",otherfield="test3"},
?assertEqual(Mod, proplists:get_value(self, Mod:somefun(something)))
end}
];
test("Should properly handle calls to imported functions") ->
[{f,
fun () ->
(#baserecmod{}):defaultimports()
end}
];
test("Should properly handle calls within module") ->
[{f,
fun () ->
#baserecmod{} = (#baserecmod{}):localcall(),
{#baserecmod{},yes} = (#baserecmod{}):localcall(yes)
end}];
test("Should export static functions properly") ->
[{f,
fun () ->
static = baserecmod:st()
end}];
test("Should respect field guards") ->
[{f,
fun () ->
more = (#baserecmod{field1=2}):recguarded(),
less = (#baserecmod{field1=0}):recguarded()
end}];
test("Should respect field guards in the extension module") ->
[{f,
fun () ->
emore = (#extrecmod{field1=2}):recguarded(),
eless = (#extrecmod{field1=0}):recguarded()
end}];
test("Should pass multiple arguments to a base module in a proper order (BUG)") ->
[{f,
fun () ->
{1,2} = (#extrecmod{}):multiarg(1,2)
end}];
?EOT.