heckle 1.4.3 → 2.0.0.b1

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,33 @@
1
+ class MiniTestHeckler < Heckle
2
+ def initialize(class_or_module, method, options)
3
+ Dir.glob(options[:test_pattern]).each {|t| load File.expand_path(t) }
4
+
5
+ super(class_or_module, method, options[:nodes])
6
+ end
7
+
8
+ def tests_pass?
9
+ silence do
10
+ MiniTest::Unit.runner = nil
11
+
12
+ MiniTest::Unit.new.run
13
+
14
+ runner = MiniTest::Unit.runner
15
+
16
+ runner.failures == 0 && runner.errors == 0
17
+ end
18
+ end
19
+
20
+ # TODO: Windows.
21
+ def silence
22
+ return yield if Heckle.debug
23
+
24
+ begin
25
+ original = MiniTest::Unit.output
26
+ MiniTest::Unit.output = File.open("/dev/null", "w")
27
+
28
+ yield
29
+ ensure
30
+ MiniTest::Unit.output = original
31
+ end
32
+ end
33
+ end
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'test/unit/autorunner'
4
3
  require 'test/unit/testcase'
5
4
  require 'heckle'
6
5
  require 'zentest_mapping'
@@ -0,0 +1,200 @@
1
+ class HeckleDummy
2
+ attr_accessor :names
3
+
4
+ def initialize
5
+ @names = []
6
+ end
7
+
8
+ def uses_call
9
+ some_func + some_other_func
10
+ end
11
+
12
+ def uses_callblock
13
+ x.y { 1 }
14
+ end
15
+
16
+ def uses_cvasgn
17
+ @@cvar = 5
18
+ @@cvar = nil
19
+ end
20
+
21
+ def uses_dasgn
22
+ loop do |dvar|
23
+ loop do
24
+ dvar = 5
25
+ dvar = nil
26
+ end
27
+ end
28
+ end
29
+
30
+ def uses_dasgncurr
31
+ loop do |dvar|
32
+ dvar = 5
33
+ dvar = nil
34
+ end
35
+ end
36
+
37
+ def uses_iasgn
38
+ @ivar = 5
39
+ @ivar = nil
40
+ end
41
+
42
+ def uses_gasgn
43
+ $gvar = 5
44
+ $gvar = nil
45
+ end
46
+
47
+ def uses_lasgn
48
+ lvar = 5
49
+ lvar = nil
50
+ end
51
+
52
+ def uses_masgn
53
+ @a, $b, c = 5, 6, 7
54
+ end
55
+
56
+ def uses_many_things
57
+ i = 1
58
+ while i < 10
59
+ i += 1
60
+ until some_func
61
+ some_other_func
62
+ end
63
+ return true if "hi there" == "changeling"
64
+ return false
65
+ end
66
+ i
67
+ end
68
+
69
+ def uses_while
70
+ while some_func
71
+ some_other_func
72
+ end
73
+ end
74
+
75
+ def uses_until
76
+ until some_func
77
+ some_other_func
78
+ end
79
+ end
80
+
81
+ def uses_numeric_literals
82
+ i = 1
83
+ i += 2147483648
84
+ i -= 3.5
85
+ end
86
+
87
+ def uses_strings
88
+ @names << "Hello, Robert"
89
+ @names << "Hello, Jeff"
90
+ @names << "Hi, Frank"
91
+ end
92
+
93
+ def uses_different_types
94
+ i = 1
95
+ b = "Hello, Joe"
96
+ c = 3.3
97
+ end
98
+
99
+ def uses_literal
100
+ i = 1
101
+ end
102
+
103
+ def uses_same_literal
104
+ i = 1
105
+ i = 1
106
+ i = 1
107
+ end
108
+
109
+ def uses_if
110
+ if some_func
111
+ if some_other_func
112
+ return
113
+ end
114
+ end
115
+ end
116
+
117
+ def uses_boolean
118
+ a = true
119
+ b = false
120
+ end
121
+
122
+ def uses_unless
123
+ unless true
124
+ if false
125
+ return
126
+ end
127
+ end
128
+ end
129
+
130
+ def uses_symbols
131
+ i = :blah
132
+ i = :blah
133
+ i = :and_blah
134
+ end
135
+
136
+ def uses_regexes
137
+ i = /a.*/
138
+ i = /c{2,4}+/
139
+ i = /123/
140
+ end
141
+
142
+ def uses_ranges
143
+ i = 6..100
144
+ i = -1..9
145
+ i = 1..4
146
+ end
147
+
148
+ def uses_nothing
149
+ end
150
+
151
+ def uses_iter
152
+ x = [ 1, 2, 3 ]
153
+ x.each { |y| y }
154
+ end
155
+
156
+ def self.is_a_klass_method?
157
+ true
158
+ end
159
+
160
+ module OuterNesting
161
+ def foo
162
+ -1
163
+ end
164
+
165
+ module InnerNesting
166
+ def foo
167
+ -2
168
+ end
169
+
170
+ class InnerClass
171
+ def bar
172
+ -1
173
+ end
174
+ end
175
+ end
176
+
177
+ module InnerNesting
178
+ def foo
179
+ -3
180
+ end
181
+
182
+ class InnerClass
183
+ module DecoyNesting
184
+ def foo
185
+ -4
186
+ end
187
+ end
188
+
189
+ def foo
190
+ 1337
191
+ end
192
+ end
193
+ end
194
+ end
195
+
196
+ private
197
+
198
+ def some_func; end
199
+ def some_other_func; end
200
+ end
@@ -96,6 +96,10 @@ class Heckled
96
96
  c = 3.3
97
97
  end
98
98
 
99
+ def uses_literal
100
+ i = 1
101
+ end
102
+
99
103
  def uses_same_literal
100
104
  i = 1
101
105
  i = 1
