1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
This commit is contained in:
Benoit Daloze 2020-03-28 00:22:48 +01:00
parent d214c188e4
commit 296f68816c
20 changed files with 192 additions and 162 deletions

View file

@ -1,28 +1,29 @@
require 'mspec/guards/version'
class BugGuard < VersionGuard
def initialize(bug, version)
def initialize(bug, requirement)
@bug = bug
if String === version
if String === requirement
MSpec.deprecate "ruby_bug with a single version", 'an exclusive range ("2.1"..."2.3")'
@version = SpecVersion.new version, true
@requirement = SpecVersion.new requirement, true
super(FULL_RUBY_VERSION, requirement)
else
super(version)
super(FULL_RUBY_VERSION, requirement)
end
@parameters = [@bug, @version]
end
def match?
return false if MSpec.mode? :no_ruby_bug
return false unless PlatformGuard.standard?
if Range === @version
if Range === @requirement
super
else
FULL_RUBY_VERSION <= @version
FULL_RUBY_VERSION <= @requirement
end
end
end
def ruby_bug(bug, version, &block)
BugGuard.new(bug, version).run_unless(:ruby_bug, &block)
def ruby_bug(bug, requirement, &block)
BugGuard.new(bug, requirement).run_unless(:ruby_bug, &block)
end

View file

@ -111,7 +111,7 @@ class SpecGuard
def add(example)
record example.description
MSpec.retrieve(:formatter).tally.counter.guards!
MSpec.formatter.tally.counter.guards!
end
def unregister

View file

@ -5,33 +5,40 @@ require 'mspec/guards/guard'
class VersionGuard < SpecGuard
FULL_RUBY_VERSION = SpecVersion.new SpecGuard.ruby_version(:full)
def initialize(version)
case version
def initialize(version, requirement)
version = SpecVersion.new(version) unless SpecVersion === version
@version = version
case requirement
when String
@version = SpecVersion.new version
@requirement = SpecVersion.new requirement
when Range
MSpec.deprecate "an empty version range end", 'a specific version' if version.end.empty?
a = SpecVersion.new version.begin
b = SpecVersion.new version.end
unless version.exclude_end?
MSpec.deprecate "an empty version range end", 'a specific version' if requirement.end.empty?
a = SpecVersion.new requirement.begin
b = SpecVersion.new requirement.end
unless requirement.exclude_end?
MSpec.deprecate "ruby_version_is with an inclusive range", 'an exclusive range ("2.1"..."2.3")'
end
@version = version.exclude_end? ? a...b : a..b
@requirement = requirement.exclude_end? ? a...b : a..b
else
raise "version must be a String or Range but was a #{version.class}"
raise "version must be a String or Range but was a #{requirement.class}"
end
@parameters = [version]
super(@version, @requirement)
end
def match?
if Range === @version
@version.include? FULL_RUBY_VERSION
if Range === @requirement
@requirement.include? @version
else
FULL_RUBY_VERSION >= @version
@version >= @requirement
end
end
end
def ruby_version_is(*args, &block)
VersionGuard.new(*args).run_if(:ruby_version_is, &block)
def version_is(base_version, requirement, &block)
VersionGuard.new(base_version, requirement).run_if(:version_is, &block)
end
def ruby_version_is(requirement, &block)
VersionGuard.new(VersionGuard::FULL_RUBY_VERSION, requirement).run_if(:ruby_version_is, &block)
end

View file

@ -14,4 +14,8 @@ module ScratchPad
def self.recorded
@record
end
def self.inspect
"<ScratchPad @record=#{@record.inspect}>"
end
end

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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)

View file

@ -281,7 +281,7 @@ class MSpecOptions
when 'j', 'junit'
config[:formatter] = JUnitFormatter
else
abort "Unknown format: #{o}\n#{@parser}" unless File.exist?(o)
abort "Unknown format: #{o}" unless File.exist?(o)
require File.expand_path(o)
if Object.const_defined?(:CUSTOM_MSPEC_FORMATTER)
config[:formatter] = CUSTOM_MSPEC_FORMATTER
@ -377,7 +377,7 @@ class MSpecOptions
def randomize
on("-H", "--random",
"Randomize the list of spec files") do
MSpec.randomize
MSpec.randomize = true
end
end
@ -392,10 +392,10 @@ class MSpecOptions
on("-V", "--verbose", "Output the name of each file processed") do
obj = Object.new
def obj.start
@width = MSpec.retrieve(:files).inject(0) { |max, f| f.size > max ? f.size : max }
@width = MSpec.files_array.inject(0) { |max, f| f.size > max ? f.size : max }
end
def obj.load
file = MSpec.retrieve :file
file = MSpec.file
STDERR.print "\n#{file.ljust(@width)}"
end
MSpec.register :start, obj

