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: 50adf1c909e2
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: fcb8d1bae32a
Choose a head ref
  • 2 commits
  • 7 files changed
  • 1 contributor

Commits on Feb 3, 2016

  1. Squashed 'spec/mspec/' changes from 150e850..7a19a59

    7a19a59 Fix raise_error to not swallow any non-matching exception in combination with should_not!
    968fab4 Do not duplicate specs.
    d13da89 Strip trailing newlines of evaluate specs
    7cb4c83 Try to use the container infrastructure
    8c6ff42 Do not inherit constants and do not call Module.constants.
    b8be21e Merge pull request #13 from nobu/fix-wrong-option
    52eeb7f Fix exception at wrong option
    
    git-subtree-dir: spec/mspec
    git-subtree-split: 7a19a5997aa313fe0faf7ff6f2edba1cecb44654
    eregon committed Feb 3, 2016
    Copy the full SHA
    7d49ba4 View commit details
  2. Copy the full SHA
    fcb8d1b View commit details
1 change: 1 addition & 0 deletions spec/mspec/.travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
sudo: false
language: ruby
script:
- bundle exec rspec
21 changes: 15 additions & 6 deletions spec/mspec/lib/mspec/matchers/raise_error.rb
Original file line number Diff line number Diff line change
@@ -9,17 +9,26 @@ def matches?(proc)
@result = proc.call
return false
rescue Exception => @actual
return false unless @exception === @actual
if matching_exception?(@actual)
return true
else
raise @actual
end
end

def matching_exception?(exc)
return false unless @exception === exc
if @message then
case @message
when String then
return false if @message != @actual.message
when Regexp then
return false if @message !~ @actual.message
when String
return false if @message != exc.message
when Regexp
return false if @message !~ exc.message
end
end

@block[@actual] if @block
# The block has its own expectations and will throw an exception if it fails
@block[exc] if @block

return true
end
8 changes: 4 additions & 4 deletions spec/mspec/lib/mspec/runner/evaluate.rb
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ def self.desc
end

def initialize(ruby, desc)
@ruby = ruby
@ruby = ruby.rstrip
@desc = desc || self.class.desc
end

@@ -18,8 +18,8 @@ def initialize(ruby, desc)
# the source does contain newline characters, sets the indent level to four
# characters.
def format(ruby, newline=true)
if /\n/ =~ ruby
lines = ruby.rstrip.each_line.to_a
if ruby.include?("\n")
lines = ruby.each_line.to_a
if /( *)/ =~ lines.first
if $1.size > 4
dedent = $1.size - 4
@@ -31,7 +31,7 @@ def format(ruby, newline=true)
end
"\n#{ruby}"
else
"'#{ruby}'"
"'#{ruby.lstrip}'"
end
end

2 changes: 1 addition & 1 deletion spec/mspec/lib/mspec/utils/name_map.rb
Original file line number Diff line number Diff line change
@@ -110,7 +110,7 @@ def map(hash, constants, mod=nil)
ms.map! { |x| x.to_s }
hash["#{name}#"] = ms.sort unless ms.empty?

map hash, m.constants, name
map hash, m.constants(false), name
end

hash
2 changes: 1 addition & 1 deletion spec/mspec/lib/mspec/utils/options.rb
Original file line number Diff line number Diff line change
@@ -150,7 +150,7 @@ def parse(argv=ARGV)
opt, arg, rest = split rest, 1
opt = "-" + opt
option = process argv, opt, opt, arg
break if option.arg?
break if !option or option.arg?
end
end

41 changes: 32 additions & 9 deletions spec/mspec/spec/matchers/raise_error_spec.rb
Original file line number Diff line number Diff line change
@@ -8,7 +8,8 @@ class UnexpectedException < Exception; end
describe RaiseErrorMatcher do
it "matches when the proc raises the expected exception" do
proc = Proc.new { raise ExpectedException }
RaiseErrorMatcher.new(ExpectedException, nil).matches?(proc).should == true
matcher = RaiseErrorMatcher.new(ExpectedException, nil)
matcher.matches?(proc).should == true
end

it "executes it's optional block if matched" do
@@ -25,28 +26,50 @@ class UnexpectedException < Exception; end

it "matches when the proc raises the expected exception with the expected message" do
proc = Proc.new { raise ExpectedException, "message" }
RaiseErrorMatcher.new(ExpectedException, "message").matches?(proc).should == true
matcher = RaiseErrorMatcher.new(ExpectedException, "message")
matcher.matches?(proc).should == true
end

it "matches when the proc raises the expected exception with a matching message" do
proc = Proc.new { raise ExpectedException, "some message" }
matcher = RaiseErrorMatcher.new(ExpectedException, /some/)
matcher.matches?(proc).should == true
end

