Skip to content

Commit

Permalink
extract imp so that we could override in ext
Browse files Browse the repository at this point in the history
  • Loading branch information
godfat committed Nov 16, 2014
1 parent 896afb9 commit 0108a82
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 134 deletions.
135 changes: 2 additions & 133 deletions lib/pork/executor.rb
@@ -1,140 +1,9 @@

require 'pork/stat'
require 'pork/imp'

module Pork
class Executor
singleton_class.module_eval do
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 &block; @tests << [:before, block]; end
def after &block; @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
thread = Thread.current
original_group, group = thread.group, ThreadGroup.new
group.add(thread)
thread[:pork_executor] = self
stat.start
execute_with_parent(io, stat)
ensure
original_group.add(thread)
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
extend Imp

init

Expand Down
18 changes: 17 additions & 1 deletion lib/pork/ext.rb
@@ -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
132 changes: 132 additions & 0 deletions lib/pork/imp.rb
@@ -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 &block; @tests << [:before, block]; end
def after &block; @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

0 comments on commit 0108a82

Please sign in to comment.