Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 439f9156f68a
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 82468862f138
Choose a head ref
  • 4 commits
  • 31 files changed
  • 1 contributor

Commits on Jul 27, 2017

  1. Squashed 'spec/mspec/' changes from 021a119..353605f

    353605f Add a small script to search for multi-line regexp patterns
    ea7600d Implement #guard and #guard_not to combine guards
    9c56e03 Add specs for SpecGuard#{run_if,run_unless}
    f1ddbbd Use stub_const in PlatformGuard specs
    e5d30e3 Remove unused file
    aa5a51b Fix a typo
    d8e4d0c Require to pass a maximum value for MSpecScript#cores
    
    git-subtree-dir: spec/mspec
    git-subtree-split: 353605f0266f344d784231a7b24a2444c3255285
    eregon committed Jul 27, 2017
    Copy the full SHA
    b7f09a5 View commit details
  2. Copy the full SHA
    ffbd2b8 View commit details
  3. Squashed 'spec/ruby/' changes from 86090df..c3e6b90

    c3e6b90 Avoid nested platform_is_not guards in Process#setrlimit spec
    25d17e1 Use #guard in String#unpack('lL')
    734643b Use #guard in IO#close_on_exec{=,?} spec and unshare specs
    8f8a787 Use #guard in pack('lL') spec
    cb5c619 Use #guard instead of if for pack('J') spec
    5afae7c Used a fixed seed in Random#rand spec
    1746e3a [spec] add some BigDecimal#to_r specs
    5b3c045 Fix spec for StringScanner#peek and check for a byte slice
    c8e9070 Add spec for String#{[],slice,byteslice} with a too large index or length
    d361f81 Add spec for the encoding of the result of String#{slice,[]}
    c171890 Simplify and just use int for the return value of rb_io_wait_readable
    908b2c5 Save and restore errno in rb_io_wait_readable spec
    fc04747 Try to fix race in rb_thread_call_without_gvl spec
    010a0be Fix C-API globals spec to not block if the sub-thread dies
    063de5b Add additional Numeric#step specs
    be77411 Simplify spec for invalid break from another thread
    8da1d68 Ensure that LocalJumpError comes from Thread#value instead of break
    60f2d29 Avoid repeating scratchpad symbols in method
    837f04b Add spec for an invalid break from another thread
    d2f0a3b Fix race in ThreadSpecs.status_of_dying_thread_after_sleep
    1332d7b Initialize $foo before spec
    e891a28 Suppress intended warning
    bfcbf9a Add specs for a Thread status after it is killed, then sleeping and woken up
    764f99e Fix copy/paste error in Thread#inspect spec
    75a88c2 Restore thread status aborting specs
    c4cf24a Remove Gemfile.lock
    79ed474 Use https instead of git protocol
    
    git-subtree-dir: spec/ruby
    git-subtree-split: c3e6b9017926f44a76e2b966c4dd35fa84c4cd3b
    eregon committed Jul 27, 2017
    Copy the full SHA
    fa78f58 View commit details
  4. Copy the full SHA
    8246886 View commit details
28 changes: 26 additions & 2 deletions spec/mspec/lib/mspec/guards/guard.rb
Original file line number Diff line number Diff line change
@@ -76,14 +76,22 @@ def yield?(invert = false)

def run_if(name, &block)
@name = name
yield if yield?(false)
if block
yield if yield?(false)
else
yield?(false)
end
ensure
unregister
end

def run_unless(name, &block)
@name = name
yield if yield?(true)
if block
yield if yield?(true)
else
yield?(true)
end
ensure
unregister
end
@@ -115,3 +123,19 @@ def match?
raise "must be implemented by the subclass"
end
end

# Combined guards

def guard(condition, &block)
raise "condition must be a Proc" unless condition.is_a?(Proc)
raise LocalJumpError, "no block given" unless block
return yield if MSpec.mode? :unguarded or MSpec.mode? :verify or MSpec.mode? :report
yield if condition.call
end

def guard_not(condition, &block)
raise "condition must be a Proc" unless condition.is_a?(Proc)
raise LocalJumpError, "no block given" unless block
return yield if MSpec.mode? :unguarded or MSpec.mode? :verify or MSpec.mode? :report
yield unless condition.call
end
2 changes: 1 addition & 1 deletion spec/mspec/lib/mspec/matchers/base.rb
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ class MSpecEnv
include MSpecMatchers
end

# Expactations are sometimes used in a module body
# Expectations are sometimes used in a module body
class Module
include MSpecMatchers
end
2 changes: 1 addition & 1 deletion spec/mspec/lib/mspec/utils/script.rb
Original file line number Diff line number Diff line change
@@ -234,7 +234,7 @@ def files_from_patterns(patterns)
files patterns
end

def cores(max = 1)
def cores(max)
require 'etc'
[Etc.nprocessors, max].min
end
246 changes: 246 additions & 0 deletions spec/mspec/spec/guards/guard_spec.rb
Original file line number Diff line number Diff line change
@@ -178,3 +178,249 @@
]
end
end

describe SpecGuard, ".run_if" do
before :each do
@guard = SpecGuard.new
ScratchPad.clear
end

it "yields if match? returns true" do
@guard.stub(:match?).and_return(true)
@guard.run_if(:name) { ScratchPad.record :yield }
ScratchPad.recorded.should == :yield
end

it "does not yield if match? returns false" do
@guard.stub(:match?).and_return(false)
@guard.run_if(:name) { fail }
end

it "returns the result of the block if match? is true" do
@guard.stub(:match?).and_return(true)
@guard.run_if(:name) { 42 }.should == 42
end

