Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed BEGIN specs.

  • Loading branch information...
commit 417cf12353e0941ec53b46dd29d90577950250c4 1 parent 95346e3
@brixen brixen authored
Showing with 21 additions and 29 deletions.
  1. +21 −29 language/BEGIN_spec.rb
View
50 language/BEGIN_spec.rb
@@ -1,57 +1,49 @@
require File.expand_path('../../spec_helper', __FILE__)
describe "The BEGIN keyword" do
- ruby_version_is "" .. "1.9" do
+ before :each do
+ ScratchPad.record []
+ end
+
+ ruby_version_is "".."1.9" do
it "runs in a new isolated scope" do
- lambda {
+ lambda do
eval "BEGIN { var_in_begin = 'foo' }; var_in_begin"
- }.should raise_error NameError
+ end.should raise_error NameError
+ end
- lambda {
+ it "does not access variables outside the eval scope" do
+ lambda do
outside_var = 'foo'
eval "BEGIN { outside_var }"
- }.should raise_error NameError
+ end.should raise_error NameError
end
end
ruby_version_is "1.9" do
it "runs in a shared scope" do
- lambda {
- eval "BEGIN { var_in_begin = 'foo' }; var_in_begin"
- }.should_not raise_error NameError
+ eval("BEGIN { var_in_begin = 'foo' }; var_in_begin").should == "foo"
+ end
- lambda {
- outside_var = 'foo'
- eval "BEGIN { outside_var }"
- }.should_not raise_error NameError
+ it "accesses variables outside the eval scope" do
+ outside_var = 'foo'
+ eval("BEGIN { var_in_begin = outside_var }; var_in_begin").should == "foo"
end
it "must appear in a top-level context" do
- lambda do
- eval "1.times{ BEGIN{ :begun }}"
- end.should raise_error(SyntaxError)
+ lambda { eval "1.times { BEGIN { 1 } }" }.should raise_error(SyntaxError)
end
end
it "runs first in a given code unit" do
- o = Object.new
- class << o
- ARY = []
- eval "ARY << 'foo'; BEGIN { ARY << 'bar' }"
- def ary; ARY; end
- end
+ eval "ScratchPad << 'foo'; BEGIN { ScratchPad << 'bar' }"
- o.ary.should == ['bar', 'foo']
+ ScratchPad.recorded.should == ['bar', 'foo']
end
it "runs multiple begins in FIFO order" do
- o = Object.new
- class << o
- ARY = []
- eval "BEGIN { ARY << 'foo' }; BEGIN { ARY << 'bar' }"
- def ary; ARY; end
- end
+ eval "BEGIN { ScratchPad << 'foo' }; BEGIN { ScratchPad << 'bar' }"
- o.ary.should == ['foo', 'bar']
+ ScratchPad.recorded.should == ['foo', 'bar']
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.