class Pork::Isolator

Public Class Methods

[](suite=Suite) click to toggle source
# File lib/pork/isolator.rb, line 7
def self.[] suite=Suite
  @map ||= {}
  @map[suite] ||= new(suite)
end

Public Instance Methods

[](index) click to toggle source
# File lib/pork/isolator.rb, line 20
def [] index
  by_groups(index) || by_source(index)
end
all_paths() click to toggle source
# File lib/pork/isolator.rb, line 16
def all_paths
  (all_tests[:files] || {}).values.flat_map(&:values).flatten(1).uniq
end
all_tests() click to toggle source
# File lib/pork/isolator.rb, line 12
def all_tests
  @all_tests ||= build_all_tests
end
by_groups(groups) click to toggle source
# File lib/pork/isolator.rb, line 24
def by_groups groups
  return unless tests = all_tests[:groups]
  paths = groups.split(',').flat_map do |g|
    tests[g.strip] || []
  end.uniq
  paths unless paths.empty?
end
by_source(source) click to toggle source
# File lib/pork/isolator.rb, line 32
def by_source source
  return unless tests = all_tests[:files]
  file_str, line_str = source.split(':')
  file, line = File.expand_path(file_str), line_str.to_i
  return unless cases = tests[file]
  if line.zero?
    cases.values.flatten(1).uniq
  else
    _, paths = cases.reverse_each.find{ |(l, _)| l <= line }
    paths
  end
end

Protected Instance Methods

build_all_tests(result={}) { |current| ... } click to toggle source
# File lib/pork/isolator.rb, line 69
def build_all_tests result={}, path=[]
  suite.tests.each_with_index.inject(result) do |
    r, ((type, imp, test, opts), index)|
    current = path + [index]

    case type
    when :describe
      Isolator[imp].build_all_tests(r, current) do |nested|
        store_path(r, nested, test, opts[:groups])
      end
    when :would
      yield(current) if block_given?
      store_path(r, current, test, opts[:groups])
    end

    r
  end
end
isolate(stat, path, super_env=nil) click to toggle source
# File lib/pork/isolator.rb, line 46
def isolate stat, path, super_env=nil
  env = Env.new(super_env)
  idx = path.first

  suite.tests.first(idx).each do |(type, arg, _)|
    case type
    when :before
      env.before << arg
    when :after
      env.after  << arg
    end
  end

  if path.size == 1
    _, desc, test = suite.tests[idx]
    suite.run(stat, desc, test, env)
  else
    Isolator[suite.tests[idx][1]].isolate(stat, path.drop(1), env)
  end

  stat
end
store_for_groups(tests, path, groups) click to toggle source
# File lib/pork/isolator.rb, line 93
def store_for_groups tests, path, groups
  r = tests[:groups] ||= {}
  groups.each do |g|
    (r[g.to_s] ||= []) << path
  end
end
store_for_source(tests, path, file, line) click to toggle source
# File lib/pork/isolator.rb, line 100
def store_for_source tests, path, file, line
  r = tests[:files] ||= {}
  ((r[File.expand_path(file)] ||= {})[line] ||= []) << path
end
store_path(tests, path, test, groups) click to toggle source
# File lib/pork/isolator.rb, line 88
def store_path tests, path, test, groups
  store_for_groups(tests, path, groups) if groups
  store_for_source(tests, path, *test.source_location)
end