summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/ajax/prototype/test/unit/hash_test.js
blob: cc3d083135bc698e066e0fe19b0be05281b8f9cc (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
new Test.Unit.Runner({
  testSet: function() {
    var h = $H({a: 'A'})

    this.assertEqual('B', h.set('b', 'B'));
    this.assertHashEqual({a: 'A', b: 'B'}, h);
    
    this.assertUndefined(h.set('c'));
    this.assertHashEqual({a: 'A', b: 'B', c: undefined}, h);
  },

  testGet: function() {
    var h = $H({a: 'A'});
    this.assertEqual('A', h.get('a'));
    this.assertUndefined(h.a);
    this.assertUndefined($H({}).get('a'));

    this.assertUndefined($H({}).get('toString'));
    this.assertUndefined($H({}).get('constructor'));
  },
  
  testUnset: function() {
    var hash = $H(Fixtures.many);
    this.assertEqual('B', hash.unset('b'));
    this.assertHashEqual({a:'A', c: 'C', d:'D#'}, hash);
    this.assertUndefined(hash.unset('z'));
    this.assertHashEqual({a:'A', c: 'C', d:'D#'}, hash);
    // not equivalent to Hash#remove
    this.assertEqual('A', hash.unset('a', 'c'));
    this.assertHashEqual({c: 'C', d:'D#'}, hash);
  },
  
  testToObject: function() {
    var hash = $H(Fixtures.many), object = hash.toObject();
    this.assertInstanceOf(Object, object);
    this.assertHashEqual(Fixtures.many, object);
    this.assertNotIdentical(Fixtures.many, object);
    hash.set('foo', 'bar');
    this.assertHashNotEqual(object, hash.toObject());
  },
  
  testConstruct: function() {
    var object = Object.clone(Fixtures.one);
    var h = new Hash(object), h2 = $H(object);
    this.assertInstanceOf(Hash, h);
    this.assertInstanceOf(Hash, h2);
    
    this.assertHashEqual({}, new Hash());
    this.assertHashEqual(object, h);
    this.assertHashEqual(object, h2);
    
    h.set('foo', 'bar');
    this.assertHashNotEqual(object, h);
    
    var clone = $H(h);
    this.assertInstanceOf(Hash, clone);
    this.assertHashEqual(h, clone);
    h.set('foo', 'foo');
    this.assertHashNotEqual(h, clone);
    this.assertIdentical($H, Hash.from);
  },
  
  testKeys: function() {
    this.assertEnumEqual([],               $H({}).keys());
    this.assertEnumEqual(['a'],            $H(Fixtures.one).keys());
    this.assertEnumEqual($w('a b c d'),    $H(Fixtures.many).keys().sort());
    this.assertEnumEqual($w('plus quad'),  $H(Fixtures.functions).keys().sort());
  },
  
  testValues: function() {
    this.assertEnumEqual([],             $H({}).values());
    this.assertEnumEqual(['A#'],         $H(Fixtures.one).values());
    this.assertEnumEqual($w('A B C D#'), $H(Fixtures.many).values().sort());
    this.assertEnumEqual($w('function function'),
      $H(Fixtures.functions).values().map(function(i){ return typeof i }));
    this.assertEqual(9, $H(Fixtures.functions).get('quad')(3));
    this.assertEqual(6, $H(Fixtures.functions).get('plus')(3));
  },
  
  testIndex: function() {
    this.assertUndefined($H().index('foo'));
    
    this.assert('a', $H(Fixtures.one).index('A#'));
    this.assert('a', $H(Fixtures.many).index('A'));
    this.assertUndefined($H(Fixtures.many).index('Z'))
  
    var hash = $H({a:1,b:'2',c:1});
    this.assert(['a','c'].include(hash.index(1)));
    this.assertUndefined(hash.index('1'));
  },
    
  testMerge: function() {
    var h = $H(Fixtures.many);
    this.assertNotIdentical(h, h.merge());
    this.assertNotIdentical(h, h.merge({}));
    this.assertInstanceOf(Hash, h.merge());
    this.assertInstanceOf(Hash, h.merge({}));
    this.assertHashEqual(h, h.merge());
    this.assertHashEqual(h, h.merge({}));
    this.assertHashEqual(h, h.merge($H()));
    this.assertHashEqual({a:'A',  b:'B', c:'C', d:'D#', aaa:'AAA' }, h.merge({aaa: 'AAA'}));
    this.assertHashEqual({a:'A#', b:'B', c:'C', d:'D#' }, h.merge(Fixtures.one));
  },
  
  testUpdate: function() {
    var h = $H(Fixtures.many);
    this.assertIdentical(h, h.update());
    this.assertIdentical(h, h.update({}));
    this.assertHashEqual(h, h.update());
    this.assertHashEqual(h, h.update({}));
    this.assertHashEqual(h, h.update($H()));
    this.assertHashEqual({a:'A',  b:'B', c:'C', d:'D#', aaa:'AAA' }, h.update({aaa: 'AAA'}));
    this.assertHashEqual({a:'A#', b:'B', c:'C', d:'D#', aaa:'AAA' }, h.update(Fixtures.one));
  },
  
  testToQueryString: function() {
    this.assertEqual('',                   $H({}).toQueryString());
    this.assertEqual('a%23=A',             $H({'a#': 'A'}).toQueryString());
    this.assertEqual('a=A%23',             $H(Fixtures.one).toQueryString());
    this.assertEqual('a=A&b=B&c=C&d=D%23', $H(Fixtures.many).toQueryString());
    this.assertEqual("a=b&c",              $H(Fixtures.value_undefined).toQueryString());
    this.assertEqual("a=b&c",              $H("a=b&c".toQueryParams()).toQueryString());
    this.assertEqual("a=b&c=",             $H(Fixtures.value_null).toQueryString());
    this.assertEqual("a=b&c=0",            $H(Fixtures.value_zero).toQueryString());
    this.assertEqual("color=r&color=g&color=b", $H(Fixtures.multiple).toQueryString());
    this.assertEqual("color=r&color=&color=g&color&color=0", $H(Fixtures.multiple_nil).toQueryString());
    this.assertEqual("color=&color",       $H(Fixtures.multiple_all_nil).toQueryString());
    this.assertEqual("",                   $H(Fixtures.multiple_empty).toQueryString());
    this.assertEqual("",                   $H({foo: {}, bar: {}}).toQueryString());
    this.assertEqual("stuff%5B%5D=%24&stuff%5B%5D=a&stuff%5B%5D=%3B", $H(Fixtures.multiple_special).toQueryString());
    this.assertHashEqual(Fixtures.multiple_special, $H(Fixtures.multiple_special).toQueryString().toQueryParams());
    this.assertIdentical(Object.toQueryString, Hash.toQueryString);
  },
  
  testInspect: function() {
    this.assertEqual('#<Hash:{}>',              $H({}).inspect());
    this.assertEqual("#<Hash:{'a': 'A#'}>",     $H(Fixtures.one).inspect());
    this.assertEqual("#<Hash:{'a': 'A', 'b': 'B', 'c': 'C', 'd': 'D#'}>", $H(Fixtures.many).inspect());
  },

  testClone: function() {
    var h = $H(Fixtures.many);
    this.assertHashEqual(h, h.clone());
    this.assertInstanceOf(Hash, h.clone());
    this.assertNotIdentical(h, h.clone());
  },
  
  testToJSON: function() {
    this.assertEqual('{\"b\": [false, true], \"c\": {\"a\": \"hello!\"}}',
      $H({'b': [undefined, false, true, undefined], c: {a: 'hello!'}}).toJSON());
  },
  
  testAbilityToContainAnyKey: function() {
    var h = $H({ _each: 'E', map: 'M', keys: 'K', pluck: 'P', unset: 'U' });
    this.assertEnumEqual($w('_each keys map pluck unset'), h.keys().sort());
    this.assertEqual('U', h.unset('unset'));
    this.assertHashEqual({ _each: 'E', map: 'M', keys: 'K', pluck: 'P' }, h);
  },
  
  testHashToTemplateReplacements: function() {
    var template = new Template("#{a} #{b}"), hash = $H({ a: "hello", b: "world" });
    this.assertEqual("hello world", template.evaluate(hash.toObject()));
    this.assertEqual("hello world", template.evaluate(hash));
    this.assertEqual("hello", "#{a}".interpolate(hash));
  },
  
  testPreventIterationOverShadowedProperties: function() {
    // redundant now that object is systematically cloned.
    var FooMaker = function(value) {
      this.key = value;
    };
    FooMaker.prototype.key = 'foo';
    var foo = new FooMaker('bar');
    this.assertEqual("key=bar", new Hash(foo).toQueryString());
    this.assertEqual("key=bar", new Hash(new Hash(foo)).toQueryString());
  }
  
});