Sha256: c3da6801720a33e38517489dabce2faf9eed504c2b634f6dc85d985b1908709a

Contents?: true

Size: 1.29 KB

Versions: 52

Compression:

Stored size: 1.29 KB

Contents

require File.expand_path('../../spec_helper', __FILE__)

describe "The BEGIN keyword" do
  ruby_version_is "" .. "1.9" do
    it "runs in a new isolated scope" do
      lambda {
        eval "BEGIN { var_in_begin = 'foo' }; var_in_begin"
      }.should raise_error NameError

      lambda {
        outside_var = 'foo'
        eval "BEGIN { outside_var }"
      }.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

      lambda {
        outside_var = 'foo'
        eval "BEGIN { outside_var }"
      }.should_not raise_error NameError
    end

    it "must appear in a top-level context" do
      lambda do
        eval "1.times{ BEGIN{ :begun }}"
      end.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

    o.ary.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

    o.ary.should == ['foo', 'bar']
  end
end

Version data entries

52 entries across 52 versions & 2 rubygems

Version Path
rhodes-7.6.0 spec/framework_spec/app/spec/language/begin_spec.rb
rhodes-7.5.1 spec/framework_spec/app/spec/language/begin_spec.rb
rhodes-7.4.1 spec/framework_spec/app/spec/language/begin_spec.rb
rhodes-7.1.17 spec/framework_spec/app/spec/language/begin_spec.rb
rhodes-6.2.0 spec/framework_spec/app/spec/language/begin_spec.rb
rhodes-6.0.11 spec/framework_spec/app/spec/language/begin_spec.rb
rhodes-5.5.18 spec/framework_spec/app/spec/language/begin_spec.rb
rhodes-5.5.17 spec/framework_spec/app/spec/language/begin_spec.rb
rhodes-5.5.15 spec/framework_spec/app/spec/language/begin_spec.rb
rhodes-5.5.0.22 spec/framework_spec/app/spec/language/begin_spec.rb
rhodes-5.5.2 spec/framework_spec/app/spec/language/begin_spec.rb
rhodes-5.5.0.7 spec/framework_spec/app/spec/language/begin_spec.rb
rhodes-5.5.0.3 spec/framework_spec/app/spec/language/begin_spec.rb
rhodes-5.5.0 spec/framework_spec/app/spec/language/begin_spec.rb
tauplatform-1.0.3 spec/framework_spec/app/spec/language/begin_spec.rb
tauplatform-1.0.2 spec/framework_spec/app/spec/language/begin_spec.rb
tauplatform-1.0.1 spec/framework_spec/app/spec/language/begin_spec.rb
rhodes-3.5.1.12 spec/framework_spec/app/spec/language/BEGIN_spec.rb
rhodes-3.3.5 spec/framework_spec/app/spec/language/BEGIN_spec.rb
rhodes-3.4.2 spec/framework_spec/app/spec/language/BEGIN_spec.rb