transpec 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.rubocop.yml +13 -0
  4. data/.travis.yml +6 -0
  5. data/Gemfile +9 -0
  6. data/Guardfile +14 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +37 -0
  9. data/Rakefile +27 -0
  10. data/bin/transpec +8 -0
  11. data/lib/transpec/ast/scanner.rb +51 -0
  12. data/lib/transpec/ast/scope_stack.rb +76 -0
  13. data/lib/transpec/cli.rb +162 -0
  14. data/lib/transpec/configuration.rb +40 -0
  15. data/lib/transpec/git.rb +24 -0
  16. data/lib/transpec/rewriter.rb +109 -0
  17. data/lib/transpec/syntax/double.rb +21 -0
  18. data/lib/transpec/syntax/matcher.rb +60 -0
  19. data/lib/transpec/syntax/method_stub.rb +142 -0
  20. data/lib/transpec/syntax/send_node_syntax.rb +39 -0
  21. data/lib/transpec/syntax/should.rb +49 -0
  22. data/lib/transpec/syntax/should_receive.rb +120 -0
  23. data/lib/transpec/syntax.rb +58 -0
  24. data/lib/transpec/util.rb +50 -0
  25. data/lib/transpec/version.rb +14 -0
  26. data/lib/transpec.rb +17 -0
  27. data/spec/.rubocop.yml +19 -0
  28. data/spec/spec_helper.rb +33 -0
  29. data/spec/spec_spec.rb +54 -0
  30. data/spec/support/file_helper.rb +25 -0
  31. data/spec/support/shared_context.rb +63 -0
  32. data/spec/transpec/ast/scanner_spec.rb +177 -0
  33. data/spec/transpec/ast/scope_stack_spec.rb +94 -0
  34. data/spec/transpec/cli_spec.rb +290 -0
  35. data/spec/transpec/configuration_spec.rb +52 -0
  36. data/spec/transpec/git_spec.rb +85 -0
  37. data/spec/transpec/rewriter_spec.rb +203 -0
  38. data/spec/transpec/syntax/double_spec.rb +88 -0
  39. data/spec/transpec/syntax/matcher_spec.rb +407 -0
  40. data/spec/transpec/syntax/method_stub_spec.rb +386 -0
  41. data/spec/transpec/syntax/should_receive_spec.rb +286 -0
  42. data/spec/transpec/syntax/should_spec.rb +262 -0
  43. data/spec/transpec/util_spec.rb +48 -0
  44. data/transpec.gemspec +32 -0
  45. metadata +233 -0