View file

@ -127,7 +127,7 @@ class MSpecScript
if formatter = config_formatter
formatter.register
MSpec.store :formatter, formatter
MSpec.formatter = formatter
end
MatchFilter.new(:include, *config[:includes]).register unless config[:includes].empty?

View file

@ -254,7 +254,7 @@ describe Object, "#guard" do
end
it "allows to combine guards" do
guard1 = VersionGuard.new 'x.x.x'
guard1 = VersionGuard.new '1.2.3', 'x.x.x'
VersionGuard.stub(:new).and_return(guard1)
guard2 = PlatformGuard.new :dummy
PlatformGuard.stub(:new).and_return(guard2)
@ -360,7 +360,7 @@ describe Object, "#guard_not" do
end
it "allows to combine guards" do
guard1 = VersionGuard.new 'x.x.x'
guard1 = VersionGuard.new '1.2.3', 'x.x.x'
VersionGuard.stub(:new).and_return(guard1)
guard2 = PlatformGuard.new :dummy
PlatformGuard.stub(:new).and_return(guard2)

View file

@ -14,44 +14,44 @@ require 'mspec/guards'
describe VersionGuard, "#match?" do
before :each do
hide_deprecation_warnings
stub_const "VersionGuard::FULL_RUBY_VERSION", SpecVersion.new('1.8.6')
@current = '1.8.6'
end
it "returns true when the argument is equal to RUBY_VERSION" do
VersionGuard.new('1.8.6').match?.should == true
VersionGuard.new(@current, '1.8.6').match?.should == true
end
it "returns true when the argument is less than RUBY_VERSION" do
VersionGuard.new('1.8').match?.should == true
VersionGuard.new('1.8.5').match?.should == true
VersionGuard.new(@current, '1.8').match?.should == true
VersionGuard.new(@current, '1.8.5').match?.should == true
end
it "returns false when the argument is greater than RUBY_VERSION" do
VersionGuard.new('1.8.7').match?.should == false
VersionGuard.new('1.9.2').match?.should == false
VersionGuard.new(@current, '1.8.7').match?.should == false
VersionGuard.new(@current, '1.9.2').match?.should == false
end
it "returns true when the argument range includes RUBY_VERSION" do
VersionGuard.new('1.8.5'..'1.8.7').match?.should == true
VersionGuard.new('1.8'..'1.9').match?.should == true
VersionGuard.new('1.8'...'1.9').match?.should == true
VersionGuard.new('1.8'..'1.8.6').match?.should == true
VersionGuard.new('1.8.5'..'1.8.6').match?.should == true
VersionGuard.new(''...'1.8.7').match?.should == true
VersionGuard.new(@current, '1.8.5'..'1.8.7').match?.should == true
VersionGuard.new(@current, '1.8'..'1.9').match?.should == true
VersionGuard.new(@current, '1.8'...'1.9').match?.should == true
VersionGuard.new(@current, '1.8'..'1.8.6').match?.should == true
VersionGuard.new(@current, '1.8.5'..'1.8.6').match?.should == true
VersionGuard.new(@current, ''...'1.8.7').match?.should == true
end
it "returns false when the argument range does not include RUBY_VERSION" do
VersionGuard.new('1.8.7'..'1.8.9').match?.should == false
VersionGuard.new('1.8.4'..'1.8.5').match?.should == false
VersionGuard.new('1.8.4'...'1.8.6').match?.should == false
VersionGuard.new('1.8.5'...'1.8.6').match?.should == false
VersionGuard.new(''...'1.8.6').match?.should == false
VersionGuard.new(@current, '1.8.7'..'1.8.9').match?.should == false
VersionGuard.new(@current, '1.8.4'..'1.8.5').match?.should == false
VersionGuard.new(@current, '1.8.4'...'1.8.6').match?.should == false
VersionGuard.new(@current, '1.8.5'...'1.8.6').match?.should == false
VersionGuard.new(@current, ''...'1.8.6').match?.should == false
end
end
describe Object, "#ruby_version_is" do
before :each do
@guard = VersionGuard.new 'x.x.x'
@guard = VersionGuard.new '1.2.3', 'x.x.x'
VersionGuard.stub(:new).and_return(@guard)
ScratchPad.clear
end
@ -88,3 +88,25 @@ describe Object, "#ruby_version_is" do
end.should raise_error(Exception)
end
end
describe Object, "#version_is" do
before :each do
hide_deprecation_warnings
end
it "returns the expected values" do
version_is('1.2.3', '1.2.2').should == true
version_is('1.2.3', '1.2.3').should == true
version_is('1.2.3', '1.2.4').should == false
version_is('1.2.3', '1').should == true
version_is('1.2.3', '1.0').should == true
version_is('1.2.3', '2').should == false
version_is('1.2.3', '2.0').should == false
version_is('1.2.3', '1.2.2'..'1.2.4').should == true
version_is('1.2.3', '1.2.2'..'1.2.3').should == true
version_is('1.2.3', '1.2.2'...'1.2.3').should == false
version_is('1.2.3', '1.2.3'..'1.2.4').should == true
end
end

