Module: Crspec::Matchers

Included in:
Expectations
Defined in:
lib/crspec/matchers.rb

Defined Under Namespace

Classes: AllMatcher, BaseMatcher, BeAKindOfMatcher, BeAnInstanceOfMatcher, BeFalsyMatcher, BeMatcher, BeNilMatcher, BeTruthyMatcher, BeWithinMatcher, ChangeMatcher, CompoundAndMatcher, CompoundOrMatcher, CustomMatcher, EndWithMatcher, EqMatcher, HaveAttributesMatcher, HaveHttpStatusMatcher, IncludeMatcher, MatchArrayMatcher, MatchMatcher, OutputMatcher, PredicateMatcher, RaiseErrorMatcher, RespondToMatcher, SatisfyMatcher, StartWithMatcher, YieldControlMatcher, YieldSuccessiveArgsMatcher, YieldWithArgsMatcher

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs, &block) ⇒ Object



885
886
887
888
889
890
891
892
893
894
# File 'lib/crspec/matchers.rb', line 885

def method_missing(name, *args, **kwargs, &block)
  case name
  when /\Abe_(.+)\z/
    PredicateMatcher.new(:"#{Regexp.last_match(1)}?", "be #{Regexp.last_match(1).tr("_", " ")}", *args, **kwargs, &block)
  when /\Ahave_(.+)\z/
    PredicateMatcher.new(:"has_#{Regexp.last_match(1)}?", "have #{Regexp.last_match(1).tr("_", " ")}", *args, **kwargs, &block)
  else
    super
  end
end

Class Method Details

.define(name, &block) ⇒ Object



879
880
881
882
883
# File 'lib/crspec/matchers.rb', line 879

def self.define(name, &block)
  define_method(name) do |*args|
    CustomMatcher.new(name, args, &block)
  end
end

Instance Method Details

#all(matcher) ⇒ Object



843
844
845
# File 'lib/crspec/matchers.rb', line 843

def all(matcher)
  AllMatcher.new(matcher)
end

#be(expected = nil) ⇒ Object



778
779
780
# File 'lib/crspec/matchers.rb', line 778

def be(expected = nil)
  BeMatcher.new(expected)
end

#be_a(klass) ⇒ Object Also known as: be_an, be_a_kind_of, be_kind_of



795
796
797
# File 'lib/crspec/matchers.rb', line 795

def be_a(klass)
  BeAKindOfMatcher.new(klass)
end

#be_an_instance_of(klass) ⇒ Object Also known as: be_instance_of



802
803
804
# File 'lib/crspec/matchers.rb', line 802

def be_an_instance_of(klass)
  BeAnInstanceOfMatcher.new(klass)
end

#be_falsyObject Also known as: be_falsey



790
791
792
# File 'lib/crspec/matchers.rb', line 790

def be_falsy
  BeFalsyMatcher.new
end

#be_nilObject



782
783
784
# File 'lib/crspec/matchers.rb', line 782

def be_nil
  BeNilMatcher.new
end

#be_truthyObject



786
787
788
# File 'lib/crspec/matchers.rb', line 786

def be_truthy
  BeTruthyMatcher.new
end

#be_within(delta) ⇒ Object



851
852
853
# File 'lib/crspec/matchers.rb', line 851

def be_within(delta)
  BeWithinMatcher.new(delta)
end

#change(receiver = nil, message = nil, &block) ⇒ Object



819
820
821
# File 'lib/crspec/matchers.rb', line 819

def change(receiver = nil, message = nil, &block)
  ChangeMatcher.new(receiver, message, &block)
end

#contain_exactly(*items) ⇒ Object



835
836
837
# File 'lib/crspec/matchers.rb', line 835

def contain_exactly(*items)
  MatchArrayMatcher.new(items)
end

#end_with(expected) ⇒ Object



859
860
861
# File 'lib/crspec/matchers.rb', line 859

def end_with(expected)
  EndWithMatcher.new(expected)
end

#eq(expected) ⇒ Object



774
775
776
# File 'lib/crspec/matchers.rb', line 774

def eq(expected)
  EqMatcher.new(expected)
end

#have_attributes(expected) ⇒ Object



839
840
841
# File 'lib/crspec/matchers.rb', line 839

def have_attributes(expected)
  HaveAttributesMatcher.new(expected)
end

#have_http_status(expected) ⇒ Object



823
824
825
# File 'lib/crspec/matchers.rb', line 823

def have_http_status(expected)
  HaveHttpStatusMatcher.new(expected)
end

#include(*expected) ⇒ Object



807
808
809
# File 'lib/crspec/matchers.rb', line 807

def include(*expected)
  IncludeMatcher.new(expected.size == 1 ? expected.first : expected)
end

#match(expected) ⇒ Object



827
828
829
# File 'lib/crspec/matchers.rb', line 827

def match(expected)
  MatchMatcher.new(expected)
end

#match_array(expected) ⇒ Object



831
832
833
# File 'lib/crspec/matchers.rb', line 831

def match_array(expected)
  MatchArrayMatcher.new(expected.to_a)
end

#output(expected = nil) ⇒ Object



863
864
865
# File 'lib/crspec/matchers.rb', line 863

def output(expected = nil)
  OutputMatcher.new(expected)
end

#raise_error(expected_exception = StandardError, message = nil) ⇒ Object



811
812
813
# File 'lib/crspec/matchers.rb', line 811

def raise_error(expected_exception = StandardError, message = nil)
  RaiseErrorMatcher.new(expected_exception, message)
end

#respond_to(*methods) ⇒ Object



815
816
817
# File 'lib/crspec/matchers.rb', line 815

def respond_to(*methods)
  RespondToMatcher.new(*methods)
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


896
897
898
# File 'lib/crspec/matchers.rb', line 896

def respond_to_missing?(name, include_private = false)
  name.to_s.match?(/\A(be|have)_.+\z/) || super
end

#satisfy(description = nil, &block) ⇒ Object



847
848
849
# File 'lib/crspec/matchers.rb', line 847

def satisfy(description = nil, &block)
  SatisfyMatcher.new(description, &block)
end

#start_with(expected) ⇒ Object



855
856
857
# File 'lib/crspec/matchers.rb', line 855

def start_with(expected)
  StartWithMatcher.new(expected)
end

#yield_controlObject



867
868
869
# File 'lib/crspec/matchers.rb', line 867

def yield_control
  YieldControlMatcher.new
end

#yield_successive_args(*args) ⇒ Object



875
876
877
# File 'lib/crspec/matchers.rb', line 875

def yield_successive_args(*args)
  YieldSuccessiveArgsMatcher.new(*args)
end

#yield_with_args(*args) ⇒ Object



871
872
873
# File 'lib/crspec/matchers.rb', line 871

def yield_with_args(*args)
  YieldWithArgsMatcher.new(*args)
end