pretentious 0.1.0 → 0.1.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e0dde9fafa6d13f604e06b79bc3d7714698829c4
4
- data.tar.gz: 7052900a02666fa5ea8a85997073a75bcee40cce
3
+ metadata.gz: 8025fc11bf85286be01fb1bde220c64e7cc49c54
4
+ data.tar.gz: ede071633cf959eb97125c7c173f445d406de366
5
5
  SHA512:
6
- metadata.gz: 4549b4ab18d247874a2ae1d9b3f9eaba6ee3201f63a923a3ad4616bc3f46dc15515e4c703c999ffddfed99e5cb967bb51e535cdf2b1812a32e5d22a3afc638a1
7
- data.tar.gz: b7cab761d44df23c0a08e256d24f8538ceb7a14372db3cafb6474f0feafafc9fa3509d0a6d3a986c913ef30bf67c718418cccb2b52642103e80fd2d39c446fd5
6
+ metadata.gz: 2d02f840b1b06223fc6cfcd6df9a5552fe698a08815b4c7a0b86adfd83e539085fe27d1551d05aca9e9521801b6a56dc3f10333b9ce9793efdb76192e0f7ba04
7
+ data.tar.gz: 6d73531a5d43c9b34d6094e10931c63d7feb0cf2efb1b884206bfc34cbf81d83d8a720e3203e1bdefa28586d9697ae9d836a6bd8297f981e6c0e5e1353e23d4d
data/bin/ddtgen CHANGED
@@ -47,14 +47,18 @@ module DdtUtils
47
47
  end
48
48
  end
49
49
 
