Created
December 11, 2009 13:53
-
-
Save madx/254213 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
require 'fileutils' | |
# ShitStorm libs | |
require File.join(File.dirname(__FILE__), '..', 'lib', 'shitstorm') | |
include ShitStorm | |
# Swap to test DB | |
db_file = File.join(File.dirname(__FILE__), 'test.db')[2..-1] | |
migrations_dir = File.join(File.dirname(__FILE__), '..', 'migrations') | |
puts "Regenerating database... [#{db_file}]" | |
FileUtils.rm(db_file) if File.exists?(db_file) | |
system("sequel -m #{migrations_dir} sqlite://#{db_file}") | |
puts "Reconnecting..." | |
Sequel::DATABASES.shift | |
Sequel.connect "sqlite://#{db_file}" | |
[Issue, Entry, Event, Comment].each do |model| | |
model.db = Sequel::DATABASES.first | |
end | |
# A small test framework, inspired from Nanotest | |
# http://github.com/mynyml/nanotest | |
module Microtest | |
extend self | |
@@fails, @@dots = [], [] | |
Context = Struct.new(:name, :setup, :teardown) | |
def assert(msg=nil, file=nil, line=nil, &block) | |
unless block.call | |
file, line = caller.first.split(':', 2) | |
@@fails << "(%s:%0.3d) %s" % | |
[file, line, prepend_context(msg || 'assertion failed')] | |
else | |
@@dots << prepend_context(msg || '.') | |
end | |
end | |
def context(name, &block) | |
@_contexts ||= [] | |
@_contexts << Context.new.tap {|c| c.name = name} | |
instance_eval &block | |
@_contexts.pop | |
end | |
def setup(&block) | |
@_contexts.last.setup = block | |
end | |
def teardown(&block) | |
@_contexts.last.teardown = block | |
end | |
def test(&block) | |
@_contexts.map {|c| c.setup }.compact.each {|s| s.call } | |
block.call | |
@_contexts.map {|c| c.teardown }.compact.each {|s| s.call } | |
end | |
def self.results | |
[ | |
@@dots.map {|m| "\e[32mok\e[0m: #{m}"}.join("\n"), | |
@@fails.map {|m| "\e[31mfail\e[0m: #{m}"}.join("\n") | |
].join("\n") | |
end | |
at_exit { puts results unless results.strip.empty? } | |
private | |
def prepend_context(msg) | |
if @_contexts.last | |
(@_contexts.map{|c| c.name} << msg).join(' ') | |
else | |
msg | |
end | |
end | |
end | |
include Microtest |
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
require File.join(File.dirname(__FILE__), 'helper') | |
context "At startup" do | |
assert("there are no issues") { Issue.count.zero? } | |
end | |
context "Adding an issue" do | |
setup { | |
Issue.create :author => 'me', | |
:title => 'title', | |
:body => 'body', | |
:ctime => Time.now | |
} | |
test do | |
assert("increases the number of issues") { Issue.count == 1 } | |
end | |
end | |
context "An issue" do | |
setup { @issue = Issue.first } | |
test do | |
assert("has an author name") { @issue.author == 'me' } | |
assert("has a title") { @issue.title == 'title' } | |
assert("has a body") { @issue.body == "<p>body</p>\n" } | |
assert("has a ctime") { @issue.ctime.is_a? Time } | |
assert("has an url") { @issue.url == "/#{@issue.id}" } | |
assert("FAIL!") { false } | |
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
shitstorm (master:43c9c87) $ ruby -rubygems spec/issue_spec.rb | |
Regenerating database... [spec/test.db] | |
Reconnecting... | |
ok: At startup there are no issues | |
ok: Adding an issue increases the number of issues | |
ok: An issue has an author name | |
ok: An issue has a title | |
ok: An issue has a body | |
ok: An issue has a ctime | |
ok: An issue has an url | |
fail: (spec/issue_spec.rb:029) An issue FAIL! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment