Skip to content

Commit

Permalink
[GR-14806] Update specs.
Browse files Browse the repository at this point in the history
PullRequest: truffleruby/1728
  • Loading branch information
eregon committed Jun 27, 2020
2 parents aecb8d3 + bbeb0a4 commit 63ac861
Show file tree
Hide file tree
Showing 29 changed files with 375 additions and 71 deletions.
19 changes: 10 additions & 9 deletions lib/truffle/timeout.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true
#--
# = timeout.rb
#
Expand Down Expand Up @@ -47,10 +48,11 @@ class Error < RuntimeError
# Represents +thr+ asking for it to be timeout at in +secs+
# seconds. At timeout, raise +exc+.
class TimeoutRequest
def initialize(secs, thr, exc)
def initialize(secs, thr, exc, message)
@left = secs
@thread = thr
@exception = exc
@message = message
end

attr_reader :thread, :left
Expand All @@ -65,7 +67,7 @@ def elapsed(time)
# Raise @exception if @thread.
def cancel
if @thread and @thread.alive?
@thread.raise @exception, 'execution expired'
@thread.raise @exception, @message
end

@left = 0
Expand Down Expand Up @@ -126,8 +128,8 @@ def self.ensure_timeout_thread_running
end
end

def self.add_timeout(time, exc)
r = TimeoutRequest.new(time, Thread.current, exc)
def self.add_timeout(time, exc, message)
r = TimeoutRequest.new(time, Thread.current, exc, message)
@chan << r
ensure_timeout_thread_running
r
Expand All @@ -136,7 +138,7 @@ def self.add_timeout(time, exc)

if Truffle::Boot.single_threaded?

def timeout(sec, exception=Error)
def timeout(sec, exception = Error, message = nil)
Truffle::Debug.log_warning 'threads are disabled, so timeout is being ignored'
yield sec
end
Expand All @@ -152,12 +154,11 @@ def timeout(sec, exception=Error)
# Timeout' into your classes so they have a #timeout method, as well as a
# module method, so you can call it directly as Timeout.timeout().

def timeout(sec, exception=Error)
def timeout(sec, exception = Error, message = nil)
return yield if sec == nil or sec.zero?
raise ThreadError, 'timeout within critical session' if Thread.respond_to?(:critical) && Thread.critical

req = Timeout.add_timeout sec, exception