50
- Pretentious.last_results.each { |klass, result|
51
- output_folder = result[:generator].location(output_folder)
52
- FileUtils.mkdir_p output_folder
53
- result[:generator].helper(output_folder)
54
- filename = result[:generator].naming(output_folder, klass)
55
- File.open(filename, 'w') {
56
- |f| f.write(result[:output])
50
+
51
+ Pretentious.last_results.each { |g, result_per_generator|
52
+ puts "#{g}:"
53
+ result_per_generator.each { |klass, result|
54
+ output_folder = result[:generator].location(output_folder)
55
+ FileUtils.mkdir_p output_folder
56
+ result[:generator].helper(output_folder)
57
+ filename = result[:generator].naming(output_folder, klass)
58
+ File.open(filename, 'w') {
59
+ |f| f.write(result[:output])
60
+ }
61
+ puts "#{filename}"
57
62
  }
58
- puts "#{filename}"
59
63
  }
60
64
 
data/example.rb CHANGED
@@ -22,7 +22,7 @@ end
22
22
  another_object = TestClass1.new("test")
23
23
  test_class_one = TestClass1.new({hello: "world", test: another_object, arr_1: [1,2,3,4,5, another_object],
24
24
  sub_hash: {yes: true, obj: another_object}})
25
- test_class_two = TestClass2.new("This is message 2")
25
+ test_class_two = TestClass2.new("This is message 2", nil)
26
26
 
27
27
  class_to_test = TestClass3.new(test_class_one, test_class_two)
28
28
  class_to_test.show_messages
@@ -42,6 +42,12 @@ end
42
42
  class_to_test4.message
43
43
  }
44
44
 
45
+ message3 = "This is message 3"
46
+ t = TestClass2.new(message3, nil)
47
+ test_class_two = TestClass2.new(t, message3)
48
+ test_class_two.test(message3)
49
+
50
+
45
51
  begin
46
52
  test_class_one.something_is_wrong
47
53
  rescue Exception=>e
data/lib/pretentious.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require "pretentious/version"
2
+ require "pretentious/generator_base"
2
3
  require "pretentious/rspec_generator"
3
4
  require "pretentious/minitest_generator"
4
5
  require "pretentious/recorded_proc"
@@ -43,23 +44,24 @@ end
43
44
  module Pretentious
44
45
 
45
46
  def self.spec_for(*klasses, &block)
46
- @results ||= {}
47
+ @spec_results ||= {}
47
48
  Pretentious::Generator.test_generator = Pretentious::RspecGenerator
48
- @results.merge!(Pretentious::Generator.generate_for(*klasses, &block))
49
+ @spec_results.merge!(Pretentious::Generator.generate_for(*klasses, &block))
49
50
  end
50
51
 
51
52
  def self.minitest_for(*klasses, &block)
52
- @results ||= {}
53
+ @minitest_results ||= {}
53
54
  Pretentious::Generator.test_generator = Pretentious::MinitestGenerator
54
- @results.merge!(Pretentious::Generator.generate_for(*klasses, &block))
55
+ @minitest_results.merge!(Pretentious::Generator.generate_for(*klasses, &block))
55
56
  end
56
57
 
57
58
  def self.clear_results
58
- @results = {}
59
+ @spec_results = {}
60
+ @minitest_results = {}
59
61
  end
60
62
 
61
63
  def self.last_results
62
- @results
64
+ {spec: @spec_results, minitest: @minitest_results}
63
65
  end
64
66
 
65
67
  def self.install_watcher
@@ -57,7 +57,7 @@ class Pretentious::Deconstructor
57
57
  def dfs(tree)
58
58
  if !tree.is_a? Hash
59
59
  value = tree
60
- definition = {id: value.object_id, class: tree.class, value: value}
60
+ definition = {id: value.object_id, class: tree.class, value: value, used_by: []}
61
61
  unless (@dependencies.include? value.object_id)
62
62
  @dependencies[value.object_id] = definition
63
63
  @declaration_order << definition
@@ -66,7 +66,7 @@ class Pretentious::Deconstructor
66
66
  else
67
67
  ref = []
68
68
 
69
- definition = {id: tree[:id], class: tree[:class]}
69
+ definition = {id: tree[:id], class: tree[:class], params_types: tree[:params_types], used_by: []}
70
70
 
71
71
  if tree[:class] == Hash
72
72
  definition[:value] = dfs_hash(tree[:composition], ref)
@@ -99,11 +99,41 @@ class Pretentious::Deconstructor
99
99
  unless (@dependencies.include? tree[:id])
100
100
  @declaration_order << definition
101
101
  @dependencies[tree[:id]] = definition
102
+
103
+ ref.each { |r|
104
+ @dependencies[r][:used_by] << definition
105
+ }
102
106
  end
107
+
103
108
  tree[:id]
104
109
  end
105
110
  end
106
111
 
112
+ def update_ref_counts(params_arr, method_call)
113
+ params_arr.each do |p|
114
+ if (@dependencies.include? p.object_id)
115
+ @dependencies[p.object_id][:used_by] << method_call
116
+ end
117
+ end
118
+ end
119
+
120
+ def inline
121
+ @dependencies.each { |id, definition|
122
+ if (definition[:used_by].size == 1)
123
+ if (definition.include?(:value) && self.class.is_primitive?(definition[:value]))
124
+ ref = definition[:used_by][0]
125
+ definition[:used_by] = :inline
126
+ references = ref[:ref]
127
+ if (references)
128
+ new_ref = references.collect { |c| c == id ? definition : c}
129
+ ref[:ref] = new_ref
130
+ end
131
+ end
132
+
133
+ end
134
+ }
135
+ end
136
+
107
137
  #creates a tree on how the object was created
108
138
  def build_tree(target_object)
109
139
  tree = {class: get_test_class(target_object), id: target_object.object_id, composition: []}
@@ -119,15 +149,15 @@ class Pretentious::Deconstructor
119
149
  tree[:block_params] = self.class.block_param_names(target_object)
120
150
  elsif target_object.methods.include? :_get_init_arguments
121
151
  args = target_object._get_init_arguments
122
-
123
152
  unless args.nil?
124
-
153
+ tree[:params_types] = args[:params_types]
125
154
  args[:params].each { |p|
126
155
  tree[:composition] << build_tree(p)
127
156
  }
128
157
 
129
158
  tree[:block] = build_tree(args[:block]) unless args[:block].nil?
130
159
  else
160
+
131
161
  if (self.class.is_primitive?(target_object))
132
162
  tree[:composition] = target_object
133
163
  elsif (target_object.class == File)
@@ -144,7 +174,7 @@ class Pretentious::Deconstructor
144
174
  end
145
175
 
146
176
 
147
- def deconstruct(*target_objects)
177
+ def deconstruct(method_call_collection, *target_objects)
148
178
 
149
179
  @declaration_order = []
150
180
  @dependencies = {}
@@ -154,10 +184,16 @@ class Pretentious::Deconstructor
154
184
  dfs(tree)
155
185
  }
