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

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(have, rewriter) ⇒ DynamicInspector

Returns a new instance of DynamicInspector.



174
175
176
177
# File 'lib/transpec/syntax/have.rb', line 174

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

Class Method Details

.register_request(have, rewriter) ⇒ Object



170
171
172
# File 'lib/transpec/syntax/have.rb', line 170

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

Instance Method Details

#collection_accessor_inspection_codeObject

rubocop:disable MethodLength



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/transpec/syntax/have.rb', line 208

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
  <<-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 = respond_to?(pluralized_name)
    |  end
    |
    |  respond_to_query_methods = !(methods & #{QUERY_METHOD_PRIORITIES.inspect}).empty?
    |
    |  if 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



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/transpec/syntax/have.rb', line 183

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

  # 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 = "collection_accessor = #{code}; " +
           'target = collection_accessor ? __send__(collection_accessor) : self; ' +
           "target.methods & #{QUERY_METHOD_PRIORITIES.inspect}"
    @rewriter.register_request(target_node, key, code)
  end

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

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

#target_nodeObject



179
180
181
# File 'lib/transpec/syntax/have.rb', line 179

def target_node
  @have.expectation.subject_node
end