nudge 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. data/VERSION +1 -1
  2. data/spec/instructions/bool/bool_duplicate_spec.rb +50 -0
  3. data/spec/instructions/bool/bool_from_float_spec.rb +41 -0
  4. data/spec/instructions/bool/bool_from_int_spec.rb +41 -0
  5. data/spec/instructions/bool/bool_pop_spec.rb +47 -0
  6. data/spec/instructions/bool/bool_rotate_spec.rb +60 -0
  7. data/spec/instructions/bool/bool_swap_spec.rb +64 -0
  8. data/spec/instructions/code/code_depth_spec.rb +39 -0
  9. data/spec/instructions/code/code_duplicate_spec.rb +50 -0
  10. data/spec/instructions/code/code_flush_spec.rb +37 -0
  11. data/spec/instructions/code/code_from_bool_spec.rb +37 -0
  12. data/spec/instructions/code/code_from_float_spec.rb +37 -0
  13. data/spec/instructions/code/code_from_int_spec.rb +37 -0
  14. data/spec/instructions/code/code_pop_spec.rb +47 -0
  15. data/spec/instructions/code/code_rotate_spec.rb +60 -0
  16. data/spec/instructions/code/code_swap_spec.rb +58 -0
  17. data/spec/instructions/float/float_duplicate_spec.rb +50 -0
  18. data/spec/instructions/float/float_from_bool_spec.rb +42 -0
  19. data/spec/instructions/float/float_from_int_spec.rb +37 -0
  20. data/spec/instructions/float/float_pop_spec.rb +47 -0
  21. data/spec/instructions/float/float_rotate_spec.rb +60 -0
  22. data/spec/instructions/float/float_swap_spec.rb +64 -0
  23. data/spec/instructions/int/int_duplicate_spec.rb +50 -0
  24. data/spec/instructions/int/int_from_bool_spec.rb +43 -0
  25. data/spec/instructions/int/int_from_float_spec.rb +37 -0
  26. data/spec/instructions/int/int_pop_spec.rb +47 -0
  27. data/spec/instructions/int/int_rotate_spec.rb +60 -0
  28. data/spec/instructions/int/int_swap_spec.rb +64 -0
  29. data/spec/instructions/name/name_duplicate_spec.rb +50 -0
  30. data/spec/instructions/name/name_pop_spec.rb +47 -0
  31. data/spec/instructions/name/name_rotate_spec.rb +60 -0
  32. data/spec/instructions/name/name_swap_spec.rb +58 -0
  33. data/spec/interpreter/interpreter_spec.rb +0 -2
  34. metadata +66 -16
  35. data/spec/instructions/code/code_stack_spec.rb +0 -386
  36. data/spec/instructions/split_these/bool_stack_spec.rb +0 -95
  37. data/spec/instructions/split_these/conversions_spec.rb +0 -103
  38. data/spec/instructions/split_these/float_stack_spec.rb +0 -92
  39. data/spec/instructions/split_these/int_stack_spec.rb +0 -81
  40. data/spec/instructions/split_these/name_stack_spec.rb +0 -84
  41. /data/spec/instructions/code/{code_fromname_spec.rb → code_from_name_spec.rb} +0 -0
