Sha256: 572c4faae6bcd9a424b18911bfe54cb360d239593e052a93be802ee500186147

Contents?: true

Size: 1.83 KB

Versions: 12

Compression:

Stored size: 1.83 KB

Contents

require 'spec_helper'
module WLang
  describe Scope do

    let(:scope){ Scope.coerce({:who => "World"}) }

    it 'acts like a stack' do
      s = scope
      s.evaluate(:who, nil).should eq("World")
      s = scope.push(:who => "World2")
      s.evaluate(:who, nil).should eq("World2")
      s = s.pop
      s.evaluate(:who, nil).should eq("World")
    end

    it 'has a `with` helper' do
      scope.with(:who => "World2") do |s|
        s.evaluate(:who, nil).should eq("World2")
      end
      scope.evaluate(:who, nil).should eq("World")
    end

    it 'allows pushing scopes' do
      scope.with Scope.coerce(:other => "World2") do |s|
        s.evaluate(:other, nil).should eq("World2")
        s.evaluate(:who, nil).should eq("World")
      end
    end

    it 'gives access to the root' do
      scope.root.should eq(scope)
      scope.with(:other => "World2") do |s|
        s.root.should eq(scope)
      end
    end

    it 'evaluates `self` accurately' do
      scope.evaluate(:self, nil).should eq(:who => "World")
    end

    it 'evaluates dot expressions correctly' do
      scope.evaluate("who.upcase", nil).should eq("WORLD")
    end

    it 'strips strings before evaluation' do
      scope.evaluate("  who  ", nil).should eq("World")
      scope.evaluate(" who.upcase ", nil).should eq("WORLD")
    end

    it 'fails when not found' do
      lambda{
        scope.evaluate(:nosuchone, nil)
      }.should throw_symbol(:fail)
    end

    it 'supports a default value instead of fail' do
      scope.evaluate(:nosuchone, nil, 12).should eq(12)
      scope.evaluate(:nosuchone, nil, nil).should be_nil
    end

    it 'supports a default value through a block instead of fail' do
      scope.evaluate(:nosuchone, nil){ 12 }.should eq(12)
      scope.evaluate(:nosuchone, nil){ nil }.should be_nil
    end

  end # describe Scope
end # module WLang

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
wlang-3.0.1 spec/unit/test_scope.rb
wlang-3.0.0 spec/unit/test_scope.rb
wlang-2.3.1 spec/unit/test_scope.rb
wlang-2.3.0 spec/unit/test_scope.rb
wlang-2.2.4 spec/unit/test_scope.rb
wlang-2.2.3 spec/unit/test_scope.rb
wlang-2.2.2 spec/unit/test_scope.rb
wlang-2.2.1 spec/unit/test_scope.rb
wlang-2.2.0 spec/unit/test_scope.rb
wlang-2.1.2 spec/unit/test_scope.rb
wlang-2.1.1 spec/unit/test_scope.rb
wlang-2.1.0 spec/unit/test_scope.rb