@@ -0,0 +1,57 @@
1
+ = minitest_project
2
+
3
+ * FIX (url)
4
+
5
+ == DESCRIPTION:
6
+
7
+ FIX (describe your package)
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * FIX (list of features or problems)
12
+
13
+ == SYNOPSIS:
14
+
15
+ FIX (code sample of usage)
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * FIX (list of requirements)
20
+
21
+ == INSTALL:
22
+
23
+ * FIX (sudo gem install, anything else)
24
+
25
+ == DEVELOPERS:
26
+
27
+ After checking out the source, run:
28
+
29
+ $ rake newb
30
+
31
+ This task will install any missing dependencies, run the tests/specs,
32
+ and generate the RDoc.
33
+
34
+ == LICENSE:
35
+
36
+ (The MIT License)
37
+
38
+ Copyright (c) 2012 FIX
39
+
40
+ Permission is hereby granted, free of charge, to any person obtaining
41
+ a copy of this software and associated documentation files (the
42
+ 'Software'), to deal in the Software without restriction, including
43
+ without limitation the rights to use, copy, modify, merge, publish,
44
+ distribute, sublicense, and/or sell copies of the Software, and to
45
+ permit persons to whom the Software is furnished to do so, subject to
46
+ the following conditions:
47
+
48
+ The above copyright notice and this permission notice shall be
49
+ included in all copies or substantial portions of the Software.
50
+
51
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
52
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
53
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
54
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
55
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
56
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
57
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new
4
+
5
+ namespace :heckle do
6
+ desc "Run heckle with all tests."
7
+ task :pass do
8
+ puts `../../../bin/heckle Doubler double`
9
+ end
10
+
11
+ desc "Run heckle with some test."
12
+ task :fail do
13
+ puts `../../../bin/heckle Doubler double --tests test/test_doubler_with_a_number.rb`
14
+ end
15
+ end
16
+
17
+ task :default => :test
@@ -0,0 +1,9 @@
1
+ class Doubler
2
+ def double x
3
+ if Numeric === x
4
+ x * 2
5
+ else
6
+ "NaN"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ require "minitest/autorun"
2
+ require "doubler"
3
+
4
+ class TestDoublerWithANumber < MiniTest::Unit::TestCase
5
+ def setup
6
+ @doubler = Doubler.new
7
+ end
8
+
9
+ def test_double_with_a_number
10
+ assert_equal 4, @doubler.double(2)
11
+ end
12
+ end
@@ -0,0 +1,16 @@
1
+ require "minitest/autorun"
2
+ require "doubler"
3
+
4
+ class TestDoublerWithBadInput < MiniTest::Unit::TestCase
5
+ def setup
6
+ @doubler = Doubler.new
7
+ end
8
+
9
+ def test_doubler_with_a_string
10
+ assert_equal "NaN", @doubler.double("2")
11
+ end
12
+
13
+ def test_doubler_with_nil
14
+ assert_equal "NaN", @doubler.double(nil)
15
+ end
16
+ end
@@ -1,10 +1,5 @@
1
- $:.unshift(File.dirname(__FILE__) + '/fixtures')
2
- $:.unshift(File.dirname(__FILE__) + '/../lib')
3
-
4
- require 'test/unit/testcase'
5
- require 'test/unit' if $0 == __FILE__
6
- require 'test_unit_heckler'
7
- require 'heckled'
1
+ require 'fixtures/heckle_dummy'
2
+ require 'heckle'
8
3
 
9
4
  class TestHeckler < Heckle
10
5
  def rand(*args)
@@ -22,60 +17,44 @@ class TestHeckler < Heckle
22
17
  def rand_symbol
23
18
  :"l33t h4x0r"
24
19
  end
25
- end
26
20
 
27
- class HeckleTestCase < Test::Unit::TestCase
28
- unless defined? Mini then
29
- undef_method :default_test
30
- alias :refute_equal :assert_not_equal
21
+ # HAX
22
+ def expand_dirs_to_files(*)
23
+ super('test/fixtures/heckle_dummy.rb')
31
24
  end
25
+ end
32
26
 
27
+ class HeckleTestCase < MiniTest::Unit::TestCase
33
28
  def setup
29
+ @klass ||= "HeckleDummy"
34
30
  @nodes ||= Heckle::MUTATABLE_NODES
35
- unless defined? @hecklee then
36
- data = self.class.name.sub(/HeckleTestCase/, '').sub(/TestHeckle/, '')
37
- data = data.gsub(/([A-Z])/, '_\1').downcase
38
- data = "_many_things" if data.empty?
39
- @hecklee = "uses#{data}"
40
- end
31
+ @method_heckled ||= 'uses_many_things'
41
32
 
42
- @heckler = TestHeckler.new("Heckled", @hecklee, @nodes) rescue nil
33
+ @heckler = TestHeckler.new(@klass, @method_heckled, @nodes)
43
34
  end
44
35
 
45
36
  def teardown
46
37
  @heckler.reset if defined?(@heckler) && @heckler
47
38
  end
48
- end
49
-
50
- class LiteralHeckleTestCase < HeckleTestCase
51
- def setup
52
- @nodes = s(:lit, :str)
53
- super
54
- end
55
-
56
- def toggle(value, toggle)
57
- toggle ? self.class::TOGGLE_VALUE : value
58
- end
59
-
60
- def test_default_structure
61
- return if self.class == LiteralHeckleTestCase
62
- assert_equal util_expected, @heckler.current_tree
63
- end
64
39
 
65
- def test_should_iterate_mutations
66
- return if self.class == LiteralHeckleTestCase
67
- @heckler.process(@heckler.current_tree)
68
- assert_equal util_expected(1), @heckler.current_tree
40
+ def assert_mutations expected, heckle
41
+ initial = heckle.current_tree.deep_clone
42
+ mutations = []
69
43
 
70
- @heckler.reset_tree
44
+ begin
45
+ heckle.process(heckle.current_tree)
46
+ mutant = heckle.current_tree
47
+ mutations << mutant
48
+ heckle.reset_tree
49
+ end until initial == mutant
71
50
 
72
- @heckler.process(@heckler.current_tree)
73
- assert_equal util_expected(2), @heckler.current_tree
51
+ mutations.delete(initial)
74
52
 
75
- @heckler.reset_tree
76
-
77
- @heckler.process(@heckler.current_tree)
78
- assert_equal util_expected(3), @heckler.current_tree
53
+ # HAX: Sorting an array of Sexps blows up in some cases.
54
+ assert_equal expected.map {|sexp| sexp.to_s }.sort,
55
+ mutations.map {|sexp| sexp.to_s }.sort,
56
+ [ "expected(#{expected.size}):", (expected - mutations).map {|m| m.pretty_inspect},
57
+ "mutations(#{mutations.size}):", (mutations - expected).map {|m| m.pretty_inspect} ].join("\n")
79
58
  end
80
59
  end
81
60
 
@@ -212,99 +191,306 @@ class TestHeckle < HeckleTestCase
212
191
  end
213
192
 
214
193
  class TestHeckleNumericLiterals < HeckleTestCase
215
- def toggle(value, toggle)
216
- value + (toggle ? 5 : 0)
194
+ def setup
195
+ @method_heckled = "uses_numeric_literals"
196
+ @nodes = s(:lit, :str)
197
+ super
217
198
  end
218
199
 
219
- def util_expected(n)
220
- s(:defn, :uses_numeric_literals,
200
+ def test_numeric_literals_original_tree
201
+ expected = s(:defn, :uses_numeric_literals,
221
202
  s(:args),
222
203
  s(:scope,
223
204
  s(:block,
224
- s(:lasgn, :i, s(:lit, toggle(1, 1 == n))),
205
+ s(:lasgn, :i, s(:lit, 1)),
225
206
  s(:lasgn, :i, s(:call, s(:lvar, :i), :+,
226
- s(:arglist, s(:lit, toggle(2147483648, 2 == n))))),
227
- s(:lasgn, :i, s(:call, s(:lvar, :i), :-, s(:arglist, s(:lit, toggle(3.5, 3 == n))))))))
207
+ s(:arglist, s(:lit, 2147483648)))),
208
+ s(:lasgn, :i, s(:call, s(:lvar, :i), :-, s(:arglist, s(:lit, 3.5)))))))
209
+
210
+ assert_equal expected, @heckler.current_tree
211
+ end
212
+
213
+ def test_numeric_literals_mutations
214
+ expected = [
215
+ s(:defn, :uses_numeric_literals,
216
+ s(:args),
217
+ s(:scope,
218
+ s(:block,
219
+ s(:lasgn, :i, s(:lit, 6)),
220
+ s(:lasgn, :i, s(:call, s(:lvar, :i), :+,
221
+ s(:arglist, s(:lit, 2147483648)))),
222
+ s(:lasgn, :i, s(:call, s(:lvar, :i), :-, s(:arglist, s(:lit, 3.5))))))),
223
+ s(:defn, :uses_numeric_literals,
224
+ s(:args),
225
+ s(:scope,
226
+ s(:block,
227
+ s(:lasgn, :i, s(:lit, 1)),
228
+ s(:lasgn, :i, s(:call, s(:lvar, :i), :+,
229
+ s(:arglist, s(:lit, 2147483653)))),
230
+ s(:lasgn, :i, s(:call, s(:lvar, :i), :-, s(:arglist, s(:lit, 3.5))))))),
231
+ s(:defn, :uses_numeric_literals,
232
+ s(:args),
233
+ s(:scope,
234
+ s(:block,
235
+ s(:lasgn, :i, s(:lit, 1)),
236
+ s(:lasgn, :i, s(:call, s(:lvar, :i), :+,
237
+ s(:arglist, s(:lit, 2147483648)))),
238
+ s(:lasgn, :i, s(:call, s(:lvar, :i), :-, s(:arglist, s(:lit, 8.5))))))),
239
+ ]
240
+
241
+ assert_mutations expected, @heckler
228
242
  end
