Sha256: d466a29660fcca4b26e1f682cc935e24ab1b892a45eb9aa5c3b369b1db713633

Contents?: true

Size: 1.98 KB

Versions: 19

Compression:

Stored size: 1.98 KB

Contents

require 'spec_helper'

describe "The begin keyword" do
  it "should be removed when used without a resuce or enusre body" do
    opal_parse('begin; 1; end').should == [:lit, 1]
    opal_parse('begin; 1; 2; end').should == [:block, [:lit, 1], [:lit, 2]]
  end

  describe "with 'rescue' bodies" do
    it "should create a s(:rescue) sexp" do
      opal_parse('begin; 1; rescue; 2; end').should == [:rescue, [:lit, 1], [:resbody, [:array], [:lit, 2]]]
    end

    it "allows multiple rescue bodies" do
      opal_parse('begin 1; rescue; 2; rescue; 3; end').should == [:rescue, [:lit, 1], [:resbody, [:array], [:lit, 2]], [:resbody, [:array], [:lit, 3]]]
    end

    it "accepts a list of classes used to match against the exception" do
      opal_parse('begin 1; rescue Klass; 2; end').should == [:rescue, [:lit, 1], [:resbody, [:array, [:const, :Klass]], [:lit, 2]]]
    end

    it "accepts an identifier to assign exception to" do
      opal_parse('begin 1; rescue => a; 2; end').should == [:rescue, [:lit, 1], [:resbody, [:array, [:lasgn, :a, [:gvar, :$!]]], [:lit, 2]]]
      opal_parse('begin 1; rescue Klass => a; 2; end').should == [:rescue, [:lit, 1], [:resbody, [:array, [:const, :Klass],[:lasgn, :a, [:gvar, :$!]]], [:lit, 2]]]
    end

    it "accepts an ivar to assign exception to" do
      opal_parse('begin 1; rescue => @a; 2; end').should == [:rescue, [:lit, 1], [:resbody, [:array, [:iasgn, :@a, [:gvar, :$!]]], [:lit, 2]]]
      opal_parse('begin 1; rescue Klass => @a; 2; end').should == [:rescue, [:lit, 1], [:resbody, [:array, [:const, :Klass],[:iasgn, :@a, [:gvar, :$!]]], [:lit, 2]]]
    end

    it "should parse newline right after rescue" do
      opal_parse("begin; 1; rescue\n 2; end").should == [:rescue, [:lit, 1], [:resbody, [:array], [:lit, 2]]]
    end
  end

  describe "with an 'ensure' block" do
    it "should propagate into single s(:ensure) statement when no rescue block given" do
      opal_parse('begin; 1; ensure; 2; end').should == [:ensure, [:lit, 1], [:lit, 2]]
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
opal-0.4.3 spec/parser/begin_spec.rb
opal-0.4.2 spec/parser/begin_spec.rb
opal-0.4.1 spec/parser/begin_spec.rb
opal-0.4.0 spec/parser/begin_spec.rb
opal-0.3.44 spec/parser/begin_spec.rb
opal-0.3.43 spec/parser/begin_spec.rb
opal-0.3.42 spec/grammar/begin_spec.rb
opal-0.3.41 spec/grammar/begin_spec.rb
opal-0.3.40 spec/grammar/begin_spec.rb
opal-0.3.39 spec/grammar/begin_spec.rb
opal-0.3.38 spec/grammar/begin_spec.rb
opal-0.3.37 spec/grammar/begin_spec.rb
opal-0.3.36 spec/grammar/begin_spec.rb
opal-0.3.35 spec/grammar/begin_spec.rb
opal-0.3.34 spec/grammar/begin_spec.rb
opal-0.3.33 spec/grammar/begin_spec.rb
opal-0.3.32 spec/grammar/begin_spec.rb
opal-0.3.31 spec/grammar/begin_spec.rb
opal-0.3.30 spec/grammar/begin_spec.rb