message ||= 'execution expired'
req = Timeout.add_timeout sec, exception, message
begin
yield sec
ensure
Expand Down
2 changes: 0 additions & 2 deletions spec/mspec/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
[![Build Status](https://travis-ci.org/ruby/mspec.svg?branch=master)](https://travis-ci.org/ruby/mspec)

## Overview

MSpec is a specialized framework that is syntax-compatible with RSpec 2 for
Expand Down
3 changes: 3 additions & 0 deletions spec/mspec/tool/sync/sync-rubyspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
BRIGHT_YELLOW = "\e[33;1m"
RESET = "\e[0m"

# git filter-branch --subdirectory-filter works fine for our use case
ENV['FILTER_BRANCH_SQUELCH_WARNING'] = '1'

class RubyImplementation
attr_reader :name

Expand Down
4 changes: 4 additions & 0 deletions spec/ruby/.rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Layout/TrailingEmptyLines:
Exclude:
- library/coverage/fixtures/some_class.rb

Layout/SpaceInLambdaLiteral:
Enabled: true
EnforcedStyle: require_space

Lint:
Enabled: true

Expand Down
7 changes: 7 additions & 0 deletions spec/ruby/core/env/delete_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
ScratchPad.recorded.should == "foo"
end

ruby_version_is "2.8" do
it "returns the result of given block if the named environment variable does not exist" do
ENV.delete("foo")
ENV.delete("foo") { |name| "bar" }.should == "bar"
end
end

it "does not evaluate the block if the environment variable exists" do
ENV["foo"] = "bar"
ENV.delete("foo") { |name| fail "Should not happen" }
Expand Down
18 changes: 18 additions & 0 deletions spec/ruby/core/exception/no_method_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,24 @@ def inspect
message.should include test_class.inspect
end
end

ruby_version_is "2.8" do
it "uses #name to display the receiver if it is a class or a module" do
klass = Class.new { def self.name; "MyClass"; end }
begin
klass.foo
rescue NoMethodError => error
error.message.lines.first.should == "undefined method `foo' for MyClass:Class"
end

mod = Module.new { def self.name; "MyModule"; end }
begin
mod.foo
rescue NoMethodError => error
error.message.lines.first.should == "undefined method `foo' for MyModule:Module"
end
end
end
end

describe "NoMethodError#dup" do
Expand Down
4 changes: 2 additions & 2 deletions spec/ruby/core/io/ioctl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@
end
end

it "raises an Errno error when ioctl fails" do
it "raises a system call error when ioctl fails" do
File.open(__FILE__, 'r') do |f|
-> {
# TIOCGWINSZ in /usr/include/asm-generic/ioctls.h
f.ioctl 0x5413, nil
}.should raise_error(Errno::ENOTTY)
}.should raise_error(SystemCallError)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/ruby/core/kernel/fixtures/classes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ class ForwardBlockWithZSuper

module Ampersand
def lambda(&block)
super(&block)
suppress_warning {super(&block)}
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/ruby/core/kernel/lambda_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

it "returns the passed Proc if given an existing Proc" do
some_proc = proc {}
l = lambda(&some_proc)
l = suppress_warning {lambda(&some_proc)}
l.should equal(some_proc)
l.lambda?.should be_false
end
Expand All @@ -55,7 +55,7 @@

it "does not create lambda-style Procs when captured with #method" do
kernel_lambda = method(:lambda)
l = kernel_lambda.call { 42 }
l = suppress_warning {kernel_lambda.call { 42 }}
l.lambda?.should be_false
l.call(:extra).should == 42
end
Expand Down
22 changes: 13 additions & 9 deletions spec/ruby/core/kernel/proc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,31 @@ def test
end

describe "Kernel#proc" do
def some_method
proc
end

ruby_version_is ""..."2.7" do
it "uses the implicit block from an enclosing method" do
def some_method
proc
end

prc = some_method { "hello" }

prc.call.should == "hello"
end
end

ruby_version_is "2.7" do
ruby_version_is "2.7"..."2.8" do
it "can be created when called with no block" do
def some_method
proc
end

-> {
some_method { "hello" }
}.should complain(/Capturing the given block using Kernel#proc is deprecated/)
end
end

ruby_version_is "2.8" do
it "raises an ArgumentError when passed no block" do
-> {
some_method { "hello" }
}.should raise_error(ArgumentError, 'tried to create Proc object without a block')
end
end
end
39 changes: 28 additions & 11 deletions spec/ruby/core/module/prepend_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,34 @@ def self.prepend_features(mod)
c.dup.new.should be_kind_of(m)
end

it "keeps the module in the chain when dupping an intermediate module" do
m1 = Module.new { def calc(x) x end }
m2 = Module.new { prepend(m1) }
c1 = Class.new { prepend(m2) }
m2dup = m2.dup
m2dup.ancestors.should == [m2dup,m1,m2]
c2 = Class.new { prepend(m2dup) }
c1.ancestors[0,3].should == [m1,m2,c1]
c1.new.should be_kind_of(m1)
c2.ancestors[0,4].should == [m2dup,m1,m2,c2]
c2.new.should be_kind_of(m1)
ruby_version_is '0'...'2.8' do
it "keeps the module in the chain when dupping an intermediate module" do
m1 = Module.new { def calc(x) x end }
m2 = Module.new { prepend(m1) }
c1 = Class.new { prepend(m2) }
m2dup = m2.dup
m2dup.ancestors.should == [m2dup,m1,m2]
c2 = Class.new { prepend(m2dup) }
c1.ancestors[0,3].should == [m1,m2,c1]
c1.new.should be_kind_of(m1)
c2.ancestors[0,4].should == [m2dup,m1,m2,c2]
c2.new.should be_kind_of(m1)
end
end

ruby_version_is '2.8' do
it "uses only new module when dupping the module" do
m1 = Module.new { def calc(x) x end }
m2 = Module.new { prepend(m1) }
c1 = Class.new { prepend(m2) }
m2dup = m2.dup
m2dup.ancestors.should == [m1,m2dup]
c2 = Class.new { prepend(m2dup) }
c1.ancestors[0,3].should == [m1,m2,c1]
c1.new.should be_kind_of(m1)
c2.ancestors[0,3].should == [m1,m2dup,c2]
c2.new.should be_kind_of(m1)
end
end

it "depends on prepend_features to add the module" do
Expand Down
2 changes: 1 addition & 1 deletion spec/ruby/core/module/refine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ def to_proc(*args)
context "when super is called in a refinement" do
it "looks in the included to refinery module" do
refined_class = ModuleSpecs.build_refined_class

refinement = Module.new do
refine refined_class do
include ModuleSpecs::IncludedModule
Expand Down
8 changes: 7 additions & 1 deletion spec/ruby/core/proc/eql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@
require_relative 'shared/equal'

describe "Proc#eql?" do
it_behaves_like :proc_equal_undefined, :eql?
ruby_version_is "0"..."2.8" do
it_behaves_like :proc_equal_undefined, :eql?
end

ruby_version_is "2.8" do
it_behaves_like :proc_equal, :eql?
end
end
8 changes: 7 additions & 1 deletion spec/ruby/core/proc/equal_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@
require_relative 'shared/equal'

describe "Proc#==" do
it_behaves_like :proc_equal_undefined, :==
ruby_version_is "0"..."2.8" do
it_behaves_like :proc_equal_undefined, :==
end

ruby_version_is "2.8" do
it_behaves_like :proc_equal, :==
end
end
2 changes: 1 addition & 1 deletion spec/ruby/core/proc/fixtures/source_location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def self.my_detached_proc

def self.my_detached_lambda
body = -> { true }
lambda(&body)
suppress_warning {lambda(&body)}
end

def self.my_detached_proc_new
Expand Down
4 changes: 2 additions & 2 deletions spec/ruby/core/proc/lambda_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
end

it "is preserved when passing a Proc with & to the lambda keyword" do
lambda(&->{}).lambda?.should be_true
lambda(&proc{}).lambda?.should be_false
suppress_warning {lambda(&->{})}.lambda?.should be_true
suppress_warning {lambda(&proc{})}.lambda?.should be_false
end

it "is preserved when passing a Proc with & to the proc keyword" do
Expand Down
14 changes: 13 additions & 1 deletion spec/ruby/core/proc/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def some_method
end
end

ruby_version_is "2.7" do
ruby_version_is "2.7"..."2.8" do
it "can be created if invoked from within a method with a block" do
-> { ProcSpecs.new_proc_in_method { "hello" } }.should complain(/Capturing the given block using Proc.new is deprecated/)
end
Expand All @@ -223,4 +223,16 @@ def some_method
}.should complain(/Capturing the given block using Proc.new is deprecated/)
end
end

ruby_version_is "2.8" do
it "raises an ArgumentError when passed no block" do
def some_method
Proc.new
end

-> { ProcSpecs.new_proc_in_method { "hello" } }.should raise_error(ArgumentError, 'tried to create Proc object without a block')
-> { ProcSpecs.new_proc_subclass_in_method { "hello" } }.should raise_error(ArgumentError, 'tried to create Proc object without a block')
-> { some_method { "hello" } }.should raise_error(ArgumentError, 'tried to create Proc object without a block')
end
end
end
4 changes: 2 additions & 2 deletions spec/ruby/core/proc/parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
end

it "sets the first element of each sub-Array to :block for parameters prefixed with ampersands" do
->&x { }.parameters.first.first.should == :block
-> &x { }.parameters.first.first.should == :block
-> x, &y { }.parameters.last.first.should == :block
proc {|&x| }.parameters.first.first.should == :block
proc {|x,&y| }.parameters.last.first.should == :block
Expand All @@ -68,7 +68,7 @@
-> x=Math::PI { }.parameters.first.last.should == :x
-> an_argument, glark, &foo { }.parameters[1].last.should == :glark
-> *rest { }.parameters.first.last.should == :rest
->&block { }.parameters.first.last.should == :block
-> &block { }.parameters.first.last.should == :block
proc {|x| }.parameters.first.last.should == :x
proc {|x=Math::PI| }.parameters.first.last.should == :x
proc {|an_argument, glark, &foo| }.parameters[1].last.should == :glark
Expand Down
2 changes: 1 addition & 1 deletion spec/ruby/core/proc/shared/call_arguments.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe :proc_call_block_args, shared: true do
it "can receive block arguments" do
Proc.new {|&b| b.send(@method)}.send(@method) {1 + 1}.should == 2
->&b { b.send(@method)}.send(@method) {1 + 1}.should == 2
-> &b { b.send(@method)}.send(@method) {1 + 1}.should == 2
proc {|&b| b.send(@method)}.send(@method) {1 + 1}.should == 2
end

Expand Down
14 changes: 7 additions & 7 deletions spec/ruby/core/proc/shared/equal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,26 @@
a.send(@method, b).should be_false
end

it "returns true if both procs have the same body and environment" do
it "returns false if procs are distinct but have the same body and environment" do
p = proc { :foo }
p2 = proc { :foo }
p.send(@method, p2).should be_true
p.send(@method, p2).should be_false
end

it "returns true if both lambdas with the same body and environment" do
it "returns false if lambdas are distinct but have same body and environment" do
x = -> { :foo }
x2 = -> { :foo }
x.send(@method, x2).should be_true
x.send(@method, x2).should be_false
end

it "returns true if both different kinds of procs with the same body and env" do
it "returns false if using comparing lambda to proc, even with the same body and env" do
p = -> { :foo }
p2 = proc { :foo }
p.send(@method, p2).should be_true
p.send(@method, p2).should be_false

x = proc { :bar }
x2 = -> { :bar }
x.send(@method, x2).should be_true
x.send(@method, x2).should be_false
end

it "returns false if other is not a Proc" do
Expand Down
Loading

0 comments on commit 63ac861

Please sign in to comment.