Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract imp so that we could override in ext
- Loading branch information
Showing
3 changed files
with
151 additions
and
134 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,25 @@ | ||
|
||
require 'pork/expect' | ||
require 'pork/executor' | ||
|
||
module Kernel | ||
def should *args, &block | ||
Thread.current.group.list.first[:pork_executor]. | ||
expect(self, *args, &block) | ||
end | ||
end | ||
|
||
module Pork | ||
module Ext | ||
def execute *args | ||
thread = Thread.current | ||
original_group, group = thread.group, ThreadGroup.new | ||
group.add(thread) | ||
thread[:pork_executor] = self | ||
super | ||
ensure | ||
original_group.add(thread) | ||
end | ||
end | ||
|
||
Executor.extend(Ext) | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
|
||
require 'pork/stat' | ||
require 'pork/expect' | ||
|
||
module Pork | ||
module Imp | ||
attr_reader :desc, :tests, :io, :stat | ||
|
||
def copy desc=:default, &suite | ||
@stash[desc] = suite | ||
end | ||
def paste desc=:default, *args | ||
module_exec(*args, &search_stash(desc)) | ||
end | ||
|
||
def before █ @tests << [:before, block]; end | ||
def after █ @tests << [:after , block]; end | ||
|
||
def describe desc=:default, &suite | ||
executor = Class.new(self){ init("#{desc}: ") } | ||
executor.module_eval(&suite) | ||
@tests << [:describe, executor] | ||
end | ||
|
||
def would desc=:default, &test | ||
@tests << [:would, desc, test] | ||
end | ||
|
||
def expect *args, &block | ||
Expect.new(stat, *args, &block) | ||
end | ||
|
||
def execute io=$stdout, stat=Stat.new | ||
stat.start | ||
execute_with_parent(io, stat) | ||
end | ||
|
||
def passed? | ||
stat.failures + stat.errors == 0 | ||
end | ||
|
||
def all_tests | ||
@all_tests ||= Hash[build_all_tests] | ||
end | ||
|
||
private | ||
def init desc='' | ||
@desc, @tests, @stash, @before, @after = desc, [], {}, [], [] | ||
@super_executor = ancestors[1..-1].find{ |a| a <= Executor } | ||
end | ||
|
||
def run desc, test | ||
assertions = stat.assertions | ||
context = new(desc) | ||
run_protected(desc) do | ||
run_before(context) | ||
context.instance_eval(&test) | ||
if assertions == stat.assertions | ||
raise Error.new('Missing assertions') | ||
end | ||
io.print '.' | ||
end | ||
ensure | ||
stat.incr_tests | ||
run_protected(desc){ run_after(context) } | ||
end | ||
|
||
def run_protected desc | ||
yield | ||
rescue Error, StandardError => e | ||
case e | ||
when Skip | ||
stat.incr_skips | ||
io.print 's' | ||
when Failure | ||
stat.add_failure(e, description_for("would #{desc}")) | ||
io.print 'F' | ||
when Error, StandardError | ||
stat.add_error( e, description_for("would #{desc}")) | ||
io.print 'E' | ||
end | ||
end | ||
|
||
protected | ||
def execute_with_parent io=$stdout, stat=Stat.new | ||
@stat, @io = stat, io | ||
@tests.each do |(type, arg, test)| | ||
case type | ||
when :before | ||
@before << arg | ||
when :after | ||
@after << arg | ||
when :describe | ||
arg.execute_with_parent(io, stat) | ||
when :would | ||
run(arg, test) | ||
end | ||
end | ||
end | ||
|
||
def search_stash desc | ||
@stash[desc] or @super_executor && @super_executor.search_stash(desc) | ||
end | ||
|
||
def run_before context | ||
@super_executor && @super_executor.run_before(context) | ||
@before.each{ |b| context.instance_eval(&b) } | ||
end | ||
|
||
def run_after context | ||
@super_executor && @super_executor.run_after(context) | ||
@after.each{ |b| context.instance_eval(&b) } | ||
end | ||
|
||
def description_for name='' | ||
"#{@desc}#{@super_executor && @super_executor.description_for}#{name}" | ||
end | ||
|
||
def build_all_tests paths=[] | ||
@tests.flat_map.with_index do |(type, arg, test), index| | ||
case type | ||
when :describe | ||
arg.build_all_tests(paths + [index]) | ||
when :would | ||
[["#{desc.chomp(': ')}#{arg}", paths + [index]]] | ||
else | ||
[] | ||
end | ||
end | ||
end | ||
end | ||
end |