pretentious 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,43 @@
1
+ module Pretentious
2
+
3
+ class RecordedProc < Proc
4
+
5
+ def initialize(target_proc, is_given_block = false)
6
+ @target_proc = target_proc
7
+ @return_value = []
8
+ @args = []
9
+ @given_block = is_given_block
10
+ @called = false
11
+ end
12
+
13
+ def given_block?
14
+ @given_block
15
+ end
16
+
17
+ def target_proc
18
+ @target_proc
19
+ end
20
+
21
+ def return_value
22
+ @return_value
23
+ end
24
+
25
+ def is_called?
26
+ @called
27
+ end
28
+
29
+ def call(*args, &block)
30
+ @called = true
31
+ @args << args
32
+ return_value = @target_proc.call(*args, &block)
33
+
34
+ unless @return_value.include? return_value
35
+ @return_value << return_value
36
+ end
37
+
38
+ return_value
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -69,8 +69,10 @@ class Pretentious::RspecGenerator
69
69
  ''
70
70
  end
71
71
 
72
+ if (dependencies.size > 0)
73
+ buffer(declare_dependencies(dependencies, test_instance.init_let_variables, 3 * @_indentation.length, declarations))
74
+ end
72
75
 
73
- buffer(declare_dependencies(dependencies, test_instance.init_let_variables, 3 * @_indentation.length, declarations))
74
76
  if (args.size > 0)
75
77
  buffer("@fixture = #{test_instance.test_class.name}.new(#{params_generator(args, test_instance.init_let_variables, declarations)})#{block_source}",3)
76
78
  else
@@ -136,6 +138,7 @@ class Pretentious::RspecGenerator
136
138
  declaration = {}
137
139
  #collect all params
138
140
  params_collection = []
141
+ mocks_collection = {}
139
142
 
140
143
  method_calls.each_key do |k|
141
144
  info_blocks_arr = method_calls[k]
@@ -149,12 +152,30 @@ class Pretentious::RspecGenerator
149
152
  params_collection << block[:block]
150
153
  end
151
154
 
155
+ block[:context][:calls].each do |mock_block|
156
+ k = "#{mock_block[:class]}_#{mock_block[:method]}"
157
+
158
+ if mocks_collection[k].nil?
159
+ mocks_collection[k] = []
160
+ end
161
+
162
+ mocks_collection[k] << mock_block
163
+ params_collection << mock_block[:result]
164
+
165
+ end if block[:context]
166
+
167
+
152
168
  end
153
169
 
154
170
  end
155
171
 
172
+ if (params_collection.size > 0)
173
+ buffer(declare_dependencies(params_collection, let_variables, 3 * @_indentation.length, declaration))
174
+ end
156
175
 
157
- buffer(declare_dependencies(params_collection, let_variables, 3 * @_indentation.length, declaration))
176
+ if (mocks_collection.keys.size > 0)
177
+ buffer(generate_rspec_stub(mocks_collection, let_variables, 3 * @_indentation.length, declaration))
178
+ end
158
179
 
159
180
  method_calls.each_key do |k|
160
181
  info_blocks_arr = method_calls[k]
@@ -178,6 +199,24 @@ class Pretentious::RspecGenerator
178
199
  buffer("end",2)
179
200
  end
180
201
 
202
+ def generate_rspec_stub(mocks_collection, let_variables, indentation_level , declaration)
203
+ indentation = ""
204
+
205
+ indentation_level.times {
206
+ indentation << ' '
207
+ }
208
+ str = ""
209
+ mocks_collection.each do |k,values|
210
+ vals = values.collect { |v| Pretentious::value_ize(v[:result], let_variables, declaration) }
211
+
212
+ #check if all vals are the same and just use one
213
+ vals = [vals[0]] if vals.uniq.size == 1
214
+
215
+ str << "#{indentation}allow_any_instance_of(#{values[0][:class].to_s}).to receive(:#{values[0][:method].to_s}).and_return(#{vals.join(', ')})\n"
216
+ end
217
+ str
218
+ end
219
+
181
220
  #def generate_specs(context_prefix, fixture, method_calls, let_variables)
182
221
  # method_calls.each_key do |k|
183
222
  # info_blocks_arr = method_calls[k]
@@ -1,3 +1,3 @@
1
1
  module Pretentious
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
data/run_test.sh CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  git add .
4
4
  gem build pretentious.gemspec
5
- gem install pretentious-0.0.7.gem
5
+ gem install pretentious-0.0.8.gem
6
6
  ruby test/test_generator.rb