229
243
  end
230
244
 
231
- class TestHeckleSymbols < LiteralHeckleTestCase
232
- TOGGLE_VALUE = :"l33t h4x0r"
245
+ class TestHeckleSymbols < HeckleTestCase
246
+ def setup
247
+ @method_heckled = "uses_symbols"
248
+ @nodes = s(:lit, :str)
249
+ super
250
+ end
233
251
 
234
- def util_expected(n = nil)
235
- s(:defn, :uses_symbols,
252
+ def test_symbols_original_tree
253
+ expected = s(:defn, :uses_symbols,
236
254
  s(:args),
237
255
  s(:scope,
238
256
  s(:block,
239
- s(:lasgn, :i, s(:lit, toggle(:blah, n == 1))),
240
- s(:lasgn, :i, s(:lit, toggle(:blah, n == 2))),
241
- s(:lasgn, :i, s(:lit, toggle(:and_blah, n == 3))))))
257
+ s(:lasgn, :i, s(:lit, :blah)),
258
+ s(:lasgn, :i, s(:lit, :blah)),
259
+ s(:lasgn, :i, s(:lit, :and_blah)))))
260
+
261
+ assert_equal expected, @heckler.current_tree
262
+ end
263
+
264
+ def test_symbols_mutations
265
+ expected = [
266
+ s(:defn, :uses_symbols,
267
+ s(:args),
268
+ s(:scope,
269
+ s(:block,
270
+ s(:lasgn, :i, s(:lit, :"l33t h4x0r")),
271
+ s(:lasgn, :i, s(:lit, :blah)),
272
+ s(:lasgn, :i, s(:lit, :and_blah))))),
273
+ s(:defn, :uses_symbols,
274
+ s(:args),
275
+ s(:scope,
276
+ s(:block,
277
+ s(:lasgn, :i, s(:lit, :blah)),
278
+ s(:lasgn, :i, s(:lit, :"l33t h4x0r")),
279
+ s(:lasgn, :i, s(:lit, :and_blah))))),
280
+ s(:defn, :uses_symbols,
281
+ s(:args),
282
+ s(:scope,
283
+ s(:block,
284
+ s(:lasgn, :i, s(:lit, :blah)),
285
+ s(:lasgn, :i, s(:lit, :blah)),
286
+ s(:lasgn, :i, s(:lit, :"l33t h4x0r"))))),
287
+ ]
288
+
289
+ assert_mutations expected, @heckler
242
290
  end
243
291
  end
244
292
 
245
- class TestHeckleRegexes < LiteralHeckleTestCase
246
- TOGGLE_VALUE = /l33t\ h4x0r/
293
+ class TestHeckleRegexes < HeckleTestCase
294
+ def setup
295
+ @method_heckled = "uses_regexes"
296
+ @nodes = s(:lit, :str)
297
+ super
298
+ end
247
299
 
248
- def util_expected(n = nil)
249
- s(:defn, :uses_regexes,
300
+ def test_regexes_original_tree
301
+ expected = s(:defn, :uses_regexes,
250
302
  s(:args),
251
303
  s(:scope,
252
304
  s(:block,
253
- s(:lasgn, :i, s(:lit, toggle(/a.*/, n == 1))),
254
- s(:lasgn, :i, s(:lit, toggle(/c{2,4}+/, n == 2))),
255
- s(:lasgn, :i, s(:lit, toggle(/123/, n == 3))))))
305
+ s(:lasgn, :i, s(:lit, /a.*/)),
306
+ s(:lasgn, :i, s(:lit, /c{2,4}+/)),
307
+ s(:lasgn, :i, s(:lit, /123/)))))
308
+
309
+ assert_equal expected, @heckler.original_tree
310
+ end
311
+
312
+ def test_regexes_mutuations
313
+ expected = [
314
+ s(:defn, :uses_regexes,
315
+ s(:args),
316
+ s(:scope,
317
+ s(:block,
318
+ s(:lasgn, :i, s(:lit, /l33t\ h4x0r/)),
319
+ s(:lasgn, :i, s(:lit, /c{2,4}+/)),
320
+ s(:lasgn, :i, s(:lit, /123/))))),
321
+ s(:defn, :uses_regexes,
322
+ s(:args),
323
+ s(:scope,
324
+ s(:block,
325
+ s(:lasgn, :i, s(:lit, /a.*/)),
326
+ s(:lasgn, :i, s(:lit, /l33t\ h4x0r/)),
327
+ s(:lasgn, :i, s(:lit, /123/))))),
328
+ s(:defn, :uses_regexes,
329
+ s(:args),
330
+ s(:scope,
331
+ s(:block,
332
+ s(:lasgn, :i, s(:lit, /a.*/)),
333
+ s(:lasgn, :i, s(:lit, /c{2,4}+/)),
334
+ s(:lasgn, :i, s(:lit, /l33t\ h4x0r/))))),
335
+ ]
336
+
337
+ assert_mutations expected, @heckler
256
338
  end
257
339
  end
258
340
 
259
- class TestHeckleRanges < LiteralHeckleTestCase
260
- TOGGLE_VALUE = 5..10
341
+ class TestHeckleRanges < HeckleTestCase
342
+ def setup
343
+ @method_heckled = "uses_ranges"
344
+ @nodes = s(:lit, :str)
345
+ super
346
+ end
261
347
 