it "does not match when the proc does not raise the expected exception" do
proc = Proc.new { raise UnexpectedException }
RaiseErrorMatcher.new(ExpectedException, nil).matches?(proc).should == false
exc = UnexpectedException.new
matcher = RaiseErrorMatcher.new(ExpectedException, nil)

matcher.matching_exception?(exc).should == false
lambda {
matcher.matches?(Proc.new { raise exc })
}.should raise_error(UnexpectedException)
end

it "does not match when the proc raises the expected exception with an unexpected message" do
proc = Proc.new { raise ExpectedException, "unexpected" }
RaiseErrorMatcher.new(ExpectedException, "expected").matches?(proc).should == false
exc = ExpectedException.new("unexpected")
matcher = RaiseErrorMatcher.new(ExpectedException, "expected")

matcher.matching_exception?(exc).should == false
lambda {
matcher.matches?(Proc.new { raise exc })
}.should raise_error(ExpectedException)
end

it "does not match when the proc does not raise an exception" do
proc = Proc.new {}
RaiseErrorMatcher.new(ExpectedException, "expected").matches?(proc).should == false
matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
matcher.matches?(proc).should == false
end

it "provides a useful failure message" do
proc = Proc.new { raise UnexpectedException, "unexpected" }
exc = UnexpectedException.new("unexpected")
matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
matcher.matches?(proc)

matcher.matching_exception?(exc).should == false
lambda {
matcher.matches?(Proc.new { raise exc })
}.should raise_error(UnexpectedException)
matcher.failure_message.should ==
["Expected ExpectedException (expected)", "but got UnexpectedException (unexpected)"]
end
74 changes: 2 additions & 72 deletions spec/mspec/spec/matchers/raise_exception_spec.rb
Original file line number Diff line number Diff line change
@@ -6,77 +6,7 @@ class ExpectedException < Exception; end
class UnexpectedException < Exception; end

describe RaiseExceptionMatcher do
it "matches when the proc raises the expected exception" do
proc = Proc.new { raise ExpectedException }
RaiseErrorMatcher.new(ExpectedException, nil).matches?(proc).should == true
end

it "executes it's optional block if matched" do
run = false
proc = Proc.new { raise ExpectedException }
matcher = RaiseErrorMatcher.new(ExpectedException, nil) { |error|
run = true
error.class.should == ExpectedException
}

matcher.matches?(proc).should == true
run.should == true
end

it "matches when the proc raises the expected exception with the expected message" do
proc = Proc.new { raise ExpectedException, "message" }
RaiseErrorMatcher.new(ExpectedException, "message").matches?(proc).should == true
end

it "does not match when the proc does not raise the expected exception" do
proc = Proc.new { raise UnexpectedException }
RaiseErrorMatcher.new(ExpectedException, nil).matches?(proc).should == false
end

it "does not match when the proc raises the expected exception with an unexpected message" do
proc = Proc.new { raise ExpectedException, "unexpected" }
RaiseErrorMatcher.new(ExpectedException, "expected").matches?(proc).should == false
end

it "does not match when the proc does not raise an exception" do
proc = Proc.new {}
RaiseErrorMatcher.new(ExpectedException, "expected").matches?(proc).should == false
end

it "does not match when the raised exception is not an instance of the expected exception" do
proc = Proc.new { raise Exception }
RaiseErrorMatcher.new(ExpectedException, nil).matches?(proc).should == false
end

it "provides a useful failure message" do
proc = Proc.new { raise UnexpectedException, "unexpected" }
matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
matcher.matches?(proc)
matcher.failure_message.should ==
["Expected ExpectedException (expected)", "but got UnexpectedException (unexpected)"]
end

it "provides a useful failure message when no exception is raised" do
proc = Proc.new { 120 }
matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
matcher.matches?(proc)
matcher.failure_message.should ==
["Expected ExpectedException (expected)", "but no exception was raised (120 was returned)"]
end

it "provides a useful negative failure message" do
proc = Proc.new { raise ExpectedException, "expected" }
matcher = RaiseErrorMatcher.new(ExpectedException, "expected")
matcher.matches?(proc)
matcher.negative_failure_message.should ==
["Expected to not get ExpectedException (expected)", ""]
end

it "provides a useful negative failure message for strict subclasses of the matched exception class" do
proc = Proc.new { raise UnexpectedException, "unexpected" }
matcher = RaiseErrorMatcher.new(Exception, nil)
matcher.matches?(proc)
matcher.negative_failure_message.should ==
["Expected to not get Exception", "but got UnexpectedException (unexpected)"]
it "is a legac alias of RaiseErrorMatcher" do
RaiseExceptionMatcher.should equal(RaiseErrorMatcher)
end
end