pretentious 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c540e7a1a93193e951dbd73f85dd6ef78f2657a9
4
- data.tar.gz: 51818210e6346db9ac4c30f549ca586cefeeb40e
3
+ metadata.gz: 594e3a65079d62df37a766461e810343529e990e
4
+ data.tar.gz: 1f3dc437941f264eb5790e630da2da760d7e9d97
5
5
  SHA512:
6
- metadata.gz: e4812956f222bd0b479bc5721ad88dc66e1ab38f3f4879938824354f868010ba4b34bf8a261f1e23469ab2cd2534b9dd3df1b0bafdaf54310e25930205a2da2f
7
- data.tar.gz: 2799fde8fdb2c36a929612ff94e642ae5986d83dd8fd77f76173b67dcb8c9513e2381daa5b2a45bdb568a7b4ef2754cefa567c9cda6b435f644076491683813c
6
+ metadata.gz: 7aa09793966b307e617c44c4776d6ca2c1176d3ce474094e5726e66c85e8026e7ac28f4066700f3eb2b1e08c2f8167f00f2cb1d713e93a0f7f5ba2ff5485b559
7
+ data.tar.gz: de79f80b3aa79eaf92e0fbcc64e04c88a024abd51127d9eff50497817b48078c0c5757fe8ff5b7a7267c72e018f26e6646ebb197e332f2134ed307d7a593ad7f
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ [![Gem Version](https://badge.fury.io/rb/pretentious.svg)](https://badge.fury.io/rb/pretentious)
2
+
1
3
  # Ruby::Pretentious
2
4
 
3
5
  Do you have a pretentious boss or development lead that pushes you to embrace BDD/TDD but for reasons hate it or them?
@@ -82,9 +84,9 @@ end
82
84
 
83
85
  Save your file and then switch to the terminal to invoke:
84
86
 
85
- ddtgen example.rb
87
+ $ ddtgen example.rb
86
88
 
87
- This will automatically generate rspec tests for Fibonacci under spec of the current working directory.
89
+ This will automatically generate rspec tests for Fibonacci under /spec of the current working directory.
88
90
 
89
91
  You can actually invoke rspec at this point, but the tests will fail. Before you do that you should edit
90
92
  spec/spec_helper.rb and put the necessary requires and definitions there.
@@ -110,7 +112,60 @@ class Fibonacci
110
112
  end
111
113
  ```
112
114
 
113
- You can also try this out with build in libraries like MD5 for example
115
+ The generated spec file should look something like this
116
+
117
+ ```ruby
118
+ require 'spec_helper'
119
+
120
+ RSpec.describe Fibonacci do
121
+
122
+ context 'Scenario 1' do
123
+ before do
124
+
125
+
126
+ @fixture = Fibonacci.new
127
+
128
+ end
129
+
130
+ it 'should pass current expectations' do
131
+
132
+ n = 1
133
+ n_1 = 2
134
+ n_2 = 3
135
+ n_3 = 4
136
+ n_4 = 5
137
+
138
+ # Fibonacci#fib when passed n = 1 should return 1
139
+ expect( @fixture.fib(n) ).to eq(1)
140
+
141
+ # Fibonacci#fib when passed n = 2 should return 1
142
+ expect( @fixture.fib(n_1) ).to eq(1)
143
+
144
+ # Fibonacci#fib when passed n = 3 should return 2
145
+ expect( @fixture.fib(n_2) ).to eq(2)
146
+
147
+ # Fibonacci#fib when passed n = 4 should return 3
148
+ expect( @fixture.fib(n_3) ).to eq(3)
149
+
150
+ # Fibonacci#fib when passed n = 5 should return 5
151
+ expect( @fixture.fib(n_4) ).to eq(5)
152
+
153
+ end
154
+ end
155
+
156
+ it 'should pass current expectations' do
157
+
158
+
159
+ # Fibonacci::say_hello when passed should return hello
160
+ expect( Fibonacci.say_hello ).to eq("hello")
161
+
162
+ end
163
+ end
164
+ ```
165
+
166
+ awesome!
167
+
168
+ You can also try this out with built-in libraries like MD5 for example ...
114
169
 
115
170
  ```ruby
116
171
  #example.rb
@@ -141,6 +196,98 @@ end
141
196
 
142
197
  Only RSpec is supported at this point. But other testing frameworks should be trivial to add support to.
143
198
 
199
+ ## Handling complex parameters and object constructors
200
+
201
+ No need to do anything special, just do as what you would do normally and the pretentious gem will figure it out.
202
+ see below for an example:
203
+
204
+ ```ruby
205
+ class TestClass1
206
+
207
+ def initialize(message)
208
+ @message = message
209
+ end
210
+
211
+ def print_message
212
+ puts @message
213
+ end
214
+
215
+ def something_is_wrong
216
+ raise StandardError.new
217
+ end
218
+ end
219
+
220
+ class TestClass2
221
+ def initialize(message)
222
+ @message = {message: message}
223
+ end
224
+
225
+ def print_message
226
+ puts @message[:message]
227
+ end
228
+ end
229
+
230
+ class TestClass3
231
+
232
+ def initialize(testclass1, testclass2)
233
+ @class1 = testclass1
234
+ @class2 = testclass2
235
+ end
236
+
237
+ def show_messages
238
+ @class1.print_message
239
+ @class2.print_message
240
+ "awesome!!!"
241
+ end
242
+
243
+ end
244
+ ```
245
+
246
+ We then instantiate the class using all sorts of weird parameters:
247
+
248
+ ```ruby
249
+ Pretentious.spec_for(TestClass3) do
250
+ another_object = TestClass1.new("test")
251
+ test_class_one = TestClass1.new({hello: "world", test: another_object, arr_1: [1,2,3,4,5, another_object],
252
+ sub_hash: {yes: true, obj: another_object}})
253
+ test_class_two = TestClass2.new("This is message 2")
254
+
255
+ class_to_test = TestClass3.new(test_class_one, test_class_two)
256
+ class_to_test.show_messages
257
+ end
258
+ ```
259
+
260
+ Creating tests for TestClass3 should yield
261
+
262
+ ```ruby
263
+ RSpec.describe TestClass3 do
264
+
265
+ context 'Scenario 1' do
266
+ before do
267
+
268
+ var_2167529620 = "test"
269
+ another_object = TestClass1.new(var_2167529620)
270
+ args = {hello: "world", test: another_object, arr_1: [1, 2, 3, 4, 5, another_object], sub_hash: {yes: true, obj: another_object}}
271
+ test_class_one = TestClass1.new(args)
272
+ args_1 = "This is message 2"
273
+ test_class_two = TestClass2.new(args_1)
274
+
275
+ @fixture = TestClass3.new(test_class_one, test_class_two)
276
+
277
+ end
278
+
279
+ it 'should pass current expectations' do
280
+
281
+
282
+ # TestClass3#show_messages when passed should return awesome!!!
283
+ expect( @fixture.show_messages ).to eq("awesome!!!")
284
+
285
+ end
286
+ end
287
+ ```
288
+
289
+ Note that creating another instance of TestClass3 will result in the creation of another Scenario
290
+
144
291
  ## Things to do after
145
292
 
146
293
  Since your tests are already written, and hopefully nobody notices its written by a machine, you may just leave it
data/bin/ddtgen CHANGED
@@ -39,7 +39,7 @@ File.open(filename, "r") do |f|
39
39
  end
40
40
  end
41
41
 
42
- eval(example_body)
42
+ eval(example_body, binding, filename, 1)
43
43
 
44
44
  #collect results
45
45
 
data/example.rb CHANGED
@@ -1,70 +1,15 @@
1
- require 'digest/md5'
2
-
3
- class Fibonacci
4
-
5
- def fib(n)
6
- return 0 if (n == 0)
7
- return 1 if (n == 1)
8
- return 1 if (n == 2)
9
- return fib(n - 1) + fib(n - 2)
10
- end
11
-
12
- def self.say_hello
13
- "hello"
14
- end
15
-
16
- end
17
-
18
- class TestClass1
1
+ $LOAD_PATH << '.'
19
2
 
20
- def initialize(message)
21
- @message = message
22
- end
23
-
24
- def print_message
25
- puts @message
26
- end
27
-
28
- def something_is_wrong
29
- raise StandardError.new
30
- end
31
- end
32
-
33
- class TestClass2
34
- def initialize(message)
35
- @message = {message: message}
36
- end
37
-
38
- def print_message
39
- puts @message[:message]
40
- end
41
- end
42
-
43
- class TestClass3
44
-
45
- def initialize(testclass1, testclass2)
46
- @class1 = testclass1
47
- @class2 = testclass2
48
- end
49
-
50
- def get_hash
51
- {hello: "world", message: {another: :hash}}
52
- end
53
-
54
- def show_messages
55
- @class1.print_message
56
- @class2.print_message
57
- "awesome!!!"
58
- end
3
+ require 'digest/md5'
4
+ require_relative './test_classes.rb'
59
5
 
60
- end
61
6
 
62
7
  Pretentious.spec_for(Fibonacci) do
63
8
 
64
9
 
65
10
  instance = Fibonacci.new
66
11
 
67
- (1..10).each do |n|
12
+ (1..5).each do |n|
68
13
  instance.fib(n)
69
14
  end
70
15
 
@@ -72,7 +17,7 @@ Pretentious.spec_for(Fibonacci) do
72
17
 
73
18
  end
74
19
 
75
- Pretentious.spec_for(TestClass1, TestClass2, TestClass3) do
20
+ Pretentious.spec_for(TestClass1, TestClass2, TestClass3, TestClass4) do
76
21
 
77
22
  another_object = TestClass1.new("test")
78
23
  test_class_one = TestClass1.new({hello: "world", test: another_object, arr_1: [1,2,3,4,5, another_object],
@@ -85,9 +30,21 @@ Pretentious.spec_for(TestClass1, TestClass2, TestClass3) do
85
30
  class_to_test = TestClass3.new(test_class_one, test_class_two)
86
31
  class_to_test.show_messages
87
32
 
33
+ class_to_test4 = TestClass4.new {
34
+ another_object.message
35
+ }
36
+
37
+ test_class_one.set_block { |message|
38
+ message
39
+ }
40
+
41
+ test_class_one.call_block {
42
+ class_to_test4.message
43
+ }
44
+
88
45
  end
89
46
 
90
47
  Pretentious.spec_for(Digest::MD5) do
91
48
  sample = "This is the digest"
92
49
  Digest::MD5.hexdigest(sample)
93
- end
50
+ end
@@ -25,6 +25,10 @@ module Pretentious
25
25
  ":#{value.to_s}"
26
26
  elsif (value.is_a? Hash)
27
27
  Pretentious::Deconstructor.pick_name(let_variables, value.object_id, declared_names)
28
+ elsif (value.is_a? Pretentious::RecordedProc)
29
+ Pretentious::Deconstructor.pick_name(let_variables, value.target_proc.object_id, declared_names)
30
+ elsif (value == nil)
31
+ "nil"
28
32
  else
29
33
  "#{value.to_s}"
30
34
  end
@@ -36,6 +40,46 @@ module Pretentious
36
40
  Pretentious::Generator.unwatch_new_instances
37
41
  end
38
42
 
43
+ class RecordedProc < Proc
44
+
45
+ def initialize(target_proc, is_given_block = false)
46
+ @target_proc = target_proc
47
+ @return_value = []
48
+ @args = []
49
+ @given_block = is_given_block
50
+ @called = false
51
+ end
52
+
53
+ def given_block?
54
+ @given_block
55
+ end
56
+
57
+ def target_proc
58
+ @target_proc
59
+ end
60
+
61
+ def return_value
62
+ @return_value
63
+ end
64
+
65
+ def is_called?
66
+ @called
67
+ end
68
+
69
+ def call(*args, &block)
70
+ @called = true
71
+ @args << args
72
+ return_value = @target_proc.call(*args, &block)
73
+
74
+ unless @return_value.include? return_value
75
+ @return_value << return_value
76
+ end
77
+
78
+ return_value
79
+ end
80
+
81
+ end
82
+
39
83
  class Generator
40
84
 
41
85
  def self.impostor_for(module_space, klass)
@@ -58,16 +102,26 @@ module Pretentious
58
102
  @_let_variables[variable_value.object_id] = v
59
103
  }
60
104
 
105
+ #{newStandInKlass}.replace_procs_with_recorders(arguments)
106
+
61
107
  info_block = {}
62
108
  info_block[:method] = method_sym
63
109
  info_block[:params] = arguments
110
+
111
+ recordedProc = if (block)
112
+ RecordedProc.new(block, true)
113
+ else
114
+ nil
115
+ end
116
+ info_block[:block] = recordedProc
117
+
64
118
  info_block[:names] = @_instance.method(method_sym).parameters
65
119
 
66
120
  begin
67
121
  if (@_instance.methods.include? method_sym)
68
- result = @_instance.send(method_sym, *arguments, &block)
122
+ result = @_instance.send(method_sym, *arguments, &recordedProc)
69
123
  else
70
- result = @_instance.send(:method_missing, method_sym, *arguments, &block)
124
+ result = @_instance.send(:method_missing, method_sym, *arguments, &recordedProc)
71
125
  end
72
126
  info_block[:result] = result
73
127
  rescue Exception=>e
@@ -93,14 +147,26 @@ module Pretentious
93
147
  ")
94
148
 
95
149
  newStandInKlass.class_exec do
150
+
151
+
152
+
96
153
  def initialize(*args, &block)
97
154
 
98
155
  @_instance_init = {params: [], block: nil}
99
156
 
157
+ self.class.replace_procs_with_recorders(args)
158
+
100
159
  @_instance_init[:params] = args
101
- @_instance_init[:block] = block
102
160
 
103
- setup_instance(*args, &block)
161
+ recordedProc = if (block)
162
+ RecordedProc.new(block, true)
163
+ else
164
+ nil
165
+ end
166
+
167
+ @_instance_init[:block] = recordedProc
168
+
169
+ setup_instance(*args, &recordedProc)
104
170
 
105
171
 
106
172
  @_method_calls = []
@@ -183,6 +249,15 @@ module Pretentious
183
249
  end
184
250
 
185
251
  class << self
252
+
253
+ def replace_procs_with_recorders(args)
254
+ (0..args.size).each do |index|
255
+ if (args[index].kind_of? Proc)
256
+ args[index] = Pretentious::RecordedProc.new(args[index]) {}
257
+ end
258
+ end
259
+ end
260
+
186
261
  def _add_instances(instance)
187
262
  @_instances = @_instances || []
188
263
  @_instances << instance unless @_instances.include? instance
@@ -285,7 +360,7 @@ module Pretentious
285
360
  newStandInKlass._instances.each do |instance|
286
361
  test_generator.generate(instance, num)
287
362
  num+=1
288
- end
363
+ end unless newStandInKlass._instances.nil?
289
364
 
290
365
  test_generator.end_spec
291
366
 
@@ -296,7 +371,7 @@ module Pretentious
296
371
 
297
372
  all_results[klass] = test_generator.output
298
373
 
299
- }
374
+ } unless klasses.nil?
300
375
 
301
376
  all_results
302
377
  end
@@ -310,7 +385,9 @@ module Pretentious
310
385
  def _set_init_arguments(*args, &block)
311
386
  @_init_arguments = @_init_arguments || {}
312
387
  @_init_arguments[:params] = args
313
- @_init_arguments[:block] = block
388
+ unless (block.nil?)
389
+ @_init_arguments[:block] = RecordedProc.new(block) {}
390
+ end
314
391
  @_variable_names= {}
315
392
 
316
393
  index = 0
@@ -63,6 +63,15 @@ class Pretentious::Deconstructor
63
63
  definition[:value] = dfs_hash(tree[:composition], ref)
64
64
  elsif tree[:class] == Array
65
65
  definition[:value] = dfs_array(tree[:composition], ref)
66
+ elsif tree[:class] == Pretentious::RecordedProc
67
+ definition[:recorded_proc] = tree[:recorded_proc]
68
+
69
+ if (!tree[:composition].nil?)
70
+ ref << dfs(tree[:composition])
71
+ else
72
+ dfs(tree[:composition])
73
+ end
74
+
66
75
  elsif tree[:composition].is_a? Array
67
76
  tree[:composition].each { |t|
68
77
  ref << dfs(t)
@@ -71,6 +80,11 @@ class Pretentious::Deconstructor
71
80
  ref << dfs(tree[:composition])
72
81
  end
73
82
 
83
+ #evaluate given block composition
84
+ if (tree[:block])
85
+ ref << dfs(tree[:block])
86
+ end
87
+
74
88
  definition[:ref] = ref
75
89
 
76
90
  unless (@dependencies.include? tree[:id])
@@ -81,6 +95,41 @@ class Pretentious::Deconstructor
81
95
  end
82
96
  end
83
97
 
98
+ #creates a tree on how the object was created
99
+ def build_tree(target_object)
100
+
101
+ tree = {class: get_test_class(target_object), id: target_object.object_id, composition: []}
102
+ if (target_object.is_a? Array)
103
+ tree[:composition] = deconstruct_array(target_object)
104
+ elsif target_object.is_a? Hash
105
+ tree[:composition] = deconstruct_hash(target_object)
106
+ elsif target_object.is_a? Pretentious::RecordedProc
107
+ tree[:composition] = deconstruct_proc(target_object)
108
+ tree[:given_block] = target_object.given_block?
109
+ tree[:recorded_proc] = target_object
110
+ tree[:id] = target_object.target_proc.object_id
111
+ tree[:block_params] = self.class.block_param_names(target_object)
112
+ elsif target_object.methods.include? :_get_init_arguments
113
+ args = target_object._get_init_arguments
114
+
115
+ unless args.nil?
116
+
117
+ args[:params].each { |p|
118
+ tree[:composition] << build_tree(p)
119
+ }
120
+
121
+ tree[:block] = build_tree(args[:block]) unless args[:block].nil?
122
+ else
123
+ tree[:composition] = target_object
124
+ end
125
+
126
+ else
127
+ tree[:composition] = target_object
128
+ end
129
+ tree
130
+ end
131
+
132
+
84
133
  def deconstruct(*target_objects)
85
134
 
86
135
  @declaration_order = []
@@ -110,7 +159,7 @@ class Pretentious::Deconstructor
110
159
  declarations[:declaration].each do |d|
111
160
 
112
161
  var_name = Pretentious::Deconstructor.pick_name(variable_map, d[:id], declared_names)
113
- output_buffer << "#{indentation}#{var_name} = #{construct(d, variable_map, declared_names)}\n"
162
+ output_buffer << "#{indentation}#{var_name} = #{construct(d, variable_map, declared_names, indentation)}\n"
114
163
 
115
164
  end
116
165
 
@@ -122,6 +171,42 @@ class Pretentious::Deconstructor
122
171
  value.is_a?(NilClass) || value.is_a?(Symbol)
123
172
  end
124
173
 
174
+ def self.block_param_names(proc)
175
+ parameters_to_join = []
176
+
177
+ parameters = proc.target_proc.parameters
178
+
179
+ parameters.each { |p|
180
+ parameters_to_join << p[1].to_s
181
+ }
182
+ parameters_to_join
183
+ end
184
+
185
+ def self.block_params_generator(proc, separator = '|')
186
+
187
+ if (proc.target_proc.parameters.size > 0)
188
+ return "#{separator}#{block_param_names(proc).join(', ')}#{separator}"
189
+ end
190
+
191
+ return ''
192
+ end
193
+
194
+ def proc_to_ruby(proc, let_variables, declared, indentation = '')
195
+ output_buffer = ""
196
+ output_buffer << "Proc.new { #{self.class.block_params_generator(proc)}\n"
197
+ output_buffer << self.class.proc_body(proc, let_variables, declared, indentation)
198
+ output_buffer << "#{indentation}}\n"
199
+ output_buffer
200
+ end
201
+
202
+ def self.proc_body(proc, let_variables, declared,indentation = '')
203
+ if (proc.return_value.size == 1)
204
+ "#{indentation * 2}#{Pretentious::value_ize(proc.return_value[0], let_variables, declared)}\n"
205
+ else
206
+ "#{indentation * 2}\# Variable return values ... can't figure out what goes in here...\n"
207
+ end
208
+ end
209
+
125
210
  def deconstruct_array(array)
126
211
  composition = []
127
212
  array.each { |v|
@@ -154,36 +239,21 @@ class Pretentious::Deconstructor
154
239
  composition
155
240
  end
156
241
 
242
+ def deconstruct_proc(proc)
243
+ if (proc.return_value.size == 1)
244
+ return build_tree(proc.return_value[0]) unless proc.return_value[0].nil?
245
+ return nil
246
+ else
247
+ nil
248
+ end
249
+ end
250
+
157
251
  def get_test_class(target_object)
158
252
  target_object.respond_to?(:test_class) ? target_object.test_class : target_object.class
159
253
  end
160
254
 
161
- #creates a tree on how the object was created
162
- def build_tree(target_object)
163
255
 
164
- tree = {class: get_test_class(target_object), id: target_object.object_id, composition: []}
165
- if (target_object.is_a? Array)
166
- tree[:composition] = deconstruct_array(target_object)
167
- elsif target_object.is_a? Hash
168
- tree[:composition] = deconstruct_hash(target_object)
169
- elsif target_object.methods.include? :_get_init_arguments
170
-
171
- args = target_object._get_init_arguments
172
- unless args.nil?
173
- args[:params].each { |p|
174
- tree[:composition] << build_tree(p)
175
- }
176
-
177
- else
178
- tree[:composition] = target_object
179
- end
180
- else
181
- tree[:composition] = target_object
182
- end
183
- tree
184
- end
185
-
186
- def self.pick_name(variable_map, object_id, declared_names = {})
256
+ def self.pick_name(variable_map, object_id, declared_names = {}, value = :no_value_passed)
187
257
  var_name = "var_#{object_id}"
188
258
 
189
259
  object_id_to_declared_names = {}
@@ -215,6 +285,10 @@ class Pretentious::Deconstructor
215
285
  end
216
286
 
217
287
  end
288
+ else
289
+ if value != :no_value_passed
290
+ return Pretentious::value_ize(value, let_variables, declared_names)
291
+ end
218
292
  end
219
293
 
220
294
  var_name
@@ -266,8 +340,8 @@ class Pretentious::Deconstructor
266
340
  output_buffer
267
341
  end
268
342
 
269
- def construct(definition, variable_map, declared_names)
270
- if (definition[:value])
343
+ def construct(definition, variable_map, declared_names, indentation = '')
344
+ if (definition.include? :value)
271
345
  if (definition[:value].is_a? Hash)
272
346
  output_hash(definition[:value], variable_map, declared_names)
273
347
  elsif (definition[:value].is_a? Array)
@@ -275,11 +349,13 @@ class Pretentious::Deconstructor
275
349
  else
276
350
  Pretentious::value_ize(definition[:value], variable_map, declared_names)
277
351
  end
352
+ elsif (definition[:class] == Pretentious::RecordedProc)
353
+ proc_to_ruby(definition[:recorded_proc], variable_map, declared_names, indentation)
278
354
  else
279
355
  params = []
280
- if (definition[:ref].size > 0)
356
+ if (definition[:ref] && definition[:ref].size > 0)
281
357
  definition[:ref].each do |v|
282
- params << Pretentious::Deconstructor.pick_name(variable_map,v, declared_names)
358
+ params << Pretentious::Deconstructor.pick_name(variable_map, v, declared_names)
283
359
  end
284
360
  "#{definition[:class]}.new(#{params.join(', ')})"
285
361
  else
@@ -51,13 +51,30 @@ class Pretentious::RspecGenerator
51
51
 
52
52
  buffer("before do",2)
53
53
  whitespace
54
- args = test_instance._init_arguments[:params]
54
+
55
55
  declarations = {}
56
- buffer(declare_dependencies(args, test_instance.init_let_variables, 3 * @_indentation.length, declarations))
56
+ dependencies = []
57
+
58
+ args = test_instance._init_arguments[:params]
59
+ block = test_instance._init_arguments[:block]
60
+ dependencies = dependencies | args
61
+
62
+ unless block.nil?
63
+ dependencies << block
64
+ end
65
+
66
+ block_source = if !block.nil? && block.is_a?(Pretentious::RecordedProc)
67
+ get_block_source(block, test_instance.init_let_variables, declarations, @_indentation * 3)
68
+ else
69
+ ''
70
+ end
71
+
72
+
73
+ buffer(declare_dependencies(dependencies, test_instance.init_let_variables, 3 * @_indentation.length, declarations))
57
74
  if (args.size > 0)
58
- buffer("@fixture = #{test_instance.test_class.name}.new(#{params_generator(args, test_instance.init_let_variables, declarations)})",3)
75
+ buffer("@fixture = #{test_instance.test_class.name}.new(#{params_generator(args, test_instance.init_let_variables, declarations)})#{block_source}",3)
59
76
  else
60
- buffer("@fixture = #{test_instance.test_class.name}.new",3)
77
+ buffer("@fixture = #{test_instance.test_class.name}.new#{block_source}",3)
61
78
  end
62
79
  whitespace
63
80
  buffer("end",2)
@@ -75,11 +92,35 @@ class Pretentious::RspecGenerator
75
92
 
76
93
  private
77
94
 
78
- def generate_expectation(fixture, method, let_variables, declarations, params, result)
95
+
96
+
97
+ def proc_function_generator(block, method)
98
+ "func_#{method.to_s}(#{Pretentious::Deconstructor.block_params_generator(block)})"
99
+ end
100
+
101
+ def get_block_source(block,let_variables, declared,indentation)
102
+ #output = ''
103
+ #output << "{ #{Pretentious::Deconstructor.block_params_generator(block)}\n"
104
+ #output << Pretentious::Deconstructor.proc_body(block, let_variables, declared,indentation)
105
+ #output << "#{indentation}}"
106
+ #output
107
+ " &#{Pretentious::Deconstructor.pick_name(let_variables, block.target_proc.object_id, declared)}"
108
+ end
109
+
110
+ def generate_expectation(fixture, method, let_variables, declarations, params, block, result)
111
+ block_source = if !block.nil? && block.is_a?(Pretentious::RecordedProc)
112
+ get_block_source(block, let_variables, declarations, @_indentation * 3)
113
+ else
114
+ ''
115
+ end
116
+
79
117
  statement = if params.size > 0
80
- "#{fixture}.#{method.to_s}(#{params_generator(params, let_variables, declarations)})"
118
+ "#{fixture}.#{method.to_s}(#{params_generator(params, let_variables, declarations)})#{block_source}"
81
119
  else
82
- "#{fixture}.#{method.to_s}"
120
+ stmt = []
121
+ stmt << "#{fixture}.#{method.to_s}"
122
+ stmt << "#{block_source}" unless block_source.empty?
123
+ stmt.join(' ')
83
124
  end
84
125
 
85
126
  if (result.kind_of? Exception)
@@ -103,9 +144,16 @@ class Pretentious::RspecGenerator
103
144
  if (!Pretentious::Deconstructor.is_primitive?(block[:result]) && !block[:result].kind_of?(Exception))
104
145
  params_collection << block[:result]
105
146
  end
147
+
148
+ unless (block[:block].nil?)
149
+ params_collection << block[:block]
150
+ end
151
+
106
152
  end
153
+
107
154
  end
108
155
 
156
+
109
157
  buffer(declare_dependencies(params_collection, let_variables, 3 * @_indentation.length, declaration))
110
158
 
111
159
  method_calls.each_key do |k|
@@ -114,7 +162,7 @@ class Pretentious::RspecGenerator
114
162
  info_blocks_arr.each do |block|
115
163
 
116
164
  buffer("# #{context_prefix}#{k} when passed #{desc_params(block)} should return #{block[:result]}", 3)
117
- generate_expectation(fixture, k, let_variables, declaration, block[:params], block[:result])
165
+ generate_expectation(fixture, k, let_variables, declaration, block[:params], block[:block], block[:result])
118
166
 
119
167
  whitespace
120
168
  end
@@ -1,3 +1,3 @@
1
1
  module Pretentious
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  git add .
4
4
  gem build pretentious.gemspec
5
- gem install pretentious-0.0.1.gem
5
+ gem install pretentious-0.0.3.gem
6
6
  ruby test/test_generator.rb
@@ -17,11 +17,6 @@ RSpec.describe Fibonacci do
17
17
  n_2 = 3
18
18
  n_3 = 4
19
19
  n_4 = 5
20
- n_5 = 6
21
- n_6 = 7
22
- n_7 = 8
23
- n_8 = 9
24
- n_9 = 10
25
20
 
26
21
  # Fibonacci#fib when passed n = 1 should return 1
27
22
  expect( @fixture.fib(n) ).to eq(1)
@@ -38,21 +33,6 @@ RSpec.describe Fibonacci do
38
33
  # Fibonacci#fib when passed n = 5 should return 5
39
34
  expect( @fixture.fib(n_4) ).to eq(5)
40
35
 
41
- # Fibonacci#fib when passed n = 6 should return 8
42
- expect( @fixture.fib(n_5) ).to eq(8)
43
-
44
- # Fibonacci#fib when passed n = 7 should return 13
45
- expect( @fixture.fib(n_6) ).to eq(13)
46
-
47
- # Fibonacci#fib when passed n = 8 should return 21
48
- expect( @fixture.fib(n_7) ).to eq(21)
49
-
50
- # Fibonacci#fib when passed n = 9 should return 34
51
- expect( @fixture.fib(n_8) ).to eq(34)
52
-
53
- # Fibonacci#fib when passed n = 10 should return 55
54
- expect( @fixture.fib(n_9) ).to eq(55)
55
-
56
36
  end
57
37
  end
58
38
 
@@ -1,63 +1 @@
1
- require 'digest/md5'
2
- require 'json'
3
- require 'pretentious'
4
-
5
- class Fibonacci
6
-
7
- def fib(n)
8
- return 0 if (n == 0)
9
- return 1 if (n == 1)
10
- return 1 if (n == 2)
11
- return fib(n - 1) + fib(n - 2)
12
- end
13
-
14
- def self.say_hello
15
- "hello"
16
- end
17
-
18
- end
19
-
20
-
21
- class TestClass1
22
-
23
- def initialize(message)
24
- @message = message
25
- end
26
-
27
- def print_message
28
- puts @message
29
- end
30
-
31
- def something_is_wrong
32
- raise StandardError.new
33
- end
34
- end
35
-
36
- class TestClass2
37
- def initialize(message)
38
- @message = {message: message}
39
- end
40
-
41
- def print_message
42
- puts @message[:message]
43
- end
44
- end
45
-
46
- class TestClass3
47
-
48
- def initialize(testclass1, testclass2)
49
- @class1 = testclass1
50
- @class2 = testclass2
51
- end
52
-
53
- def get_hash
54
- {hello: "world", message: {another: :hash}}
55
- end
56
-
57
- def show_messages
58
- @class1.print_message
59
- @class2.print_message
60
- "awesome!!!"
61
- end
62
-
63
- end
1
+ require_relative '../test_classes'
@@ -13,22 +13,37 @@ RSpec.describe TestClass1 do
13
13
  it 'should pass current expectations' do
14
14
 
15
15
 
16
+ # TestClass1#message when passed should return test
17
+ expect( @fixture.message ).to eq("test")
18
+
16
19
  end
17
20
  end
18
21
 
19
22
  context 'Scenario 2' do
20
23
  before do
21
24
 
22
- var_2167529620 = "test"
23
- another_object = TestClass1.new(var_2167529620)
24
- var_2167516320 = {hello: "world", test: another_object, arr_1: [1, 2, 3, 4, 5, another_object], sub_hash: {yes: true, obj: another_object}}
25
+ var_2165592540 = "test"
26
+ another_object = TestClass1.new(var_2165592540)
27
+ var_2165586880 = {hello: "world", test: another_object, arr_1: [1, 2, 3, 4, 5, another_object], sub_hash: {yes: true, obj: another_object}}
25
28
 
26
- @fixture = TestClass1.new(var_2167516320)
29
+ @fixture = TestClass1.new(var_2165586880)
27
30
 
28
31
  end
29
32
 
30
33
  it 'should pass current expectations' do
31
34
 
35
+ var_2165592540 = "test"
36
+ another_object = TestClass1.new(var_2165592540)
37
+ var_2165586880 = {hello: "world", test: another_object, arr_1: [1, 2, 3, 4, 5, another_object], sub_hash: {yes: true, obj: another_object}}
38
+ var_2159732800 = Proc.new { |message|
39
+ var_2165586880
40
+ }
41
+
42
+ var_8 = nil
43
+ var_2159719320 = Proc.new {
44
+ # Variable return values ... can't figure out what goes in here...
45
+ }
46
+
32
47
 
33
48
  # TestClass1#print_message when passed should return
34
49
  expect( @fixture.print_message ).to be_nil
@@ -36,6 +51,12 @@ RSpec.describe TestClass1 do
36
51
  # TestClass1#print_message when passed should return
37
52
  expect( @fixture.print_message ).to be_nil
38
53
 
54
+ # TestClass1#set_block when passed should return #<Pretentious::RecordedProc:0x000001017568c0@example.rb:73>
55
+ expect( @fixture.set_block &var_2159732800 ).to eq(var_2159732800)
56
+
57
+ # TestClass1#call_block when passed should return {:hello=>"world", :test=>#<TestClass1:0x0000010228a1d8 @message="test", @_init_arguments={:params=>["test"]}, @_variable_names={2165592540=>"message"}>, :arr_1=>[1, 2, 3, 4, 5, #<TestClass1:0x0000010228a1d8 @message="test", @_init_arguments={:params=>["test"]}, @_variable_names={2165592540=>"message"}>], :sub_hash=>{:yes=>true, :obj=>#<TestClass1:0x0000010228a1d8 @message="test", @_init_arguments={:params=>["test"]}, @_variable_names={2165592540=>"message"}>}}
58
+ expect( @fixture.call_block &var_2159719320 ).to eq(var_2165586880)
59
+
39
60
  end
40
61
  end
41
62
 
@@ -5,8 +5,8 @@ RSpec.describe TestClass3 do
5
5
  context 'Scenario 1' do
6
6
  before do
7
7
 
8
- var_2167529620 = "test"
9
- another_object = TestClass1.new(var_2167529620)
8
+ var_2165592540 = "test"
9
+ another_object = TestClass1.new(var_2165592540)
10
10
  args = {hello: "world", test: another_object, arr_1: [1, 2, 3, 4, 5, another_object], sub_hash: {yes: true, obj: another_object}}
11
11
  test_class_one = TestClass1.new(args)
12
12
  args_1 = "This is message 2"
@@ -28,8 +28,8 @@ RSpec.describe TestClass3 do
28
28
  context 'Scenario 2' do
29
29
  before do
30
30
 
31
- var_2167529620 = "test"
32
- another_object = TestClass1.new(var_2167529620)
31
+ var_2165592540 = "test"
32
+ another_object = TestClass1.new(var_2165592540)
33
33
  args = {hello: "world", test: another_object, arr_1: [1, 2, 3, 4, 5, another_object], sub_hash: {yes: true, obj: another_object}}
34
34
  test_class_one = TestClass1.new(args)
35
35
  args_1 = "This is message 2"
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe TestClass4 do
4
+
5
+ context 'Scenario 1' do
6
+ before do
7
+
8
+ var_2165592540 = "test"
9
+ var_2159747700 = Proc.new {
10
+ "test"
11
+ }
12
+
13
+
14
+ @fixture = TestClass4.new &var_2159747700
15
+
16
+ end
17
+
18
+ it 'should pass current expectations' do
19
+
20
+
21
+ end
22
+ end
23
+
24
+ end
@@ -24,6 +24,14 @@ class TestClass1
24
24
  @message = message
25
25
  end
26
26
 
27
+ def set_block(&block)
28
+ @block=block
29
+ end
30
+
31
+ def call_block
32
+ @block.call(@message)
33
+ end
34
+
27
35
  def print_message
28
36
  puts @message
29
37
  end
@@ -33,6 +41,7 @@ class TestClass1
33
41
  end
34
42
  end
35
43
 
44
+
36
45
  class TestClass2
37
46
  def initialize(message)
38
47
  @message = {message: message}
@@ -56,49 +65,90 @@ class TestClass3
56
65
  "awesome!!!"
57
66
  end
58
67
 
59
- end
60
-
61
- #examples
62
-
63
- results_ddt = Pretentious::Generator.generate_for(Fibonacci) do
64
-
65
- instance = Fibonacci.new
66
-
67
- (1..10).each do |n|
68
- instance.fib(n)
68
+ def swap_hash(j, &block)
69
+ h = []
70
+ j.each do |k,v|
71
+ h << block.call(v,k)
72
+ end
73
+ h
69
74
  end
70
75
 
71
- Fibonacci.say_hello
72
- end
73
-
74
- results_ddt.each_value { |v| puts v}
76
+ def check_proc
77
+ @class2.call(1,2,3)
78
+ end
75
79
 
76
- results_md5 = Pretentious::Generator.generate_for(Digest::MD5) do
77
- sample = "This is the digest"
78
- Digest::MD5.hexdigest(sample)
79
80
  end
80
81
 
81
- results_md5.each_value { |v| puts v}
82
+ #examples
83
+ #
84
+ #results_ddt = Pretentious::Generator.generate_for(Fibonacci) do
85
+ #
86
+ # instance = Fibonacci.new
87
+ #
88
+ # (1..10).each do |n|
89
+ # instance.fib(n)
90
+ # end
91
+ #
92
+ # Fibonacci.say_hello
93
+ #end
94
+ #
95
+ #results_ddt.each_value { |v| puts v}
96
+ #
97
+ #results_md5 = Pretentious::Generator.generate_for(Digest::MD5) do
98
+ # sample = "This is the digest"
99
+ # Digest::MD5.hexdigest(sample)
100
+ #end
101
+ #
102
+ #results_md5.each_value { |v| puts v}
103
+
104
+ #results_composition = Pretentious::Generator.generate_for(TestClass3, TestClass2, TestClass1) do
105
+ # another_object = TestClass1.new("test")
106
+ # test_class_one = TestClass1.new({hello: "world", test: another_object, arr_1: [1,2,3,4,5, another_object],
107
+ # sub_hash: {yes: true, obj: another_object}})
108
+ # test_class_two = TestClass2.new("This is message 2")
109
+ #
110
+ # class_to_test = TestClass3.new(test_class_one, test_class_two)
111
+ # class_to_test.show_messages
112
+ #
113
+ # class_to_test = TestClass3.new(test_class_one, test_class_two)
114
+ # class_to_test.show_messages
115
+ #
116
+ # puts another_object._deconstruct_to_ruby
117
+ #
118
+ # class_to_test.swap_hash({a: 1, b: 2}) do |v, k|
119
+ # "#{k}_#{v}"
120
+ # end
121
+ #
122
+ # begin
123
+ # another_object.something_is_wrong
124
+ # rescue Exception=>e
125
+ # end
126
+ #
127
+ # class_to_test = TestClass3.new(test_class_one, Proc.new { |a,b,c| "hello world!"})
128
+ # class_to_test.check_proc
129
+ #
130
+ #
131
+ #
132
+ #end
133
+
134
+ results_composition = Pretentious::Generator.generate_for(TestClass1, TestClass2, TestClass3) do
82
135
 
83
- results_composition = Pretentious::Generator.generate_for(TestClass3, TestClass2, TestClass1) do
84
136
  another_object = TestClass1.new("test")
85
137
  test_class_one = TestClass1.new({hello: "world", test: another_object, arr_1: [1,2,3,4,5, another_object],
86
- sub_hash: {yes: true, obj: another_object}})
87
- test_class_two = TestClass2.new("This is message 2")
88
-
89
- class_to_test = TestClass3.new(test_class_one, test_class_two)
90
- class_to_test.show_messages
91
-
92
- class_to_test = TestClass3.new(test_class_one, test_class_two)
93
- class_to_test.show_messages
94
-
95
- puts another_object._deconstruct_to_ruby
96
-
97
- begin
98
- another_object.something_is_wrong
99
- rescue Exception=>e
100
- end
101
-
138
+ sub_hash: {yes: true, obj: another_object}})
139
+ #test_class_two = TestClass2.new("This is message 2")
140
+ #
141
+ #class_to_test = TestClass3.new(test_class_one, test_class_two)
142
+ #class_to_test.show_messages
143
+ #
144
+ #class_to_test = TestClass3.new(test_class_one, test_class_two)
145
+ #class_to_test.show_messages
146
+
147
+ test_class_one.set_block { |message|
148
+ message
149
+ }
150
+
151
+ test_class_one.call_block
102
152
 
103
153
  end
104
154
 
@@ -0,0 +1,98 @@
1
+ require 'digest/md5'
2
+
3
+ class Fibonacci
4
+
5
+ def fib(n)
6
+ return 0 if (n == 0)
7
+ return 1 if (n == 1)
8
+ return 1 if (n == 2)
9
+ return fib(n - 1) + fib(n - 2)
10
+ end
11
+
12
+ def self.say_hello
13
+ "hello"
14
+ end
15
+
16
+ end
17
+
18
+
19
+ class TestClass1
20
+
21
+ def initialize(message)
22
+ @message = message
23
+ end
24
+
25
+ def set_block(&block)
26
+ @block = block
27
+ end
28
+
29
+ def call_block
30
+ @block.call(@message)
31
+ end
32
+
33
+ def message
34
+ @message
35
+ end
36
+
37
+ def print_message
38
+ puts @message
39
+ end
40
+
41
+ def something_is_wrong
42
+ raise StandardError.new
43
+ end
44
+ end
45
+
46
+
47
+ class TestClass2
48
+ def initialize(message)
49
+ @message = {message: message}
50
+ end
51
+
52
+ def print_message
53
+ puts @message[:message]
54
+ end
55
+ end
56
+
57
+ class TestClass3
58
+
59
+ def initialize(testclass1, testclass2)
60
+ @class1 = testclass1
61
+ @class2 = testclass2
62
+ end
63
+
64
+ def show_messages
65
+ @class1.print_message
66
+ @class2.print_message
67
+ "awesome!!!"
68
+ end
69
+
70
+ def swap_hash(j, &block)
71
+ h = []
72
+ j.each do |k,v|
73
+ h << block.call(v,k)
74
+ end
75
+ h
76
+ end
77
+
78
+ def check_proc
79
+ @class2.call(1,2,3)
80
+ end
81
+
82
+ end
83
+
84
+ class TestClass4
85
+
86
+ def initialize(&block)
87
+ @message = block.call
88
+ end
89
+
90
+ def message
91
+ @message
92
+ end
93
+
94
+ def to_s
95
+ @message
96
+ end
97
+
98
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pretentious
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Emmanuel Dayo
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-28 00:00:00.000000000 Z
11
+ date: 2015-11-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: binding_of_caller
@@ -80,8 +80,9 @@ files:
80
80
  - spec/test_class1_spec.rb
81
81
  - spec/test_class2_spec.rb
82
82
  - spec/test_class3_spec.rb
83
+ - spec/test_class4_spec.rb
83
84
  - test/test_generator.rb
84
- - test_class3_spec.rb
85
+ - test_classes.rb
85
86
  homepage: https://github.com/jedld/pretentious
86
87
  licenses:
87
88
  - MIT
@@ -114,5 +115,6 @@ test_files:
114
115
  - spec/test_class1_spec.rb
115
116
  - spec/test_class2_spec.rb
116
117
  - spec/test_class3_spec.rb
118
+ - spec/test_class4_spec.rb
117
119
  - test/test_generator.rb
118
120
  has_rdoc:
@@ -1 +0,0 @@
1
- ["RSpec.describe TestClass3 do\n\n context 'Scenario 1' do\n before do\n\n var_2172810880 = \"test\"\n another_object = TestClass1.new(var_2172810880)\n message = {hello: \"world\", test: another_object, arr_1: [1, 2, 3, 4, 5, another_object], sub_hash: {yes: true, obj: another_object}}\n test_class_one = TestClass1.new(message)\n message_1 = \"This is message 2\"\n test_class_two = TestClass2.new(message_1)\n\n @fixture = TestClass3.new(test_class_one, test_class_two)\n\n end\n\n it 'should pass current expectations' do\n\n\n # TestClass3#show_messages when passed should return awesome!!!\n expect ( @fixture.show_messages ).to equal(\"awesome!!!\")\n\n end\n end\n\n context 'Scenario 2' do\n before do\n\n var_2172810880 = \"test\"\n another_object = TestClass1.new(var_2172810880)\n message = {hello: \"world\", test: another_object, arr_1: [1, 2, 3, 4, 5, another_object], sub_hash: {yes: true, obj: another_object}}\n test_class_one = TestClass1.new(message)\n message_1 = \"This is message 2\"\n test_class_two = TestClass2.new(message_1)\n\n @fixture = TestClass3.new(test_class_one, test_class_two)\n\n end\n\n it 'should pass current expectations' do\n\n\n # TestClass3#show_messages when passed should return awesome!!!\n expect ( @fixture.show_messages ).to equal(\"awesome!!!\")\n\n end\n end\n\nend\n"]