Sha256: 84a8403e9d3987ea90c7255d23280c3f5fdaf1100e1f02e7afbc7c29477649cb

Contents?: true

Size: 1.95 KB

Versions: 1

Compression:

Stored size: 1.95 KB

Contents

require 'support/spec_helper'

require "crystal/support/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 return boolean (from error)" do
    h = SafeHash.new
    lambda{raise "" unless h.a?.class.equal?(NilClass)}.should raise_error
    lambda{raise "" unless h.a.a?.class.equal?(NilClass)}.should raise_error
  end
  
  it "should treat assigned nil as value (from error)" do
    h = SafeHash.new
    h.v = nil
    h.v?.should be_true
    h.v!.should == nil
  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

1 entries across 1 versions & 1 rubygems

Version Path
crystal-0.0.13 spec/support/safe_hash_spec.rb