it "returns nil if given a block and match? is false" do
@guard.stub(:match?).and_return(false)
@guard.run_if(:name) { 42 }.should == nil
end

it "returns what #match? returns when no block is given" do
@guard.stub(:match?).and_return(true)
@guard.run_if(:name).should == true
@guard.stub(:match?).and_return(false)
@guard.run_if(:name).should == false
end
end

describe SpecGuard, ".run_unless" do
before :each do
@guard = SpecGuard.new
ScratchPad.clear
end

it "yields if match? returns false" do
@guard.stub(:match?).and_return(false)
@guard.run_unless(:name) { ScratchPad.record :yield }
ScratchPad.recorded.should == :yield
end

it "does not yield if match? returns true" do
@guard.stub(:match?).and_return(true)
@guard.run_unless(:name) { fail }
end

it "returns the result of the block if match? is false" do
@guard.stub(:match?).and_return(false)
@guard.run_unless(:name) { 42 }.should == 42
end

it "returns nil if given a block and match? is true" do
@guard.stub(:match?).and_return(true)
@guard.run_unless(:name) { 42 }.should == nil
end

it "returns the opposite of what #match? returns when no block is given" do
@guard.stub(:match?).and_return(true)
@guard.run_unless(:name).should == false
@guard.stub(:match?).and_return(false)
@guard.run_unless(:name).should == true
end
end

describe Object, "#guard" do
before :each do
ScratchPad.clear
end

after :each do
MSpec.clear_modes
end

it "allows to combine guards" do
guard1 = VersionGuard.new 'x.x.x'
VersionGuard.stub(:new).and_return(guard1)
guard2 = PlatformGuard.new :dummy
PlatformGuard.stub(:new).and_return(guard2)

guard1.stub(:match?).and_return(true)
guard2.stub(:match?).and_return(true)
guard -> { ruby_version_is "2.4" and platform_is :linux } do
ScratchPad.record :yield
end
ScratchPad.recorded.should == :yield

guard1.stub(:match?).and_return(false)
guard2.stub(:match?).and_return(true)
guard -> { ruby_version_is "2.4" and platform_is :linux } do
fail
end

guard1.stub(:match?).and_return(true)
guard2.stub(:match?).and_return(false)
guard -> { ruby_version_is "2.4" and platform_is :linux } do
fail
end

guard1.stub(:match?).and_return(false)
guard2.stub(:match?).and_return(false)
guard -> { ruby_version_is "2.4" and platform_is :linux } do
fail
end
end

it "yields when the Proc returns true" do
guard -> { true } do
ScratchPad.record :yield
end
ScratchPad.recorded.should == :yield
end

it "does not yield when the Proc returns false" do
guard -> { false } do
fail
end
end

it "yields if MSpec.mode?(:unguarded) is true" do
MSpec.register_mode :unguarded

guard -> { false } do
ScratchPad.record :yield1
end
ScratchPad.recorded.should == :yield1

guard -> { true } do
ScratchPad.record :yield2
end
ScratchPad.recorded.should == :yield2
end

it "yields if MSpec.mode?(:verify) is true" do
MSpec.register_mode :verify

guard -> { false } do
ScratchPad.record :yield1
end
ScratchPad.recorded.should == :yield1

guard -> { true } do
ScratchPad.record :yield2
end
ScratchPad.recorded.should == :yield2
end

it "yields if MSpec.mode?(:report) is true" do
MSpec.register_mode :report

guard -> { false } do
ScratchPad.record :yield1
end
ScratchPad.recorded.should == :yield1

guard -> { true } do
ScratchPad.record :yield2
end
ScratchPad.recorded.should == :yield2
end

it "raises an error if no Proc is given" do
-> { guard :foo }.should raise_error(RuntimeError)
end

it "requires a block" do
-> {
guard(-> { true })
}.should raise_error(LocalJumpError)
-> {
guard(-> { false })
}.should raise_error(LocalJumpError)
end
end

describe Object, "#guard_not" do
before :each do
ScratchPad.clear
end

it "allows to combine guards" do
guard1 = VersionGuard.new 'x.x.x'
VersionGuard.stub(:new).and_return(guard1)
guard2 = PlatformGuard.new :dummy
PlatformGuard.stub(:new).and_return(guard2)

guard1.stub(:match?).and_return(true)
guard2.stub(:match?).and_return(true)
guard_not -> { ruby_version_is "2.4" and platform_is :linux } do
fail
end

guard1.stub(:match?).and_return(false)
guard2.stub(:match?).and_return(true)
guard_not -> { ruby_version_is "2.4" and platform_is :linux } do
ScratchPad.record :yield1
end
ScratchPad.recorded.should == :yield1

guard1.stub(:match?).and_return(true)
guard2.stub(:match?).and_return(false)
guard_not -> { ruby_version_is "2.4" and platform_is :linux } do
ScratchPad.record :yield2
end
ScratchPad.recorded.should == :yield2

guard1.stub(:match?).and_return(false)
guard2.stub(:match?).and_return(false)
guard_not -> { ruby_version_is "2.4" and platform_is :linux } do
ScratchPad.record :yield3
end
ScratchPad.recorded.should == :yield3
end

it "yields when the Proc returns false" do
guard_not -> { false } do
ScratchPad.record :yield
end
ScratchPad.recorded.should == :yield
end

it "does not yield when the Proc returns true" do
guard_not -> { true } do
fail
end
end

it "raises an error if no Proc is given" do
-> { guard_not :foo }.should raise_error(RuntimeError)
end

it "requires a block" do
-> {
guard_not(-> { true })
}.should raise_error(LocalJumpError)
-> {
guard_not(-> { false })
}.should raise_error(LocalJumpError)
end
end
Loading