1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Tests for Prim.php |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Itafroma\Zork\Tests; |
9
|
|
|
|
10
|
|
|
use Itafroma\Zork\ServiceContainer; |
11
|
|
|
use Itafroma\Zork\Exception\ConstantAlreadyDefinedException; |
12
|
|
|
use \PHPUnit_Framework_TestCase; |
13
|
|
|
use function Itafroma\Zork\flagword; |
14
|
|
|
use function Itafroma\Zork\make_slot; |
15
|
|
|
use function Itafroma\Zork\msetg; |
16
|
|
|
use function Itafroma\Zork\psetg; |
17
|
|
|
use function Itafroma\Zork\newstruc; |
18
|
|
|
|
19
|
|
|
class PrimTest extends ZorkTest |
20
|
|
|
{ |
21
|
|
|
const FLAGSIZEMAX = 36; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Test Itafroma\Zork\msetg(). |
25
|
|
|
* |
26
|
|
|
* @covers ::Itafroma\Zork\msetg |
27
|
|
|
* @dataProvider propertyProvider |
28
|
|
|
*/ |
29
|
|
|
public function testMsetg($property_key, $property_value) |
30
|
|
|
{ |
31
|
|
|
$atoms = $this->container->get('atoms'); |
32
|
|
|
|
33
|
|
|
$this->assertEquals($property_value, msetg($property_key, $property_value)); |
34
|
|
|
$this->assertEquals($property_value, $atoms->get($property_key)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Test Itafroma\Zork\msetg() with extant key using the same value. |
39
|
|
|
* |
40
|
|
|
* @covers ::Itafroma\Zork\msetg |
41
|
|
|
* @dataProvider propertyProvider |
42
|
|
|
*/ |
43
|
|
|
public function testMsetgExtantKeySameValue($property_key, $property_value) |
44
|
|
|
{ |
45
|
|
|
msetg($property_key, $property_value); |
46
|
|
|
|
47
|
|
|
try { |
48
|
|
|
msetg($property_key, $property_value); |
49
|
|
|
} |
50
|
|
|
catch (ConstantAlreadyDefinedException $e) { |
51
|
|
|
$this->fail('Itafroma\Zork\Exception\ConstantAlreadyDefinedException should not be thrown when global value is reassigned the same value.'); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Test Itafroma\Zork\msetg() with extant key using a different value. |
57
|
|
|
* |
58
|
|
|
* @covers ::Itafroma\Zork\msetg |
59
|
|
|
* @dataProvider propertyProvider |
60
|
|
|
* @expectedException Itafroma\Zork\Exception\ConstantAlreadyDefinedException |
61
|
|
|
*/ |
62
|
|
|
public function testMsetgExtantKeyDifferentValue($property_key, $property_value1, $property_value2) |
63
|
|
|
{ |
64
|
|
|
msetg($property_key, $property_value1); |
65
|
|
|
msetg($property_key, $property_value2); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Test Itafroma\Zork\psetg(). |
70
|
|
|
* |
71
|
|
|
* @covers ::Itafroma\Zork\psetg |
72
|
|
|
* @dataProvider propertyProvider |
73
|
|
|
*/ |
74
|
|
|
public function testPsetg($property_key, $property_value) |
75
|
|
|
{ |
76
|
|
|
$atoms = $this->container->get('atoms'); |
77
|
|
|
|
78
|
|
|
$this->assertEquals($property_value, psetg($property_key, $property_value)); |
79
|
|
|
$this->assertContains($property_key, $atoms->get('PURE-LIST')); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Test Itafroma\Zork\psetg() with extant key. |
84
|
|
|
* |
85
|
|
|
* @covers ::Itafroma\Zork\psetg |
86
|
|
|
* @dataProvider propertyProvider |
87
|
|
|
*/ |
88
|
|
|
public function testPsetgExtantKey($property_key, $property_value1, $property_value2) |
89
|
|
|
{ |
90
|
|
|
$atoms = $this->container->get('atoms'); |
91
|
|
|
|
92
|
|
|
psetg($property_key, $property_value1); |
93
|
|
|
|
94
|
|
|
$this->assertEquals($property_value2, psetg($property_key, $property_value2)); |
95
|
|
|
$this->assertContains($property_key, $atoms->get('PURE-LIST')); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Test Itafroma\Zork\psetg() with extant key and PURE-CAREFUL set. |
100
|
|
|
* |
101
|
|
|
* @covers ::Itafroma\Zork\psetg |
102
|
|
|
* @dataProvider propertyProvider |
103
|
|
|
* @expectedException Itafroma\Zork\Exception\PsetgDuplicateException |
104
|
|
|
*/ |
105
|
|
|
public function testPsetgExtantKeyWithPureCareful($property_key, $property_value1, $property_value2) |
106
|
|
|
{ |
107
|
|
|
$atoms = $this->container->get('atoms'); |
108
|
|
|
|
109
|
|
|
$atoms->set('PURE-CAREFUL', true); |
110
|
|
|
|
111
|
|
|
psetg($property_key, $property_value1); |
112
|
|
|
psetg($property_key, $property_value2); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Test Itafroma\Zork\flagword(). |
117
|
|
|
* |
118
|
|
|
* @covers ::Itafroma\Zork\flagword |
119
|
|
|
* @dataProvider flagProvider |
120
|
|
|
*/ |
121
|
|
|
public function testFlagword($flags) |
122
|
|
|
{ |
123
|
|
|
$atoms = $this->container->get('atoms'); |
124
|
|
|
|
125
|
|
|
$this->assertEquals(self::FLAGSIZEMAX, flagword(...$flags)); |
126
|
|
|
|
127
|
|
|
$tot = 1; |
128
|
|
|
foreach ($flags as $flag) { |
129
|
|
|
$this->assertEquals($tot, $atoms->get($flag)); |
130
|
|
|
$tot *= 2; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Test Itafroam\Zork\flagword() when GROUP-GLUE flag is set. |
136
|
|
|
* |
137
|
|
|
* @covers ::Itafroma\Zork\flagword |
138
|
|
|
* @dataProvider flagProvider |
139
|
|
|
*/ |
140
|
|
|
public function testFlagwordGroupGlueEnabled($flags) |
141
|
|
|
{ |
142
|
|
|
$atoms = $this->container->get('atoms'); |
143
|
|
|
$oblists = $this->container->get('oblists'); |
144
|
|
|
|
145
|
|
|
$initial_oblist = $oblists->get('INITIAL'); |
146
|
|
|
$initial_oblist->set('GROUP-GLUE', true); |
147
|
|
|
|
148
|
|
|
flagword(...$flags); |
149
|
|
|
|
150
|
|
|
foreach ($flags as $flag) { |
151
|
|
|
$this->assertFalse($atoms->exists($flag)); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Test Itafroma\Zork\flagword() with too many flags. |
157
|
|
|
* |
158
|
|
|
* @covers ::Itafroma\Zork\flagword |
159
|
|
|
* @dataProvider flagOverflowProvider |
160
|
|
|
* @expectedException Itafroma\Zork\Exception\FlagwordException |
161
|
|
|
*/ |
162
|
|
|
public function testFlagwordOverflow($flags) |
163
|
|
|
{ |
164
|
|
|
flagword(...$flags); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Test Itafroma\Zork\newstruc(). |
169
|
|
|
* |
170
|
|
|
* @covers ::Itafroma\Zork\newstruc |
171
|
|
|
* @expectedException \BadFunctionCallException |
172
|
|
|
*/ |
173
|
|
|
public function testNewstruc() |
174
|
|
|
{ |
175
|
|
|
newstruc('PrimTest-struc', 'PrimTest-type', ...['one', 'two', 'three']); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Test Itafroma\Zork\make_slot() slot creation. |
180
|
|
|
* |
181
|
|
|
* @covers ::Itafroma\Zork\make_slot |
182
|
|
|
* @dataProvider propertyProvider |
183
|
|
|
*/ |
184
|
|
|
public function testMakeSlotCreate($property_key, $property_value) |
185
|
|
|
{ |
186
|
|
|
$atoms = $this->container->get('atoms'); |
187
|
|
|
|
188
|
|
|
$slots = make_slot($property_key, $property_value); |
189
|
|
|
|
190
|
|
|
$this->assertInternalType('callable', $slots[$property_key]); |
191
|
|
|
$this->assertEquals($slots[$property_key], $atoms->get('SLOTS')[$property_key]); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Test Itafroma\Zork\make_slot() creation on an already-bound name. |
196
|
|
|
* |
197
|
|
|
* @covers ::Itafroma\Zork\make_slot |
198
|
|
|
* @dataProvider propertyProvider |
199
|
|
|
* @expectedException Itafroma\Zork\Exception\SlotNameAlreadyUsedException |
200
|
|
|
*/ |
201
|
|
|
public function testMakeSlotCreateDuplicate($property_key, $property_value) |
202
|
|
|
{ |
203
|
|
|
msetg($property_key, $property_value); |
204
|
|
|
make_slot($property_key, $property_value); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Test Itafroma\Zork\make_slot() slot write. |
209
|
|
|
* |
210
|
|
|
* @covers ::Itafroma\Zork\make_slot |
211
|
|
|
* @dataProvider objectPropertyProvider |
212
|
|
|
*/ |
213
|
|
|
public function testMakeSlotWrite($struc, $property_key, $property_value) |
214
|
|
|
{ |
215
|
|
|
$slots = make_slot($property_key, $property_value); |
216
|
|
|
$return = $slots[$property_key]($struc, $property_value); |
217
|
|
|
|
218
|
|
|
$this->assertEquals($struc, $return); |
219
|
|
|
$this->assertEquals($property_value, $return->oprops[$property_key]); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Test Itafroma\Zork\make_slot() slot read. |
224
|
|
|
* |
225
|
|
|
* @covers ::Itafroma\Zork\make_slot |
226
|
|
|
* @dataProvider objectPropertyProvider |
227
|
|
|
*/ |
228
|
|
|
public function testMakeSlotRead($struc, $property_key, $property_value) |
229
|
|
|
{ |
230
|
|
|
$slots = make_slot($property_key, $property_value); |
231
|
|
|
|
232
|
|
|
$slots[$property_key]($struc, $property_value); |
233
|
|
|
|
234
|
|
|
$this->assertEquals($property_value, $slots[$property_key]($struc)); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Generate a list of flags for a bit field. |
239
|
|
|
* |
240
|
|
|
* @param int $size The size of the bit field. |
241
|
|
|
* @return array The generated list of flags. |
242
|
|
|
*/ |
243
|
|
|
protected function generateFlags($size) |
244
|
|
|
{ |
245
|
|
|
// Subtracting one is done to account for the off-by-one bug replicated |
246
|
|
|
// in \Itafroma\Zork\flagword(). |
247
|
|
|
$flags = []; |
248
|
|
|
for ($i = 0; $i < $size - 1; ++$i) { |
249
|
|
|
$flags[] = 'ZorkTest-flag-' . $i; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $flags; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Provide a list of flags up to the maximum bit field size. |
257
|
|
|
*/ |
258
|
|
|
public function flagProvider() |
259
|
|
|
{ |
260
|
|
|
return [[$this->generateFlags(self::FLAGSIZEMAX)]]; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Provide a list of flags beyond the maximum bit field size. |
265
|
|
|
*/ |
266
|
|
|
public function flagOverflowProvider() |
267
|
|
|
{ |
268
|
|
|
return [[$this->generateFlags(self::FLAGSIZEMAX + 10)]]; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|