2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
30 test("joinPath", 1, function() {
31 var value = base.joinPath("path/to", "test.html");
32 equals(value, "path/to/test.html");
35 test("endsWith", 9, function() {
36 ok(base.endsWith("xyz", ""));
37 ok(base.endsWith("xyz", "z"));
38 ok(base.endsWith("xyz", "yz"));
39 ok(base.endsWith("xyz", "xyz"));
40 ok(!base.endsWith("xyz", "wxyz"));
41 ok(!base.endsWith("xyz", "gwxyz"));
42 ok(base.endsWith("", ""));
43 ok(!base.endsWith("", "z"));
44 ok(!base.endsWith("xyxy", "yx"));
47 test("trimExtension", 6, function() {
48 equals(base.trimExtension("xyz"), "xyz");
49 equals(base.trimExtension("xy.z"), "xy");
50 equals(base.trimExtension("x.yz"), "x");
51 equals(base.trimExtension("x.y.z"), "x.y");
52 equals(base.trimExtension(".xyz"), "");
53 equals(base.trimExtension(""), "");
56 test("joinPath with empty parent", 1, function() {
57 var value = base.joinPath("", "test.html");
58 equals(value, "test.html");
61 test("dirName", 3, function() {
62 equals(base.dirName("foo.html"), "foo.html");
63 equals(base.dirName("foo/bar.html"), "foo");
64 equals(base.dirName("foo/bar/baz.html"), "foo/bar");
67 test("uniquifyArray", 5, function() {
68 deepEqual(base.uniquifyArray([]), []);
69 deepEqual(base.uniquifyArray(["a"]), ["a"]);
70 deepEqual(base.uniquifyArray(["a", "b"]), ["a", "b"]);
71 deepEqual(base.uniquifyArray(["a", "b", "b"]), ["a", "b"]);
72 deepEqual(base.uniquifyArray(["a", "b", "b", "a"]), ["a", "b"]);
75 test("flattenArray", 5, function() {
76 deepEqual(base.flattenArray([]), []);
77 deepEqual(base.flattenArray([["a"]]), ["a"]);
78 deepEqual(base.flattenArray([["a"], ["b"]]), ["a", "b"]);
79 deepEqual(base.flattenArray([["a"], ["b", "c"]]), ["a", "b", "c"]);
80 deepEqual(base.flattenArray([["a"], [], ["b"]]), ["a", "b"]);
83 test("filterDictionary", 3, function() {
88 deepEqual(base.filterDictionary(dictionary, function() { return true; }), {
92 deepEqual(base.filterDictionary(dictionary, function() { return false; }), { });
93 deepEqual(base.filterDictionary(dictionary, function(key) { return key == 'foo'; }), {
98 test("mapDictionary", 3, function() {
99 deepEqual(base.mapDictionary({}, function(value) { return value - 10; }), {});
104 deepEqual(base.mapDictionary(dictionary, function(value) { return value - 10; }), {
108 deepEqual(base.mapDictionary(dictionary, function(value) {
116 test("filterTree", 2, function() {
132 function isLeaf(node)
134 return !!node.actual;
137 function actualIsText(node)
139 return node.actual == 'TEXT';
142 var all = base.filterTree(tree, isLeaf, function() { return true });
144 'path/to/test.html': {
148 'path/another.html': {
154 var text = base.filterTree(tree, isLeaf, actualIsText);
156 'path/another.html': {
163 test("UpdateTracker", 20, function() {
169 dict.forEach(function(item, key, updated) {
175 function dumpUpdatedKeys()
178 dict.forEach(function(item, key, updated) {
179 updated && updates.push(key);
185 dict = new base.UpdateTracker();
186 dict.update("5", {});
187 deepEqual(dumpUpdatedKeys(), ["5"]);
188 dict.update("6", {});
189 dict.update("7", {});
190 deepEqual(dumpUpdatedKeys(), ["5", "6", "7"]);
191 deepEqual(dict.get("6"), {});
192 ok(dict.exists("7"));
194 deepEqual(dumpUpdatedKeys(), []);
195 deepEqual(dumpKeys(), ["5", "6", "7"]);
196 dict.update("5", {});
197 deepEqual(dumpUpdatedKeys(), ["5"]);
198 dict.update("4", {});
199 deepEqual(dumpUpdatedKeys(), ["4", "5"]);
200 deepEqual(dumpKeys(), ["4", "5", "6", "7"]);
202 deepEqual(dumpKeys(), ["4", "5"]);
203 deepEqual(dumpUpdatedKeys(), []);
205 deepEqual(dumpKeys(), []);
209 deepEqual(dumpUpdatedKeys(), ["one"]);
211 deepEqual(dumpUpdatedKeys(), ["one", "two"]);
212 dict.update("three");
214 deepEqual(dumpKeys(), ["one", "three", "two"]);
216 dict.purge(function() {
219 deepEqual(dumpKeys(), ["two"]);
220 equal(removeCount, 2);
222 var removeCounter = { count: 0 };
223 dict.purge(function() {
226 equal(removeCounter.count, 1);
227 dict.purge(function() {
228 equal(String(this), "four");
231 dict = new base.UpdateTracker();
234 dict.forEach(function(item) {
235 equal(this, thisObject);
240 test("extends", 14, function() {
242 var LikeDiv = base.extends("div", {
244 this.textContent = "awesome";
246 method: function(msg) {
251 var LikeLikeDiv = base.extends(LikeDiv, {
253 this.className = "like";
257 var LikeP = base.extends("p", {
258 init: function(content) {
259 this.textContent = content
263 var LikeProgress = base.extends("progress", {
270 var LikeLikeProgress = base.extends(LikeProgress, {
271 completed: function() {
276 document.body.appendChild(new LikeDiv());
277 equals(document.body.lastChild.tagName, "DIV");
278 equals(document.body.lastChild.innerHTML, "awesome");
279 equals(document.body.lastChild.method(), 42);
280 document.body.removeChild(document.body.lastChild);
282 document.body.appendChild(new LikeLikeDiv());
283 equals(document.body.lastChild.tagName, "DIV");
284 equals(document.body.lastChild.innerHTML, "awesome");
285 equals(document.body.lastChild.method(), 42);
286 equals(document.body.lastChild.className, "like");
287 document.body.removeChild(document.body.lastChild);
289 document.body.appendChild(new LikeP("super"));
290 equals(document.body.lastChild.tagName, "P");
291 equals(document.body.lastChild.innerHTML, "super");
293 document.body.lastChild.method();
295 document.body.removeChild(document.body.lastChild);
297 document.body.appendChild(new LikeProgress());
298 equals(document.body.lastChild.tagName, "PROGRESS");
299 // Safari 5.1 lacks the <progress> element.
300 // equals(document.body.lastChild.position, 0.1);
301 equals(document.body.lastChild.innerHTML, "");
303 document.body.lastChild.method();
305 document.body.removeChild(document.body.lastChild);
307 document.body.appendChild(new LikeLikeProgress());
308 equals(document.body.lastChild.tagName, "PROGRESS");
309 // Safari 5.1 lacks the <progress> element.
310 // equals(document.body.lastChild.position, 0.1);
311 document.body.lastChild.completed();
312 // Safari 5.1 lacks the <progress> element.
313 // equals(document.body.lastChild.position, 1);
314 document.body.removeChild(document.body.lastChild);
317 test("relativizeTime", 14, function() {
318 var time = new Date();
319 equals(base.relativizeTime(time), "Just now");
320 time.setMinutes(time.getMinutes() - 1);
321 equals(base.relativizeTime(time), "1 minute ago");
322 time.setMinutes(time.getMinutes() - 1);
323 equals(base.relativizeTime(time), "2 minutes ago");
324 time.setMinutes(time.getMinutes() - 1);
325 equals(base.relativizeTime(time), "3 minutes ago");
326 time.setMinutes(time.getMinutes() - 56);
327 equals(base.relativizeTime(time), "59 minutes ago");
328 time.setMinutes(time.getMinutes() - 1);
329 equals(base.relativizeTime(time), "1 hour ago");
330 time.setMinutes(time.getMinutes() - 29);
331 equals(base.relativizeTime(time), "1 hour ago");
332 time.setMinutes(time.getMinutes() - 2);
333 equals(base.relativizeTime(time), "2 hours ago");
334 time.setMinutes(time.getMinutes() - 29);
335 equals(base.relativizeTime(time), "2 hours ago");
336 time.setHours(time.getHours() - 1);
337 equals(base.relativizeTime(time), "3 hours ago");
338 time.setHours(time.getHours() - 20);
339 equals(base.relativizeTime(time), "23 hours ago");
340 time.setHours(time.getHours() - 1);
341 equals(base.relativizeTime(time), "1 day ago");
342 time.setDate(time.getDate() - 1);
343 equals(base.relativizeTime(time), "2 days ago");
344 time.setDate(time.getDate() - 998);
345 equals(base.relativizeTime(time), "1000 days ago");
348 test("getURLParameter", 1, function() {
349 ok(!base.getURLParameter('non-existant'));
352 test("parseJSONP", 6, function() {
353 deepEqual(base.parseJSONP(""), {});
354 deepEqual(base.parseJSONP('p({"key": "value"})'), {"key": "value"});
355 deepEqual(base.parseJSONP('ADD_RESULTS({"dummy":"data"});'), {"dummy":"data"});
356 deepEqual(base.parseJSONP('{"dummy":"data"}'), {"dummy":"data"});
357 deepEqual(base.parseJSONP('ADD_RESULTS({"builder(1)":"data"});'), {"builder(1)":"data"});
358 deepEqual(base.parseJSONP('{"builder(1)":"data"}'), {"builder(1)":"data"});