-
Notifications
You must be signed in to change notification settings - Fork 52
/
should.rb
90 lines (72 loc) · 2.41 KB
/
should.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# coding: utf-8
require 'transpec/syntax'
require 'transpec/syntax/mixin/should_base'
require 'transpec/syntax/mixin/monkey_patch'
require 'transpec/syntax/mixin/expectizable'
require 'transpec/util'
module Transpec
class Syntax
class Should < Syntax
include Mixin::ShouldBase, Mixin::MonkeyPatch, Mixin::Expectizable, Util
define_dynamic_analysis do |rewriter|
register_syntax_availability_analysis_request(
rewriter,
:expect_available?,
[:expect]
)
end
def dynamic_analysis_target?
super && receiver_node && [:should, :should_not].include?(method_name)
end
def expectize!(negative_form = 'not_to')
fail ContextError.new("##{method_name}", '#expect', selector_range) unless expect_available?
if proc_subject?
replace(range_of_subject_method_taking_block, 'expect')
else
wrap_subject_in_expect!
end
replace(should_range, positive? ? 'to' : negative_form)
@current_syntax_type = :expect
add_record(RecordBuilder.build(self, negative_form))
end
def proc_subject?
return true if proc_literal?(subject_node)
return false unless subject_node.block_type?
send_node = subject_node.children.first
receiver_node, method_name, = *send_node
receiver_node.nil? && method_name == :expect
end
def name_of_subject_method_taking_block
range_of_subject_method_taking_block.source
end
private
def expect_available?
syntax_available?(__method__)
end
def range_of_subject_method_taking_block
send_node = subject_node.children.first
send_node.loc.expression
end
class RecordBuilder < Transpec::RecordBuilder
param_names :should, :negative_form_of_to
def old_syntax
syntax = if should.proc_subject?
"#{should.name_of_subject_method_taking_block} { }.should"
else
'obj.should'
end
syntax << '_not' unless should.positive?
syntax
end
def new_syntax
syntax = if should.proc_subject?
'expect { }.'
else
'expect(obj).'
end
syntax << (should.positive? ? 'to' : negative_form_of_to)
end
end
end
end
end