@@ -5,7 +5,6 @@ RSpec.describe Fibonacci do
5
5
  context 'Scenario 1' do
6
6
  before do
7
7
 
8
-
9
8
  @fixture = Fibonacci.new
10
9
 
11
10
  end
@@ -38,7 +37,6 @@ RSpec.describe Fibonacci do
38
37
 
39
38
  it 'should pass current expectations' do
40
39
 
41
-
42
40
  # Fibonacci::say_hello should return hello
43
41
  expect( Fibonacci.say_hello ).to eq("hello")
44
42
 
@@ -0,0 +1,148 @@
1
+ require 'spec_helper'
2
+
3
+ class TestSubClass
4
+ def test_method
5
+ "a return string"
6
+ end
7
+ end
8
+
9
+ class TestClass
10
+
11
+ def initialize
12
+ @test_class2 = TestSubClass.new
13
+ end
14
+
15
+ def message(params1)
16
+ @params1 = params1
17
+ end
18
+
19
+ def method_with_assign=(params2)
20
+ @params2 = "#{params2}!"
21
+ end
22
+
23
+ def method_with_usage
24
+ @test_class2.test_method
25
+ end
26
+ end
27
+
28
+ class DummyGenerator
29
+
30
+ def initialize
31
+ @data = []
32
+ end
33
+
34
+ def begin_spec(test_class)
35
+ @data << {begin: test_class}
36
+ end
37
+
38
+ def generate(test_instance, instance_count)
39
+ @data << {instance: test_instance.class.to_s,
40
+ instance_method_calls: test_instance.method_calls,
41
+ instance_count: instance_count}
42
+ end
43
+
44
+ def end_spec()
45
+ @data << :end
46
+ end
47
+
48
+ def output
49
+ @data
50
+ end
51
+
52
+ end
53
+
54
+ RSpec.describe Pretentious::Generator do
55
+
56
+ context 'Pretentious::Generator#impostor_for' do
57
+ before do
58
+ @impostor = Pretentious::Generator.impostor_for(Object, TestClass)
59
+ end
60
+
61
+ it "should be a class" do
62
+ expect(@impostor.kind_of? Class).to be
63
+ end
64
+
65
+ it "subject class should be valid" do
66
+ expect(@impostor.test_class).to eq TestClass
67
+ end
68
+
69
+ it 'show contain the same methods' do
70
+ expect(@impostor.instance_methods).to include :message
71
+ expect(@impostor.instance_methods).to include :method_with_assign=
72
+ end
73
+ end
74
+
75
+ context "Pretentious::Generator#replace_class" do
76
+
77
+ around(:each) do |example|
78
+ @old_class = TestClass
79
+ module_space, klass, last, new_class = Pretentious::Generator.replace_class(TestClass)
80
+ @new_class = new_class
81
+ example.run
82
+ Pretentious::Generator.restore_class(module_space, klass, last)
83
+ end
84
+
85
+ it "new class should be named like the old class" do
86
+ expect(@new_class.to_s).to eq "TestClassImpostor"
87
+ expect(TestClass == @new_class).to be
88
+ end
89
+
90
+ it "a new kind of class should be created" do
91
+ expect(@old_class == @new_class).to_not be
92
+ end
93
+
94
+ it "should set instance variables like the real thing" do
95
+ instance = @new_class.new
96
+ instance.message("hello")
97
+ expect(instance.instance_variable_get(:@params1)).to eq "hello"
98
+ instance.method_with_assign="world"
99
+ expect(instance.instance_variable_get(:@params2)).to eq "world!"
100
+ end
101
+ end
102
+
103
+ context "Pretentious::Generator#generate_for" do
104
+ around(:each) do |example|
105
+ Pretentious::Generator.test_generator= DummyGenerator
106
+ example.run
107
+ Pretentious::Generator.test_generator= nil
108
+ end
109
+
110
+ it "generates call artifacts for target class" do
111
+ call_artifacts = Pretentious::Generator.generate_for(TestClass) do
112
+ instance = TestClass.new
113
+ instance.message("hello")
114
+ end
115
+
116
+ expect(call_artifacts).to eq({TestClass=>
117
+ [{:begin=>TestClass},
118
+ {:instance=>"TestClassImpostor",
119
+ :instance_method_calls=>
120
+ [{:method=>:message,
121
+ :params=>["hello"],
122
+ :block=>nil,
123
+ :names=>[[:req, :params1]],
124
+ :context=>{:calls=>[]}, :result=>"hello"}],
125
+ :instance_count=>1}, :end]})
126
+ end
127
+
128
+ context "auto mocks generator" do
129
+
130
+ it "generates a stub call structure" do
131
+
132
+ call_artifacts = Pretentious::Generator.generate_for(TestClass._stub(TestSubClass)) do
133
+ instance = TestClass.new
134
+ instance.method_with_usage
135
+ end
136
+
137
+ expect(call_artifacts).to eq({ TestClass => [{:begin=>TestClass},
138
+ {:instance=>"TestClassImpostor",
139
+ :instance_method_calls=>[{:method=>:method_with_usage,
140
+ :params=>[], :block=>nil, :names=>[],
141
+ :context=>{:calls=>[{:method=>:test_method, :params=>[],
142
+ :block=>nil, :names=>[], :result=>"a return string",
143
+ :class=>TestSubClass}]}, :result=>"a return string"}],
144
+ :instance_count=>1}, :end]})
145
+ end
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Pretentious::Generator do
4
+
5
+ context 'Pretentious::Deconstructor#build_tree' do
6
+
7
+ before do
8
+ @fixture = Pretentious::Generator.new
9
+ end
10
+
11
+ it "classes should have a stub class section" do
12
+ Fibonacci._stub(String)
13
+ expect(Fibonacci._get_mock_classes).to eq([String])
14
+ end
15
+
16
+ it "tracks object calls" do
17
+ result = Pretentious::Generator.generate_for(Fibonacci) do
18
+ Fibonacci.say_hello
19
+ end
20
+ expect(result).to eq({
21
+ Fibonacci => "require 'spec_helper'\n\nRSpec.describe Fibonacci do\n\n it 'should pass current expectations' do\n\n # Fibonacci::say_hello should return hello\n expect( Fibonacci.say_hello ).to eq(\"hello\")\n\n end\nend\n"
22
+ })
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -12,7 +12,6 @@ RSpec.describe TestClass1 do
12
12
 