View file

@ -55,22 +55,6 @@ describe ContextState, "#to_s" do
it "returns a description string for self when passed a String" do
ContextState.new("SomeClass").to_s.should == "SomeClass"
end
it "returns a description string for self when passed a Module, String" do
ContextState.new(Object, "when empty").to_s.should == "Object when empty"
end
it "returns a description string for self when passed a Module and String beginning with '#'" do
ContextState.new(Object, "#to_s").to_s.should == "Object#to_s"
end
it "returns a description string for self when passed a Module and String beginning with '.'" do
ContextState.new(Object, ".to_s").to_s.should == "Object.to_s"
end
it "returns a description string for self when passed a Module and String beginning with '::'" do
ContextState.new(Object, "::to_s").to_s.should == "Object::to_s"
end
end
describe ContextState, "#description" do
@ -464,11 +448,14 @@ describe ContextState, "#process" do
end
it "shuffles the spec list if MSpec.randomize? is true" do
MSpec.randomize
MSpec.should_receive(:shuffle)
@state.it("") { }
@state.process
MSpec.randomize false
MSpec.randomize = true
begin
MSpec.should_receive(:shuffle)
@state.it("") { }
@state.process
ensure
MSpec.randomize = false
end
end
it "sets the current MSpec ContextState" do

View file

@ -14,7 +14,7 @@ end
describe ExampleState, "#describe" do
before :each do
@context = ContextState.new Object, "#to_s"
@context = ContextState.new "Object#to_s"
@state = ExampleState.new @context, "it"
end
@ -64,16 +64,16 @@ end
describe ExampleState, "#filtered?" do
before :each do
MSpec.store :include, nil
MSpec.store :exclude, nil
MSpec.store :include, []
MSpec.store :exclude, []
@state = ExampleState.new ContextState.new("describe"), "it"
@filter = double("filter")
end
after :each do
MSpec.store :include, nil
MSpec.store :exclude, nil
MSpec.store :include, []
MSpec.store :exclude, []
end
it "returns false if MSpec include filters list is empty" do

View file

@ -1,4 +1,5 @@
require 'spec_helper'
require 'mspec/expectations/expectations'
require 'mspec/helpers/tmp'
require 'mspec/helpers/fs'
require 'mspec/matchers/base'
@ -8,7 +9,7 @@ require 'mspec/runner/example'
describe MSpec, ".register_files" do
it "records which spec files to run" do
MSpec.register_files [:one, :two, :three]
MSpec.retrieve(:files).should == [:one, :two, :three]
MSpec.files_array.should == [:one, :two, :three]
end
end
@ -66,9 +67,9 @@ end
describe MSpec, ".randomize" do
it "sets the flag to randomize spec execution order" do
MSpec.randomize?.should == false
MSpec.randomize
MSpec.randomize = true
MSpec.randomize?.should == true
MSpec.randomize false
MSpec.randomize = false
MSpec.randomize?.should == false
end
end
@ -342,17 +343,19 @@ describe MSpec, ".files" do
end
it "shuffles the file list if .randomize? is true" do
MSpec.randomize
MSpec.randomize = true
MSpec.should_receive(:shuffle)
MSpec.files
MSpec.randomize false
MSpec.randomize = false
end
it "registers the current file" do
MSpec.should_receive(:store).with(:file, :one)
MSpec.should_receive(:store).with(:file, :two)
MSpec.should_receive(:store).with(:file, :three)
load = double("load")
files = []
load.stub(:load).and_return { files << MSpec.file }
MSpec.register :load, load
MSpec.files
files.should == [:one, :two, :three]
end
end

View file

@ -1044,7 +1044,7 @@ describe "The -H, --random option" do
end
it "registers the MSpec randomize mode" do
MSpec.should_receive(:randomize).twice
MSpec.should_receive(:randomize=).twice
["-H", "--random"].each do |opt|
@options.parse opt
end

View file

@ -250,8 +250,8 @@ describe MSpecScript, "#register" do
it "registers :formatter with the formatter instance" do
@formatter.stub(:new).and_return(@formatter)
MSpec.should_receive(:store).with(:formatter, @formatter)
@script.register
MSpec.formatter.should be(@formatter)
end
it "does not register :formatter if config[:formatter] is false" do