Sha256: 2075237f66a221b72b18747721e1982f806c932c718c657a27e963cd53b4041a

Contents?: true

Size: 1.61 KB

Versions: 20

Compression:

Stored size: 1.61 KB

Contents

require File.join(File.dirname(__FILE__), %w[.. .. .. spec_helper])

describe SC::HashStruct, 'hash operations' do
  
  include SC::SpecHelpers

  it "should allow arbitrary keys to be set/read using hash methods" do
    e = SC::HashStruct.new
    e[:foo] = :bar
    e[:foo].should eql(:bar)
  end
  
  it "should set any hash options passed to new" do
    e = SC::HashStruct.new :foo => :bar
    e[:foo].should eql(:bar)
  end
  
  it "should read any missing methods from the hash" do
    e = SC::HashStruct.new :foo => :bar
    e.foo.should eql(:bar)
  end
  
  it "should write missing methods ending in = to the hash" do
    e = SC::HashStruct.new 
    e.foo = :bar
    e[:foo].should eql(:bar)
  end
  
  it "should treat string and hash keys as the same" do
    e = SC::HashStruct.new
    e['foo'] = :bar
    e[:foo].should eql(:bar)
    
    e[:foo2] = :bar
    e['foo2'].should eql(:bar)
  end
  
  it "should convert all keys to symbols (i.e. if you get keys, they will always be symbols)" do
    e = SC::HashStruct.new
    e['string'] = :foo
    e[:symbol] = :foo
        
    expected = [:string, :symbol]
    idx=0
    e.keys.sort { |a,b| a.to_s <=> b.to_s }.each do |k|
      k.should eql(expected[idx])
      idx += 1
    end
  end
  
  it "should raise error if key cannot be converted to symbol" do
    a = SC::HashStruct.new
    
    # numbers respond to to_sym but return nil
    lambda { a[1] = :foo }.should raise_error
    lambda { a[1] }.should raise_error
    
    # Object does not respond to to_sym
    lambda { a[Object.new] = :foo }.should raise_error
    lambda { a[Object.new] }.should raise_error
  end
    
  
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
sproutit-sproutcore-1.0.0.20090407205609 spec/lib/models/hash_struct/hash_spec.rb
sproutit-sproutcore-1.0.0.20090408130025 spec/lib/models/hash_struct/hash_spec.rb
sproutit-sproutcore-1.0.0.20090416161445 spec/lib/models/hash_struct/hash_spec.rb
sproutit-sproutcore-1.0.20090721145236 spec/lib/models/hash_struct/hash_spec.rb
sproutcore-1.0.1046 spec/lib/models/hash_struct/hash_spec.rb
sproutcore-1.0.1043 spec/lib/models/hash_struct/hash_spec.rb
sproutcore-1.0.1042 spec/lib/models/hash_struct/hash_spec.rb
sproutcore-1.0.1037 spec/lib/models/hash_struct/hash_spec.rb
sproutcore-1.0.1035 spec/lib/models/hash_struct/hash_spec.rb
sproutcore-1.0.1031 spec/lib/models/hash_struct/hash_spec.rb
sproutcore-1.0.1030 spec/lib/models/hash_struct/hash_spec.rb
sproutcore-1.0.1029 spec/lib/models/hash_struct/hash_spec.rb
sproutcore-1.0.1027 spec/lib/models/hash_struct/hash_spec.rb
sproutcore-1.0.1028 spec/lib/models/hash_struct/hash_spec.rb
sproutcore-1.0.1026 spec/lib/models/hash_struct/hash_spec.rb
sproutcore-1.0.1025 spec/lib/models/hash_struct/hash_spec.rb
sproutcore-1.0.1024 spec/lib/models/hash_struct/hash_spec.rb
sproutcore-1.0.1009 spec/lib/models/hash_struct/hash_spec.rb
sproutcore-1.0.1008 spec/lib/models/hash_struct/hash_spec.rb
sproutcore-1.0.1003 spec/lib/models/hash_struct/hash_spec.rb