156
186
 
187
+ method_call_collection.each do |m|
188
+ update_ref_counts(m[:params], m)
189
+ end
190
+
191
+ inline
192
+
157
193
  {declaration: @declaration_order, dependency: @dependencies}
158
194
  end
159
195
 
160
- def deconstruct_to_ruby(indentation_level = 0, variable_map = {}, declared_names = {}, *target_objects)
196
+ def deconstruct_to_ruby(indentation_level = 0, variable_map = {}, declared_names = {}, method_call_collection = [], *target_objects)
161
197
  output_buffer = ""
162
198
  indentation = ""
163
199
 
@@ -168,12 +204,13 @@ class Pretentious::Deconstructor
168
204
  target_objects.each { |target_object|
169
205
  variable_map.merge!(target_object._variable_map) if target_object.methods.include?(:_variable_map) && !target_object._variable_map.nil?
170
206
  }
171
- declarations, dependencies = deconstruct *target_objects
172
- declarations[:declaration].each do |d|
173
-
174
- var_name = Pretentious::Deconstructor.pick_name(variable_map, d[:id], declared_names)
175
- output_buffer << "#{indentation}#{var_name} = #{construct(d, variable_map, declared_names, indentation)}\n"
207
+ declarations, dependencies = deconstruct method_call_collection, *target_objects
176
208
 
209
+ declarations[:declaration].each do |d|
210
+ unless d[:used_by] == :inline
211
+ var_name = Pretentious::Deconstructor.pick_name(variable_map, d[:id], declared_names)
212
+ output_buffer << "#{indentation}#{var_name} = #{construct(d, variable_map, declared_names, indentation)}\n"
213
+ end
177
214
  end
178
215
 
179
216
  output_buffer
@@ -272,7 +309,7 @@ class Pretentious::Deconstructor
272
309
  object_id_to_declared_names = {}
273
310
 
274
311
  declared_names.each { |k,v|
275
- object_id_to_declared_names[v[:object_id]] = k
312
+ object_id_to_declared_names[v[:object_id]] = k if v
276
313
  } if declared_names
277
314
 
278
315
  #return immediately if already mapped
@@ -281,7 +318,6 @@ class Pretentious::Deconstructor
281
318
  if (!variable_map.nil? && variable_map.include?(object_id))
282
319
 
283
320
  candidate_name = variable_map[object_id].to_s
284
-
285
321
  if !declared_names.include?(candidate_name)
286
322
  var_name = candidate_name
287
323
  declared_names[candidate_name] = {count: 1, object_id: object_id}
@@ -369,8 +405,27 @@ class Pretentious::Deconstructor
369
405
  else
370
406
  params = []
371
407
  if (definition[:ref] && definition[:ref].size > 0)
372
- definition[:ref].each do |v|
373
- params << Pretentious::Deconstructor.pick_name(variable_map, v, declared_names)
408
+
409
+ i = 0
410
+ params_types = definition[:params_types]
411
+ definition[:ref].each_with_index do |v, index|
412
+
413
+ type = :param
414
+ if (params_types)
415
+ type = params_types[index][0]
416
+ i+=1
417
+ end
418
+
419
+ #to inline?
420
+ if (v.is_a? Hash)
421
+ params << Pretentious::value_ize(v[:value], variable_map, declared_names)
422
+ else
423
+ if (type == :block)
424
+ params << "&#{Pretentious::Deconstructor.pick_name(variable_map, v, declared_names)}"
425
+ else
426
+ params << Pretentious::Deconstructor.pick_name(variable_map, v, declared_names)
427
+ end
428
+ end
374
429
  end
