Class: GTK::Tests

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTests

Returns a new instance of Tests.


10
11
12
13
14
# File 'dragon/tests.rb', line 10

def initialize
  @failed = []
  @passed = []
  @inconclusive = []
end

Instance Attribute Details

#failedObject

Returns the value of attribute failed


8
9
10
# File 'dragon/tests.rb', line 8

def failed
  @failed
end

#inconclusiveObject

Returns the value of attribute inconclusive


8
9
10
# File 'dragon/tests.rb', line 8

def inconclusive
  @inconclusive
end

#passedObject

Returns the value of attribute passed


8
9
10
# File 'dragon/tests.rb', line 8

def passed
  @passed
end

Instance Method Details

#log_inconclusive(m) ⇒ Object


72
73
74
75
# File 'dragon/tests.rb', line 72

def log_inconclusive m
  self.inconclusive << {m: m}
  log "Inconclusive."
end

#log_no_tests_foundObject


82
83
84
85
86
87
88
89
90
91
92
# File 'dragon/tests.rb', line 82

def log_no_tests_found
  log <<-S
No tests were found. To create a test. Define a method
that begins with test_. For example:
#+begin_src
def test_game_over args, assert

end
#+end_src
S
end

#log_passed(m) ⇒ Object


77
78
79
80
# File 'dragon/tests.rb', line 77

def log_passed m
  self.passed << {m: m}
  log "Passed."
end

#log_test_running(m) ⇒ Object


94
95
96
# File 'dragon/tests.rb', line 94

def log_test_running m
  log "** Running: #{m}"
end

#log_test_signature_incorrect(m) ⇒ Object


102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'dragon/tests.rb', line 102

def log_test_signature_incorrect m
  log "TEST METHOD INVALID:", <<-S
I found a test method called :#{m}. But it needs to have
the following method signature:
#+begin_src
def #{m} args, assert

end
#+end_src
Please update the method signature to match the code above. If you
did not intend this to be a test method. Rename the method so it does
not start with "test_".
S
end

#mark_test_failed(m, e) ⇒ Object


62
63
64
65
66
# File 'dragon/tests.rb', line 62

def mark_test_failed m, e
  message = "Failed."
  self.failed << { m: m, e: e }
  log message
end

117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'dragon/tests.rb', line 117

def print_summary
  log "** Summary"
  log "*** Passed"
  log "#{self.passed.length} test(s) passed."
  self.passed.each { |h| log "**** :#{h[:m]}" }
  log "*** Inconclusive"
  if self.inconclusive.length > 0
    log_once :assertion_ok_note, <<-S
NOTE FOR INCONCLUSIVE TESTS: No assertion was performed in the test.
Add assert.ok! at the end of the test if you are using your own assertions.
S
  end
  log "#{self.inconclusive.length} test(s) inconclusive."
  self.inconclusive.each { |h| log "**** :#{h[:m]}" }
  log "*** Failed"
  log "#{self.failed.length} test(s) failed."
  self.failed.each do |h|
    log "**** Test name: :#{h[:m]}"
    log "#{h[:e].to_s.gsub("* ERROR:", "").strip}"
  end
end

#run_test(m) ⇒ Object


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'dragon/tests.rb', line 16

def run_test m
  args = Args.new $gtk, nil
  assert = Assert.new
  begin
    log_test_running m
    send(m, args, assert)
    if !assert.assertion_performed
      log_inconclusive m
    else
      log_passed m
    end
  rescue Exception => e
    if test_signature_invalid_exception? e, m
      log_test_signature_incorrect m
    else
      mark_test_failed m, e
    end
  end
end

#running?Boolean

Returns:

  • (Boolean)

68
69
70
# File 'dragon/tests.rb', line 68

def running?
  @is_running
end

#startObject


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'dragon/tests.rb', line 45

def start
  log "* TEST: gtk.test.start has been invoked."
  if test_methods_focused.length != 0
    @is_running = true
    test_methods_focused.each { |m| run_test m }
    print_summary
    @is_running = false
  elsif test_methods.length == 0
    log_no_tests_found
  else
    @is_running = true
    test_methods.each { |m| run_test m }
    print_summary
    @is_running = false
  end
end

#test_methodsObject


40
41
42
# File 'dragon/tests.rb', line 40

def test_methods
  Object.methods.find_all { |m| m.start_with? "test_" }
end

#test_methods_focusedObject


36
37
38
# File 'dragon/tests.rb', line 36

def test_methods_focused
  Object.methods.find_all { |m| m.start_with?( "focus_test_") }
end

#test_signature_invalid_exception?(e, m) ⇒ Boolean

Returns:

  • (Boolean)

98
99
100
# File 'dragon/tests.rb', line 98

def test_signature_invalid_exception? e, m
  e.to_s.include?(m.to_s) && e.to_s.include?("wrong number of arguments")
end