@@ -1,386 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "../../spec_helper")
2
- include Nudge
3
-
4
- theseInstructions = [
5
- CodePopInstruction,
6
- CodeSwapInstruction,
7
- CodeDuplicateInstruction,
8
- CodeRotateInstruction
9
- ]
10
-
11
- namesTheyNeed = {
12
- CodePopInstruction => 1,
13
- CodeSwapInstruction => 2,
14
- CodeDuplicateInstruction => 1,
15
- CodeRotateInstruction => 3
16
- }
17
-
18
- resultTuples = {
19
- CodePopInstruction => {["block {}", "do int_add"]=>["block {}"]},
20
- CodeSwapInstruction => {["block {}", "do int_add"]=>["do int_add", "block {}"]},
21
- CodeDuplicateInstruction => {["block {}"] => ["block {}", "block {}"]},
22
- CodeRotateInstruction => {["block {}", "do int_add", "sample int(2)"] => ["do int_add", "sample int(2)", "block {}"]}
23
- }
24
-
25
-
26
- theseInstructions.each do |instName|
27
- describe instName do
28
- before(:each) do
29
- @context = Interpreter.new
30
- @i1 = instName.new(@context)
31
- end
32
-
33
- it "should have its context right" do
34
- @i1.context.should == @context
35
- end
36
-
37
- [:preconditions?, :setup, :derive, :cleanup].each do |methodName|
38
- it "should respond to \##{methodName}" do
39
- @i1 = instName.new(@context)
40
- @i1.should respond_to(methodName)
41
- end
42
- end
43
-
44
- describe "\#go" do
45
- before(:each) do
46
- @i1 = instName.new(@context)
47
- @context.clear_stacks
48
- @empty_block_code_value = ValuePoint.new("code", "block {}")
49
- end
50
-
51
- describe "\#preconditions?" do
52
- it "should check that there are enough parameters" do
53
- 8.times {@context.stacks[:code].push(@empty_block_code_value)}
54
- @i1.preconditions?.should == true
55
- end
56
-
57
- it "should raise an error if the preconditions aren't met" do
58
- @context.clear_stacks # there are no params at all
59
- lambda{@i1.preconditions?}.should raise_error(Instruction::NotEnoughStackItems)
60
- end
61
-
62
- it "should successfully run #go only if all preconditions are met" do
63
- 7.times {@context.stacks[:code].push(@empty_block_code_value)}
64
- @i1.should_receive(:cleanup)
65
- @i1.go
66
- end
67
- end
68
-
69
- describe "\#cleanup" do
70
- describe "should restructure the stack" do
71
- examples = resultTuples[instName]
72
- examples.each do |inputs, finalStackState|
73
- params = inputs.inspect
74
- expected = finalStackState.inspect
75
- it "should end up with #{expected} on the \:code stack, starting with #{params}" do
76
- inputs.each {|i| @context.stacks[:code].push(ValuePoint.new("code",i))}
77
- @i1.go
78
- finalStackState.reverse.each {|i| @context.stacks[:code].pop.raw.should == i}
79
- end
80
- end
81
- end
82
- end
83
- end
84
- end
85
- end
86
-
87
-
88
- describe CodeDepthInstruction do
89
- before(:each) do
90
- @context = Interpreter.new
91
- @i1 = CodeDepthInstruction.new(@context)
92
- end
93
-
94
- it "should have its context set" do
95
- @i1.context.should == @context
96
- end
97
-
98
- [:preconditions?, :setup, :derive, :cleanup].each do |methodName|
99
- it "should respond to \##{methodName}" do
100
- @i1.should respond_to(methodName)
101
- end
102
- end
103
-
104
- describe "\#go" do
105
- before(:each) do
106
- @i1 = CodeDepthInstruction.new(@context)
107
- @context.clear_stacks
108
- @empty_block_code_value = ValuePoint.new("code", "block {}")
109
- end
110
-
111
- describe "\#preconditions?" do
112
- it "should check that the :int stack responds to #depth" do
113
- @i1.preconditions?.should == true
114
- end
115
- end
116
-
117
- describe "\#cleanup" do
118
- it "should count the items on the stack and push it onto the :int stack" do
119
- @context.stacks[:int].depth.should == 0
120
- @i1.go # there are no code literals
121
- @context.stacks[:int].peek.value.should == 0
122
- 7.times {@context.stacks[:code].push @empty_block_code_value}
123
- @i1.go
124
- @context.stacks[:int].peek.value.should == 7
125
- end
126
- end
127
- end
128
- end
129
-
130
-
131
- describe CodeFlushInstruction do
132
- before(:each) do
133
- @context = Interpreter.new
134
- @i1 = CodeFlushInstruction.new(@context)
135
- end
136
-
137
- it "should have its context set right" do
138
- @i1.context.should == @context
139
- end
140
-
141
- [:preconditions?, :setup, :derive, :cleanup].each do |methodName|
142
- it "should respond to \##{methodName}" do
143
- @i1.should respond_to(methodName)
144
- end
145
- end
146
-
147
- describe "\#go" do
148
- before(:each) do
149
- @i1 = CodeFlushInstruction.new(@context)
150
- @context.clear_stacks
151
- @empty_block_code_value = ValuePoint.new("code", "block {}")
152
- end
153
-
154
- describe "\#preconditions?" do
155
- it "should check that the :int stack responds to #depth" do
156
- @i1.preconditions?.should == true
157
- end
158
- end
159
-
160
- describe "\#cleanup" do
161
- it "should remove all items on the stack" do
162
- 11.times {@context.stacks[:code].push(@empty_block_code_value)}
163
- @context.stacks[:code].depth.should == 11
164
- @i1.go
165
- @context.stacks[:code].depth.should == 0
166
- end
167
- end
168
- end
169
- end
170
-
171
-
172
- describe CodeShoveInstruction do
173
- before(:each) do
174
- @context = Interpreter.new
175
- @i1 = CodeShoveInstruction.new(@context)
176
- end
177
-
178
- it "should check its context is set" do
179
- @i1.context.should == @context
180
- end
181
-
182
- [:preconditions?, :setup, :derive, :cleanup].each do |methodName|
183
- it "should respond to \##{methodName}" do
184
- @i1.should respond_to(methodName)
185
- end
186
- end
187
-
188
- describe "\#go" do
189
- before(:each) do
190
- @i1 = CodeShoveInstruction.new(@context)
191
- @context.clear_stacks
192
- @empty_block_code_value = ValuePoint.new("code", "block {}")
193
-
194
- end
195
-
196
- describe "\#preconditions?" do
197
- it "should check that there is one :int and at least one :code" do
198
- @context.stacks[:int].push(ValuePoint.new("int", 4))
199
- @context.stacks[:code].push(@empty_block_code_value)
200
- @i1.preconditions?.should == true
201
- end
202
- end
203
-
204
- describe "\#cleanup" do
205
- before(:each) do
206
- @context.clear_stacks
207
- 11.times {@context.stacks[:code].push(@empty_block_code_value)}
208
- @context.stacks[:code].push(ValuePoint.new("code","do int_add")) # making it 12 deep
209
- end
210
-
211
- it "should not move the top item if the integer is negative" do
212
- @context.stacks[:int].push(ValuePoint.new("int", -99))
213
- @i1.go
214
- @context.stacks[:code].depth.should == 12
215
- @context.stacks[:code].peek.value.should == "do int_add"
216
- end
217
-
218
- it "should not move the top item if the integer is zero" do
219
- @context.stacks[:int].push(ValuePoint.new("int", 0))
220
- @i1.go
221
- @context.stacks[:code].depth.should == 12
222
- @context.stacks[:code].peek.value.should == "do int_add"
223
- end
224
-
225
- it "should move the top item farther down if the value is less than the depth" do
226
- @context.stacks[:int].push(ValuePoint.new("int", 1000))
227
- @i1.go
228
- @context.stacks[:code].depth.should == 12
229
- @context.stacks[:code].entries[0].value.should == "do int_add"
230
- end
231
-
232
- it "should move the top item to the bottom if the value is more than the depth" do
233
- @context.stacks[:int].push(ValuePoint.new("int", 4))
234
- @i1.go
235
- @context.stacks[:code].depth.should == 12
236
- @context.stacks[:code].entries[11].value.should == "block {}"
237
- @context.stacks[:code].entries[7].value.should == "do int_add"
238
- end
239
- end
240
- end
241
- end
242
-
243
-
244
- describe CodeYankInstruction do
245
- before(:each) do
246
- @context = Interpreter.new
247
- @i1 = CodeYankInstruction.new(@context)
248
- end
249
-
250
- it "should check its context is set" do
251
- @i1.context.should == @context
252
- end
253
-
254
- [:preconditions?, :setup, :derive, :cleanup].each do |methodName|
255
- it "should respond to \##{methodName}" do
256
- @i1.should respond_to(methodName)
257
- end
258
- end
259
-
260
- describe "\#go" do
261
- before(:each) do
262
- @i1 = CodeYankInstruction.new(@context)
263
- @context.clear_stacks
264
- @int1 = ValuePoint.new("int", 3)
265
- @empty_block_code_value = ValuePoint.new("code", "block {}")
266
- end
267
-
268
- describe "\#preconditions?" do
269
- it "should check that there is one :name and at least one :int" do
270
- @context.stacks[:code].push(@empty_block_code_value)
271
- @context.stacks[:int].push(@int1)
272
- @i1.preconditions?.should == true
273
- end
274
- end
275
-
276
- describe "\#cleanup" do
277
- before(:each) do
278
- @context.clear_stacks
279
- (4..6).each {|i| @context.stacks[:code].push(ValuePoint.new("code","ref a_#{i}"))}
280
- end
281
-
282
- it "should not change anything if the position integer is negative" do
283
- @context.stacks[:int].push(ValuePoint.new("int", -99))
284
- @i1.go
285
- and_now = @context.stacks[:code].entries.collect {|i| i.value}
286
- and_now.should == ["ref a_4", "ref a_5", "ref a_6"]
287
- end
288
-
289
- it "should not change anything if the position integer is zero" do
290
- @context.stacks[:int].push(ValuePoint.new("int", 0))
291
- @i1.go
292
- and_now = @context.stacks[:code].entries.collect {|i| i.value}
293
- and_now.should == ["ref a_4", "ref a_5", "ref a_6"]
294
- end
295
-
296
- it "should pull the last item on the stack to the top if the position is more than the stackdepth" do
297
- @context.stacks[:int].push(ValuePoint.new("int", 1000))
298
- @i1.go
299
- and_now = @context.stacks[:code].entries.collect {|i| i.value}
300
- and_now.should == ["ref a_5", "ref a_6", "ref a_4"]
301
- end
302
-
303
- it "should yank the indicated item to the top of the stack, counting from the 'top' 'down'" do
304
- @context.stacks[:int].push(ValuePoint.new("int", 1))
305
- @i1.go
306
- and_now = @context.stacks[:code].entries.collect {|i| i.value}
307
- and_now.should == ["ref a_4", "ref a_6", "ref a_5"]
308
- end
309
- end
310
- end
311
- end
312
-
313
-
314
- describe CodeYankdupInstruction do
315
- before(:each) do
316
- @context = Interpreter.new
317
- @i1 = CodeYankdupInstruction.new(@context)
318
- end
319
-
320
- it "should check its context is set" do
321
- @i1.context.should == @context
322
- end
323
-
324
- [:preconditions?, :setup, :derive, :cleanup].each do |methodName|
325
- it "should respond to \##{methodName}" do
326
- @i1.should respond_to(methodName)
327
- end
328
- end
329
-
330
- describe "\#go" do
331
- before(:each) do
332
- @i1 = CodeYankdupInstruction.new(@context)
333
- @context.clear_stacks
334
- @int1 = ValuePoint.new("int", 3)
335
- @empty_block_code_value = ValuePoint.new("code", "block {}")
336
- end
337
-
338
- describe "\#preconditions?" do
339
- it "should check that there is one :int and at least one :code" do
340
- @context.stacks[:code].push(@empty_block_code_value)
341
- @context.stacks[:int].push(@int1)
342
- @i1.preconditions?.should == true
343
- end
344
- end
345
-
346
- describe "\#cleanup" do
347
- before(:each) do
348
- @context.clear_stacks
349
- (1..5).each {|i| @context.stacks[:code].push(ValuePoint.new("int", i))}
350
- end
351
-
352
- it "should duplicate the top item if the position integer is negative" do
353
- @context.stacks[:int].push(ValuePoint.new("int", -99))
354
- @i1.go
355
- and_now = @context.stacks[:code].entries.collect {|i| i.value}
356
- and_now.should == [1,2,3,4,5, 5]
357
- end
358
-
359
- it "should duplicate the top item if the position integer is zero" do
360
- @context.stacks[:int].push(ValuePoint.new("int", 0))
361
- @i1.go
362
- and_now = @context.stacks[:code].entries.collect {|i| i.value}
363
- and_now.should == [1,2,3,4,5, 5]
364
- end
365
-
366
- it "should clone the bottom item and push it if the position is more than the stackdepth" do
367
- @context.stacks[:int].push(ValuePoint.new("int", 1000))
368
- @i1.go
369
- and_now = @context.stacks[:code].entries.collect {|i| i.value}
370
- and_now.should == [1,2,3,4,5, 1]
371
- end
372
-
373
- it "should push a copy of the indicated item to the top of the stack, counting from the 'top down'" do
374
- @context.stacks[:int].push(ValuePoint.new("int", 2))
375
- @i1.go
376
- and_now = @context.stacks[:code].entries.collect {|i| i.value}
377
- and_now.should == [1,2,3,4,5, 3]
378
-
379
- @context.stacks[:int].push(ValuePoint.new("int", 4))
380
- @i1.go
381
- and_now = @context.stacks[:code].entries.collect {|i| i.value}
382
- and_now.should == [1,2,3,4,5, 3,2]
383
- end
384
- end
385
- end
386
- end
@@ -1,95 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "../../spec_helper")
2
- include Nudge
3
-
4
- theseInstructions = [
5
- BoolPopInstruction,
6
- BoolSwapInstruction,
7
- BoolDuplicateInstruction,
8
- BoolRotateInstruction
9
- ]
10
-
11
- boolsTheyNeed = {
12
- BoolPopInstruction => 1,
13
- BoolSwapInstruction => 2,
14
- BoolDuplicateInstruction => 1,
15
- BoolRotateInstruction => 3
16
- }
17
-
18
- resultTuples = {
19
- BoolPopInstruction => {[false, true]=>[false]},
20
- BoolSwapInstruction => {[false, true]=>[true, false]},
21
- BoolDuplicateInstruction => {[true] => [true, true]},
22
- BoolRotateInstruction => {[true, false, false] => [false, false, true]}
23
- }
24
-
25
- theseInstructions.each do |instName|
26
- describe instName do
27
- before(:each) do
28
- @context = Interpreter.new
29
- @i1 = instName.new(@context)
30
- end
31
-
32
- it "should have its context right" do
33
- @i1.context.should == @context
34
- end
35
-
36
- [:preconditions?, :setup, :derive, :cleanup].each do |methodName|
37
- it "should respond to \##{methodName}" do
38
- @i1 = instName.new(@context)
39
- @i1.should respond_to(methodName)
40
- end
41
- end
42
-
43
- describe "\#go" do
44
- before(:each) do
45
- @i1 = instName.new(@context)
46
- @context.clear_stacks
47
- @bool1 = ValuePoint.new("bool", "true")
48
- end
49
-
50
- describe "\#preconditions?" do
51
- it "should check that there are enough parameters" do
52
- 10.times {@context.stacks[:bool].push(@bool1)}
53
- @i1.preconditions?.should == true
54
- end
55
-
56
- it "should raise an error if the preconditions aren't met" do
57
- @context.clear_stacks # there are no params at all
58
- lambda{@i1.preconditions?}.should raise_error(Instruction::NotEnoughStackItems)
59
- end
60
-
61
- it "should successfully run #go only if all preconditions are met" do
62
- 5.times {@context.stacks[:bool].push(@bool1)}
63
- @i1.should_receive(:cleanup)
64
- @i1.go
65
- end
66
- end
67
-
68
- describe "\#cleanup" do
69
- describe "should restructure the stack" do
70
- examples = resultTuples[instName]
71
- examples.each do |inputs, finalStackState|
72
- params = inputs.inspect
73
- expected = finalStackState.inspect
74
- it "should end up with #{expected} on the \:bool stack, starting with #{params}" do
75
- inputs.each {|i| @context.stacks[:bool].push(ValuePoint.new("bool", i.to_s))}
76
- @i1.go
77
- finalStackState.reverse.each {|i| @context.stacks[:bool].pop.value.should == i}
78
- end
79
- end
80
- end
81
- end
82
- end
83
- end
84
- end
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
@@ -1,103 +0,0 @@
1
- #encoding: utf-8
2
- require File.join(File.dirname(__FILE__), "../../spec_helper")
3
- include Nudge
4
-
5
- theseInstructions = [
6
- IntFromBoolInstruction,
7
- BoolFromIntInstruction,
8
- IntFromFloatInstruction,
9
- FloatFromIntInstruction,
10
- FloatFromBoolInstruction,
11
- BoolFromFloatInstruction,
12
- CodeFromFloatInstruction,
13
- CodeFromIntInstruction,
14
- CodeFromBoolInstruction
15
- ]
16
-
17
- itNeeds = {
18
- IntFromBoolInstruction => {"stack" => :bool, "stackname" => "bool", "becomes" => :int},
19
- IntFromFloatInstruction => {"stack" => :float, "stackname" => "float", "becomes" => :int},
20
- BoolFromIntInstruction => {"stack" => :int, "stackname" => "int", "becomes" => :bool},
21
- BoolFromFloatInstruction => {"stack" => :float, "stackname" => "float", "becomes" => :bool},
22
- FloatFromIntInstruction => {"stack" => :int, "stackname" => "int", "becomes" => :float},
23
- FloatFromBoolInstruction => {"stack" => :bool, "stackname" => "bool", "becomes" => :float},
24
- CodeFromFloatInstruction => {"stack" => :float, "stackname" => "float", "becomes" => :code},
25
- CodeFromIntInstruction => {"stack" => :int, "stackname" => "int", "becomes" => :code},
26
- CodeFromBoolInstruction => {"stack" => :bool, "stackname" => "bool", "becomes" => :code}}
27
-
28
- whatHappens = {
29
- IntFromBoolInstruction => {false => 0, true => 1},
30
- IntFromFloatInstruction => {1.1 => 1, -31.71 => -31},
31
- BoolFromIntInstruction => {0 => false, -2 => true, 122 => true},
32
- BoolFromFloatInstruction => {0.0 => false, -2.2 => true, 122.2 => true},
33
- FloatFromIntInstruction => {0 => 0.0, -2 => -2.0, 99 => 99.0},
34
- FloatFromBoolInstruction => {false => 0.0, true => 1.0},
35
- CodeFromFloatInstruction => {1.3 => "value «float»\n«float» 1.3", -2.8 => "value «float»\n«float» -2.8"},
36
- CodeFromIntInstruction => {1882 => "value «int»\n«int» 1882", -9111 => "value «int»\n«int» -9111"},
37
- CodeFromBoolInstruction => {false => "value «bool»\n«bool» false", true => "value «bool»\n«bool» true"}}
38
-
39
-
40
- theseInstructions.each do |instName|
41
- describe instName do
42
- before(:each) do
43
- @context = Interpreter.new
44
- @i1 = instName.new(@context)
45
- end
46
-
47
- it "should have the right context" do
48
- @i1.context.should == @context
49
- end
50
-
51
- [:preconditions?, :setup, :derive, :cleanup].each do |methodName|
52
- it "should respond to \##{methodName}" do
53
- @i1 = instName.new(@context)
54
- @i1.should respond_to(methodName)
55
- end
56
- end
57
-
58
- describe "\#go" do
59
- before(:each) do
60
- @i1 = instName.new(@context)
61
- @context.clear_stacks
62
- @stackSymbol = itNeeds[instName]["stack"]
63
- @stackName = itNeeds[instName]["stackname"]
64
- @someValue = "anything_at_all"
65
- @it_becomes = itNeeds[instName]["becomes"]
66
- @myStarter = ValuePoint.new(@stackName,@someValue)
67
- end
68
-
69
- describe "\#preconditions?" do
70
- it "should check that there are enough parameters" do
71
- @context.stacks[@stackSymbol].push(@myStarter)
72
- @i1.preconditions?.should == true
73
- end
74
-
75
- it "should raise an error if the preconditions aren't met" do
76
- @context.clear_stacks # there are no params at all
77
- lambda{@i1.preconditions?}.should raise_error(Instruction::NotEnoughStackItems)
78
- end
79
-
80
- it "should successfully run #go only if all preconditions are met" do
81
- @context.stacks[@stackSymbol].push(@myStarter)
82
- @i1.should_receive(:cleanup)
83
- @i1.go
84
- end
85
-
86
- describe "\#cleanup" do
87
- describe "should create and push the new (expected) value to the right place" do
88
- before(:each) do
89
- @context.clear_stacks
90
- end
91
- whatHappens[instName].each do |k,v|
92
- it "\'#{k}\' becomes #{itNeeds[instName]["becomes"]} \'#{v}\'" do
93
- @context.stacks[@stackSymbol].push(ValuePoint.new(@stackName,k))
94
- @i1.go
95
- @context.stacks[@it_becomes].peek.value.should == v
96
- end
97
- end
98
- end
99
- end
100
- end
101
- end
102
- end
103
- end
@@ -1,92 +0,0 @@
1
- require File.join(File.dirname(__FILE__), "../../spec_helper")
2
- include Nudge
3
-
4
- theseInstructions = [
5
- FloatPopInstruction,
6
- FloatSwapInstruction,
7
- FloatDuplicateInstruction,
8
- FloatRotateInstruction
9
- ]
10
-
11
- floatsTheyNeed = {
12
- FloatPopInstruction => 1,
13
- FloatSwapInstruction => 2,
14
- FloatDuplicateInstruction => 1,
15
- FloatRotateInstruction => 3
16
- }
17
-
18
- resultTuples = {
19
- FloatPopInstruction => {[1.0,2.0]=>[1.0]},
20
- FloatSwapInstruction => {[1.1,2.2]=>[2.2,1.1]},
21
- FloatDuplicateInstruction => {[33.3] => [33.3,33.3]},
22
- FloatRotateInstruction => {[1.1,2.2,3.3] => [2.2,3.3,1.1]}
23
- }
24
-
25
- theseInstructions.each do |instName|
26
- describe instName do
27
- before(:each) do
28
- @context = Interpreter.new
29
- @i1 = instName.new(@context)
30
- end
31
-
32
- it "should have its context right" do
33
- @i1.context.should == @context
34
- end
35
-
36
- [:preconditions?, :setup, :derive, :cleanup].each do |methodName|
37
- it "should respond to \##{methodName}" do
38
- @i1 = instName.new(@context)
39
- @i1.should respond_to(methodName)
40
- end
41
- end
42
-
43
- describe "\#go" do
44
- before(:each) do
45
- @i1 = instName.new(@context)
46
- @context.clear_stacks
47
- @float1 = ValuePoint.new("float", 1.0)
48
- end
49
-
50
- describe "\#preconditions?" do
51
- it "should check that there are enough parameters" do
52
- 10.times {@context.stacks[:float].push(@float1)}
53
- @i1.preconditions?.should == true
54
- end
55
-
56
- it "should raise an error if the preconditions aren't met" do
57
- @context.clear_stacks # there are no params at all
58
- lambda{@i1.preconditions?}.should raise_error(Instruction::NotEnoughStackItems)
59
- end
60
-
61
- it "should successfully run #go only if all preconditions are met" do
62
- 5.times {@context.stacks[:float].push(@float1)}
63
- @i1.should_receive(:cleanup)
64
- @i1.go
65
- end
66
- end
67
-
68
- describe "\#cleanup" do
69
- describe "should restructure the stack" do
70
- examples = resultTuples[instName]
71
- examples.each do |inputs, finalStackState|
72
- params = inputs.inspect
73
- expected = finalStackState.inspect
74
- it "should end up with #{expected} on the \:float stack, starting with #{params}" do
75
- inputs.each {|i| @context.stacks[:float].push(ValuePoint.new("float", i))}
76
- @i1.go
77
- finalStackState.reverse.each {|i| @context.stacks[:float].pop.value.should == i}
78
- end
79
- end
80
- end
81
- end
82
- end
83
- end
84
- end
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-