375
430
  "#{definition[:class]}.new(#{params.join(', ')})"
376
431
  else
@@ -40,12 +40,13 @@ module Pretentious
40
40
 
41
41
  def initialize(*args, &block)
42
42
 
43
- @_instance_init = {params: [], block: nil}
43
+ @_instance_init = {object_id: self.object_id, params: [], block: nil}
44
44
 
45
45
  self.class.replace_procs_with_recorders(args)
46
46
 
47
47
  @_instance_init[:params] = args
48
48
 
49
+
49
50
  recordedProc = if (block)
50
51
  RecordedProc.new(block, true)
51
52
  else
@@ -55,7 +56,9 @@ module Pretentious
55
56
  @_instance_init[:block] = recordedProc
56
57
 
57
58
  setup_instance(*args, &recordedProc)
58
-
59
+ param_types = @_instance.method(:initialize).parameters
60
+ puts "#{@_instance.class.name} params -> #{param_types.inspect}"
61
+ @_instance_init[:params_types] = param_types
59
62
 
60
63
  @_method_calls = []
61
64
  @_method_calls_by_method = {}
@@ -73,6 +76,10 @@ module Pretentious
73
76
  @_init_let_variables[variable_value.object_id] = v
74
77
  }
75
78
 
79
+ args.each_with_index { |a, index|
80
+ @_init_let_variables[a.object_id] = param_types[index][1].to_s if param_types.size == 2
81
+ }
82
+
76
83
  self.class._add_instances(self)
77
84
  end
78
85
 
@@ -207,8 +214,6 @@ module Pretentious
207
214
  @_methods_for_test = @_methods_for_test || []
208
215
  @_let_variables = @_let_variables || {}
209
216
 
210
-
211
- #puts \"local_variables\"
212
217
  v_locals = caller_context.eval('local_variables')
213
218
 