13
13
  it 'should pass current expectations' do
14
14
 
15
-
16
15
  # TestClass1#message should return test
17
16
  expect( @fixture.message ).to eq("test")
18
17
 
@@ -22,25 +21,25 @@ RSpec.describe TestClass1 do
22
21
  context 'Scenario 2' do
23
22
  before do
24
23
 
25
- var_2157646020 = "test"
26
- another_object = TestClass1.new(var_2157646020)
27
- var_2157641020 = {hello: "world", test: another_object, arr_1: [1, 2, 3, 4, 5, another_object], sub_hash: {yes: true, obj: another_object}}
24
+ var_2158495560 = "test"
25
+ another_object = TestClass1.new(var_2158495560)
26
+ var_2158482220 = {hello: "world", test: another_object, arr_1: [1, 2, 3, 4, 5, another_object], sub_hash: {yes: true, obj: another_object}}
28
27
 
29
- @fixture = TestClass1.new(var_2157641020)
28
+ @fixture = TestClass1.new(var_2158482220)
30
29
 
31
30
  end
32
31
 
33
32
  it 'should pass current expectations' do
34
33
 
35
- var_2157646020 = "test"
36
- another_object = TestClass1.new(var_2157646020)
37
- var_2157641020 = {hello: "world", test: another_object, arr_1: [1, 2, 3, 4, 5, another_object], sub_hash: {yes: true, obj: another_object}}
38
- var_2157563100 = Proc.new { |message|
39
- var_2157641020
34
+ var_2158495560 = "test"
35
+ another_object = TestClass1.new(var_2158495560)
36
+ var_2158482220 = {hello: "world", test: another_object, arr_1: [1, 2, 3, 4, 5, another_object], sub_hash: {yes: true, obj: another_object}}
37
+ var_2158348900 = Proc.new { |message|
38
+ var_2158482220
40
39
  }
41
40
 
42
41
  e = nil
43
- var_2157558520 = Proc.new {
42
+ var_2158332200 = Proc.new {
44
43
  # Variable return values ... can't figure out what goes in here...
45
44
  }
46
45
 
@@ -51,11 +50,11 @@ RSpec.describe TestClass1 do
51
50
  # TestClass1#print_message should return
52
51
  expect( @fixture.print_message ).to be_nil
53
52
 
54
- # TestClass1#set_block should return #<Pretentious::RecordedProc:0x000001013377d0@example.rb:71>
55
- expect( @fixture.set_block &var_2157563100 ).to eq(var_2157563100)
53
+ # TestClass1#set_block should return #<Pretentious::RecordedProc:0x000001014b2380@example.rb:71>
54
+ expect( @fixture.set_block &var_2158348900 ).to eq(var_2158348900)
56
55
 
57
- # TestClass1#call_block should return {:hello=>"world", :test=>#<TestClass1:0x00000101361f80 @message="test", @_init_arguments={:params=>["test"]}, @_variable_names={2157646020=>"message"}>, :arr_1=>[1, 2, 3, 4, 5, #<TestClass1:0x00000101361f80 @message="test", @_init_arguments={:params=>["test"]}, @_variable_names={2157646020=>"message"}>], :sub_hash=>{:yes=>true, :obj=>#<TestClass1:0x00000101361f80 @message="test", @_init_arguments={:params=>["test"]}, @_variable_names={2157646020=>"message"}>}}
58
- expect( @fixture.call_block &var_2157558520 ).to eq(var_2157641020)
56
+ # TestClass1#call_block should return {:hello=>"world", :test=>#<TestClass1:0x00000101500d00 @message="test", @_init_arguments={:params=>["test"]}, @_variable_names={2158495560=>"message"}>, :arr_1=>[1, 2, 3, 4, 5, #<TestClass1:0x00000101500d00 @message="test", @_init_arguments={:params=>["test"]}, @_variable_names={2158495560=>"message"}>], :sub_hash=>{:yes=>true, :obj=>#<TestClass1:0x00000101500d00 @message="test", @_init_arguments={:params=>["test"]}, @_variable_names={2158495560=>"message"}>}}
57
+ expect( @fixture.call_block &var_2158332200 ).to eq(var_2158482220)
59
58
 
60
59
  # TestClass1#something_is_wrong should return StandardError
61
60
  expect { @fixture.something_is_wrong }.to raise_error
@@ -12,7 +12,6 @@ RSpec.describe TestClass2 do
12
12
 
13
13
  it 'should pass current expectations' do
14
14
 
15
-
16
15
  # TestClass2#print_message should return
17
16
  expect( @fixture.print_message ).to be_nil
18
17
 
@@ -5,8 +5,8 @@ RSpec.describe TestClass3 do
5
5
  context 'Scenario 1' do
6
6
  before do
7
7
 
8
- var_2157646020 = "test"
9
- another_object = TestClass1.new(var_2157646020)
8
+ var_2158495560 = "test"
9
+ another_object = TestClass1.new(var_2158495560)
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"
@@ -18,7 +18,6 @@ RSpec.describe TestClass3 do
18
18
 
19
19
  it 'should pass current expectations' do
20
20
 
21
-
22
21
  # TestClass3#show_messages should return awesome!!!
23
22
  expect( @fixture.show_messages ).to eq("awesome!!!")
24
23
 
@@ -28,8 +27,8 @@ RSpec.describe TestClass3 do
28
27
  context 'Scenario 2' do
29
28
  before do
30
29
 
31
- var_2157646020 = "test"
32
- another_object = TestClass1.new(var_2157646020)
30
+ var_2158495560 = "test"
31
+ another_object = TestClass1.new(var_2158495560)
33
32
  args = {hello: "world", test: another_object, arr_1: [1, 2, 3, 4, 5, another_object], sub_hash: {yes: true, obj: another_object}}
34
33
  test_class_one = TestClass1.new(args)
35
34
  args_1 = "This is message 2"
@@ -41,7 +40,6 @@ RSpec.describe TestClass3 do
41
40
 
42
41
  it 'should pass current expectations' do
43
42
 
44
-
45
43
  # TestClass3#show_messages should return awesome!!!
46
44
  expect( @fixture.show_messages ).to eq("awesome!!!")
47
45
 
@@ -5,19 +5,18 @@ RSpec.describe TestClass4 do
5
5
  context 'Scenario 1' do
6
6
  before do
7
7
 
8
- var_2157646020 = "test"
9
- var_2157574220 = Proc.new {
8
+ var_2158495560 = "test"
9
+ var_2158363380 = Proc.new {
10
10
  "test"
11
11
  }
12
12
 
13
13
 
14
- @fixture = TestClass4.new &var_2157574220
14
+ @fixture = TestClass4.new &var_2158363380
15
15
 
16
16
  end
17
17
 
18
18
  it 'should pass current expectations' do
19
19
 
20
-
21
20
  end
22
21
  end
23
22