262
- def util_expected(n = nil)
263
- s(:defn, :uses_ranges,
348
+ def test_ranges_original_tree
349
+ expected = s(:defn, :uses_ranges,
264
350
  s(:args),
265
351
  s(:scope,
266
352
  s(:block,
267
- s(:lasgn, :i, s(:lit, toggle(6..100, n == 1))),
268
- s(:lasgn, :i, s(:lit, toggle(-1..9, n == 2))),
269
- s(:lasgn, :i, s(:lit, toggle(1..4, n == 3))))))
353
+ s(:lasgn, :i, s(:lit, 6..100)),
354
+ s(:lasgn, :i, s(:lit, -1..9)),
355
+ s(:lasgn, :i, s(:lit, 1..4)))))
356
+
357
+ assert_equal expected, @heckler.current_tree
358
+ end
359
+
360
+ def test_ranges_mutations
361
+ expected = [
362
+ s(:defn, :uses_ranges,
363
+ s(:args),
364
+ s(:scope,
365
+ s(:block,
366
+ s(:lasgn, :i, s(:lit, 5..10)),
367
+ s(:lasgn, :i, s(:lit, -1..9)),
368
+ s(:lasgn, :i, s(:lit, 1..4))))),
369
+ s(:defn, :uses_ranges,
370
+ s(:args),
371
+ s(:scope,
372
+ s(:block,
373
+ s(:lasgn, :i, s(:lit, 6..100)),
374
+ s(:lasgn, :i, s(:lit, 5..10)),
375
+ s(:lasgn, :i, s(:lit, 1..4))))),
376
+ s(:defn, :uses_ranges,
377
+ s(:args),
378
+ s(:scope,
379
+ s(:block,
380
+ s(:lasgn, :i, s(:lit, 6..100)),
381
+ s(:lasgn, :i, s(:lit, -1..9)),
382
+ s(:lasgn, :i, s(:lit, 5..10))))),
383
+ ]
384
+
385
+ assert_mutations expected, @heckler
386
+
270
387
  end
271
388
  end
272
389
 
273
- class TestHeckleSameLiteral < LiteralHeckleTestCase
274
- TOGGLE_VALUE = 6
390
+ class TestHeckleSameLiteral < HeckleTestCase
391
+ def setup
392
+ @method_heckled = "uses_same_literal"
393
+ @nodes = s(:lit, :str)
394
+ super
395
+ end
275
396
 
276
- def util_expected(n = nil)
277
- s(:defn, :uses_same_literal,
397
+ def test_same_literal_original_tree
398
+ expected = s(:defn, :uses_same_literal,
278
399
  s(:args),
279
400
  s(:scope,
280
401
  s(:block,
281
- s(:lasgn, :i, s(:lit, toggle(1, n == 1))),
282
- s(:lasgn, :i, s(:lit, toggle(1, n == 2))),
283
- s(:lasgn, :i, s(:lit, toggle(1, n == 3))))))
402
+ s(:lasgn, :i, s(:lit, 1)),
403
+ s(:lasgn, :i, s(:lit, 1)),
404
+ s(:lasgn, :i, s(:lit, 1)))))
405
+
406
+ assert_equal expected, @heckler.current_tree
407
+ end
408
+
409
+ def test_same_literal_mutations
410
+ expected = [
411
+ s(:defn, :uses_same_literal,
412
+ s(:args),
413
+ s(:scope,
414
+ s(:block,
415
+ s(:lasgn, :i, s(:lit, 6)),
416
+ s(:lasgn, :i, s(:lit, 1)),
417
+ s(:lasgn, :i, s(:lit, 1))))),
418
+ s(:defn, :uses_same_literal,
419
+ s(:args),
420
+ s(:scope,
421
+ s(:block,
422
+ s(:lasgn, :i, s(:lit, 1)),
423
+ s(:lasgn, :i, s(:lit, 6)),
424
+ s(:lasgn, :i, s(:lit, 1))))),
425
+ s(:defn, :uses_same_literal,
426
+ s(:args),
427
+ s(:scope,
428
+ s(:block,
429
+ s(:lasgn, :i, s(:lit, 1)),
430
+ s(:lasgn, :i, s(:lit, 1)),
431
+ s(:lasgn, :i, s(:lit, 6))))),
432
+ ]
433
+
434
+ assert_mutations expected, @heckler
284
435
  end
285
436
  end
286
437
 
287
- class TestHeckleStrings < LiteralHeckleTestCase
288
- TOGGLE_VALUE = "l33t h4x0r"
438
+ class TestHeckleStrings < HeckleTestCase
439
+ def setup
440
+ @method_heckled = "uses_strings"
441
+ @nodes = s(:lit, :str)
442
+ super
443
+ end
289
444
 
290
- def util_expected(n = nil)
291
- s(:defn, :uses_strings,
445
+ def test_strings_original_tree
446
+ expected = s(:defn, :uses_strings,
292
447
  s(:args),
293
448
  s(:scope,
294
449
  s(:block,
295
- s(:call, s(:ivar, :@names), :<<, s(:arglist, s(:str, toggle("Hello, Robert", n == 1)))),
296
- s(:call, s(:ivar, :@names), :<<, s(:arglist, s(:str, toggle("Hello, Jeff", n == 2)))),
297
- s(:call, s(:ivar, :@names), :<<, s(:arglist, s(:str, toggle("Hi, Frank", n == 3)))))))
450
+ s(:call, s(:ivar, :@names), :<<, s(:arglist, s(:str, "Hello, Robert"))),
451
+ s(:call, s(:ivar, :@names), :<<, s(:arglist, s(:str, "Hello, Jeff"))),
452
+ s(:call, s(:ivar, :@names), :<<, s(:arglist, s(:str, "Hi, Frank"))))))
453
+
454
+ assert_equal expected, @heckler.current_tree
455
+ end
456
+
457
+ def test_strings_mutations
458
+ expected = [
459
+ s(:defn, :uses_strings,
460
+ s(:args),
461
+ s(:scope,
462
+ s(:block,
463
+ s(:call, s(:ivar, :@names), :<<, s(:arglist, s(:str, "l33t h4x0r"))),
464
+ s(:call, s(:ivar, :@names), :<<, s(:arglist, s(:str, "Hello, Jeff"))),
465
+ s(:call, s(:ivar, :@names), :<<, s(:arglist, s(:str, "Hi, Frank")))))),
466
+ s(:defn, :uses_strings,
467
+ s(:args),
468
+ s(:scope,
469
+ s(:block,
470
+ s(:call, s(:ivar, :@names), :<<, s(:arglist, s(:str, "Hello, Robert"))),
471
+ s(:call, s(:ivar, :@names), :<<, s(:arglist, s(:str, "l33t h4x0r"))),
472
+ s(:call, s(:ivar, :@names), :<<, s(:arglist, s(:str, "Hi, Frank")))))),
473
+ s(:defn, :uses_strings,
474
+ s(:args),
475
+ s(:scope,
476
+ s(:block,
477
+ s(:call, s(:ivar, :@names), :<<, s(:arglist, s(:str, "Hello, Robert"))),
478
+ s(:call, s(:ivar, :@names), :<<, s(:arglist, s(:str, "Hello, Jeff"))),
479
+ s(:call, s(:ivar, :@names), :<<, s(:arglist, s(:str, "l33t h4x0r")))))),
480
+ ]
481
+
482
+ assert_mutations expected, @heckler
298
483
  end
299
484
  end
300
485
 
301
486
  class TestHeckleIf < HeckleTestCase
302
487
  def setup
488
+ @method_heckled = "uses_if"
303
489
  @nodes = s(:if)
304
490
  super
305
491
  end
306
492
 
