Class: Surrogate::RSpec::SubstitutionMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/surrogate/rspec/substitution_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(original_class, options = {}) ⇒ SubstitutionMatcher

Returns a new instance of SubstitutionMatcher.



4
5
6
7
8
9
10
# File 'lib/surrogate/rspec/substitution_matcher.rb', line 4

def initialize(original_class, options={})
  @original_class = original_class
  @comparison     = nil
  @subset_only    = options.fetch :subset, false
  @types          = options.fetch :types,  true
  @names          = options.fetch :names,  false
end

Instance Method Details

#failure_messageObject



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
# File 'lib/surrogate/rspec/substitution_matcher.rb', line 18

def failure_message
  extra_instance_methods   = @comparison[:instance][:not_on_actual   ].to_a # these come in as sets
  extra_class_methods      = @comparison[:class   ][:not_on_actual   ].to_a
  missing_instance_methods = @comparison[:instance][:not_on_surrogate].to_a
  missing_class_methods    = @comparison[:class   ][:not_on_surrogate].to_a
  instance_type_mismatch   = @comparison[:instance][:types           ]
  class_type_mismatch      = @comparison[:class   ][:types           ]
  instance_name_mismatch   = @comparison[:instance][:names           ]
  class_name_mismatch      = @comparison[:class   ][:names           ]


  differences = []
  differences << "has extra instance methods: #{extra_instance_methods.inspect}" if extra_instance_methods.any?
  differences << "has extra class methods: #{extra_class_methods.inspect}"       if extra_class_methods.any?
  differences << "is missing instance methods: #{missing_instance_methods}"      if !@subset_only && missing_instance_methods.any?
  differences << "is missing class methods: #{missing_class_methods}"            if !@subset_only && missing_class_methods.any?

  if @types # this conditional is not tested, nor are these error messages
    instance_type_mismatch.each { |name, types| differences << "##{name} had types #{types.inspect}" }
    class_type_mismatch.each    { |name, types| differences << ".#{name} had types #{types.inspect}" }
  end

  if @names # this conditional is not tested, nor are these error messages
    instance_name_mismatch.each { |method_name, param_names| differences << "##{method_name} had parameter names #{param_names.inspect}" }
    class_type_mismatch.each    { |method_name, param_names| differences << ".#{method_name} had parameter names #{param_names.inspect}" }
  end
  "Was not substitutable because surrogate " << differences.join("\n")
end

#failure_message_when_negatedObject



47
48
49
# File 'lib/surrogate/rspec/substitution_matcher.rb', line 47

def failure_message_when_negated
  "Should not have been substitute, but was"
end

#matches?(surrogate) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
# File 'lib/surrogate/rspec/substitution_matcher.rb', line 12

def matches?(surrogate)
  @comparison = ApiComparer.new(surrogate, @original_class).compare
  comparing_fields(@comparison, @subset_only, @types, @names).values.inject(:+).empty?
end