Module: Nitrous::Assertions

Included in:
Test
Defined in:
lib/nitrous/assertions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(mod) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/nitrous/assertions.rb', line 42

def self.included(mod)
  mod.module_eval do
    def self.method_added(method)
      Assertions.method_added(method)
    end
  end
end

.method_added(method) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/nitrous/assertions.rb', line 30

def self.method_added(method)
  return unless method.to_s =~ /!$/
  name = method.to_s.gsub("!", '')
  module_eval <<-"end;"
    def #{name}(*args, &b)
      collect_errors do
        #{method}(*args, &b)
      end
    end
  end;
end

Instance Method Details

#assert!(value, message = nil) ⇒ Object



54
55
56
57
# File 'lib/nitrous/assertions.rb', line 54

def assert!(value, message=nil)
  fail(message || "#{value.inspect} is not true.") unless value
  yield if block_given?
end

#assert_equal!(expected, actual, message = nil) ⇒ Object



64
65
66
67
# File 'lib/nitrous/assertions.rb', line 64

def assert_equal!(expected, actual, message=nil)
  fail(message || "Expected: <#{expected}> but was <#{actual}>") unless expected == actual
  yield if block_given?
end

#assert_match!(pattern, string, message = nil) ⇒ Object



74
75
76
77
# File 'lib/nitrous/assertions.rb', line 74

def assert_match!(pattern, string, message=nil)
  pattern = Regexp.new(Regexp.escape(pattern)) if pattern.is_a?(String)
  fail(message || "#{string} expected to be =~ #{pattern}") unless string =~ pattern
end

#assert_nil!(value) ⇒ Object



59
60
61
62
# File 'lib/nitrous/assertions.rb', line 59

def assert_nil!(value)
  fail("#{value.inspect} is not nil.") unless value.nil?
  yield if block_given?
end

#assert_not_equal!(not_expected, actual, message = nil) ⇒ Object



69
70
71
72
# File 'lib/nitrous/assertions.rb', line 69

def assert_not_equal!(not_expected, actual, message=nil)
  fail(message || "Expected: <#{not_expected}> not to equal <#{actual}>") unless not_expected != actual
  yield if block_given?
end

#assert_not_raised!(type = Exception, &block) ⇒ Object Also known as: assert_nothing_raised!



88
89
90
91
92
# File 'lib/nitrous/assertions.rb', line 88

def assert_not_raised!(type=Exception, &block)
  yield
rescue type => e
  fail("Expected a(n) #{type} not to be raised but a(n) #{e.class} was raised.\n#{e.message}")
end

#assert_raise!(type = Exception, &block) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/nitrous/assertions.rb', line 79

def assert_raise!(type=Exception, &block)
  yield
  passed = true
  raise
rescue Exception => e
  fail("Expected a(n) #{type} to be raised but raised a(n) #{e.class}") if e.class != type
  fail("Expected a(n) #{type} to be raised") if passed
end

#fail(message) ⇒ Object



50
51
52
# File 'lib/nitrous/assertions.rb', line 50

def fail(message)
  raise AssertionFailedError.new(message, @current_test.filename)
end