2 * @fileoverview Tests for stand-alone functions in dygraph-utils.js
4 * @author danvdk@gmail.com (Dan Vanderkam)
7 var UtilsTestCase
= TestCase("utils-tests");
9 UtilsTestCase
.prototype.testUpdate
= function() {
16 assertEquals(1, a
['a']);
17 assertEquals([1, 2, 3], a
['b']);
18 assertEquals({x
: 1, y
: 2}, a
['c']);
19 assertEquals({f
: 10, g
: 20}, a
['d']);
21 Dygraph
.update(a
, { c
: { x
: 2 } });
22 assertEquals({x
: 2}, a
['c']);
24 Dygraph
.update(a
, { d
: null });
25 assertEquals(null, a
['d']);
27 Dygraph
.update(a
, { a
: 10, b
: [1, 2] });
28 assertEquals(10, a
['a']);
29 assertEquals([1, 2], a
['b']);
30 assertEquals({x
: 2}, a
['c']);
31 assertEquals(null, a
['d']);
34 UtilsTestCase
.prototype.testUpdateDeep
= function() {
41 assertEquals(1, a
['a']);
42 assertEquals([1, 2, 3], a
['b']);
43 assertEquals({x
: 1, y
: 2}, a
['c']);
44 assertEquals({f
: 10, g
: 20}, a
['d']);
46 Dygraph
.updateDeep(a
, { c
: { x
: 2 } });
47 assertEquals({x
: 2, y
: 2}, a
['c']);
49 Dygraph
.updateDeep(a
, { d
: null });
50 assertEquals(null, a
['d']);
52 Dygraph
.updateDeep(a
, { a
: 10, b
: [1, 2] });
53 assertEquals(10, a
['a']);
54 assertEquals([1, 2], a
['b']);
55 assertEquals({x
: 2, y
: 2}, a
['c']);
56 assertEquals(null, a
['d']);
59 UtilsTestCase
.prototype.testUpdateDeepDecoupled
= function() {
63 c
: { x
: "original", y
: 2},
67 Dygraph
.updateDeep(b
, a
);
73 assertEquals(1, a
.b
[0]);
76 assertEquals("original", a
.c
.x
);
80 UtilsTestCase
.prototype.testIterator_nopredicate
= function() {
81 var array
= ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
82 var iter
= Dygraph
.createIterator(array
, 1, 4);
83 assertTrue(iter
.hasNext
);
84 assertEquals('b', iter
.peek
);
85 assertEquals('b', iter
.next());
86 assertTrue(iter
.hasNext
);
88 assertEquals('c', iter
.peek
);
89 assertEquals('c', iter
.next());
91 assertTrue(iter
.hasNext
);
92 assertEquals('d', iter
.next());
94 assertTrue(iter
.hasNext
);
95 assertEquals('e', iter
.next());
97 assertFalse(iter
.hasNext
);
100 UtilsTestCase
.prototype.testIterator_predicate
= function() {
101 var array
= ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
102 var iter
= Dygraph
.createIterator(array
, 1, 4,
103 function(array
, idx
) { return array
[idx
] !== 'd' });
104 assertTrue(iter
.hasNext
);
105 assertEquals('b', iter
.peek
);
106 assertEquals('b', iter
.next());
107 assertTrue(iter
.hasNext
);
109 assertEquals('c', iter
.peek
);
110 assertEquals('c', iter
.next());
112 assertTrue(iter
.hasNext
);
113 assertEquals('e', iter
.next());
115 assertFalse(iter
.hasNext
);
118 UtilsTestCase
.prototype.testIterator_empty
= function() {
120 var iter
= Dygraph
.createIterator([], 0, 0);
121 assertFalse(iter
.hasNext
);
124 UtilsTestCase
.prototype.testIterator_outOfRange
= function() {
125 var array
= ['a', 'b', 'c'];
126 var iter
= Dygraph
.createIterator(array
, 1, 4,
127 function(array
, idx
) { return array
[idx
] !== 'd' });
128 assertTrue(iter
.hasNext
);
129 assertEquals('b', iter
.peek
);
130 assertEquals('b', iter
.next());
131 assertTrue(iter
.hasNext
);
133 assertEquals('c', iter
.peek
);
134 assertEquals('c', iter
.next());
136 assertFalse(iter
.hasNext
);
139 // Makes sure full array is tested, and that the predicate isn't called
140 // with invalid boundaries.
141 UtilsTestCase
.prototype.testIterator_whole_array
= function() {
142 var array
= ['a', 'b', 'c'];
143 var iter
= Dygraph
.createIterator(array
, 0, array
.length
,
144 function(array
, idx
) {
145 if (idx
< 0 || idx
>= array
.length
) {
151 assertTrue(iter
.hasNext
);
152 assertEquals('a', iter
.next());
153 assertTrue(iter
.hasNext
);
154 assertEquals('b', iter
.next());
155 assertTrue(iter
.hasNext
);
156 assertEquals('c', iter
.next());
157 assertFalse(iter
.hasNext
);
158 assertNull(iter
.next());
161 UtilsTestCase
.prototype.testIterator_no_args
= function() {
162 var array
= ['a', 'b', 'c'];
163 var iter
= Dygraph
.createIterator(array
);
164 assertTrue(iter
.hasNext
);
165 assertEquals('a', iter
.next());
166 assertTrue(iter
.hasNext
);
167 assertEquals('b', iter
.next());
168 assertTrue(iter
.hasNext
);
169 assertEquals('c', iter
.next());
170 assertFalse(iter
.hasNext
);
171 assertNull(iter
.next());
175 UtilsTestCase.prototype.testDateSet = function() {
176 var base = new Date(1383455100000);
177 var d = new Date(base);
179 // A one hour shift -- this is surprising behavior!
180 d.setMilliseconds(10);
181 assertEquals(3600010, d.getTime() - base.getTime());
183 // setDateSameTZ compensates for this surprise.
185 Dygraph.setDateSameTZ(d, {ms: 10});
186 assertEquals(10, d.getTime() - base.getTime());