porolog 0.0.7 → 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,874 @@
1
+ #
2
+ # test/porolog/instantiation_test.rb - Test Suite for Porolog::Instantiation
3
+ #
4
+ # Luis Esteban 2 May 2018
5
+ # created
6
+ #
7
+
8
+ require_relative '../test_helper'
9
+
10
+ describe 'Porolog' do
11
+
12
+ before(:all) do
13
+ reset
14
+ end
15
+
16
+ describe 'Instantiation' do
17
+
18
+ let(:goal1) { new_goal :generic, :m, :n }
19
+ let(:goal2) { new_goal :specifc, :p, :q }
20
+ let(:goal3) { new_goal :other, :x, :y }
21
+ let(:variable1) { goal1.variable(:x) }
22
+ let(:variable2) { goal2.variable(:y) }
23
+ let(:variable3) { goal3.variable(:z) }
24
+ let(:variable4) { goal3.variable(:v) }
25
+ let(:instantiation) { Instantiation.new variable1, nil, variable2, nil }
26
+
27
+ describe '.reset' do
28
+
29
+ it 'should delete/unregister all instantiations' do
30
+ assert_equal 0, Instantiation.instantiations.size
31
+
32
+ Instantiation.new variable1, nil, variable2, nil
33
+ Instantiation.new variable1, nil, variable2, 2
34
+ Instantiation.new variable1, nil, variable3, nil
35
+ Instantiation.new variable1, 1, variable3, nil
36
+ Instantiation.new variable1, nil, variable2, :head
37
+
38
+ assert_equal 5, Instantiation.instantiations.size
39
+
40
+ assert Instantiation.reset, name
41
+
42
+ assert_equal 0, Instantiation.instantiations.size
43
+ end
44
+
45
+ end
46
+
47
+ describe '.instantiations' do
48
+
49
+ it 'should be empty to start with' do
50
+ assert_equal({}, Instantiation.instantiations)
51
+ end
52
+
53
+ it 'should return all registered instantiations' do
54
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
55
+ instantiation2 = Instantiation.new variable1, nil, variable2, 2
56
+ instantiation3 = Instantiation.new variable1, nil, variable3, nil
57
+ instantiation4 = Instantiation.new variable1, 1, variable3, nil
58
+ instantiation5 = Instantiation.new variable2, nil, variable3, nil
59
+
60
+ assert_equal(
61
+ {
62
+ instantiation1.signature => instantiation1,
63
+ instantiation2.signature => instantiation2,
64
+ instantiation3.signature => instantiation3,
65
+ instantiation4.signature => instantiation4,
66
+ instantiation5.signature => instantiation5,
67
+ },
68
+ Instantiation.instantiations
69
+ )
70
+ end
71
+
72
+ end
73
+
74
+ describe '.new' do
75
+
76
+ it 'should create a new instantiation' do
77
+ instantiation = Instantiation.new variable1, nil, variable2, nil
78
+
79
+ assert_instance_of Instantiation, instantiation
80
+ end
81
+
82
+ it 'should not create duplicate instantiations' do
83
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
84
+ instantiation2 = Instantiation.new variable1, nil, variable2, 2
85
+ instantiation3 = Instantiation.new variable1, nil, variable3, nil
86
+ instantiation4 = Instantiation.new variable1, 1, variable3, nil
87
+ instantiation5 = Instantiation.new variable1, nil, variable2, nil
88
+
89
+ assert_equal instantiation1, instantiation5
90
+ assert_equal(
91
+ {
92
+ [variable1, nil, variable2, nil] => instantiation1,
93
+ [variable1, nil, variable2, 2 ] => instantiation2,
94
+ [variable1, 1, variable3, nil] => instantiation4,
95
+ [variable1, nil, variable3, nil] => instantiation3,
96
+ },
97
+ Instantiation.instantiations
98
+ )
99
+ end
100
+
101
+ end
102
+
103
+ describe '#initialize' do
104
+
105
+ it 'should initialize variable1, variable2, index1, and index2' do
106
+ instantiation = Instantiation.new variable1, nil, variable2, nil
107
+
108
+ assert_Instantiation instantiation, variable1, variable2, nil, nil
109
+ end
110
+
111
+ it 'should append itself to its variables' do
112
+ instantiation = Instantiation.new variable1, nil, variable2, nil
113
+
114
+ assert_equal [instantiation], variable1.instantiations
115
+ assert_equal [instantiation], variable2.instantiations
116
+ end
117
+
118
+ it 'should register the instantiation' do
119
+ instantiation = Instantiation.new variable1, nil, variable2, nil
120
+
121
+ assert_equal [instantiation], Instantiation.instantiations.values
122
+ assert_equal [[variable1, nil, variable2, nil]], Instantiation.instantiations.keys
123
+ assert_equal(
124
+ {
125
+ [variable1, nil, variable2, nil] => instantiation
126
+ },
127
+ Instantiation.instantiations
128
+ )
129
+ end
130
+
131
+ end
132
+
133
+ describe '#signature' do
134
+
135
+ it 'should return the signature of the instantiation' do
136
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
137
+ instantiation2 = Instantiation.new variable1, 4, variable2, :head
138
+
139
+ assert_equal [variable1, nil, variable2, nil ], instantiation1.signature
140
+ assert_equal [variable1, 4, variable2, :head], instantiation2.signature
141
+ end
142
+
143
+ end
144
+
145
+ describe '#inspect' do
146
+
147
+ it 'should show its variables and their indices' do
148
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
149
+ instantiation2 = Instantiation.new variable1, 4, variable2, :head
150
+
151
+ assert_equal 'Goal1.:x = Goal2.:y', instantiation1.inspect
152
+ assert_equal 'Goal1.:x[4] = Goal2.:y[:head]', instantiation2.inspect
153
+ end
154
+
155
+ end
156
+
157
+ describe '#remove' do
158
+
159
+ it 'should remove itself from instantiations of both variables' do
160
+ instantiation = Instantiation.new variable1, nil, variable2, nil
161
+
162
+ assert_equal [instantiation], variable1.instantiations
163
+ assert_equal [instantiation], variable2.instantiations
164
+
165
+ instantiation.remove
166
+
167
+ assert_equal [], variable1.instantiations
168
+ assert_equal [], variable2.instantiations
169
+ end
170
+
171
+ it 'should be marked as deleted' do
172
+ instantiation = Instantiation.new variable1, nil, variable2, nil
173
+
174
+ refute instantiation.deleted?, 'instantiation should not be marked deleted'
175
+
176
+ instantiation.remove
177
+
178
+ assert instantiation.deleted?, 'instantiation should be marked deleted'
179
+ end
180
+
181
+ it 'should unregister itself' do
182
+ assert_equal [], Instantiation.instantiations.values
183
+
184
+ instantiation = Instantiation.new variable1, nil, variable2, nil
185
+
186
+ assert_equal [instantiation], Instantiation.instantiations.values
187
+ assert_equal [[variable1, nil, variable2, nil]], Instantiation.instantiations.keys
188
+ assert_equal(
189
+ {
190
+ [variable1, nil, variable2, nil] => instantiation
191
+ },
192
+ Instantiation.instantiations
193
+ )
194
+
195
+ instantiation.remove
196
+
197
+ assert_equal [], Instantiation.instantiations.values
198
+ assert_equal [], Instantiation.instantiations.keys
199
+ assert_equal(
200
+ {
201
+ },
202
+ Instantiation.instantiations
203
+ )
204
+ end
205
+
206
+ end
207
+
208
+ describe '#other_goal_to' do
209
+
210
+ it 'should return the goal of the other variable' do
211
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
212
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
213
+
214
+ assert_equal goal2, instantiation1.other_goal_to(variable1)
215
+ assert_equal goal1, instantiation1.other_goal_to(variable2)
216
+ assert_nil instantiation1.other_goal_to(variable3)
217
+ assert_nil instantiation2.other_goal_to(variable1)
218
+ assert_equal goal3, instantiation2.other_goal_to(variable2)
219
+ assert_equal goal2, instantiation2.other_goal_to(variable3)
220
+ end
221
+
222
+ end
223
+
224
+ describe '#goals' do
225
+
226
+ it 'should return the goals' do
227
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
228
+ instantiation2 = Instantiation.new variable1, 4, variable3, :head
229
+
230
+ assert_equal [goal1, goal2], instantiation1.goals
231
+ assert_equal [goal1, goal3], instantiation2.goals
232
+ end
233
+
234
+ end
235
+
236
+ describe '#variables' do
237
+
238
+ it 'should return both variables' do
239
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
240
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
241
+
242
+ assert_equal [variable1,variable2], instantiation1.variables
243
+ assert_equal [variable2,variable3], instantiation2.variables
244
+ end
245
+
246
+ end
247
+
248
+ describe '#values' do
249
+
250
+ it 'should return all values instantiated to its variables' do
251
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
252
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
253
+
254
+ assert_equal [], instantiation1.values
255
+ assert_equal [], instantiation2.values
256
+
257
+ variable3.instantiate Value.new('word',goal3)
258
+
259
+ assert_equal ['word'], instantiation1.values
260
+ assert_equal ['word'], instantiation2.values
261
+ end
262
+
263
+ it 'should return all indexed values instantiated to its variables' do
264
+ instantiation1 = Instantiation.new variable1, nil, variable2, 3
265
+ instantiation2 = Instantiation.new variable2, 4, variable3, nil
266
+
267
+ assert_equal [], instantiation1.values
268
+ assert_equal [], instantiation2.values
269
+
270
+ variable2.instantiate Value.new([2,3,5,7,11,13,17,19,23],goal2)
271
+
272
+ assert_equal 7, variable1.value
273
+ assert_equal [2,3,5,7,11,13,17,19,23], variable2.value
274
+ assert_equal 11, variable3.value
275
+
276
+ assert_equal [7], instantiation1.values
277
+ assert_equal [11], instantiation2.values
278
+
279
+ variable2.uninstantiate goal2
280
+
281
+ assert_equal variable1, variable1.value
282
+ assert_equal variable2, variable2.value
283
+ assert_equal variable3, variable3.value
284
+
285
+ assert_equal [], instantiation1.values
286
+ assert_equal [], instantiation2.values
287
+
288
+ words = %w{two three five seven eleven thirteen seventeen nineteen twenty-three}
289
+ variable2.instantiate Value.new(words, goal2)
290
+
291
+ assert_equal 'seven', variable1.value
292
+ assert_equal words, variable2.value
293
+ assert_equal 'eleven', variable3.value
294
+
295
+ assert_equal ['seven'], instantiation1.values
296
+ assert_equal ['eleven'], instantiation2.values
297
+ end
298
+
299
+ end
300
+
301
+ describe '#value_indexed' do
302
+
303
+ it 'should return unknown tail when value has an unknown tail and the index is greater than the fixed portion of the list' do
304
+ assert_equal UNKNOWN_TAIL, instantiation.value_indexed([2,3,5,7,UNKNOWN_TAIL],99)
305
+ end
306
+
307
+ it 'should return indexed element when value has an unknown tail and the index is less than the fixed portion of the list' do
308
+ assert_equal 5, instantiation.value_indexed([2,3,5,7,UNKNOWN_TAIL],2)
309
+ end
310
+
311
+ it 'should return the component when value is a Variable and the index is a Symbol' do
312
+ value = goal1.value([2,3,5,7,UNKNOWN_TAIL])
313
+
314
+ assert_equal 2, instantiation.value_indexed(value,:head)
315
+ assert_equal [3,5,7,UNKNOWN_TAIL], instantiation.value_indexed(value,:tail)
316
+ end
317
+
318
+ it 'should return nil when value is nil' do
319
+ assert_nil instantiation.value_indexed(nil,nil)
320
+ assert_nil instantiation.value_indexed(nil,4)
321
+ assert_nil instantiation.value_indexed(nil,:head)
322
+ assert_nil instantiation.value_indexed(nil,:tail)
323
+ end
324
+
325
+ it 'should return nil when value is a Variable' do
326
+ assert_nil instantiation.value_indexed(variable3,nil)
327
+ assert_nil instantiation.value_indexed(variable3,4)
328
+ assert_nil instantiation.value_indexed(variable3,:head)
329
+ assert_nil instantiation.value_indexed(variable3,:tail)
330
+ end
331
+
332
+ it 'should return nil when value is a Symbol' do
333
+ assert_nil instantiation.value_indexed(:z,nil)
334
+ assert_nil instantiation.value_indexed(:z,4)
335
+ assert_nil instantiation.value_indexed(:z,:head)
336
+ assert_nil instantiation.value_indexed(:z,:tail)
337
+ end
338
+
339
+ it 'should return nil when value is an empty Array and index is :tail' do
340
+ assert_nil instantiation.value_indexed([],:tail)
341
+ end
342
+
343
+ it 'should return the indexed value when index is an Integer' do
344
+ assert_equal 7, instantiation.value_indexed([2,3,5,7,11],3)
345
+ assert_equal 14, instantiation.value_indexed(14,3)
346
+ assert_equal 'd', instantiation.value_indexed('word',3)
347
+ assert_equal 6.4, instantiation.value_indexed(6.4,3)
348
+ end
349
+
350
+ it 'should return the component when index is a Symbol' do
351
+ assert_equal 5, instantiation.value_indexed([2,3,5,7,11],:length)
352
+ assert_equal 11, instantiation.value_indexed([2,3,5,7,11],:last)
353
+ assert_equal [2,3,5,7,11], instantiation.value_indexed([2,3,5,7,11],:nothing)
354
+ end
355
+
356
+ it 'should return the dynamically sized head when index is an Array' do
357
+ assert_equal [3,5,7,11], instantiation.value_indexed([2,3,5,7,11],[])
358
+ assert_equal [], instantiation.value_indexed([2,3,5,7,11],[0])
359
+ assert_equal [2], instantiation.value_indexed([2,3,5,7,11],[1])
360
+ assert_equal [2,3], instantiation.value_indexed([2,3,5,7,11],[2])
361
+ assert_equal [2], instantiation.value_indexed([2,3,5,7,11],[1,1])
362
+ end
363
+
364
+ it 'should raise an error when index is unexpected' do
365
+ assert_raises Instantiation::UnhandledIndexError do
366
+ assert_equal [2,3,5,7,11], instantiation.value_indexed([2,3,5,7,11],12.34)
367
+ end
368
+ assert_raises Instantiation::UnhandledIndexError do
369
+ assert_equal [2,3,5,7,11], instantiation.value_indexed([2,3,5,7,11],'hello')
370
+ end
371
+ assert_raises Instantiation::UnhandledIndexError do
372
+ assert_equal [2,3,5,7,11], instantiation.value_indexed([2,3,5,7,11],Object)
373
+ end
374
+ object = Object.new
375
+ assert_raises Instantiation::UnhandledIndexError do
376
+ assert_equal [2,3,5,7,11], instantiation.value_indexed([2,3,5,7,11],object)
377
+ end
378
+ end
379
+
380
+ end
381
+
382
+ describe '#values_for' do
383
+
384
+ it 'should return all values instantiated to the specified variable' do
385
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
386
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
387
+
388
+ assert_equal [], instantiation1.values_for(variable1)
389
+ assert_equal [], instantiation1.values_for(variable2)
390
+ assert_equal [], instantiation1.values_for(variable3)
391
+ assert_equal [], instantiation2.values_for(variable1)
392
+ assert_equal [], instantiation2.values_for(variable2)
393
+ assert_equal [], instantiation2.values_for(variable3)
394
+
395
+ variable3.instantiate Value.new('word',goal3)
396
+
397
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word' }, [
398
+ 'Goal1.:m',
399
+ 'Goal1.:n',
400
+ 'Goal1.:x',
401
+ ' Goal2.:y',
402
+ ' Goal3.:z',
403
+ ' Goal3."word"',
404
+ ].join("\n")
405
+ assert_Goal_variables goal2, { p: nil, q: nil, y: 'word' }, [
406
+ 'Goal2.:p',
407
+ 'Goal2.:q',
408
+ 'Goal2.:y',
409
+ ' Goal1.:x',
410
+ ' Goal3.:z',
411
+ ' Goal3."word"',
412
+ ].join("\n")
413
+ assert_Goal_variables goal3, { x: nil, y: nil, z: 'word' }, [
414
+ 'Goal3.:x',
415
+ 'Goal3.:y',
416
+ 'Goal3.:z',
417
+ ' Goal2.:y',
418
+ ' Goal1.:x',
419
+ ' Goal3."word"',
420
+ ].join("\n")
421
+
422
+ assert_equal ['word'], instantiation1.values_for(variable1)
423
+ assert_equal [], instantiation1.values_for(variable2)
424
+ assert_equal [], instantiation1.values_for(variable3)
425
+ assert_equal [], instantiation2.values_for(variable1)
426
+ assert_equal ['word'], instantiation2.values_for(variable2)
427
+ assert_equal [], instantiation2.values_for(variable3)
428
+ end
429
+
430
+ it 'should return all indexed values instantiated to its variables' do
431
+ instantiation1 = Instantiation.new variable1, nil, variable2, 3
432
+ instantiation2 = Instantiation.new variable2, 4, variable3, nil
433
+
434
+ assert_equal [], instantiation1.values_for(variable1)
435
+ assert_equal [], instantiation1.values_for(variable2)
436
+ assert_equal [], instantiation2.values_for(variable2)
437
+ assert_equal [], instantiation2.values_for(variable3)
438
+
439
+ variable2.instantiate Value.new([2,3,5,7,11,13,17,19,23],goal2)
440
+
441
+ assert_equal 7, variable1.value
442
+ assert_equal [2,3,5,7,11,13,17,19,23], variable2.value
443
+ assert_equal 11, variable3.value
444
+
445
+ assert_equal [7], instantiation1.values_for(variable1)
446
+ assert_equal [], instantiation1.values_for(variable2)
447
+ assert_equal [], instantiation2.values_for(variable2)
448
+ assert_equal [11], instantiation2.values_for(variable3)
449
+
450
+ variable2.uninstantiate goal2
451
+
452
+ assert_equal variable1, variable1.value
453
+ assert_equal variable2, variable2.value
454
+ assert_equal variable3, variable3.value
455
+
456
+ assert_equal [], instantiation1.values
457
+ assert_equal [], instantiation2.values
458
+
459
+ words = %w{two three five seven eleven thirteen seventeen nineteen twenty-three}
460
+ variable2.instantiate Value.new(words, goal2)
461
+
462
+ assert_equal 'seven', variable1.value
463
+ assert_equal words, variable2.value
464
+ assert_equal 'eleven', variable3.value
465
+
466
+ assert_equal ['seven'], instantiation1.values
467
+ assert_equal ['eleven'], instantiation2.values
468
+ end
469
+
470
+ it 'should not infinitely recurse' do
471
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
472
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
473
+ instantiation3 = Instantiation.new variable3, nil, variable1, nil
474
+
475
+ assert_equal [], instantiation1.values_for(variable1)
476
+ assert_equal [], instantiation1.values_for(variable2)
477
+ assert_equal [], instantiation2.values_for(variable2)
478
+ assert_equal [], instantiation2.values_for(variable3)
479
+ assert_equal [], instantiation3.values_for(variable3)
480
+ assert_equal [], instantiation3.values_for(variable1)
481
+ end
482
+
483
+ it 'should return an uninstantiated tail when the head is known only' do
484
+ instantiation1 = Instantiation.new variable1, :head, variable2, nil
485
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
486
+ instantiation3 = Instantiation.new variable3, nil, variable4, nil
487
+
488
+ goal3.instantiate :v, 'head', goal3
489
+
490
+ assert_Goal_variables goal1, { m: nil, n: nil, x: [goal3.value('head'), UNKNOWN_TAIL] }, [
491
+ 'Goal1.:m',
492
+ 'Goal1.:n',
493
+ 'Goal1.:x',
494
+ ' [:head]Goal2.:y',
495
+ ' Goal3.:z',
496
+ ' Goal3.:v',
497
+ ' Goal3."head"',
498
+ ].join("\n")
499
+
500
+ assert_Goal_variables goal3, { x: nil, y: nil, z: 'head', v: 'head' }, [
501
+ 'Goal3.:x',
502
+ 'Goal3.:y',
503
+ 'Goal3.:z',
504
+ ' Goal2.:y',
505
+ ' Goal1.:x[:head]',
506
+ ' Goal3.:v',
507
+ ' Goal3."head"',
508
+ 'Goal3.:v',
509
+ ' Goal3.:z',
510
+ ' Goal2.:y',
511
+ ' Goal1.:x[:head]',
512
+ ' Goal3."head"',
513
+ ].join("\n")
514
+
515
+ assert_equal [['head', UNKNOWN_TAIL]], instantiation1.values_for(variable1)
516
+ assert_equal [], instantiation1.values_for(variable2)
517
+ assert_equal ['head'], instantiation2.values_for(variable2)
518
+ assert_equal [], instantiation2.values_for(variable3)
519
+ assert_equal ['head'], instantiation3.values_for(variable3)
520
+ assert_equal [], instantiation3.values_for(variable4)
521
+ end
522
+
523
+ it 'should return an uninstantiated head when the tail is known only' do
524
+ instantiation1 = Instantiation.new variable1, :tail, variable2, nil
525
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
526
+ instantiation3 = Instantiation.new variable3, nil, variable4, nil
527
+
528
+ goal3.instantiate :v, ['this','is','the','tail'], goal3
529
+
530
+ assert_Goal_variables goal1, { m: nil, n: nil, x: [nil, 'this','is','the','tail'] }, [
531
+ 'Goal1.:m',
532
+ 'Goal1.:n',
533
+ 'Goal1.:x',
534
+ ' [:tail]Goal2.:y',
535
+ ' Goal3.:z',
536
+ ' Goal3.:v',
537
+ ' Goal3.["this", "is", "the", "tail"]',
538
+ ].join("\n")
539
+
540
+ assert_Goal_variables goal3, { x: nil, y: nil, z: ['this','is','the','tail'], v: ['this','is','the','tail'] }, [
541
+ 'Goal3.:x',
542
+ 'Goal3.:y',
543
+ 'Goal3.:z',
544
+ ' Goal2.:y',
545
+ ' Goal1.:x[:tail]',
546
+ ' Goal3.:v',
547
+ ' Goal3.["this", "is", "the", "tail"]',
548
+ 'Goal3.:v',
549
+ ' Goal3.:z',
550
+ ' Goal2.:y',
551
+ ' Goal1.:x[:tail]',
552
+ ' Goal3.["this", "is", "the", "tail"]',
553
+ ].join("\n")
554
+
555
+ assert_equal [[nil, 'this','is','the','tail']], instantiation1.values_for(variable1)
556
+ assert_equal [], instantiation1.values_for(variable2)
557
+ assert_equal [goal3.value(['this','is','the','tail'])], instantiation2.values_for(variable2)
558
+ assert_equal [], instantiation2.values_for(variable3)
559
+ assert_equal [goal3.value(['this','is','the','tail'])], instantiation3.values_for(variable3)
560
+ assert_equal [], instantiation3.values_for(variable4)
561
+ end
562
+
563
+ it 'should return a list with uninstantiated elements and tail with the value at the index' do
564
+ instantiation1 = Instantiation.new variable1, 3, variable2, nil
565
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
566
+ instantiation3 = Instantiation.new variable3, nil, variable4, nil
567
+
568
+ goal3.instantiate :v, 45.9, goal3
569
+
570
+ assert_Goal_variables goal1, { m: nil, n: nil, x: [nil, nil, nil, goal3.value(45.9), UNKNOWN_TAIL] }, [
571
+ 'Goal1.:m',
572
+ 'Goal1.:n',
573
+ 'Goal1.:x',
574
+ ' [3]Goal2.:y',
575
+ ' Goal3.:z',
576
+ ' Goal3.:v',
577
+ ' Goal3.45.9',
578
+ ].join("\n")
579
+
580
+ assert_Goal_variables goal3, { x: nil, y: nil, z: 45.9, v: 45.9 }, [
581
+ 'Goal3.:x',
582
+ 'Goal3.:y',
583
+ 'Goal3.:z',
584
+ ' Goal2.:y',
585
+ ' Goal1.:x[3]',
586
+ ' Goal3.:v',
587
+ ' Goal3.45.9',
588
+ 'Goal3.:v',
589
+ ' Goal3.:z',
590
+ ' Goal2.:y',
591
+ ' Goal1.:x[3]',
592
+ ' Goal3.45.9',
593
+ ].join("\n")
594
+
595
+ assert_equal [[nil, nil, nil, 45.9, UNKNOWN_TAIL]], instantiation1.values_for(variable1)
596
+ assert_equal [], instantiation1.values_for(variable2)
597
+ assert_equal [goal3.value(45.9)], instantiation2.values_for(variable2)
598
+ assert_equal [], instantiation2.values_for(variable3)
599
+ assert_equal [goal3.value(45.9)], instantiation3.values_for(variable3)
600
+ assert_equal [], instantiation3.values_for(variable4)
601
+ end
602
+
603
+ it 'should return the value of variable1 when the specified variable is variable2 and variable1 is not a Variable' do
604
+ value1 = goal1.value('titanium')
605
+
606
+ instantiation1 = Instantiation.new value1, nil, variable3, nil
607
+
608
+ assert_equal [value1], instantiation1.values_for(variable3)
609
+ end
610
+
611
+ end
612
+
613
+ describe '#value_at_index' do
614
+
615
+ describe 'when index is nil' do
616
+
617
+ it 'should return nil for a nil value' do
618
+ assert_nil instantiation.value_at_index(nil,nil)
619
+ end
620
+
621
+ it 'should return the value for a non-nil value' do
622
+ assert_equal 'orange', instantiation.value_at_index('orange',nil)
623
+ end
624
+
625
+ end
626
+
627
+ describe 'when index is Integer' do
628
+
629
+ it 'should return nil for nil value' do
630
+ assert_nil instantiation.value_at_index(nil,5)
631
+ end
632
+
633
+ it 'should return an Array for an Integer indexed value' do
634
+ assert_equal [nil,nil,nil,nil,nil,'word',UNKNOWN_TAIL], instantiation.value_at_index('word',5)
635
+ end
636
+
637
+ end
638
+
639
+ describe 'when index is Symbol' do
640
+
641
+ it 'should return an Array for a head indexed value' do
642
+ assert_equal [['word','from','list'],UNKNOWN_TAIL], instantiation.value_at_index(['word','from','list'],:head)
643
+ end
644
+
645
+ it 'should return an Array for a tail indexed value' do
646
+ assert_equal [nil,'word','from','list'], instantiation.value_at_index(['word','from','list'],:tail)
647
+ end
648
+
649
+ it 'should return an error for an unrecognised symbol indexed value' do
650
+ assert_raises Instantiation::UnhandledIndexError do
651
+ assert_equal :error, instantiation.value_at_index(['word','from','list'],:size)
652
+ end
653
+ end
654
+
655
+ end
656
+
657
+ describe 'when index is Array' do
658
+
659
+ it 'should return an Array for a tail indexed value' do
660
+ assert_equal [nil,'word','from','list'], instantiation.value_at_index(['word','from','list'],[])
661
+ end
662
+
663
+ it 'should return an Array for a tail indexed value' do
664
+ assert_equal [['word','from','list'], UNKNOWN_TAIL], instantiation.value_at_index(['word','from','list'],[1])
665
+ end
666
+
667
+ it 'should return an Array for a tail indexed value' do
668
+ assert_equal [['word','from','list'], UNKNOWN_TAIL], instantiation.value_at_index(['word','from','list'],[2])
669
+ end
670
+
671
+ it 'should return an Array for a tail indexed value' do
672
+ assert_equal [['word','from','list'], UNKNOWN_TAIL], instantiation.value_at_index(['word','from','list'],[-2])
673
+ end
674
+
675
+ end
676
+
677
+ describe 'when index is Float' do
678
+
679
+ it 'should return an error for an unrecognised symbol indexed value' do
680
+ assert_raises Instantiation::UnhandledIndexError do
681
+ assert_equal ['word','from','list'], instantiation.value_at_index(['word','from','list'],2.3)
682
+ end
683
+ end
684
+
685
+ end
686
+
687
+ it 'should return all values instantiated to the specified variable' do
688
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
689
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
690
+
691
+ assert_equal [], instantiation1.values_for(variable1)
692
+ assert_equal [], instantiation1.values_for(variable2)
693
+ assert_equal [], instantiation1.values_for(variable3)
694
+ assert_equal [], instantiation2.values_for(variable1)
695
+ assert_equal [], instantiation2.values_for(variable2)
696
+ assert_equal [], instantiation2.values_for(variable3)
697
+
698
+ variable3.instantiate Value.new('word',goal3)
699
+
700
+ assert_Goal_variables goal1, { m: nil, n: nil, x: 'word' }, [
701
+ 'Goal1.:m',
702
+ 'Goal1.:n',
703
+ 'Goal1.:x',
704
+ ' Goal2.:y',
705
+ ' Goal3.:z',
706
+ ' Goal3."word"',
707
+ ].join("\n")
708
+
709
+ assert_Goal_variables goal2, { p: nil, q: nil, y: 'word' }, [
710
+ 'Goal2.:p',
711
+ 'Goal2.:q',
712
+ 'Goal2.:y',
713
+ ' Goal1.:x',
714
+ ' Goal3.:z',
715
+ ' Goal3."word"',
716
+ ].join("\n")
717
+
718
+ assert_Goal_variables goal3, { x: nil, y: nil, z: 'word' }, [
719
+ 'Goal3.:x',
720
+ 'Goal3.:y',
721
+ 'Goal3.:z',
722
+ ' Goal2.:y',
723
+ ' Goal1.:x',
724
+ ' Goal3."word"',
725
+ ].join("\n")
726
+
727
+ assert_equal ['word'], instantiation1.values_for(variable1)
728
+ assert_equal [], instantiation1.values_for(variable2)
729
+ assert_equal [], instantiation1.values_for(variable3)
730
+ assert_equal [], instantiation2.values_for(variable1)
731
+ assert_equal ['word'], instantiation2.values_for(variable2)
732
+ assert_equal [], instantiation2.values_for(variable3)
733
+ end
734
+
735
+ it 'should return all indexed values instantiated to its variables' do
736
+ instantiation1 = Instantiation.new variable1, nil, variable2, 3
737
+ instantiation2 = Instantiation.new variable2, 4, variable3, nil
738
+
739
+ assert_equal [], instantiation1.values_for(variable1)
740
+ assert_equal [], instantiation1.values_for(variable2)
741
+ assert_equal [], instantiation2.values_for(variable2)
742
+ assert_equal [], instantiation2.values_for(variable3)
743
+
744
+ variable2.instantiate Value.new([2,3,5,7,11,13,17,19,23],goal2)
745
+
746
+ assert_equal 7, variable1.value
747
+ assert_equal [2,3,5,7,11,13,17,19,23], variable2.value
748
+ assert_equal 11, variable3.value
749
+
750
+ assert_equal [7], instantiation1.values_for(variable1)
751
+ assert_equal [], instantiation1.values_for(variable2)
752
+ assert_equal [], instantiation2.values_for(variable2)
753
+ assert_equal [11], instantiation2.values_for(variable3)
754
+
755
+ variable2.uninstantiate goal2
756
+
757
+ assert_equal variable1, variable1.value
758
+ assert_equal variable2, variable2.value
759
+ assert_equal variable3, variable3.value
760
+
761
+ assert_equal [], instantiation1.values
762
+ assert_equal [], instantiation2.values
763
+
764
+ words = %w{two three five seven eleven thirteen seventeen nineteen twenty-three}
765
+ variable2.instantiate Value.new(words, goal2)
766
+
767
+ assert_equal 'seven', variable1.value
768
+ assert_equal words, variable2.value
769
+ assert_equal 'eleven', variable3.value
770
+
771
+ assert_equal ['seven'], instantiation1.values
772
+ assert_equal ['eleven'], instantiation2.values
773
+ end
774
+
775
+ it 'should not infinitely recurse' do
776
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
777
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
778
+ instantiation3 = Instantiation.new variable3, nil, variable1, nil
779
+
780
+ assert_equal [], instantiation1.values_for(variable1)
781
+ assert_equal [], instantiation1.values_for(variable2)
782
+ assert_equal [], instantiation2.values_for(variable2)
783
+ assert_equal [], instantiation2.values_for(variable3)
784
+ assert_equal [], instantiation3.values_for(variable3)
785
+ assert_equal [], instantiation3.values_for(variable1)
786
+ end
787
+
788
+ end
789
+
790
+ describe '#without_index_on' do
791
+
792
+ it 'should return true for a variable without an index in the instantiation' do
793
+ instantiation = Instantiation.new variable1, nil, variable2, nil
794
+
795
+ assert_equal true, instantiation.without_index_on?(variable1)
796
+ assert_equal true, instantiation.without_index_on?(variable2)
797
+ end
798
+
799
+ it 'should return correctly when the instantiation has one index' do
800
+ instantiation = Instantiation.new variable1, 3, variable2, nil
801
+
802
+ assert_equal false, instantiation.without_index_on?(variable1)
803
+ assert_equal true, instantiation.without_index_on?(variable2)
804
+ end
805
+
806
+ it 'should return correctly when the instantiation has one index (other one)' do
807
+ instantiation = Instantiation.new variable1, nil, variable2, 3
808
+
809
+ assert_equal true, instantiation.without_index_on?(variable1)
810
+ assert_equal false, instantiation.without_index_on?(variable2)
811
+ end
812
+
813
+ it 'should return false when the instantiation has two indexes' do
814
+ instantiation = Instantiation.new variable1, 3, variable2, 3
815
+
816
+ assert_equal false, instantiation.without_index_on?(variable1)
817
+ assert_equal false, instantiation.without_index_on?(variable2)
818
+ end
819
+
820
+ it 'should return false for a variable not in the instantiation' do
821
+ instantiation = Instantiation.new variable1, nil, variable2, nil
822
+
823
+ assert_equal false, instantiation.without_index_on?(variable3)
824
+ end
825
+
826
+ end
827
+
828
+ describe '#deleted?' do
829
+
830
+ it 'should return true when removed' do
831
+ refute instantiation.deleted?, 'instantiation should not be marked deleted'
832
+
833
+ instantiation.remove
834
+
835
+ assert instantiation.deleted?, 'instantiation should be marked deleted'
836
+ end
837
+
838
+ it 'should return true when the goal of variable1 is deleted' do
839
+ refute instantiation.deleted?, 'instantiation should not be marked deleted'
840
+
841
+ Goal.goals.delete(goal1)
842
+
843
+ assert instantiation.deleted?, 'instantiation should be marked deleted'
844
+ end
845
+
846
+ it 'should return true when the goal of variable2 is deleted' do
847
+ refute instantiation.deleted?, 'instantiation should not be marked deleted'
848
+
849
+ Goal.goals.delete(goal2)
850
+
851
+ assert instantiation.deleted?, 'instantiation should be marked deleted'
852
+ end
853
+
854
+ end
855
+
856
+ describe '#belongs_to?' do
857
+
858
+ it 'should correctly determine the relationship between an instantition and a goal' do
859
+ instantiation1 = Instantiation.new variable1, nil, variable2, nil
860
+ instantiation2 = Instantiation.new variable2, nil, variable3, nil
861
+
862
+ assert instantiation1.belongs_to?(goal1), 'instantiation1 should belong to goal1'
863
+ assert instantiation1.belongs_to?(goal2), 'instantiation1 should belong to goal2'
864
+ refute instantiation1.belongs_to?(goal3), 'instantiation1 should not belong to goal3'
865
+ refute instantiation2.belongs_to?(goal1), 'instantiation2 should not belong to goal1'
866
+ assert instantiation2.belongs_to?(goal2), 'instantiation2 should belong to goal2'
867
+ assert instantiation2.belongs_to?(goal3), 'instantiation2 should belong to goal3'
868
+ end
869
+
870
+ end
871
+
872
+ end
873
+
874
+ end