Created
October 4, 2013 15:17
-
-
Save wrl/6827658 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LameError < RuntimeError | |
end | |
ShibeTest::Suite.new('ShibeTest') do | |
should_pass 'assert true' do |assert| | |
assert.true { true } | |
end | |
should_fail 'fail assert true' do |assert| | |
assert.true { false } | |
end | |
should_pass 'assert false' do |assert| | |
assert.false { false } | |
end | |
should_fail 'fail assert false' do |assert| | |
assert.false { true } | |
end | |
should_pass 'raise' do |assert| | |
assert.raises(LameError) { raise LameError } | |
end | |
should_fail 'fail raise' do |assert| | |
assert.raises(LameError) { 'ja hallo' } | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# _ _ _ w o w _ _ | |
# __| |_ (_) |__ ___| |_ ___ __| |_ | |
# (_-< ' \| | '_ \/ -_) _/ -_|_-< _| | |
# /__/_||_|_|_.__/\___|\__\___/__/\__| | |
# such assertive so confidence | |
# | |
# mruby compatible!!!! (with the sprintf gem) | |
# | |
# | |
# This is free and unencumbered software released into the public domain. | |
# | |
# Anyone is free to copy, modify, publish, use, compile, sell, or | |
# distribute this software, either in source code form or as a compiled | |
# binary, for any purpose, commercial or non-commercial, and by any | |
# means. | |
# | |
# In jurisdictions that recognize copyright laws, the author or authors | |
# of this software dedicate any and all copyright interest in the | |
# software to the public domain. We make this dedication for the benefit | |
# of the public at large and to the detriment of our heirs and | |
# successors. We intend this dedication to be an overt act of | |
# relinquishment in perpetuity of all present and future rights to this | |
# software under copyright law. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
# OTHER DEALINGS IN THE SOFTWARE. | |
# | |
# For more information, please refer to <http://unlicense.org/> | |
# | |
module ShibeTest | |
OUTPUT_TPL = { | |
:running => ' -- %s...', | |
:passed => ' %s ', | |
:failed => ' !! %s: %s', | |
:failed_nomsg => ' !! %s ' | |
} | |
@suites = {} | |
@passed = 0 | |
@failed = 0 | |
class << self | |
def add_suite(suite) | |
@suites[suite.name] = suite | |
@passed += suite.passed | |
@failed += suite.failed | |
end | |
def report | |
{:passed => @passed, | |
:failed => @failed} | |
end | |
end | |
class FailedAssertionError < RuntimeError | |
attr_reader :block | |
def initialize(msg, block) | |
@block = block | |
super msg | |
end | |
end | |
class Assertor | |
# im ded | |
private | |
def fail(msg, blk) | |
msg << ' at ' << blk.source_location.join(' line ') | |
raise FailedAssertionError.new(msg, blk) | |
end | |
# wow such confident!!! | |
# ill check for u (1 sec) | |
public | |
def true(&b) | |
fail('expected true', b) unless yield | |
end | |
# ya i dont think so either | |
def false(&b) | |
fail('expected false', b) if yield | |
end | |
# ill be on the lookout cuz | |
def raises(exc = StandardError, &b) | |
yield | |
rescue exc | |
else | |
fail('expected ' + exc.to_s, b) | |
end | |
end | |
class Suite | |
attr_reader :name | |
attr_reader :passed | |
attr_reader :failed | |
def initialize(name, &block) | |
@name = name | |
@passed = 0 | |
@failed = 0 | |
puts "#{name}:" | |
self.instance_eval(&block) | |
puts '' | |
puts " [#{@passed}/#{@passed + @failed}] passed\n\n" | |
ShibeTest::add_suite(self) | |
end | |
def case_start(what) | |
print sprintf(OUTPUT_TPL[:running], what) + "\r" | |
end | |
def case_fail(what, why=nil) | |
if why | |
puts sprintf(OUTPUT_TPL[:failed], what, why) | |
else | |
puts sprintf(OUTPUT_TPL[:failed_nomsg], what) | |
end | |
@failed += 1 | |
end | |
def case_pass(what) | |
puts sprintf(OUTPUT_TPL[:passed], what) | |
@passed += 1 | |
end | |
# # # # # # # # # # # # # # # # # # # # | |
# use the methods below for testing!! # | |
# # # # # # # # # # # # # # # # # # # # | |
# never doubted u for a sec | |
def should_pass(what, &block) | |
assertor = Assertor.new | |
case_start(what) | |
block.call(assertor) | |
rescue FailedAssertionError => e | |
case_fail(what, e.message) | |
return false | |
else | |
case_pass(what) | |
return true | |
end | |
# maybe if u would have been more supportive | |
def should_fail(what, &block) | |
assertor = Assertor.new | |
case_start(what) | |
block.call(assertor) | |
rescue FailedAssertionError | |
case_pass(what) | |
return true | |
else | |
case_fail(what) | |
return false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment