Class: GTK::Assert

Inherits:
Object show all
Defined in:
dragon/assert.rb

Overview

This is a tiny assertion api for the unit testing portion of Game Toolkit.

  1. Create a file called tests.rb under mygame.

  2. Any method that begins with the word test_ will be considered a test.

def test_this_works args, assert

assert.equal! 1, 1

end

  1. To run a test, save the file while the game is running.

To add an assertion open up this class and write:

class Assert

def custom_assertion actual, expected, message = nil
  # this tells Game Toolkit that an assertion was performed (so that the test isn't marked inconclusive).
  @assertion_performed = true

  # perform your custom logic here and raise an exception to denote a failure.

  raise "Some Error. #{message}."
end

end

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#assertion_performedObject (readonly)

Returns the value of attribute assertion_performed


37
38
39
# File 'dragon/assert.rb', line 37

def assertion_performed
  @assertion_performed
end

Instance Method Details

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

Assert if two values are equal.

def test_does_this_work args, assert

a = 1
b = 1
assert.equal! a, b

end


97
98
99
100
101
102
103
104
105
# File 'dragon/assert.rb', line 97

def equal! actual, expected, message = nil
  @assertion_performed = true
  if actual != expected
    actual_string = "#{actual}#{actual.nil? ? " (nil) " : " " }".strip
    message = "actual:\n#{actual_string}\n\ndid not equal\n\nexpected:\n#{expected}.\n#{message}"
    raise message
  end
  nil
end

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

Assert if a value is a falsey value.

def test_does_this_work args, assert

some_result = nil
assert.false! some_result

end


77
78
79
80
81
82
83
84
# File 'dragon/assert.rb', line 77

def false! value, message = nil
  @assertion_performed = true
  if value
    message = "#{value} was not falsey.\n#{message}"
    raise message
  end
  nil
end

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

Assert if a value is explicitly nil (not false).

def test_does_this_work args, assert

a = nil
b = false
assert.nil! a # this will pass
assert.nil! b # this will throw an exception.

end


129
130
131
132
133
134
135
136
# File 'dragon/assert.rb', line 129

def nil! value, message = nil
  @assertion_performed = true
  if !value.nil?
    message = "#{value} was supposed to be nil, but wasn't.\n#{message}"
    raise message
  end
  nil
end

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


107
108
109
110
111
112
113
114
115
# File 'dragon/assert.rb', line 107

def not_equal! actual, expected, message = nil
  @assertion_performed = true
  if actual == expected
    actual_string = "#{actual}#{actual.nil? ? " (nil) " : " " }".strip
    message = "actual:\n#{actual_string}\n\nequaled\n\nexpected:\n#{expected}.\n#{message}"
    raise message
  end
  nil
end

#ok!Object

Use this if you are throwing your own exceptions and you want to mark the tests as ran (so that it wont be marked as inconclusive).


42
43
44
# File 'dragon/assert.rb', line 42

def ok!
  @assertion_performed = true
end

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

Assert if a value is a truthy value. All assert methods take an optional final parameter that is the message to display to the user.

def test_does_this_work args, assert

some_result = Person.new
assert.true! some_result
# OR
assert.true! some_result, "Person was not created."

end


58
59
60
61
62
63
64
65
# File 'dragon/assert.rb', line 58

def true! value, message = nil
  @assertion_performed = true
  if !value
    message = "#{value} was not truthy.\n#{message}"
    raise "#{message}"
  end
  nil
end