@@ -0,0 +1,85 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Transpec
6
+ describe Git do
7
+ include_context 'isolated environment'
8
+
9
+ describe '.command_available?' do
10
+ subject { Git.command_available? }
11
+
12
+ context 'when git command is found in PATH' do
13
+ it { should be_true }
14
+ end
15
+
16
+ context 'when git command is not found in PATH' do
17
+ before { stub_const('Transpec::Git::GIT', 'non-existent-command') }
18
+ it { should be_false }
19
+ end
20
+ end
21
+
22
+ shared_context 'inside of git repository' do
23
+ around do |example|
24
+ Dir.mkdir('repo')
25
+ Dir.chdir('repo') do
26
+ `git init`
27
+ example.run
28
+ end
29
+ end
30
+ end
31
+
32
+ describe '.inside_of_repository?' do
33
+ subject { Git.inside_of_repository? }
34
+
35
+ context 'when the current directory is inside of git repository' do
36
+ include_context 'inside of git repository'
37
+ it { should be_true }
38
+ end
39
+
40
+ context 'when the current directory is not inside of git repository' do
41
+ it { should be_false }
42
+ end
43
+ end
44
+
45
+ describe '.clean?' do
46
+ include_context 'inside of git repository'
47
+
48
+ subject { Git.clean? }
49
+
50
+ before do
51
+ File.write('foo', 'This is a sample file')
52
+ `git add .`
53
+ `git commit -m 'Initial commit'`
54
+ end
55
+
56
+ context 'when there are no changes' do
57
+ it { should be_true }
58
+ end
59
+
60
+ context 'when there is an untracked file' do
61
+ before { File.write('bar', 'This is an untracked file') }
62
+ it { should be_false }
63
+ end
64
+
65
+ context 'when there is a deleted file' do
66
+ before { File.delete('foo') }
67
+ it { should be_false }
68
+ end
69
+
70
+ context 'when there is a not staged change' do
71
+ before { File.write('foo', 'This is modified content') }
72
+ it { should be_false }
73
+ end
74
+
75
+ context 'when there is a staged change' do
76
+ before do
77
+ File.write('foo', 'This is modified content')
78
+ `git add .`
79
+ end
80
+
81
+ it { should be_false }
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,203 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Transpec
6
+ describe Rewriter do
7
+ subject(:rewriter) { Rewriter.new(configuration) }
8
+ let(:configuration) { Configuration.new }
9
+
10
+ describe '#rewrite_file!' do
11
+ include_context 'isolated environment'
12
+
13
+ let(:file_path) { 'sample_spec.rb' }
14
+
15
+ before do
16
+ File.write(file_path, 'This is a spec')
17
+ rewriter.stub(:rewrite).and_return('This is the rewritten spec')
18
+ end
19
+
20
+ it 'overwrites the passed file path' do
21
+ rewriter.rewrite_file!(file_path)
22
+ File.read(file_path).should == 'This is the rewritten spec'
23
+ end
24
+ end
25
+
26
+ describe '#rewrite' do
27
+ subject { rewriter.rewrite(source) }
28
+
29
+ let(:source) do
30
+ <<-END
31
+ describe 'example group' do
32
+ it 'is an example' do
33
+ something = mock('something')
34
+ something.stub!(:message)
35
+ something.should_receive(:message)
36
+ something.should_not == 'foo'
37
+ end
38
+ end
39
+ END
40
+ end
41
+
42
+ context 'when the configuration #convert_to_expect_to_matcher? is true' do
43
+ before { configuration.convert_to_expect_to_matcher = true }
44
+
45
+ context 'and configuration #negative_form_of_to is "not_to"' do
46
+ before { configuration.negative_form_of_to = 'not_to' }
47
+
48
+ it 'invokes Should#expectize! with "not_to"' do
49
+ Syntax::Should.any_instance.should_receive(:expectize!).with('not_to', anything)
50
+ rewriter.rewrite(source)
51
+ end
52
+ end
53
+
54
+ context 'and configuration #negative_form_of_to is "to_not"' do
55
+ before { configuration.negative_form_of_to = 'to_not' }
56
+
57
+ it 'invokes Should#expectize! with "to_not"' do
58
+ Syntax::Should.any_instance.should_receive(:expectize!).with('to_not', anything)
59
+ rewriter.rewrite(source)
60
+ end
61
+ end
62
+
63
+ context 'and configuration #parenthesize_matcher_arg is true' do
64
+ before { configuration.parenthesize_matcher_arg = true }
65
+
66
+ it 'invokes Should#expectize! with true as second argument' do
67
+ Syntax::Should.any_instance.should_receive(:expectize!).with(anything, true)
68
+ rewriter.rewrite(source)
69
+ end
70
+ end
71
+
72
+ context 'and configuration #parenthesize_matcher_arg is false' do
73
+ before { configuration.parenthesize_matcher_arg = false }
74
+
75
+ it 'invokes Should#expectize! with false as second argument' do
76
+ Syntax::Should.any_instance.should_receive(:expectize!).with(anything, false)
77
+ rewriter.rewrite(source)
78
+ end
79
+ end
80
+ end
81
+
82
+ context 'when the configuration #convert_to_expect_to_matcher? is false' do
83
+ before { configuration.convert_to_expect_to_matcher = false }
84
+
85
+ it 'does not invoke Should#expectize!' do
86
+ Syntax::Should.any_instance.should_not_receive(:expectize!)
87
+ rewriter.rewrite(source)
88
+ end
89
+ end
90
+
91
+ context 'when the configuration #convert_to_expect_to_receive? is true' do
92
+ before { configuration.convert_to_expect_to_receive = true }
93
+
94
+ context 'and configuration #negative_form_of_to is "not_to"' do
95
+ before { configuration.negative_form_of_to = 'not_to' }
96
+
97
+ it 'invokes ShouldReceive#expectize! with "not_to"' do
98
+ Syntax::ShouldReceive.any_instance.should_receive(:expectize!).with('not_to')
99
+ rewriter.rewrite(source)
100
+ end
101
+ end
102
+
103
+ context 'and configuration #negative_form_of_to is "to_not"' do
104
+ before { configuration.negative_form_of_to = 'to_not' }
105
+
106
+ it 'invokes ShouldReceive#expectize! with "to_not"' do
107
+ Syntax::ShouldReceive.any_instance.should_receive(:expectize!).with('to_not')
108
+ rewriter.rewrite(source)
109
+ end
110
+ end
111
+ end
112
+
113
+ context 'when the configuration #convert_to_expect_to_receive? is false' do
114
+ before { configuration.convert_to_expect_to_receive = false }
115
+
116
+ it 'does not invoke ShouldReceive#expectize!' do
117
+ Syntax::ShouldReceive.any_instance.should_not_receive(:expectize!)
118
+ rewriter.rewrite(source)
119
+ end
120
+ end
121
+
122
+ context 'when the configuration #convert_to_allow_to_receive? is true' do
123
+ before { configuration.convert_to_allow_to_receive = true }
124
+
125
+ it 'invokes MethodStub#allowize!' do
126
+ Syntax::MethodStub.any_instance.should_receive(:allowize!)
127
+ rewriter.rewrite(source)
128
+ end
129
+ end
130
+
131
+ context 'when the configuration #convert_to_allow_to_receive? is false' do
132
+ before { configuration.convert_to_allow_to_receive = false }
133
+
134
+ it 'does not invoke MethodStub#allowize!' do
135
+ Syntax::MethodStub.any_instance.should_not_receive(:allowize!)
136
+ rewriter.rewrite(source)
137
+ end
138
+ end
139
+
140
+ context 'when the source have overlapped rewrite targets' do
141
+ let(:source) do
142
+ <<-END
143
+ describe 'example group' do
144
+ it 'is an example' do
145
+ object.stub(:message => mock('something'))
146
+ end
147
+ end
148
+ END
149
+ end
150
+
151
+ let(:expected_source) do
152
+ <<-END
153
+ describe 'example group' do
154
+ it 'is an example' do
155
+ allow(object).to receive(:message).and_return(double('something'))
156
+ end
157
+ end
158
+ END
159
+ end
160
+
161
+ it 'rewrites all targets properly' do
162
+ should == expected_source
163
+ end
164
+ end
165
+
166
+ context 'when the source have a monkey-patched expectation outside of example group context' do
167
+ before do
168
+ configuration.convert_to_expect_to_matcher = true
169
+ rewriter.stub(:warn)
170
+ end
171
+
172
+ let(:source) do
173
+ <<-END
174
+ describe 'example group' do
175
+ class SomeClass
176
+ def some_method
177
+ 1.should == 1
178
+ end
179
+ end
180
+
181
+ it 'is an example' do
182
+ SomeClass.new.some_method
183
+ end
184
+ end
185
+ END
186
+ end
187
+
188
+ it 'does not rewrite the expectation to non-monkey-patch syntax' do
189
+ should == source
190
+ end
191
+
192
+ it 'warns to user' do
193
+ rewriter.should_receive(:warn) do |message|
194
+ message.should =~ /cannot/i
195
+ message.should =~ /context/i
196
+ end
197
+
198
+ rewriter.rewrite(source)
199
+ end
200
+ end
201
+ end
202
+ end
203
+ end
@@ -0,0 +1,88 @@
1
+ # coding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ module Transpec
6
+ class Syntax
7
+ describe Double do
8
+ include_context 'parsed objects'
9
+
10
+ subject(:double_object) do
11
+ AST::Scanner.scan(ast) do |node, ancestor_nodes|
12
+ next unless Double.target_node?(node)
13
+ return Double.new(
14
+ node,
15
+ ancestor_nodes,
16
+ in_example_group_context?,
17
+ source_rewriter
18
+ )
19
+ end
20
+ fail 'No double node is found!'
21
+ end
22
+
23
+ let(:in_example_group_context?) { true }
24
+
25
+ describe '#method_name' do
26
+ let(:source) do
27
+ <<-END
28
+ it 'includes something' do
29
+ something = double('something')
30
+ [1, something].should include(something)
31
+ end
32
+ END
33
+ end
34
+
35
+ it 'returns the method name' do
36
+ double_object.method_name.should == :double
37
+ end
38
+ end
39
+
40
+ describe '#replace_deprecated_method!' do
41
+ before do
42
+ double_object.replace_deprecated_method!
43
+ end
44
+
45
+ [:mock, :stub].each do |method|
46
+ context "when it is ##{method}" do
47
+ let(:source) do
48
+ <<-END
49
+ it 'includes something' do
50
+ something = #{method}('something')
51
+ [1, something].should include(something)
52
+ end
53
+ END
54
+ end
55
+
56
+ let(:expected_source) do
57
+ <<-END
58
+ it 'includes something' do
59
+ something = double('something')
60
+ [1, something].should include(something)
61
+ end
62
+ END
63
+ end
64
+
65
+ it 'replaces with #double' do
66
+ rewritten_source.should == expected_source
67
+ end
68
+ end
69
+ end
70
+
71
+ context 'when it is #double' do
72
+ let(:source) do
73
+ <<-END
74
+ it 'includes something' do
75
+ something = double('something')
76
+ [1, something].should include(something)
77
+ end
78
+ END
79
+ end
80
+
81
+ it 'does nothing' do
82
+ rewritten_source.should == source
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end