Class: Transpec::Syntax::Have::DynamicInspector

Inherits:
Object
  • Object
show all
Defined in:
lib/transpec/syntax/have/dynamic_inspector.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(have, rewriter) ⇒ DynamicInspector

Returns a new instance of DynamicInspector.



15
16
17
18
# File 'lib/transpec/syntax/have/dynamic_inspector.rb', line 15

def initialize(have, rewriter)
  @have = have
  @rewriter = rewriter
end

Instance Attribute Details

#haveObject (readonly)

Returns the value of attribute have.



13
14
15
# File 'lib/transpec/syntax/have/dynamic_inspector.rb', line 13

def have
  @have
end

#rewriterObject (readonly)

Returns the value of attribute rewriter.



13
14
15
# File 'lib/transpec/syntax/have/dynamic_inspector.rb', line 13

def rewriter
  @rewriter
end

Class Method Details

.register_request(have, rewriter) ⇒ Object



9
10
11
# File 'lib/transpec/syntax/have/dynamic_inspector.rb', line 9

def self.register_request(have, rewriter)
  new(have, rewriter).register_request
end

Instance Method Details

#available_query_methods_inspection_codeObject

rubocop:enable MethodLength



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/transpec/syntax/have/dynamic_inspector.rb', line 105

def available_query_methods_inspection_code
  <<-END.gsub(/^\s+\|/, '').chomp
    |collection_accessor = #{collection_accessor_inspection_code}
    |target = if collection_accessor
    |           #{subject_code}.__send__(collection_accessor)
    |         else
    |           #{subject_code}
    |         end
    |target.methods & #{QUERY_METHOD_PRIORITIES.inspect}
  END
end

#collection_accessor_inspection_codeObject

rubocop:disable MethodLength



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
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/transpec/syntax/have/dynamic_inspector.rb', line 63

def collection_accessor_inspection_code
  # `expect(owner).to have(n).things` invokes private owner#things with Object#__send__
  # if the owner does not respond to any of #size, #count and #length.
  #
  # rubocop:disable LineLength
  # https://github.com/rspec/rspec-expectations/blob/v2.14.3/lib/rspec/matchers/built_in/have.rb#L48-L58
  # rubocop:enable LineLength
  @collection_accessor_inspection_code ||= <<-END.gsub(/^\s+\|/, '').chomp
    |begin
    |  exact_name = #{have.items_name.inspect}
    |
    |  inflector = if defined?(ActiveSupport::Inflector) &&
    |                   ActiveSupport::Inflector.respond_to?(:pluralize)
    |                ActiveSupport::Inflector
    |              elsif defined?(Inflector)
    |                Inflector
    |              else
    |                nil
    |              end
    |
    |  if inflector
    |    pluralized_name = inflector.pluralize(exact_name).to_sym
    |    respond_to_pluralized_name = #{subject_code}.respond_to?(pluralized_name)
    |  end
    |
    |  respond_to_query_methods =
    |    !(#{subject_code}.methods & #{QUERY_METHOD_PRIORITIES.inspect}).empty?
    |
    |  if #{subject_code}.respond_to?(exact_name)
    |    exact_name
    |  elsif respond_to_pluralized_name
    |    pluralized_name
    |  elsif respond_to_query_methods
    |    nil
    |  else
    |    exact_name
    |  end
    |end
  END
end

#register_requestObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/transpec/syntax/have/dynamic_inspector.rb', line 36

def register_request
  key = :collection_accessor
  code = collection_accessor_inspection_code
  rewriter.register_request(target_node, key, code, target_type)

  # Give up inspecting query methods of collection accessor with arguments
  # (e.g. have(2).errors_on(variable)) since this is a context of #instance_eval.
  unless have.items_method_has_arguments?
    key = :available_query_methods
    code = available_query_methods_inspection_code
    rewriter.register_request(target_node, key, code, target_type)
  end

  key = :collection_accessor_is_private?
  code = "#{subject_code}.private_methods.include?(#{have.items_name.inspect})"
  rewriter.register_request(target_node, key, code, target_type)

  key = :project_requires_collection_matcher?
  code = 'defined?(RSpec::Rails) || defined?(RSpec::CollectionMatchers)'
  rewriter.register_request(target_node, key, code, :context)
end

#subject_codeObject



58
59
60
# File 'lib/transpec/syntax/have/dynamic_inspector.rb', line 58

def subject_code
  have.explicit_subject? ? 'self' : 'subject'
end

#target_nodeObject



20
21
22
23
24
25
26
# File 'lib/transpec/syntax/have/dynamic_inspector.rb', line 20

def target_node
  if have.explicit_subject?
    have.expectation.subject_node
  else
    have.expectation.node
  end
end

#target_typeObject



28
29
30
31
32
33
34
# File 'lib/transpec/syntax/have/dynamic_inspector.rb', line 28

def target_type
  if have.explicit_subject?
    :object
  else
    :context
  end
end