summaryrefslogtreecommitdiff
path: root/spec/mspec/lib/mspec/runner
diff options
context:
space:
mode:
Diffstat (limited to 'spec/mspec/lib/mspec/runner')
-rw-r--r--spec/mspec/lib/mspec/runner/actions/constants_leak_checker.rb11
-rw-r--r--spec/mspec/lib/mspec/runner/context.rb33
-rw-r--r--spec/mspec/lib/mspec/runner/example.rb12
-rw-r--r--spec/mspec/lib/mspec/runner/formatters/method.rb14
-rw-r--r--spec/mspec/lib/mspec/runner/formatters/spinner.rb2
-rw-r--r--spec/mspec/lib/mspec/runner/mspec.rb82
-rw-r--r--spec/mspec/lib/mspec/runner/object.rb4
7 files changed, 82 insertions, 76 deletions
diff --git a/spec/mspec/lib/mspec/runner/actions/constants_leak_checker.rb b/spec/mspec/lib/mspec/runner/actions/constants_leak_checker.rb
index fd0a3efe14..abfb6dd0ee 100644
--- a/spec/mspec/lib/mspec/runner/actions/constants_leak_checker.rb
+++ b/spec/mspec/lib/mspec/runner/actions/constants_leak_checker.rb
@@ -1,9 +1,14 @@
class ConstantsLockFile
LOCK_FILE_NAME = '.mspec.constants'
+ def self.lock_file
+ @prefix ||= File.expand_path(MSpecScript.get(:prefix) || '.')
+ "#{@prefix}/#{LOCK_FILE_NAME}"
+ end
+
def self.load
- if File.exist?(LOCK_FILE_NAME)
- File.readlines(LOCK_FILE_NAME).map(&:chomp)
+ if File.exist?(lock_file)
+ File.readlines(lock_file).map(&:chomp)
else
[]
end
@@ -11,7 +16,7 @@ class ConstantsLockFile
def self.dump(ary)
contents = ary.map(&:to_s).uniq.sort.join("\n") + "\n"
- File.write(LOCK_FILE_NAME, contents)
+ File.write(lock_file, contents)
end
end
diff --git a/spec/mspec/lib/mspec/runner/context.rb b/spec/mspec/lib/mspec/runner/context.rb
index d55b0dcd9e..62483590bb 100644
--- a/spec/mspec/lib/mspec/runner/context.rb
+++ b/spec/mspec/lib/mspec/runner/context.rb
@@ -12,15 +12,14 @@
class ContextState
attr_reader :state, :parent, :parents, :children, :examples, :to_s
- def initialize(mod, options = nil)
- @to_s = mod.to_s
- if options.is_a? Hash
- @options = options
- else
- @to_s += "#{".:#".include?(options[0,1]) ? "" : " "}#{options}" if options
- @options = { }
- end
- @options[:shared] ||= false
+ MOCK_VERIFY = -> { Mock.verify_count }
+ MOCK_CLEANUP = -> { Mock.cleanup }
+ EXPECTATION_MISSING = -> { raise SpecExpectationNotFoundError }
+
+ def initialize(description, options = nil)
+ raise "#describe options should be a Hash or nil" unless Hash === options or options.nil?
+ @to_s = description.to_s
+ @shared = options && options[:shared]
@parsed = false
@before = { :all => [], :each => [] }
@@ -32,10 +31,6 @@ class ContextState
@parent = nil
@parents = [self]
@children = []
-
- @mock_verify = Proc.new { Mock.verify_count }
- @mock_cleanup = Proc.new { Mock.cleanup }
- @expectation_missing = Proc.new { raise SpecExpectationNotFoundError }
end
# Remove caching when a ContextState is dup'd for shared specs.
@@ -47,7 +42,7 @@ class ContextState
# Returns true if this is a shared +ContextState+. Essentially, when
# created with: describe "Something", :shared => true { ... }
def shared?
- return @options[:shared]
+ @shared
end
# Set the parent (enclosing) +ContextState+ for this state. Creates
@@ -207,7 +202,7 @@ class ContextState
if protect "before :all", pre(:all)
@examples.each do |state|
MSpec.repeat do
- @state = state
+ @state = state
example = state.example
MSpec.actions :before, state
@@ -216,20 +211,20 @@ class ContextState
if example
passed = protect nil, example
MSpec.actions :example, state, example
- protect nil, @expectation_missing if !MSpec.expectation? and passed
+ protect nil, EXPECTATION_MISSING if !MSpec.expectation? and passed
end
end
protect "after :each", post(:each)
- protect "Mock.verify_count", @mock_verify
+ protect "Mock.verify_count", MOCK_VERIFY
- protect "Mock.cleanup", @mock_cleanup
+ protect "Mock.cleanup", MOCK_CLEANUP
MSpec.actions :after, state
@state = nil
end
end
protect "after :all", post(:all)
else
- protect "Mock.cleanup", @mock_cleanup
+ protect "Mock.cleanup", MOCK_CLEANUP
end
MSpec.actions :leave
diff --git a/spec/mspec/lib/mspec/runner/example.rb b/spec/mspec/lib/mspec/runner/example.rb
index 82feba0b03..0d9f0d618c 100644
--- a/spec/mspec/lib/mspec/runner/example.rb
+++ b/spec/mspec/lib/mspec/runner/example.rb
@@ -3,12 +3,12 @@ require 'mspec/runner/mspec'
# Holds some of the state of the example (i.e. +it+ block) that is
# being evaluated. See also +ContextState+.
class ExampleState
- attr_reader :context, :it, :example
+ attr_reader :context, :it, :example
def initialize(context, it, example = nil)
- @context = context
- @it = it
- @example = example
+ @context = context
+ @it = it
+ @example = example
end
def context=(context)
@@ -25,8 +25,8 @@ class ExampleState
end
def filtered?
- incl = MSpec.retrieve(:include) || []
- excl = MSpec.retrieve(:exclude) || []
+ incl = MSpec.include
+ excl = MSpec.exclude
included = incl.empty? || incl.any? { |f| f === description }
included &&= excl.empty? || !excl.any? { |f| f === description }
!included
diff --git a/spec/mspec/lib/mspec/runner/formatters/method.rb b/spec/mspec/lib/mspec/runner/formatters/method.rb
index 8fe02575c4..925858c845 100644
--- a/spec/mspec/lib/mspec/runner/formatters/method.rb
+++ b/spec/mspec/lib/mspec/runner/formatters/method.rb
@@ -6,13 +6,13 @@ class MethodFormatter < BaseFormatter
def initialize(out = nil)
super(out)
@methods = Hash.new do |h, k|
- hash = {}
- hash[:examples] = 0
- hash[:expectations] = 0
- hash[:failures] = 0
- hash[:errors] = 0
- hash[:exceptions] = []
- h[k] = hash
+ h[k] = {
+ examples: 0,
+ expectations: 0,
+ failures: 0,
+ errors: 0,
+ exceptions: []
+ }
end
end
diff --git a/spec/mspec/lib/mspec/runner/formatters/spinner.rb b/spec/mspec/lib/mspec/runner/formatters/spinner.rb
index 8815e1a48a..817d8c02be 100644
--- a/spec/mspec/lib/mspec/runner/formatters/spinner.rb
+++ b/spec/mspec/lib/mspec/runner/formatters/spinner.rb
@@ -78,7 +78,7 @@ class SpinnerFormatter < BaseFormatter
# Callback for the MSpec :start event. Stores the total
# number of files that will be processed.
def start
- @total = MSpec.retrieve(:files).size
+ @total = MSpec.files_array.size
compute_progress
print progress_line
end
diff --git a/spec/mspec/lib/mspec/runner/mspec.rb b/spec/mspec/lib/mspec/runner/mspec.rb
index 5528f27cf8..0e2496d3d7 100644
--- a/spec/mspec/lib/mspec/runner/mspec.rb
+++ b/spec/mspec/lib/mspec/runner/mspec.rb
@@ -10,7 +10,6 @@ class MSpecEnv
end
module MSpec
-
@exit = nil
@abort = nil
@start = nil
@@ -20,8 +19,8 @@ module MSpec
@after = nil
@leave = nil
@finish = nil
- @exclude = nil
- @include = nil
+ @exclude = []
+ @include = []
@leave = nil
@load = nil
@unload = nil
@@ -33,13 +32,19 @@ module MSpec
@guarded = []
@features = {}
@exception = nil
- @randomize = nil
- @repeat = nil
+ @randomize = false
+ @repeat = 1
@expectation = nil
@expectations = false
- def self.describe(mod, options = nil, &block)
- state = ContextState.new mod, options
+ class << self
+ attr_reader :file, :include, :exclude
+ attr_writer :repeat, :randomize
+ attr_accessor :formatter
+ end
+
+ def self.describe(description, options = nil, &block)
+ state = ContextState.new description, options
state.parent = current
MSpec.register_current state
@@ -57,6 +62,10 @@ module MSpec
actions :finish
end
+ def self.files_array
+ @files
+ end
+
def self.each_file(&block)
if ENV["MSPEC_MULTI"]
while file = STDIN.gets
@@ -74,7 +83,7 @@ module MSpec
# The parent closed the connection without QUIT
abort "the parent did not send QUIT"
else
- return unless files = retrieve(:files)
+ return unless files = @files
shuffle files if randomize?
files.each(&block)
end
@@ -83,10 +92,11 @@ module MSpec
def self.files
each_file do |file|
setup_env
- store :file, file
+ @file = file
actions :load
protect("loading #{file}") { Kernel.load file }
actions :unload
+ raise "#{file} was executed but did not reset the current example: #{@current}" if @current
end
end
@@ -101,7 +111,7 @@ module MSpec
def self.protect(location, &block)
begin
- @env.instance_eval(&block)
+ @env.instance_exec(&block)
return true
rescue SystemExit => e
raise e
@@ -130,17 +140,17 @@ module MSpec
# Sets the toplevel ContextState to +state+.
def self.register_current(state)
- store :current, state
+ @current = state
end
# Sets the toplevel ContextState to +nil+.
def self.clear_current
- store :current, nil
+ @current = nil
end
# Returns the toplevel ContextState.
def self.current
- retrieve :current
+ @current
end
# Stores the shared ContextState keyed by description.
@@ -155,17 +165,17 @@ module MSpec
# Stores the exit code used by the runner scripts.
def self.register_exit(code)
- store :exit, code
+ @exit = code
end
# Retrieves the stored exit code.
def self.exit_code
- retrieve(:exit).to_i
+ @exit.to_i
end
# Stores the list of files to be evaluated.
def self.register_files(files)
- store :files, files
+ @files = files
end
# Stores one or more substitution patterns for transforming
@@ -176,7 +186,7 @@ module MSpec
#
# See also +tags_file+.
def self.register_tags_patterns(patterns)
- store :tags_patterns, patterns
+ @tags_patterns = patterns
end
# Registers an operating mode. Modes recognized by MSpec:
@@ -187,30 +197,30 @@ module MSpec
# :report - specs that are guarded are reported
# :unguarded - all guards are forced off
def self.register_mode(mode)
- modes = retrieve :modes
+ modes = @modes
modes << mode unless modes.include? mode
end
# Clears all registered modes.
def self.clear_modes
- store :modes, []
+ @modes = []
end
# Returns +true+ if +mode+ is registered.
def self.mode?(mode)
- retrieve(:modes).include? mode
+ @modes.include? mode
end
def self.enable_feature(feature)
- retrieve(:features)[feature] = true
+ @features[feature] = true
end
def self.disable_feature(feature)
- retrieve(:features)[feature] = false
+ @features[feature] = false
end
def self.feature_enabled?(feature)
- retrieve(:features)[feature] || false
+ @features[feature] || false
end
def self.retrieve(symbol)
@@ -259,21 +269,17 @@ module MSpec
end
end
- def self.randomize(flag = true)
- @randomize = flag
- end
-
def self.randomize?
- @randomize == true
- end
-
- def self.repeat=(times)
- @repeat = times
+ @randomize
end
def self.repeat
- (@repeat || 1).times do
+ if @repeat == 1
yield
+ else
+ @repeat.times do
+ yield
+ end
end
end
@@ -289,17 +295,17 @@ module MSpec
# Records that an expectation has been encountered in an example.
def self.expectation
- store :expectations, true
+ @expectations = true
end
# Returns true if an expectation has been encountered
def self.expectation?
- retrieve :expectations
+ @expectations
end
# Resets the flag that an expectation has been encountered in an example.
def self.clear_expectations
- store :expectations, false
+ @expectations = false
end
# Transforms a spec filename into a tags filename by applying each
@@ -313,9 +319,9 @@ module MSpec
#
# See also +register_tags_patterns+.
def self.tags_file
- patterns = retrieve(:tags_patterns) ||
+ patterns = @tags_patterns ||
[[%r(spec/), 'spec/tags/'], [/_spec.rb$/, '_tags.txt']]
- patterns.inject(retrieve(:file).dup) do |file, pattern|
+ patterns.inject(@file.dup) do |file, pattern|
file.gsub(*pattern)
end
end
diff --git a/spec/mspec/lib/mspec/runner/object.rb b/spec/mspec/lib/mspec/runner/object.rb
index d5d9650795..58d98cc4df 100644
--- a/spec/mspec/lib/mspec/runner/object.rb
+++ b/spec/mspec/lib/mspec/runner/object.rb
@@ -7,8 +7,8 @@ class Object
MSpec.current.after at, &block
end
- private def describe(mod, msg = nil, options = nil, &block)
- MSpec.describe mod, msg, &block
+ private def describe(description, options = nil, &block)
+ MSpec.describe description, options, &block
end
private def it(desc, &block)