summaryrefslogtreecommitdiffhomepage
path: root/dragon/assert.rb
blob: 0c26f290eab774b3c5b0c143c8cd440149875ce7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# coding: utf-8
# Copyright 2019 DragonRuby LLC
# MIT License
# assert.rb has been released under MIT (*only this file*).

module GTK
=begin
This is a tiny assertion api for the unit testing portion of Game Toolkit.

@example

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

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

@example

To add an assertion open up this class and write:

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

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

    raise "Some Error. #{message}."
  end
end
=end
  class Assert
    attr :assertion_performed

=begin
Us 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).
=end
    def ok!
      @assertion_performed = true
    end

=begin
Assert if a value is a thruthy value. All assert method take an optional final parameter that is the message to display to the user.

@example

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
=end
    def true! value, message = nil
      @assertion_performed = true
      if !value
        message = "#{value} was not truthy.\n#{message}"
        raise "#{message}"
      end
      nil
    end

=begin
Assert if a value is a falsey value.

@example

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

=begin
Assert if two values are equal.

@example

def test_does_this_work args, assert
  a = 1
  b = 1
  assert.equal! a, b
end
=end
    def equal! actual, expected, message = nil
      @assertion_performed = true
      if actual != expected
        actual_string = "#{actual}#{actual.nil? ? " (nil) " : " " }".strip
        message = "actual:\n#{actual_string} did not equal\nexpected:\n#{expected}.\n#{message}"
        raise message
      end
      nil
    end

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

@example

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
=end
    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
  end
end