diff options
| author | Amir Rajan <[email protected]> | 2020-02-27 20:14:39 -0600 |
|---|---|---|
| committer | Amir Rajan <[email protected]> | 2020-02-27 20:14:39 -0600 |
| commit | 5d0c0305854633122b3e800dd9058e93c2950eb6 (patch) | |
| tree | 0283825a7055cebd59ad1b3aff1b3dec1f2ac03c /dragon/tests.rb | |
| parent | 07468bf3f309c21ea3f7fcf47195ca275177dc7f (diff) | |
| download | dragonruby-game-toolkit-contrib-5d0c0305854633122b3e800dd9058e93c2950eb6.tar.gz dragonruby-game-toolkit-contrib-5d0c0305854633122b3e800dd9058e93c2950eb6.zip | |
open sourced parts of game toolkit.
Diffstat (limited to 'dragon/tests.rb')
| -rw-r--r-- | dragon/tests.rb | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/dragon/tests.rb b/dragon/tests.rb new file mode 100644 index 0000000..fc6450e --- /dev/null +++ b/dragon/tests.rb @@ -0,0 +1,136 @@ +# coding: utf-8 +# Copyright 2019 DragonRuby LLC +# MIT License +# tests.rb has been released under MIT (*only this file*). + +module GTK + class Tests + attr_accessor :failed, :passed, :inconclusive + + def initialize + @failed = [] + @passed = [] + @inconclusive = [] + end + + 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 + + def test_methods_focused + Object.methods.find_all { |m| m.start_with?( "focus_test_") } + end + + def test_methods + Object.methods.find_all { |m| m.start_with? "test_" } + end + + 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 + + def mark_test_failed m, e + message = "Failed." + self.failed << { m: m, e: e } + log message + end + + def running? + @is_running + end + + def log_inconclusive m + self.inconclusive << {m: m} + log "Inconclusive." + 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 + + def log_passed m + self.passed << {m: m} + log "Passed." + end + + 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 + + def log_test_running m + log "** Running: #{m}" + end + + def test_signature_invalid_exception? e, m + e.to_s.include?(m.to_s) && e.to_s.include?("wrong number of arguments") + end + + 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 + + def print_summary + log "** Summary" + log "*** Passed" + log "#{self.passed.length} test(s) passed." + self.passed.each { |h| log "**** :#{h[:m]}" } + log "*** Inconclusive" + 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 + end +end |