214
219
  v_locals.each { |v|
@@ -402,15 +407,18 @@ module Pretentious
402
407
  end
403
408
  @_variable_names= {}
404
409
 
405
- index = 0
406
- params = method(:initialize).parameters
410
+ params = if (self.respond_to? :test_class )
411
+ test_class.instance_method(:initialize).parameters
412
+ else
413
+ method(:initialize).parameters
414
+ end
415
+ @_init_arguments[:params_types] = params
407
416
 
408
- args.each do |arg|
417
+ args.each_with_index do |arg, index|
409
418
  p = params[index]
410
419
  if p.size > 1
411
420
  @_variable_names[arg.object_id] = p[1].to_s
412
421
  end unless p.nil?
413
- index+=1
414
422
  end unless args.nil?
415
423
 
416
424
  end
@@ -420,11 +428,11 @@ module Pretentious
420
428
  end
421
429
 
422
430
  def _deconstruct
423
- Pretentious::Deconstructor.new().deconstruct(self)
431
+ Pretentious::Deconstructor.new().deconstruct([], self)
424
432
  end
425
433
 
426
- def _deconstruct_to_ruby(indentation = 0)
427
- Pretentious::Deconstructor.new().deconstruct_to_ruby(indentation, _variable_map, {}, self)
434
+ def _deconstruct_to_ruby(var_name = nil, indentation = 0)
435
+ Pretentious::Deconstructor.new().deconstruct_to_ruby(indentation, _variable_map.merge({self.object_id => var_name}), {}, [], self)
428
436
  end
429
437
 
430
438
  end
@@ -0,0 +1,13 @@
1
+ module Pretentious
2
+ class GeneratorBase
3
+
4
+ def buffer(line, level = 0)
5
+ @output_buffer << "#{indentation(level)}#{line}\n"
6
+ end
7
+
8
+ def buffer_inline(line, level = 0)
9
+ @output_buffer << "#{indentation(level)}#{line}"
10
+ end
11
+
12
+ end
13
+ end
@@ -1,4 +1,4 @@
1
- class Pretentious::MinitestGenerator
1
+ class Pretentious::MinitestGenerator < Pretentious::GeneratorBase
2
2
 
3
3
  def initialize(options = {})
4
4
  @deconstructor = Pretentious::Deconstructor.new
@@ -18,10 +18,6 @@ class Pretentious::MinitestGenerator
18
18
  buffer
19
19
  end
20
20
 
21
- def buffer(line, level = 0)
22
- @output_buffer << "#{indentation(level)}#{line}\n" if !line.nil? && line.strip != ''
23
- end
24
-
25
21
  def whitespace(level = 0)
26
22
  @output_buffer << "#{indentation(level)}\n"
27
23
  end
@@ -50,35 +46,10 @@ class Pretentious::MinitestGenerator
50
46
  class_method_calls = test_instance.method_calls_by_method
51
47
  generate_specs("#{test_instance.test_class.name}::",test_instance.test_class.name, class_method_calls, test_instance.let_variables)
52
48
  else
53
- buffer("class Scenario#{instance_count} < Test#{@test_class.name}",0)
49
+ buffer("class #{test_instance.test_class.name}Scenario#{instance_count} < Test#{@test_class.name}",0)
54
50
 
55
51
  buffer("def setup",1)
56
- declarations = {}
57
- dependencies = []
58
-
59
- args = test_instance._init_arguments[:params]
60
- block = test_instance._init_arguments[:block]
61
- dependencies = dependencies | args
62
-
63
- unless block.nil?
64
- dependencies << block
65
- end
66
-
67
- block_source = if !block.nil? && block.is_a?(Pretentious::RecordedProc)
68
- get_block_source(block, test_instance.init_let_variables, declarations, @_indentation.length * 2)
69
- else
70
- ''
71
- end
72
-
73
- if (dependencies.size > 0)
74
- buffer(declare_dependencies(dependencies, test_instance.init_let_variables, 2, declarations))
75
- end
76
-
77
- if (args.size > 0)
78
- buffer("@fixture = #{test_instance.test_class.name}.new(#{params_generator(args, test_instance.init_let_variables, declarations)})#{block_source}",2)
79
- else
80
- buffer("@fixture = #{test_instance.test_class.name}.new#{block_source}",2)
81
- end
52
+ buffer_inline(test_instance._deconstruct_to_ruby('@fixture', 2 * @_indentation.length))
82
53
  buffer("end", 1)
83
54
  whitespace
84
55
 
@@ -137,7 +108,6 @@ class Pretentious::MinitestGenerator
137
108
 
138
109
  def generate_specs(context_prefix, fixture, method_calls, let_variables)
139
110
  buffer("def test_current_expectation",1)
140
- whitespace
141
111
  declaration = {}
142
112
  #collect all params
143
113
  params_collection = []
@@ -310,7 +280,7 @@ class Pretentious::MinitestGenerator
310
280
  deconstructor = Pretentious::Deconstructor.new
311
281
 
312
282
  args = remove_primitives(args, variable_map)
313
- deconstructor.deconstruct_to_ruby(level * @_indentation.length, variable_map, declarations, *args)
283
+ deconstructor.deconstruct_to_ruby(level * @_indentation.length, variable_map, declarations, [], *args)
314
284
  end
315
285
 
316
286
  def remove_primitives(args, let_lookup)
@@ -1,4 +1,4 @@
1
- class Pretentious::RspecGenerator
1
+ class Pretentious::RspecGenerator < Pretentious::GeneratorBase
2
2
 
3
3
  def initialize(options = {})
4
4
  @deconstructor = Pretentious::Deconstructor.new
@@ -18,9 +18,6 @@ class Pretentious::RspecGenerator
18
18
  buffer
19
19
  end
20
20
 
21
- def buffer(line, level = 0)
22
- @output_buffer << "#{indentation(level)}#{line}\n"
23
- end
24
21
 
25
22
  def whitespace(level = 0)
26
23
  @output_buffer << "#{indentation(level)}\n"
@@ -46,40 +43,13 @@ class Pretentious::RspecGenerator
46
43
  if (test_instance.is_a? Class)
47
44
  #class methods
48
45
  class_method_calls = test_instance.method_calls_by_method
49
- generate_specs("#{test_instance.test_class.name}::",test_instance.test_class.name, class_method_calls, test_instance.let_variables)
46
+ generate_specs("#{test_instance.test_class.name}::", test_instance.test_class.name,
47
+ class_method_calls, test_instance.let_variables)
50
48
  else
51
49
  buffer("context 'Scenario #{instance_count}' do",1)
52
50
 
53
51
  buffer("before do",2)
54
- whitespace
55
-
56
- declarations = {}
57
- dependencies = []
58
-
59
- args = test_instance._init_arguments[:params]
60
- block = test_instance._init_arguments[:block]
61
- dependencies = dependencies | args
62
-
63
- unless block.nil?
64
- dependencies << block
65
- end
66
-
67
- block_source = if !block.nil? && block.is_a?(Pretentious::RecordedProc)
68
- get_block_source(block, test_instance.init_let_variables, declarations, @_indentation * 3)
69
- else
70
- ''
71
- end
72
-
73
- if (dependencies.size > 0)
74
- buffer(declare_dependencies(dependencies, test_instance.init_let_variables, 3 * @_indentation.length, declarations))
75
- end
76
-
77
- if (args.size > 0)
78
- buffer("@fixture = #{test_instance.test_class.name}.new(#{params_generator(args, test_instance.init_let_variables, declarations)})#{block_source}",3)
79
- else
80
- buffer("@fixture = #{test_instance.test_class.name}.new#{block_source}",3)
81
- end
82
- whitespace
52
+ buffer_inline(test_instance._deconstruct_to_ruby('@fixture', 3 * @_indentation.length))
83
53
  buffer("end",2)
84
54
  whitespace
85
55
 
@@ -135,15 +105,16 @@ class Pretentious::RspecGenerator
135
105
 
136
106
  def generate_specs(context_prefix, fixture, method_calls, let_variables)
137
107
  buffer("it 'should pass current expectations' do",2)
138
- whitespace
139
108
  declaration = {}
140
109
  #collect all params
141
110
  params_collection = []
142
111
  mocks_collection = {}
112
+ method_call_collection = []
143
113
 
144
114
  method_calls.each_key do |k|
145
115
  info_blocks_arr = method_calls[k]
146
116
  info_blocks_arr.each do |block|
117
+ method_call_collection << block
147
118
  params_collection = params_collection | block[:params]
148
119
  if (!Pretentious::Deconstructor.is_primitive?(block[:result]) && !block[:result].kind_of?(Exception))
149
120
  params_collection << block[:result]
@@ -171,7 +142,7 @@ class Pretentious::RspecGenerator
171
142
  end
172
143
 
173
144
  if (params_collection.size > 0)
174
- buffer(declare_dependencies(params_collection, let_variables, 3 * @_indentation.length, declaration))
145
+ buffer(declare_dependencies(params_collection, let_variables, 3 * @_indentation.length, declaration, []))
175
146
  end
176
147
 
177
148
  if (mocks_collection.keys.size > 0)
@@ -282,11 +253,11 @@ class Pretentious::RspecGenerator
282
253
  params.join(" ,")
283
254
  end
284
255
 
285
- def declare_dependencies(args, variable_map, level, declarations)
256
+ def declare_dependencies(args, variable_map, level, declarations, method_call_collection)
286
257
  deconstructor = Pretentious::Deconstructor.new
287
258
 
288
259
  args = remove_primitives(args, variable_map)
289
- deconstructor.deconstruct_to_ruby(level, variable_map, declarations, *args)
260
+ deconstructor.deconstruct_to_ruby(level, variable_map, declarations, method_call_collection, *args)
290
261
  end
291
262
 
292
263
  def remove_primitives(args, let_lookup)