transpec 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +18 -0
- data/.rubocop.yml +13 -0
- data/.travis.yml +6 -0
- data/Gemfile +9 -0
- data/Guardfile +14 -0
- data/LICENSE.txt +22 -0
- data/README.md +37 -0
- data/Rakefile +27 -0
- data/bin/transpec +8 -0
- data/lib/transpec/ast/scanner.rb +51 -0
- data/lib/transpec/ast/scope_stack.rb +76 -0
- data/lib/transpec/cli.rb +162 -0
- data/lib/transpec/configuration.rb +40 -0
- data/lib/transpec/git.rb +24 -0
- data/lib/transpec/rewriter.rb +109 -0
- data/lib/transpec/syntax/double.rb +21 -0
- data/lib/transpec/syntax/matcher.rb +60 -0
- data/lib/transpec/syntax/method_stub.rb +142 -0
- data/lib/transpec/syntax/send_node_syntax.rb +39 -0
- data/lib/transpec/syntax/should.rb +49 -0
- data/lib/transpec/syntax/should_receive.rb +120 -0
- data/lib/transpec/syntax.rb +58 -0
- data/lib/transpec/util.rb +50 -0
- data/lib/transpec/version.rb +14 -0
- data/lib/transpec.rb +17 -0
- data/spec/.rubocop.yml +19 -0
- data/spec/spec_helper.rb +33 -0
- data/spec/spec_spec.rb +54 -0
- data/spec/support/file_helper.rb +25 -0
- data/spec/support/shared_context.rb +63 -0
- data/spec/transpec/ast/scanner_spec.rb +177 -0
- data/spec/transpec/ast/scope_stack_spec.rb +94 -0
- data/spec/transpec/cli_spec.rb +290 -0
- data/spec/transpec/configuration_spec.rb +52 -0
- data/spec/transpec/git_spec.rb +85 -0
- data/spec/transpec/rewriter_spec.rb +203 -0
- data/spec/transpec/syntax/double_spec.rb +88 -0
- data/spec/transpec/syntax/matcher_spec.rb +407 -0
- data/spec/transpec/syntax/method_stub_spec.rb +386 -0
- data/spec/transpec/syntax/should_receive_spec.rb +286 -0
- data/spec/transpec/syntax/should_spec.rb +262 -0
- data/spec/transpec/util_spec.rb +48 -0
- data/transpec.gemspec +32 -0
- metadata +233 -0
@@ -0,0 +1,407 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
module Transpec
|
6
|
+
class Syntax
|
7
|
+
describe Matcher do
|
8
|
+
include_context 'parsed objects'
|
9
|
+
include_context 'should object'
|
10
|
+
|
11
|
+
subject(:matcher) do
|
12
|
+
Matcher.new(should_object.matcher_node, in_example_group_context?, source_rewriter)
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:in_example_group_context?) { true }
|
16
|
+
|
17
|
+
describe '#method_name' do
|
18
|
+
context 'when it is operator matcher' do
|
19
|
+
let(:source) do
|
20
|
+
<<-END
|
21
|
+
it 'is 1' do
|
22
|
+
subject.should == 1
|
23
|
+
end
|
24
|
+
END
|
25
|
+
end
|
26
|
+
|
27
|
+
# (block
|
28
|
+
# (send nil :it
|
29
|
+
# (str "is 1"))
|
30
|
+
# (args)
|
31
|
+
# (send
|
32
|
+
# (send
|
33
|
+
# (send nil :subject) :should) :==
|
34
|
+
# (int 1)))
|
35
|
+
|
36
|
+
it 'returns the method name' do
|
37
|
+
matcher.method_name.should == :==
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'when it is non-operator matcher' do
|
42
|
+
let(:source) do
|
43
|
+
<<-END
|
44
|
+
it 'is 1' do
|
45
|
+
subject.should eq(1)
|
46
|
+
end
|
47
|
+
END
|
48
|
+
end
|
49
|
+
|
50
|
+
# (block
|
51
|
+
# (send nil :it
|
52
|
+
# (str "is 1"))
|
53
|
+
# (args)
|
54
|
+
# (send
|
55
|
+
# (send nil :subject) :should
|
56
|
+
# (send nil :eq
|
57
|
+
# (int 1))))
|
58
|
+
|
59
|
+
it 'returns the method name' do
|
60
|
+
matcher.method_name.should == :eq
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
describe '#correct_operator!' do
|
66
|
+
before do
|
67
|
+
matcher.correct_operator!(parenthesize_arg)
|
68
|
+
end
|
69
|
+
|
70
|
+
let(:parenthesize_arg) { true }
|
71
|
+
|
72
|
+
context 'when it is `== 1` form' do
|
73
|
+
let(:source) do
|
74
|
+
<<-END
|
75
|
+
it 'is 1' do
|
76
|
+
subject.should == 1
|
77
|
+
end
|
78
|
+
END
|
79
|
+
end
|
80
|
+
|
81
|
+
let(:expected_source) do
|
82
|
+
<<-END
|
83
|
+
it 'is 1' do
|
84
|
+
subject.should eq(1)
|
85
|
+
end
|
86
|
+
END
|
87
|
+
end
|
88
|
+
|
89
|
+
it 'converts into `eq(1)` form' do
|
90
|
+
rewritten_source.should == expected_source
|
91
|
+
end
|
92
|
+
|
93
|
+
# Operator methods allow their argument to be in the next line,
|
94
|
+
# but non-operator methods do not.
|
95
|
+
#
|
96
|
+
# [1] pry(main)> 1 ==
|
97
|
+
# [1] pry(main)* 1
|
98
|
+
# => true
|
99
|
+
# [2] pry(main)> 1.eql?
|
100
|
+
# ArgumentError: wrong number of arguments (0 for 1)
|
101
|
+
context 'and its argument is in the next line' do
|
102
|
+
let(:source) do
|
103
|
+
<<-END
|
104
|
+
it 'is 1' do
|
105
|
+
subject.should ==
|
106
|
+
1
|
107
|
+
end
|
108
|
+
END
|
109
|
+
end
|
110
|
+
|
111
|
+
let(:expected_source) do
|
112
|
+
<<-END
|
113
|
+
it 'is 1' do
|
114
|
+
subject.should eq(
|
115
|
+
1
|
116
|
+
)
|
117
|
+
end
|
118
|
+
END
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'inserts parentheses properly' do
|
122
|
+
rewritten_source.should == expected_source
|
123
|
+
end
|
124
|
+
|
125
|
+
context 'and false is passed as `parenthesize_arg` argument' do
|
126
|
+
let(:parenthesize_arg) { false }
|
127
|
+
|
128
|
+
it 'inserts parentheses properly because they are necessary' do
|
129
|
+
rewritten_source.should == expected_source
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
context "when it is `== { 'key' => 'value' }` form" do
|
136
|
+
let(:source) do
|
137
|
+
<<-END
|
138
|
+
it 'is 1' do
|
139
|
+
subject.should == { 'key' => 'value' }
|
140
|
+
end
|
141
|
+
END
|
142
|
+
end
|
143
|
+
|
144
|
+
let(:expected_source) do
|
145
|
+
<<-END
|
146
|
+
it 'is 1' do
|
147
|
+
subject.should eq({ 'key' => 'value' })
|
148
|
+
end
|
149
|
+
END
|
150
|
+
end
|
151
|
+
|
152
|
+
it "converts into `eq({ 'key' => 'value' })` form" do
|
153
|
+
rewritten_source.should == expected_source
|
154
|
+
end
|
155
|
+
|
156
|
+
context 'and false is passed as `parenthesize_arg` argument' do
|
157
|
+
let(:parenthesize_arg) { false }
|
158
|
+
|
159
|
+
it 'inserts parentheses to avoid the hash from be interpreted as a block' do
|
160
|
+
rewritten_source.should == expected_source
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
[
|
166
|
+
[:===, 'case-equals to'],
|
167
|
+
[:<, 'is less than'],
|
168
|
+
[:<=, 'is less than or equals to'],
|
169
|
+
[:>, 'is greater than'],
|
170
|
+
[:>=, 'is greater than or equals to']
|
171
|
+
].each do |operator, description|
|
172
|
+
context "when it is `#{operator} 1` form" do
|
173
|
+
let(:source) do
|
174
|
+
<<-END
|
175
|
+
it '#{description} 1' do
|
176
|
+
subject.should #{operator} 1
|
177
|
+
end
|
178
|
+
END
|
179
|
+
end
|
180
|
+
|
181
|
+
let(:expected_source) do
|
182
|
+
<<-END
|
183
|
+
it '#{description} 1' do
|
184
|
+
subject.should be #{operator} 1
|
185
|
+
end
|
186
|
+
END
|
187
|
+
end
|
188
|
+
|
189
|
+
it "converts into `be #{operator} 1` form" do
|
190
|
+
rewritten_source.should == expected_source
|
191
|
+
end
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
context 'when it is `=~ /pattern/` form' do
|
196
|
+
let(:source) do
|
197
|
+
<<-END
|
198
|
+
it 'matches the pattern' do
|
199
|
+
subject.should =~ /pattern/
|
200
|
+
end
|
201
|
+
END
|
202
|
+
end
|
203
|
+
|
204
|
+
let(:expected_source) do
|
205
|
+
<<-END
|
206
|
+
it 'matches the pattern' do
|
207
|
+
subject.should match(/pattern/)
|
208
|
+
end
|
209
|
+
END
|
210
|
+
end
|
211
|
+
|
212
|
+
it 'converts into `match(/pattern/)` form' do
|
213
|
+
rewritten_source.should == expected_source
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
context 'when it is `=~ [1, 2]` form' do
|
218
|
+
let(:source) do
|
219
|
+
<<-END
|
220
|
+
it 'contains 1 and 2' do
|
221
|
+
subject.should =~ [1, 2]
|
222
|
+
end
|
223
|
+
END
|
224
|
+
end
|
225
|
+
|
226
|
+
let(:expected_source) do
|
227
|
+
<<-END
|
228
|
+
it 'contains 1 and 2' do
|
229
|
+
subject.should match_array([1, 2])
|
230
|
+
end
|
231
|
+
END
|
232
|
+
end
|
233
|
+
|
234
|
+
it 'converts into `match_array([1, 2])` form' do
|
235
|
+
rewritten_source.should == expected_source
|
236
|
+
end
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
describe '#parenthesize!' do
|
241
|
+
before do
|
242
|
+
matcher.parenthesize!(always)
|
243
|
+
end
|
244
|
+
|
245
|
+
let(:always) { true }
|
246
|
+
|
247
|
+
context 'when its argument is already in parentheses' do
|
248
|
+
let(:source) do
|
249
|
+
<<-END
|
250
|
+
it 'is 1' do
|
251
|
+
subject.should eq(1)
|
252
|
+
end
|
253
|
+
END
|
254
|
+
end
|
255
|
+
|
256
|
+
it 'does nothing' do
|
257
|
+
rewritten_source.should == source
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
context 'when its argument is not in parentheses' do
|
262
|
+
let(:source) do
|
263
|
+
<<-END
|
264
|
+
it 'is 1' do
|
265
|
+
subject.should eq 1
|
266
|
+
end
|
267
|
+
END
|
268
|
+
end
|
269
|
+
|
270
|
+
context 'and true is passed as `always` argument' do
|
271
|
+
let(:always) { true }
|
272
|
+
|
273
|
+
let(:expected_source) do
|
274
|
+
<<-END
|
275
|
+
it 'is 1' do
|
276
|
+
subject.should eq(1)
|
277
|
+
end
|
278
|
+
END
|
279
|
+
end
|
280
|
+
|
281
|
+
it 'inserts parentheses' do
|
282
|
+
rewritten_source.should == expected_source
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
context 'and false is passed as `always` argument' do
|
287
|
+
let(:always) { false }
|
288
|
+
|
289
|
+
let(:expected_source) do
|
290
|
+
<<-END
|
291
|
+
it 'is 1' do
|
292
|
+
subject.should eq 1
|
293
|
+
end
|
294
|
+
END
|
295
|
+
end
|
296
|
+
|
297
|
+
it 'does not nothing' do
|
298
|
+
rewritten_source.should == expected_source
|
299
|
+
end
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
context 'when its multiple arguments are not in parentheses' do
|
304
|
+
let(:source) do
|
305
|
+
<<-END
|
306
|
+
it 'contains 1 and 2' do
|
307
|
+
subject.should include 1, 2
|
308
|
+
end
|
309
|
+
END
|
310
|
+
end
|
311
|
+
|
312
|
+
let(:expected_source) do
|
313
|
+
<<-END
|
314
|
+
it 'contains 1 and 2' do
|
315
|
+
subject.should include(1, 2)
|
316
|
+
end
|
317
|
+
END
|
318
|
+
end
|
319
|
+
|
320
|
+
it 'inserts parentheses' do
|
321
|
+
rewritten_source.should == expected_source
|
322
|
+
end
|
323
|
+
end
|
324
|
+
|
325
|
+
context 'when its argument is a string literal' do
|
326
|
+
let(:source) do
|
327
|
+
<<-END
|
328
|
+
it "is 'string'" do
|
329
|
+
subject.should eq 'string'
|
330
|
+
end
|
331
|
+
END
|
332
|
+
end
|
333
|
+
|
334
|
+
let(:expected_source) do
|
335
|
+
<<-END
|
336
|
+
it "is 'string'" do
|
337
|
+
subject.should eq('string')
|
338
|
+
end
|
339
|
+
END
|
340
|
+
end
|
341
|
+
|
342
|
+
it 'inserts parentheses' do
|
343
|
+
rewritten_source.should == expected_source
|
344
|
+
end
|
345
|
+
end
|
346
|
+
|
347
|
+
context 'when its argument is a here document' do
|
348
|
+
let(:source) do
|
349
|
+
<<-END
|
350
|
+
it 'returns the document' do
|
351
|
+
subject.should eq <<-HEREDOC
|
352
|
+
foo
|
353
|
+
HEREDOC
|
354
|
+
end
|
355
|
+
END
|
356
|
+
end
|
357
|
+
|
358
|
+
# (block
|
359
|
+
# (send nil :it
|
360
|
+
# (str "returns the document"))
|
361
|
+
# (args)
|
362
|
+
# (send
|
363
|
+
# (send nil :subject) :should
|
364
|
+
# (send nil :eq
|
365
|
+
# (str " foo\n"))))
|
366
|
+
|
367
|
+
it 'does nothing' do
|
368
|
+
rewritten_source.should == source
|
369
|
+
end
|
370
|
+
end
|
371
|
+
|
372
|
+
context 'when its argument is a here document with interpolation' do
|
373
|
+
let(:source) do
|
374
|
+
<<-'END'
|
375
|
+
it 'returns the document' do
|
376
|
+
string = 'foo'
|
377
|
+
subject.should eq <<-HEREDOC
|
378
|
+
#{string}
|
379
|
+
HEREDOC
|
380
|
+
end
|
381
|
+
END
|
382
|
+
end
|
383
|
+
|
384
|
+
# (block
|
385
|
+
# (send nil :it
|
386
|
+
# (str "returns the document"))
|
387
|
+
# (args)
|
388
|
+
# (begin
|
389
|
+
# (lvasgn :string
|
390
|
+
# (str "foo"))
|
391
|
+
# (send
|
392
|
+
# (send nil :subject) :should
|
393
|
+
# (send nil :eq
|
394
|
+
# (dstr
|
395
|
+
# (str " ")
|
396
|
+
# (begin
|
397
|
+
# (lvar :string))
|
398
|
+
# (str "\n"))))))
|
399
|
+
|
400
|
+
it 'does nothing' do
|
401
|
+
rewritten_source.should == source
|
402
|
+
end
|
403
|
+
end
|
404
|
+
end
|
405
|
+
end
|
406
|
+
end
|
407
|
+
end
|