Module: Assit::Assertions

Included in:
Object
Defined in:
lib/assit/assertions.rb

Overview

Contains the assertion methods for the framework

Instance Method Summary collapse

Instance Method Details

#assit_block(&block) ⇒ Object

Executes the given block and asserts if the result is true. This allows you to assert on complex, custom expressions and be able to disable those expressions together with the assertions. See the README for more.

The block will be passed a single array, to which error messages can be append. The assertion will always fail if an error is appended to the array.



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

def assit_block(&block)
  errors = []
  assit((block.call(errors) && errors.size == 0), errors.join(', '))
end

#assit_equal(expected, actual, message = "Object expected to be equal") ⇒ Object

Assert if two objects are equal



12
13
14
15
16
17
# File 'lib/assit/assertions.rb', line 12

def assit_equal(expected, actual, message = "Object expected to be equal")
  if(expected != actual)
    message << " expected #{expected} but was #{actual}"
    assit(false, message)
  end
end

#assit_fail(message = "Assertion with assit_fail") ⇒ Object

Fails the assertion



28
29
30
# File 'lib/assit/assertions.rb', line 28

def assit_fail(message = "Assertion with assit_fail")
  assit(false, message)
end

#assit_kind_of(klass, object, message = "Object of wrong type") ⇒ Object

Assert if something is of the right type



20
21
22
23
24
25
# File 'lib/assit/assertions.rb', line 20

def assit_kind_of(klass, object, message = "Object of wrong type")
  if(!object.kind_of?(klass))
    message << " (Expected #{klass} but was #{object.class})"
    assit(false, message)
  end
end

#assit_not_nil(object, message = "Object is nil") ⇒ Object

Assert if an object is not nil



7
8
9
# File 'lib/assit/assertions.rb', line 7

def assit_not_nil(object, message = "Object is nil")
  assit(object != nil, message)
end

#assit_quack(object, methods, message = "Quack assert failed.") ⇒ Object

Duck typing assertion: This checks if the given object responds to the given method calls. This won’t detect any calls that will be handled through method_missing, of course.

Methods can be a single method name, or an Enumerable with multiple names



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/assit/assertions.rb', line 37

def assit_quack(object, methods, message = "Quack assert failed.")
  unless(methods.kind_of?(Enumerable))
    methods = [methods]
  end
  
  methods.each do |method|
    unless(object.respond_to?(method.to_sym))
      assit(false, "#{message} - Method: #{method.to_s}")
    end
  end
end

#assit_real_string(object, message = "Not a non-empty string.") ⇒ Object

Asserts that the given element is a string that is not nil and not an empty string, or a string only containing whitspaces



51
52
53
54
55
# File 'lib/assit/assertions.rb', line 51

def assit_real_string(object, message = "Not a non-empty string.")
  unless(object && object.kind_of?(String) && object.strip != "")
    assit(false, message)
  end
end