Sha256: 05cf003a1e166453f51c9911a95bbc72a0f9c2bb20816029c1729c70a44aeea1

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

require "#{File.expand_path(File.dirname(__FILE__))}/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_ext-0.0.11 spec/support/safe_hash_spec.rb