Class: Spec::ShouldNegator

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

Instance Method Summary collapse

Methods inherited from ShouldBase

#default_message, #diff_as_string, #fail_with_message, #old_default_message

Constructor Details

#initialize(target) ⇒ ShouldNegator

Returns a new instance of ShouldNegator.



5
6
7
# File 'lib/spec/api/helper/should_negator.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



63
64
65
66
67
# File 'lib/spec/api/helper/should_negator.rb', line 63

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

Instance Method Details

#a_kind_of(expected_class) ⇒ Object



27
28
29
# File 'lib/spec/api/helper/should_negator.rb', line 27

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

#an_instance_of(expected_class) ⇒ Object



23
24
25
# File 'lib/spec/api/helper/should_negator.rb', line 23

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

#be(expected = :no_arg) ⇒ Object



17
18
19
20
21
# File 'lib/spec/api/helper/should_negator.rb', line 17

def be(expected = :no_arg)
  @be_seen = true
  return self if (expected == :no_arg)
  fail_with_message(default_message("should not be", expected)) if (@target.equal?(expected))
end

#equal(expected) ⇒ Object



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

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

#match(expected) ⇒ Object



35
36
37
# File 'lib/spec/api/helper/should_negator.rb', line 35

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

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



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/spec/api/helper/should_negator.rb', line 39

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

#respond_to(message) ⇒ Object



31
32
33
# File 'lib/spec/api/helper/should_negator.rb', line 31

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

#satisfyObject



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

def satisfy
  fail_with_message "Supplied expectation was satisfied, but should not have been" if (yield @target)
end

#throw(symbol = :___this_is_a_symbol_that_will_never_occur___) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/spec/api/helper/should_negator.rb', line 51

def throw(symbol=:___this_is_a_symbol_that_will_never_occur___)
  begin
    catch symbol do
      @target.call
      return true
    end
    fail_with_message(default_message("should not throw", symbol.inspect))
  rescue NameError
    true
  end
end