Tests/Specs: Use Chai for assertions, instead of Jasmine's assertions.
[mootools.git] / Specs / Types / Hash.js
blob6d8512833109cb4e0c8dc57f43931f34b3fb9d0b
1 /*
2 ---
3 name: Hash
4 requires: ~
5 provides: ~
6 ...
7 */
10 //<1.2compat>
12 (function(){
14 var hash2 = new Hash({ a: 'string', b: 233, c: {} });
16 describe("Hash Methods", function(){
18         // Hash.constructor
20         it('should return a new hash', function(){
21                 expect(Hash.type(new Hash())).to.equal(true);
22         });
24         it('should return a copy of a hash', function(){
25                 var hash = new Hash({a: 1, b: 2, c: 3});
26                 var copy = new Hash(hash);
27                 expect(copy).to.not.equal(hash);
28                 expect(copy).to.deep.equal(hash);
29         });
31         // Hash.erase
33         it('should remove a key and its value from the hash', function(){
34                 var hash = new Hash({a: 1, b: 2, c: 3});
35                 expect(hash.erase('a')).to.deep.equal(new Hash({b:2,c:3}));
36                 expect(hash.erase('d')).to.deep.equal(new Hash({b:2,c:3}));
38                 hash = new Hash({a: 1, b: 2, c: 3});
39                 expect(hash.erase('a')).to.deep.equal(new Hash({b:2,c:3}));
40                 expect(hash.erase('d')).to.deep.equal(new Hash({b:2,c:3}));
41         });
43         // Hash.get
45         it('should return the value corresponding to the specified key otherwise null', function(){
46                 var hash = new Hash({a: 1, b: 2, c: 3});
47                 expect(hash.get('c')).to.equal(3);
48                 expect(hash.get('d')).to.equal(null);
49         });
51         // Hash.set
53         it('should set the key with the corresponding value', function(){
54                 var myHash = new Hash({a: 1, b: 2, c: 3}).set('c', 7).set('d', 8);
55                 expect(myHash).to.deep.equal(new Hash({a:1,b:2,c:7,d:8}));
56         });
58         // Hash.empty
60         it('should empty the hash', function(){
61                 var hash = new Hash({a: 1, b: 2, c: 3});
62                 expect(hash.empty()).to.deep.equal(new Hash());
63         });
65         // Hash.include
67         it('should include a key value if the hash does not have the key otherwise ignore', function(){
68                 var hash = new Hash({a: 1, b: 2, c: 3});
69                 expect(hash.include('e', 7)).to.deep.equal(new Hash({a:1,b:2,c:3,e:7}));
70                 expect(hash.include('a', 7)).to.deep.equal(new Hash({a:1,b:2,c:3,e:7}));
71         });
73         // Hash.keyOf | Hash.indexOf
75         it('should return the key of the value or null if not found', function(){
76                 var hash = new Hash({a: 1, b: 2, c: 3, d: 1});
77                 expect(hash.keyOf(1)).to.equal('a');
78                 expect(hash.keyOf('not found')).to.equal(null);
80                 expect(hash.indexOf(1)).to.equal('a');
81                 expect(hash.indexOf('not found')).to.equal(null);
82         });
84         // Hash.has
86         it('should return true if the hash has the key otherwise false', function(){
87                 var hash = new Hash({a: 1, b: 2, c: 3});
88                 expect(hash.has('a')).to.equal(true);
89                 expect(hash.has('d')).to.equal(false);
90         });
92         // Hash.hasValue | Hash.contains
94         it('should return true if the hash hasValue otherwise false', function(){
95                 var hash = new Hash({a: 1, b: 2, c: 3});
96                 expect(hash.hasValue(1)).to.equal(true);
97                 expect(hash.hasValue('not found')).to.equal(false);
99                 expect(hash.contains(1)).to.equal(true);
100                 expect(hash.contains('not found')).to.equal(false);
101         });
103         // Hash.getClean
105         it('should getClean JavaScript object', function(){
106                 var hash = new Hash({a: 1, b: 2, c: 3});
107                 expect(hash.getClean()).to.deep.equal({a:1,b:2,c:3});
108         });
110         // Hash.extend
112         it('should extend a Hash with an object', function(){
113                 var hash = new Hash({a: 1, b: 2, c: 3});
114                 expect(hash.extend({a:4,d:7,e:8})).to.deep.equal(new Hash({a:4,b:2,c:3,d:7,e:8}));
115         });
117         it('should extend a Hash with another Hash', function(){
118                 var hash = new Hash({a: 1, b: 2, c: 3});
119                 expect(hash.extend(new Hash({a:4,d:7,e:8}))).to.deep.equal(new Hash({a:4,b:2,c:3,d:7,e:8}));
120         });
122         // Hash.combine
124         it('should merge a Hash with an object', function(){
125                 var hash = new Hash({a: 1, b: 2, c: 3});
126                 expect(hash.combine({a:4,d:7,e:8})).to.deep.equal(new Hash({a:1,b:2,c:3,d:7,e:8}));
127         });
129         it('should merge a Hash with another Hash', function(){
130                 var hash = new Hash({a: 1, b: 2, c: 3});
131                 expect(hash.combine(new Hash({a:4,d:7,e:8}))).to.deep.equal(new Hash({a:1,b:2,c:3,d:7,e:8}));
132         });
134         // Hash.each
136         it('should iterate through each property', function(){
137                 var newHash = new Hash();
138                 var hash = new Hash({a: 1, b: 2, c: 3});
139                 hash.each(function(value, key){
140                         newHash.set(key, value);
141                 });
142                 expect(newHash).to.deep.equal(hash);
143         });
145         // Hash.map
147         it('should map a new Hash according to the comparator', function(){
148                 expect(hash2.map(Number.type)).to.deep.equal(new Hash({a:false,b:true,c:false}));
149         });
151         // Hash.filter
153         it('should filter the Hash according to the comparator', function(){
154                 expect(hash2.filter(Number.type)).to.deep.equal(new Hash({b:233}));
155         });
157         // Hash.every
159         it('should return true if every value matches the comparator, otherwise false', function(){
160                 expect(hash2.every($defined)).to.equal(true);
161                 expect(hash2.every(Number.type)).to.equal(false);
162         });
164         // Hash.some
166         it('should return true if some of the values match the comparator, otherwise false', function(){
167                 expect(hash2.some(Number.type)).to.equal(true);
168                 expect(hash2.some(Array.type)).to.equal(false);
169         });
171         // Hash.getKeys
173         it('getKeys should return an empty array', function(){
174                 expect(new Hash().getKeys()).to.deep.equal([]);
175         });
177         it('should return an array containing the keys of the hash', function(){
178                 expect(hash2.getKeys()).to.deep.equal(['a', 'b', 'c']);
179         });
181         // Hash.getValues
183         it('getValues should return an empty array', function(){
184                 expect(new Hash().getValues()).to.deep.equal([]);
185         });
187         it('should return an array with the values of the hash', function(){
188                 expect(hash2.getValues()).to.deep.equal(['string', 233, {}]);
189         });
191         // Hash.toQueryString
193         it('should return a query string', function(){
194                 var myHash = new Hash({apple: "red", lemon: "yellow"});
195                 expect(myHash.toQueryString()).to.equal('apple=red&lemon=yellow');
197                 var myHash2 = new Hash({apple: ['red', 'yellow'], lemon: ['green', 'yellow']});
198                 expect(myHash2.toQueryString()).to.equal('apple[0]=red&apple[1]=yellow&lemon[0]=green&lemon[1]=yellow');
200                 var myHash3 = new Hash({fruits: {apple: ['red', 'yellow'], lemon: ['green', 'yellow']}});
201                 expect(myHash3.toQueryString()).to.equal('fruits[apple][0]=red&fruits[apple][1]=yellow&fruits[lemon][0]=green&fruits[lemon][1]=yellow');
202         });
206 })();
208 //</1.2compat>