Class: Crspec::Matchers::CustomMatcher

Inherits:
BaseMatcher show all
Defined in:
lib/crspec/matchers.rb

Instance Attribute Summary

Attributes inherited from BaseMatcher

#actual, #expected

Instance Method Summary collapse

Methods inherited from BaseMatcher

#and, #or

Constructor Details

#initialize(name, expected_args, &definition_block) ⇒ CustomMatcher

Returns a new instance of CustomMatcher.



708
709
710
711
712
713
714
715
716
717
718
719
# File 'lib/crspec/matchers.rb', line 708

def initialize(name, expected_args, &definition_block)
  super(expected_args)
  @name = name
  @expected_args = expected_args
  @definition_block = definition_block
  @match_proc = nil
  @failure_message_proc = nil
  @negated_failure_message_proc = nil
  @description_proc = nil
  @chains = {}
  instance_exec(*expected_args, &definition_block) if definition_block
end

Instance Method Details

#chain(name, &block) ⇒ Object



759
760
761
762
763
764
765
766
# File 'lib/crspec/matchers.rb', line 759

def chain(name, &block)
  matcher = self
  define_singleton_method(name) do |*args|
    matcher.instance_variable_get(:@chains)[name] = args
    matcher.instance_exec(*args, &block) if block
    matcher
  end
end

#description(&block) ⇒ Object



751
752
753
754
755
756
757
# File 'lib/crspec/matchers.rb', line 751

def description(&block)
  if block
    @description_proc = block
    return
  end
  @description_proc ? instance_exec(&@description_proc) : @name.to_s.tr("_", " ")
end

#failure_message(&block) ⇒ Object

DSL form (with block) stores the message proc; query form (no block) returns the rendered message.



727
728
729
730
731
732
733
734
735
736
737
# File 'lib/crspec/matchers.rb', line 727

def failure_message(&block)
  if block
    @failure_message_proc = block
    return
  end
  if @failure_message_proc
    instance_exec(@actual, &@failure_message_proc)
  else
    "Expected #{@actual.inspect} to #{description}"
  end
end

#failure_message_when_negated(&block) ⇒ Object



739
740
741
742
743
744
745
746
747
748
749
# File 'lib/crspec/matchers.rb', line 739

def failure_message_when_negated(&block)
  if block
    @negated_failure_message_proc = block
    return
  end
  if @negated_failure_message_proc
    instance_exec(@actual, &@negated_failure_message_proc)
  else
    "Expected #{@actual.inspect} not to #{description}"
  end
end

#match(&block) ⇒ Object



721
722
723
# File 'lib/crspec/matchers.rb', line 721

def match(&block)
  @match_proc = block
end

#matches?(actual) ⇒ Boolean

Returns:

  • (Boolean)


768
769
770
771
# File 'lib/crspec/matchers.rb', line 768

def matches?(actual)
  @actual = actual
  @match_proc ? instance_exec(actual, &@match_proc) : true
end