/
hash_spec.rb
89 lines (77 loc) · 2.23 KB
/
hash_spec.rb
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
require File.dirname(__FILE__) + "/../spec_helper"
describe "The Hash class" do
it "is a direct subclass of NSMutableDictionary" do
Hash.class.should == Class
Hash.superclass.should == NSMutableDictionary
end
it "can be subclassed and later instantiated" do
k = Class.new(Hash)
a = k.new
a.class.should == k
a[42] = 123
a[42].should == 123
end
end
describe "An Hash object" do
it "is an instance of the Hash class" do
{}.class.should == Hash
{}.kind_of?(Hash).should == true
{}.instance_of?(Hash).should == true
end
it "is mutable" do
a = {}
a[42] = 123
a[42].should == 123
end
it "can have a singleton class" do
a = {}
def a.foo; 42; end
a.foo.should == 42
a[42] = 123
a[42].should == 123
end
it "can have a singleton class with an attr_accessor" do
a = {}
class << a
attr_accessor :foo
end
a.foo = 42
a.foo.should == 42
end
it "properly hashes pure Cocoa objects" do
a = NSCalendarDate.dateWithYear(2010, month:5, day:2, hour:0, minute:0, second:0, timeZone:nil)
b = NSCalendarDate.dateWithYear(2010, month:5, day:2, hour:0, minute:0, second:0, timeZone:nil)
h = {}
h[a] = :bad
h[b] = :ok
h.size.should == 1
h[a].should == :ok
end
end
describe "An NSDictionary object" do
before(:all) do
require 'yaml'
end
it "is an instance of the Hash class" do
a = NSDictionary.dictionary
a.is_a?(Hash).should == true
a.class.should == Hash
a = NSDictionary.dictionaryWithObject(42, forKey:42)
a.is_a?(Hash).should == true
a.class.should == Hash
end
it "is immutable" do
a = NSDictionary.dictionary
a.size.should == 0
lambda { a[42] = 123 }.should raise_error(RuntimeError)
end
it "can be transformed to yaml using #to_yaml" do
NSDictionary.dictionaryWithDictionary({:a => "ok", :c => 42}).to_yaml.should == "--- \n:a: ok\n:c: 42\n"
end
it "can include Foundation objects and be correctly transformed to yaml" do
a = NSString.stringWithString("a")
ok = NSString.stringWithString("ok")
ary = NSArray.arrayWithArray([42, 21])
NSDictionary.dictionaryWithDictionary({a => ok, :c => ary}).to_yaml.should == "--- \na: ok\n:c:\n- 42\n- 21\n"
end
end