Sha256: d269eff719f4beeb7204a7faf1702f16429e8ab317aaa24f2d28218dd5dfecff

Contents?: true

Size: 1.63 KB

Versions: 4

Compression:

Stored size: 1.63 KB

Contents

require 'spec'

require 'active_support'
require "#{File.dirname __FILE__}/../../lib/rails_ext/config_files/safe_hash"

describe "SafeHash and SafeNil" do
  it "should allow check for value presence" do
    h = SafeHash.new :a => :b
    h.a?.should be_true
    h.b?.should be_false
    
    h.should include(:a)
    h.should_not include(:b)
  end
  
  it "should allow owerride values" do
    h = SafeHash.new :a => :b
    h[:b] = :c
    h.b!.should == :c
  end
  
  it "general behaviour" do
    h = SafeHash.new :key => :value
    
    h.key.should == :value
    h.key(:missing).should == :value
    
    h[:key].should == :value
    h[:key, :missing].should == :value
    
    h['key'].should == :value
    h['key', :missing].should == :value
        
    h.a.b.c[:d].e('missing').should == 'missing'
    h.a.b.c[:d][:e, 'missing'].should == 'missing'
  end
  
  it "should build hierarchies of SafeHash" do
    h = SafeHash.new :a => {:a => :b}
    
    h.a.a.should == :b
    h.a.missing.b.c('missing').should == 'missing'
  end
  
  it "should require setting if ! used" do
    h = SafeHash.new :a => :v, :b => {:c => :v}
    
    h.a!.should == :v
    h.b.c!.should == :v
    h.b!.c!.should == :v
    
    lambda{h.j!}.should raise_error(/No key j/) 
    lambda{h.j.b.c!}.should raise_error(/No key c/)
  end
  
  it "should be able to update itself" do
    h = SafeHash.new 
    h.b?.should be_false
    h[:a] = :a
    h.a!.should == :a
  end
  
  it "should implement include?" do
    h = SafeHash.new :a => :b
    h.include?(:a).should be_true
    h.include?('a').should be_true
    h.include?(:b).should be_false

    h.b.include?(:a).should be_false
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rails-ext-0.2.21 spec/rails_ext/safe_hash_spec.rb
rails-ext-0.2.20 spec/rails_ext/safe_hash_spec.rb
rails-ext-0.2.19 spec/rails_ext/safe_hash_spec.rb
rails-ext-0.2.18 spec/rails_ext/safe_hash_spec.rb