summaryrefslogtreecommitdiff
path: root/spec/hashie/dash_spec.rb
blob: 97ed3c1d93610abe5fd6b49f06392d18f992f1c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require File.dirname(__FILE__) + '/../spec_helper'

class DashTest < Hashie::Dash
  property :first_name
  property :email
  property :count, :default => 0
end

describe Hashie::Dash do
  it 'should be a subclass of Hashie::Hash' do
    (Hashie::Dash < Hash).should be_true
  end
  
  describe ' creating properties' do
    it 'should add the property to the list' do
      DashTest.property :not_an_att
      DashTest.properties.include?('not_an_att').should be_true
    end
    
    it 'should create a method for reading the property' do
      DashTest.new.respond_to?(:first_name).should be_true
    end
    
    it 'should create a method for writing the property' do
      DashTest.new.respond_to?(:first_name=).should be_true
    end
  end
  
  describe ' writing to properties' do
    before do
      @dash = DashTest.new
    end
    
    it 'should not be able to write to a non-existent property using []=' do
      lambda{@dash['abc'] = 123}.should raise_error(NoMethodError)
    end
    
    it 'should be able to write to an existing property using []=' do
      lambda{@dash['first_name'] = 'Bob'}.should_not raise_error
    end
    
    it 'should be able to read/write to an existing property using a method call' do
      @dash.first_name = 'Franklin'
      @dash.first_name.should == 'Franklin'
    end
  end
  
  describe ' initializing with a Hash' do
    it 'should not be able to initialize non-existent properties' do
      lambda{DashTest.new(:bork => 'abc')}.should raise_error(NoMethodError)
    end
    
    it 'should set properties that it is able to' do
      DashTest.new(:first_name => 'Michael').first_name.should == 'Michael'
    end
  end
  
  describe ' defaults' do
    before do
      @dash = DashTest.new
    end
    
    it 'should return the default value for defaulted' do
      DashTest.property :defaulted, :default => 'abc'
      DashTest.new.defaulted.should == 'abc'
    end
  end
end