307
- def test_default_structure
493
+ def test_if_original_tree
308
494
  expected = s(:defn, :uses_if,
309
495
  s(:args),
310
496
  s(:scope,
@@ -317,421 +503,513 @@ class TestHeckleIf < HeckleTestCase
317
503
  assert_equal expected, @heckler.current_tree
318
504
  end
319
505
 
320
- def test_should_flip_if_to_unless
321
- expected = s(:defn, :uses_if,
322
- s(:args),
323
- s(:scope,
324
- s(:block,
325
- s(:if,
326
- s(:call, nil, :some_func, s(:arglist)),
327
- s(:if, s(:call, nil, :some_other_func, s(:arglist)), nil, s(:return)),
328
- nil))))
329
-
330
- @heckler.process(@heckler.current_tree)
331
- assert_equal expected, @heckler.current_tree
332
-
333
- @heckler.reset_tree
334
-
335
- expected = s(:defn, :uses_if,
336
- s(:args),
337
- s(:scope,
338
- s(:block,
339
- s(:if,
340
- s(:call, nil, :some_func, s(:arglist)),
341
- nil,
342
- s(:if, s(:call, nil, :some_other_func, s(:arglist)), s(:return), nil)))))
343
-
344
- @heckler.process(@heckler.current_tree)
345
- assert_equal expected, @heckler.current_tree
506
+ def test_if_mutations
507
+ expected = [
508
+ s(:defn,
509
+ :uses_if,
510
+ s(:args),
511
+ s(:scope,
512
+ s(:block,
513
+ s(:if,
514
+ s(:call, nil, :some_func, s(:arglist)),
515
+ nil,
516
+ s(:if, s(:call, nil, :some_other_func, s(:arglist)), s(:return), nil))))),
517
+ s(:defn,
518
+ :uses_if,
519
+ s(:args),
520
+ s(:scope,
521
+ s(:block,
522
+ s(:if,
523
+ s(:call, nil, :some_func, s(:arglist)),
524
+ s(:if, s(:call, nil, :some_other_func, s(:arglist)), nil, s(:return)),
525
+ nil))))
526
+ ]
527
+
528
+ assert_mutations expected, @heckler
346
529
  end
347
530
  end
348
531
 
349
532
  class TestHeckleBoolean < HeckleTestCase
350
-
351
533
  def setup
534
+ @method_heckled = "uses_boolean"
352
535
  @nodes = s(:true, :false)
353
536
  super
354
537
  end
355
538
 
356
- def toggle(value, toggle)
357
- (toggle ? ! value : value).to_s.intern
358
- end
359
-
360
- def util_expected(n = nil)
361
- s(:defn, :uses_boolean,
362
- s(:args),
363
- s(:scope,
364
- s(:block,
365
- s(:lasgn, :a, s(toggle(true, n == 1))),
366
- s(:lasgn, :b, s(toggle(false, n == 2))))))
367
- end
539
+ def test_boolean_original_tree
540
+ expected = s(:defn, :uses_boolean,
541
+ s(:args),
542
+ s(:scope,
543
+ s(:block,
544
+ s(:lasgn, :a, s(:true)),
545
+ s(:lasgn, :b, s(:false)))))
368
546
 
369
- def test_default_structure
370
- assert_equal util_expected, @heckler.current_tree
547
+ assert_equal expected, @heckler.current_tree
371
548
  end
372
549
 
373
- def test_should_flip_true_to_false_and_false_to_true
374
- @heckler.process(@heckler.current_tree)
375
- assert_equal util_expected(1), @heckler.current_tree
376
-
377
- @heckler.reset_tree
378
-
379
- @heckler.process(@heckler.current_tree)
380
- assert_equal util_expected(2), @heckler.current_tree
550
+ def test_boolean_mutations
551
+ expected = [
552
+ s(:defn, :uses_boolean,
553
+ s(:args),
554
+ s(:scope,
555
+ s(:block,
556
+ s(:lasgn, :a, s(:false)),
557
+ s(:lasgn, :b, s(:false))))),
558
+ s(:defn, :uses_boolean,
559
+ s(:args),
560
+ s(:scope,
561
+ s(:block,
562
+ s(:lasgn, :a, s(:true)),
563
+ s(:lasgn, :b, s(:true))))),
564
+ ]
565
+
566
+ assert_mutations expected, @heckler
381
567
  end
382
568
  end
383
569
 
384
570
  class TestHeckleWhile < HeckleTestCase
385
571
  def setup
572
+ @method_heckled = "uses_while"
386
573
  @nodes = s(:while)
387
574
  super
388
575
  end
389
576
 
390
- def test_default_structure
391
- expected = s(:defn, :uses_while,
392
- s(:args),
393
- s(:scope,
394
- s(:block,
395
- s(:while, s(:call, nil, :some_func, s(:arglist)),
396
- s(:call, nil, :some_other_func, s(:arglist)), true))))
577
+ def test_while_original_tree
578
+ expected = s(:defn, :uses_while,
579
+ s(:args),
580
+ s(:scope,
581
+ s(:block,
582
+ s(:while, s(:call, nil, :some_func, s(:arglist)),
583
+ s(:call, nil, :some_other_func, s(:arglist)), true))))
584
+
397
585
  assert_equal expected, @heckler.current_tree
398
586
  end
399
587
 
400
- def test_flips_while_to_until
401
- expected = s(:defn, :uses_while,
402
- s(:args),
403
- s(:scope,
404
- s(:block,
405
- s(:until, s(:call, nil, :some_func, s(:arglist)),
406
- s(:call, nil, :some_other_func, s(:arglist)), true))))
407
- @heckler.process(@heckler.current_tree)
408
- assert_equal expected, @heckler.current_tree
588
+ def test_while_mutations
589
+ expected = [
590
+ s(:defn, :uses_while,
591
+ s(:args),
592
+ s(:scope,
593
+ s(:block,
594
+ s(:until, s(:call, nil, :some_func, s(:arglist)),
595
+ s(:call, nil, :some_other_func, s(:arglist)), true))))]
596
+
597
+ assert_mutations expected, @heckler
409
598
  end
410
599
  end
411
600
 
412
601
  class TestHeckleUntil < HeckleTestCase
413
602
  def setup
603
+ @method_heckled = "uses_until"
414
604
  @nodes = s(:until)
415
605
  super
416
606
  end
417
607
 
418
- def test_default_structure
419
- expected = s(:defn, :uses_until,
420
- s(:args),
421
- s(:scope,
422
- s(:block,
423
- s(:until, s(:call, nil, :some_func, s(:arglist)),
424
- s(:call, nil, :some_other_func, s(:arglist)), true))))
608
+ def test_until_original_tree
609
+ expected = s(:defn, :uses_until,
610
+ s(:args),
611
+ s(:scope,
612
+ s(:block,
613
+ s(:until, s(:call, nil, :some_func, s(:arglist)),
614
+ s(:call, nil, :some_other_func, s(:arglist)), true))))
615
+
425
616
  assert_equal expected, @heckler.current_tree
426
617
  end
427
618
 
428
- def test_flips_until_to_while
429
- expected = s(:defn, :uses_until,
430
- s(:args),
431
- s(:scope,
432
- s(:block,
433
- s(:while, s(:call, nil, :some_func, s(:arglist)),
434
- s(:call, nil, :some_other_func, s(:arglist)), true))))
435
- @heckler.process(@heckler.current_tree)
436
- assert_equal expected, @heckler.current_tree
619
+ def test_until_mutations
620
+ expected = [
621
+ s(:defn, :uses_until,
622
+ s(:args),
623
+ s(:scope,
624
+ s(:block,
625
+ s(:while, s(:call, nil, :some_func, s(:arglist)),
626
+ s(:call, nil, :some_other_func, s(:arglist)), true))))]
627
+
628
+ assert_mutations expected, @heckler
437
629
  end
438
630
  end
439
631
 
440
632
  class TestHeckleCall < HeckleTestCase
441
-
442
- def test_call_deleted
443
- expected = s(:defn, :uses_call,
444
- s(:args),
445
- s(:scope,
446
- s(:block,
447
- s(:nil))))
448
-
449
- @heckler.process(@heckler.current_tree) # some_func
450
- @heckler.reset_tree
451
- @heckler.process(@heckler.current_tree) # some_other_func
452
- @heckler.reset_tree
453
- @heckler.process(@heckler.current_tree) # +
454
- assert_equal expected, @heckler.current_tree
633
+ def setup
634
+ @method_heckled = "uses_call"
635
+ super
455
636
  end
456
637
 
457
- def test_default_structure
458
- expected = s(:defn, :uses_call,
459
- s(:args),
460
- s(:scope,
461
- s(:block,
462
- s(:call,
463
- s(:call, nil, :some_func, s(:arglist)),
464
- :+,
465
- s(:arglist, s(:call, nil, :some_other_func, s(:arglist)))))))
638
+ def test_call_original_tree
639
+ expected = s(:defn, :uses_call,
640
+ s(:args),
641
+ s(:scope,
642
+ s(:block,
643
+ s(:call,
644
+ s(:call, nil, :some_func, s(:arglist)),
645
+ :+,
646
+ s(:arglist, s(:call, nil, :some_other_func, s(:arglist)))))))
466
647
 
467
648
  assert_equal expected, @heckler.current_tree
468
649
  end
469
650
 
651
+ def test_call_mutations
652
+ expected = [
653
+ s(:defn, :uses_call,
654
+ s(:args),
655
+ s(:scope,
656
+ s(:block,
657
+ s(:call,
658
+ s(:call, nil, :some_func, s(:arglist)),
659
+ :+,
660
+ s(:arglist, s(:nil)))))),
661
+ s(:defn, :uses_call,
662
+ s(:args),
663
+ s(:scope,
664
+ s(:block,
665
+ s(:call,
666
+ s(:nil),
667
+ :+,
668
+ s(:arglist, s(:call, nil, :some_other_func, s(:arglist))))))),
669
+ s(:defn, :uses_call,
670
+ s(:args),
671
+ s(:scope,
672
+ s(:block,
673
+ s(:nil)))),
674
+ ]
675
+
676
+ assert_mutations expected, @heckler
677
+ end
470
678
  end
471
679
 
472
680
  class TestHeckleCallblock < HeckleTestCase
473
-
474
681
  def setup
682
+ @method_heckled = "uses_callblock"
475
683
  @nodes = s(:call)
476
684
  super
477
685
  end
478
686
 
479
- def test_default_structure
480
- expected = s(:defn, :uses_callblock,
481
- s(:args),
482
- s(:scope,
483
- s(:block,
484
- s(:iter,
485
- s(:call, s(:call, nil, :x, s(:arglist)), :y, s(:arglist)),
486
- nil,
487
- s(:lit, 1)))))
687
+ def test_callblock_original_tree
688
+ expected = s(:defn, :uses_callblock,
689
+ s(:args),
690
+ s(:scope,
691
+ s(:block,
692
+ s(:iter,
693
+ s(:call, s(:call, nil, :x, s(:arglist)), :y, s(:arglist)),
694
+ nil,
695
+ s(:lit, 1)))))
488
696
 
489
697
  assert_equal expected, @heckler.current_tree
490
698
  end
491
- def test_callblock_deleted
492
- expected = s(:defn, :uses_callblock,
493
- s(:args),
494
- s(:scope,
495
- s(:block,
496
- s(:iter,
497
- s(:call, s(:nil), :y, s(:arglist)),
498
- nil,
499
- s(:lit, 1)))))
699
+ def test_callblock_mutations
700
+ expected = [
701
+ s(:defn, :uses_callblock,
702
+ s(:args),
703
+ s(:scope,
704
+ s(:block,
705
+ s(:iter,
706
+ s(:call, s(:nil), :y, s(:arglist)),
707
+ nil,
708
+ s(:lit, 1)))))
709
+ ]
500
710
 
501
- @heckler.process(@heckler.current_tree)
502
- assert_equal expected, @heckler.current_tree
711
+ assert_mutations expected, @heckler
503
712
  end
504
713
  end
505
714
 
506
715
  class TestHeckleClassMethod < HeckleTestCase
507
716
  def setup
508
- @hecklee = "self.is_a_klass_method?"
717
+ @method_heckled = "self.is_a_klass_method?"
509
718
  @nodes = s(:true)
510
719
  super
511
720
  end
512
721
 
513
- def test_default_structure
514
- expected = s(:defs, s(:self), :is_a_klass_method?,
515
- s(:args),
516
- s(:scope,
517
- s(:block,
518
- s(:true))))
722
+ def test_class_method_original_tree
723
+ expected = s(:defs, s(:self), :is_a_klass_method?,
724
+ s(:args),
725
+ s(:scope,
726
+ s(:block,
727
+ s(:true))))
728
+
519
729
  assert_equal expected, @heckler.current_tree
520
730
  end
521
731
 
522
- def test_heckle_class_methods
523
- expected = s(:defs, s(:self), :is_a_klass_method?,
524
- s(:args),
525
- s(:scope,
526
- s(:block,
527
- s(:false))))
528
- @heckler.process(@heckler.current_tree)
529
- assert_equal expected, @heckler.current_tree
732
+ def test_class_methods_mutations
733
+ expected = [
734
+ s(:defs, s(:self), :is_a_klass_method?,
735
+ s(:args),
736
+ s(:scope,
737
+ s(:block,
738
+ s(:false))))
739
+ ]
740
+
741
+ assert_mutations expected, @heckler
530
742
  end
531
743
  end
532
744
 
533
745
  class TestHeckleCvasgn < HeckleTestCase
534
-
535
746
  def setup
747
+ @method_heckled = "uses_cvasgn"
536
748
  @nodes = s(:cvasgn)
537
749
  super
538
750
  end
539
751
 
540
- def test_cvasgn_val
541
- expected = s(:defn, :uses_cvasgn,
542
- s(:args),
543
- s(:scope,
544
- s(:block,
545
- s(:cvasgn, :@@cvar, s(:nil)),
546
- s(:cvasgn, :@@cvar, s(:nil)))))
752
+ def test_cvasgn_original_tree
753
+ expected = s(:defn, :uses_cvasgn,
754
+ s(:args),
755
+ s(:scope,
756
+ s(:block,
757
+ s(:cvasgn, :@@cvar, s(:lit, 5)),
758
+ s(:cvasgn, :@@cvar, s(:nil)))))
547
759
 
548
- @heckler.process(@heckler.current_tree)
549
760
  assert_equal expected, @heckler.current_tree
550
761
  end
551
762
 
552
- def test_cvasgn_nil
553
- expected = s(:defn, :uses_cvasgn,
554
- s(:args),
555
- s(:scope,
556
- s(:block,
557
- s(:cvasgn, :@@cvar, s(:lit, 5)),
558
- s(:cvasgn, :@@cvar, s(:lit, 42)))))
559
-
560
- @heckler.process(@heckler.current_tree)
561
- @heckler.reset_tree
562
- @heckler.process(@heckler.current_tree)
563
- assert_equal expected, @heckler.current_tree
763
+ def test_cvasgn_mutations
764
+ expected = [
765
+ s(:defn, :uses_cvasgn,
766
+ s(:args),
767
+ s(:scope,
768
+ s(:block,
769
+ s(:cvasgn, :@@cvar, s(:lit, 5)),
770
+ s(:cvasgn, :@@cvar, s(:lit, 42))))),
771
+ s(:defn, :uses_cvasgn,
772
+ s(:args),
773
+ s(:scope,
774
+ s(:block,
775
+ s(:cvasgn, :@@cvar, s(:nil)),
776
+ s(:cvasgn, :@@cvar, s(:nil))))),
777
+ ]
778
+
779
+ assert_mutations expected, @heckler
564
780
  end
565
-
566
781
  end
567
782
 
568
783
  class TestHeckleIasgn < HeckleTestCase
569
-
570
784
  def setup
785
+ @method_heckled = "uses_iasgn"
571
786
  @nodes = s(:iasgn)
572
787
  super
573
788
  end
574
789
 
575
- def test_iasgn_val
576
- expected = s(:defn, :uses_iasgn,
577
- s(:args),
578
- s(:scope,
579
- s(:block,
580
- s(:iasgn, :@ivar, s(:nil)),
581
- s(:iasgn, :@ivar, s(:nil)))))
790
+ def test_iasgn_original_tree
791
+ expected = s(:defn, :uses_iasgn,
792
+ s(:args),
793
+ s(:scope,
794
+ s(:block,
795
+ s(:iasgn, :@ivar, s(:lit, 5)),
796
+ s(:iasgn, :@ivar, s(:nil)))))
582
797
 
583
- @heckler.process(@heckler.current_tree)
584
798
  assert_equal expected, @heckler.current_tree
585
799
  end
586
800
 
587
- def test_iasgn_nil
588
- expected = s(:defn, :uses_iasgn,
589
- s(:args),
590
- s(:scope,
591
- s(:block,
592
- s(:iasgn, :@ivar, s(:lit, 5)),
593
- s(:iasgn, :@ivar, s(:lit, 42)))))
801
+ def test_iasgn_mutations
802
+ expected = [
803
+ s(:defn, :uses_iasgn,
804
+ s(:args),
805
+ s(:scope,
806
+ s(:block,
807
+ s(:iasgn, :@ivar, s(:lit, 5)),
808
+ s(:iasgn, :@ivar, s(:lit, 42))))),
809
+ s(:defn, :uses_iasgn,
810
+ s(:args),
811
+ s(:scope,
812
+ s(:block,
813
+ s(:iasgn, :@ivar, s(:nil)),
814
+ s(:iasgn, :@ivar, s(:nil))))),
815
+ ]
594
816
 
595
- @heckler.process(@heckler.current_tree)
596
- @heckler.reset_tree
597
- @heckler.process(@heckler.current_tree)
598
- assert_equal expected, @heckler.current_tree
817
+ assert_mutations expected, @heckler
599
818
  end
600
819
 
601
820
  end
602
821
 
603
822
  class TestHeckleGasgn < HeckleTestCase
604
-
605
823
  def setup
824
+ @method_heckled = "uses_gasgn"
825
+ s(:defn, :uses_cvasgn,
826
+ s(:args),
827
+ s(:scope,
828
+ s(:block,
829
+ s(:cvasgn, :@@cvar, s(:lit, 5)),
830
+ s(:cvasgn, :@@cvar, s(:lit, 42)))))
606
831
  @nodes = s(:gasgn)
607
832
  super
608
833
  end
609
834
 
610
- def test_gasgn_val
611
- expected = s(:defn, :uses_gasgn,
612
- s(:args),
613
- s(:scope,
614
- s(:block,
615
- s(:gasgn, :$gvar, s(:nil)),
616
- s(:gasgn, :$gvar, s(:nil)))))
835
+ def test_gasgn_original_tree
836
+ expected = s(:defn, :uses_gasgn,
837
+ s(:args),
838
+ s(:scope,
839
+ s(:block,
840
+ s(:gasgn, :$gvar, s(:lit, 5)),
841
+ s(:gasgn, :$gvar, s(:nil)))))
617
842
 
618
- @heckler.process(@heckler.current_tree)
619
843
  assert_equal expected, @heckler.current_tree
620
844
  end
621
845
 
622
- def test_gasgn_nil
623
- expected = s(:defn, :uses_gasgn,
624
- s(:args),
625
- s(:scope,
626
- s(:block,
627
- s(:gasgn, :$gvar, s(:lit, 5)),
628
- s(:gasgn, :$gvar, s(:lit, 42)))))
846
+ def test_gasgn_mutations
847
+ expected = [
848
+ s(:defn, :uses_gasgn,
849
+ s(:args),
850
+ s(:scope,
851
+ s(:block,
852
+ s(:gasgn, :$gvar, s(:lit, 5)),
853
+ s(:gasgn, :$gvar, s(:lit, 42))))),
854
+ s(:defn, :uses_gasgn,
855
+ s(:args),
856
+ s(:scope,
857
+ s(:block,
858
+ s(:gasgn, :$gvar, s(:nil)),
859
+ s(:gasgn, :$gvar, s(:nil))))),
860
+ ]
629
861
 
630
- @heckler.process(@heckler.current_tree)
631
- @heckler.reset_tree
632
- @heckler.process(@heckler.current_tree)
633
- assert_equal expected, @heckler.current_tree
862
+ assert_mutations expected, @heckler
634
863
  end
635
864
 
636
865
  end
637
866
 
638
867
  class TestHeckleLasgn < HeckleTestCase
639
-
640
868
  def setup
869
+ @method_heckled = "uses_lasgn"
641
870
  @nodes = s(:lasgn)
642
871
  super
643
872
  end
644
873
 
645
- def test_lasgn_val
646
- expected = s(:defn, :uses_lasgn,
647
- s(:args),
648
- s(:scope,
649
- s(:block,
650
- s(:lasgn, :lvar, s(:nil)),
651
- s(:lasgn, :lvar, s(:nil)))))
874
+ def test_lasgn_original_tree
875
+ expected = s(:defn, :uses_lasgn,
876
+ s(:args),
877
+ s(:scope,
878
+ s(:block,
879
+ s(:lasgn, :lvar, s(:lit, 5)),
880
+ s(:lasgn, :lvar, s(:nil)))))
652
881
 
653
- @heckler.process(@heckler.current_tree)
654
882
  assert_equal expected, @heckler.current_tree
655
883
  end
656
884
 
657
- def test_lasgn_nil
658
- expected = s(:defn, :uses_lasgn,
659
- s(:args),
660
- s(:scope,
661
- s(:block,
662
- s(:lasgn, :lvar, s(:lit, 5)),
663
- s(:lasgn, :lvar, s(:lit, 42)))))
664
-
665
- @heckler.process(@heckler.current_tree)
666
- @heckler.reset_tree
667
- @heckler.process(@heckler.current_tree)
668
- assert_equal expected, @heckler.current_tree
885
+ def test_lasgn_mutations
886
+ expected = [
887
+ s(:defn, :uses_lasgn,
888
+ s(:args),
889
+ s(:scope,
890
+ s(:block,
891
+ s(:lasgn, :lvar, s(:nil)),
892
+ s(:lasgn, :lvar, s(:nil))))),
893
+ s(:defn, :uses_lasgn,
894
+ s(:args),
895
+ s(:scope,
896
+ s(:block,
897
+ s(:lasgn, :lvar, s(:lit, 5)),
898
+ s(:lasgn, :lvar, s(:lit, 42))))),
899
+ ]
900
+
901
+ assert_mutations expected, @heckler
669
902
  end
670
-
671
903
  end
672
904
 
673
905
  class TestHeckleMasgn < HeckleTestCase
674
-
675
906
  def setup
907
+ @method_heckled = "uses_masgn"
676
908
  @nodes = s(:dasgn, :dasgn_curr, :iasgn, :gasgn, :lasgn)
677
909
  super
678
910
  end
679
911
 
680
- def test_masgn
681
- expected = s(:defn, :uses_masgn,
682
- s(:args),
683
- s(:scope,
684
- s(:block,
685
- s(:masgn,
686
- s(:array,
687
- s(:lasgn, :_heckle_dummy),
688
- s(:gasgn, :$b),
689
- s(:lasgn, :c)),
690
- s(:array, s(:lit, 5), s(:lit, 6), s(:lit, 7))))))
912
+ def test_masgn_original_tree
913
+ expected = s(:defn, :uses_masgn,
914
+ s(:args),
915
+ s(:scope,
916
+ s(:block,
917
+ s(:masgn,
918
+ s(:array, s(:iasgn, :@a), s(:gasgn, :$b), s(:lasgn, :c)),
919
+ s(:array, s(:lit, 5), s(:lit, 6), s(:lit, 7))))))
691
920
 
692
- @heckler.process(@heckler.current_tree)
693
921
  assert_equal expected, @heckler.current_tree
694
922
  end
695
923
 
924
+ def test_masgn_mutations
925
+ expected = [
926
+ s(:defn, :uses_masgn,
927
+ s(:args),
928
+ s(:scope,
929
+ s(:block,
930
+ s(:masgn,
931
+ s(:array, s(:iasgn, :_heckle_dummy), s(:gasgn, :$b), s(:lasgn, :c)),
932
+ s(:array, s(:lit, 5), s(:lit, 6), s(:lit, 7)))))),
933
+ s(:defn, :uses_masgn,
934
+ s(:args),
935
+ s(:scope,
936
+ s(:block,
937
+ s(:masgn,
938
+ s(:array, s(:iasgn, :@a), s(:gasgn, :_heckle_dummy), s(:lasgn, :c)),
939
+ s(:array, s(:lit, 5), s(:lit, 6), s(:lit, 7)))))),
940
+ s(:defn, :uses_masgn,
941
+ s(:args),
942
+ s(:scope,
943
+ s(:block,
944
+ s(:masgn,
945
+ s(:array,s(:iasgn, :@a), s(:gasgn, :$b), s(:lasgn, :_heckle_dummy)),
946
+ s(:array, s(:lit, 5), s(:lit, 6), s(:lit, 7)))))),
947
+ ]
948
+
949
+ assert_mutations expected, @heckler
950
+ end
951
+
696
952
  end
697
953
 
698
954
  class TestHeckleIter < HeckleTestCase
699
955
  def setup
956
+ @method_heckled = "uses_iter"
700
957
  @nodes = [ :call, :lasgn ]
701
958
  super
702
959
  end
703
-
704
- def test_iter
705
- expected = s(:defn, :uses_iter,
706
- s(:args),
707
- s(:scope,
708
- s(:block,
709
- s(:lasgn, :x, s(:nil)),
710
- s(:iter,
711
- s(:call, s(:lvar, :x), :each, s(:arglist)),
712
- s(:lasgn, :y),
713
- s(:lvar, :y)))))
714
-
715
- # This call causes the replacement of [:lasgn, :x...] above to
716
- # become [:lasgn, :nil]. We then reset the tree to ensure that
717
- # the original method is maintained. We are really trying to test
718
- # the reset_tree method here, not the actual changes.
719
960
 
720
- @heckler.process(@heckler.current_tree)
721
- assert_equal(expected, @heckler.current_tree)
961
+ def test_iter_original_tree
962
+ expected = s(:defn, :uses_iter,
963
+ s(:args),
964
+ s(:scope,
965
+ s(:block,
966
+ s(:lasgn, :x, s(:array, s(:lit, 1), s(:lit, 2), s(:lit, 3))),
967
+ s(:iter,
968
+ s(:call, s(:lvar, :x), :each, s(:arglist)),
969
+ s(:lasgn, :y), s(:lvar, :y)))))
722
970
 
723
- @heckler.reset_tree
724
- expected = s(:defn, :uses_iter,
725
- s(:args),
726
- s(:scope,
727
- s(:block,
728
- s(:lasgn, :x, s(:array, s(:lit, 1), s(:lit, 2), s(:lit, 3))),
729
- s(:iter,
730
- s(:call, s(:lvar, :x), :each, s(:arglist)),
731
- s(:lasgn, :_heckle_dummy),
732
- s(:call, nil, :y, s(:arglist))))))
971
+ assert_equal expected, @heckler.current_tree
972
+ end
733
973
 
734
- @heckler.process(@heckler.current_tree)
735
- assert_equal(expected, @heckler.current_tree)
974
+ def test_iter_mutations
975
+ expected = [
976
+ s(:defn, :uses_iter,
977
+ s(:args),
978
+ s(:scope,
979
+ s(:block,
980
+ s(:lasgn, :x, s(:nil)),
981
+ s(:iter,
982
+ s(:call, s(:lvar, :x), :each, s(:arglist)),
983
+ s(:lasgn, :y), s(:lvar, :y))))),
984
+ s(:defn, :uses_iter,
985
+ s(:args),
986
+ s(:scope,
987
+ s(:block,
988
+ s(:lasgn, :x, s(:array, s(:lit, 1), s(:lit, 2), s(:lit, 3))),
989
+ s(:iter,
990
+ s(:call, s(:lvar, :x), :each, s(:arglist)),
991
+ s(:lasgn, :_heckle_dummy), s(:lvar, :y))))),
992
+ ]
993
+
994
+
995
+ assert_mutations expected, @heckler
996
+ end
997
+ end
998
+
999
+
1000
+ class TestHeckleFindsNestedClassAndModule < HeckleTestCase
1001
+ def setup
1002
+ @klass = "HeckleDummy::OuterNesting::InnerNesting::InnerClass"
1003
+ @method_heckled = "foo"
1004
+ @nodes = []
1005
+ super
1006
+ end
1007
+
1008
+ def test_nested_class_and_module_original_tree
1009
+ expected = s(:defn, :foo, s(:args), s(:scope,
1010
+ s(:block,
1011
+ s(:lit, 1337))))
1012
+
1013
+ assert_equal expected, @heckler.current_tree
736
1014
  end
737
1015
  end