Sha256: 63fb00e9f63f57860961f4a92147e1eb9c4e4f9e26dd32d456a913cfd38ffcfe

Contents?: true

Size: 1.73 KB

Versions: 23

Compression:

Stored size: 1.73 KB

Contents

module BreakSpecs
  class Driver
    def initialize(ensures=false)
      @ensures = ensures
    end

    def note(value)
      ScratchPad << value
    end
  end

  class Block < Driver
    def break_nil
      note :a
      note yielding {
        note :b
        break
        note :c
      }
      note :d
    end

    def break_value
      note :a
      note yielding {
        note :b
        break :break
        note :c
      }
      note :d
    end

    def yielding
      note :aa
      note yield
      note :bb
    end
  end
end

describe "The break statement in a block" do
  before :each do
    ScratchPad.record []
    @program = BreakSpecs::Block.new
  end

  it "returns nil to method invoking the method yielding to the block when not passed an argument" do
    @program.break_nil
    ScratchPad.recorded.should == [:a, :aa, :b, nil, :d]
  end

  it "returns a value to the method invoking the method yielding to the block" do
    @program.break_value
    ScratchPad.recorded.should == [:a, :aa, :b, :break, :d]
  end
end

describe "Break inside a while loop" do
  describe "with a value" do
    it "exists the loop and returns the value" do
      a = while true; break; end;           a.should == nil
      a = while true; break nil; end;       a.should == nil
      a = while true; break 1; end;         a.should == 1
      a = while true; break []; end;        a.should == []
      a = while true; break [1]; end;       a.should == [1]
    end
  end

  it "stops a while loop when run" do
    i = 0
    while true
      break if i == 2
      i+=1
    end
    i.should == 2
  end

  it "causes a call with a block to return when run" do
    at = 0
    0.upto(5) do |i|
      at = i
      break i if i == 2
    end.should == 2
    at.should == 2
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
opal-0.3.41 spec/language/break_spec.rb
opal-0.3.40 spec/language/break_spec.rb
opal-0.3.39 spec/language/break_spec.rb
opal-0.3.38 spec/language/break_spec.rb
opal-0.3.37 spec/language/break_spec.rb
opal-0.3.36 spec/language/break_spec.rb
opal-0.3.35 spec/language/break_spec.rb
opal-0.3.34 spec/language/break_spec.rb
opal-0.3.33 spec/language/break_spec.rb
opal-0.3.32 spec/language/break_spec.rb
opal-0.3.31 spec/language/break_spec.rb
opal-0.3.30 spec/language/break_spec.rb
opal-0.3.29 spec/language/break_spec.rb
opal-0.3.28 spec/language/break_spec.rb
opal-0.3.27 spec/language/break_spec.rb
opal-0.3.26 spec/language/break_spec.rb
opal-0.3.25 spec/language/break_spec.rb
opal-0.3.22 spec/language/break_spec.rb
opal-0.3.21 test/language/break_spec.rb
opal-0.3.20 test/language/break_spec.rb