Class: Spec::ShouldHelper

Inherits:
ShouldBase show all
Defined in:
lib/spec/api/helper/should_helper.rb

Instance Method Summary collapse

Methods inherited from ShouldBase

#default_message, #diff_as_string, #fail_with_message, #old_default_message

Constructor Details

#initialize(target) ⇒ ShouldHelper

Returns a new instance of ShouldHelper.



5
6
7
# File 'lib/spec/api/helper/should_helper.rb', line 5

def initialize(target)
  @target = target
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



58
59
60
61
62
# File 'lib/spec/api/helper/should_helper.rb', line 58

def method_missing(sym, *args)
  return if @target.send("#{sym}?", *args)
  fail_with_message(default_message("should be #{sym}" + (args.empty? ? '' : (' ' + args.join(', '))))) if @be_seen
  fail_with_message(default_message("should #{sym}" + (args.empty? ? '' : (' ' + args.join(', '))))) unless @be_seen
end

Instance Method Details

#a_kind_of(expected_class) ⇒ Object



50
51
52
# File 'lib/spec/api/helper/should_helper.rb', line 50

def a_kind_of expected_class
  fail_with_message(default_message("should be a kind of", expected_class)) unless @target.kind_of? expected_class
end

#an_instance_of(expected_class) ⇒ Object



46
47
48
# File 'lib/spec/api/helper/should_helper.rb', line 46

def an_instance_of expected_class
  fail_with_message(default_message("should be an instance of", expected_class)) unless @target.instance_of? expected_class
end

#be(expected = :___no_arg) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/spec/api/helper/should_helper.rb', line 38

def be(expected = :___no_arg)
  @be_seen = true
  return self if (expected == :___no_arg)
  return if (expected == false and @target.nil?)
  return if (expected == true and (!@target.nil?) and (@target != false))
  fail_with_message(default_message("should be", expected)) unless (@target.equal?(expected))
end

#equal(expected) ⇒ Object



34
35
36
# File 'lib/spec/api/helper/should_helper.rb', line 34

def equal(expected)
  fail_with_message(default_message("should equal", expected)) unless (@target == expected)
end

#have(expected_number = nil) ⇒ Object



13
14
15
# File 'lib/spec/api/helper/should_helper.rb', line 13

def have(expected_number=nil)
  HaveHelper.new(@target, :exactly, expected_number)
end

#have_at_least(expected_number = nil) ⇒ Object



21
22
23
# File 'lib/spec/api/helper/should_helper.rb', line 21

def have_at_least(expected_number=nil)
  HaveHelper.new(@target, :at_least, expected_number)
end

#have_at_most(expected_number = nil) ⇒ Object



25
26
27
# File 'lib/spec/api/helper/should_helper.rb', line 25

def have_at_most(expected_number=nil)
  HaveHelper.new(@target, :at_most, expected_number)
end

#have_exactly(expected_number = nil) ⇒ Object



17
18
19
# File 'lib/spec/api/helper/should_helper.rb', line 17

def have_exactly(expected_number=nil)
  HaveHelper.new(@target, :exactly, expected_number)
end

#match(expected) ⇒ Object



64
65
66
# File 'lib/spec/api/helper/should_helper.rb', line 64

def match(expected)
   fail_with_message(default_message("should match", expected)) unless (@target =~ expected)
end

#notObject



9
10
11
# File 'lib/spec/api/helper/should_helper.rb', line 9

def not
  ShouldNegator.new(@target)
end

#raise(exception = Exception, message = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/spec/api/helper/should_helper.rb', line 68

def raise(exception=Exception, message=nil)
  begin
    @target.call
  rescue exception => e
    e.message.should_equal message unless message.nil?
    return
  rescue => e
    fail_with_message("#{default_message("should raise", exception)} but raised #{e.inspect}")
  end
  fail_with_message("#{default_message("should raise", exception)} but raised nothing")
end

#respond_to(message) ⇒ Object



54
55
56
# File 'lib/spec/api/helper/should_helper.rb', line 54

def respond_to message
  fail_with_message(default_message("should respond to", message)) unless @target.respond_to? message
end

#satisfy(&block) ⇒ Object



29
30
31
32
# File 'lib/spec/api/helper/should_helper.rb', line 29

def satisfy(&block)
  return if block.call(@target)
  fail_with_message "Supplied expectation was not satisfied"
end

#throw(symbol) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/spec/api/helper/should_helper.rb', line 80

def throw(symbol)
  begin
    catch symbol do
      @target.call
      fail_with_message(default_message("should throw", symbol.inspect))
    end
  rescue NameError
    fail_with_message(default_message("should throw", symbol.inspect))
  end
end