1 // ========================================================================== 2 // Project: SproutCore - JavaScript Application Framework 3 // Copyright: ©2006-2011 Strobe Inc. and contributors. 4 // ©2008-2011 Apple Inc. All rights reserved. 5 // License: Licensed under MIT license (see license.js) 6 // ========================================================================== 7 // ======================================================================== 8 // SC.guidFor Tests 9 // ======================================================================== 10 /*globals module test ok isObj equals expects */ 11 12 module("SC.guidFor"); 13 14 var sameGuid = function(a, b, message) { 15 equals( SC.guidFor(a), SC.guidFor(b), message ); 16 } 17 18 var diffGuid = function(a, b, message) { 19 ok( SC.guidFor(a) !== SC.guidFor(b), message); 20 } 21 22 var nanGuid = function(obj) { 23 var type = SC.typeOf(obj); 24 ok( isNaN(parseInt(SC.guidFor(obj), 0)), "guids for " + type + "don't parse to numbers") 25 } 26 27 test("Object", function() { 28 var a = {}, b = {}; 29 30 sameGuid( a, a, "same object always yields same guid" ); 31 diffGuid( a, b, "different objects yield different guids" ); 32 nanGuid( a ) 33 }) 34 35 test("strings", function() { 36 var a = "string A", aprime = "string A", b = "String B"; 37 38 sameGuid( a, a, "same string always yields same guid" ); 39 sameGuid( a, aprime, "identical strings always yield the same guid" ); 40 diffGuid( a, b, "different strings yield different guids" ); 41 nanGuid( a ); 42 }) 43 44 test("numbers", function() { 45 var a = 23, aprime = 23, b = 34; 46 47 sameGuid( a, a, "same numbers always yields same guid" ); 48 sameGuid( a, aprime, "identical numbers always yield the same guid" ); 49 diffGuid( a, b, "different numbers yield different guids" ); 50 nanGuid( a ); 51 }); 52 53 test("booleans", function() { 54 var a = true, aprime = true, b = false; 55 56 sameGuid( a, a, "same booleans always yields same guid" ); 57 sameGuid( a, aprime, "identical booleans always yield the same guid" ); 58 diffGuid( a, b, "different boolean yield different guids" ); 59 nanGuid( a ); 60 nanGuid( b ); 61 }); 62 63 test("null and undefined", function() { 64 var a = null, aprime = null, b = undefined; 65 66 sameGuid( a, a, "null always returns the same guid" ); 67 sameGuid( b, b, "undefined always returns the same guid" ); 68 sameGuid( a, aprime, "different nulls return the same guid" ); 69 diffGuid( a, b, "null and undefined return different guids" ); 70 nanGuid( a ); 71 nanGuid( b ); 72 }); 73 74 test("arrays", function() { 75 var a = ["a", "b", "c"], aprime = ["a", "b", "c"], b = ["1", "2", "3"]; 76 77 sameGuid( a, a, "same instance always yields same guid" ); 78 diffGuid( a, aprime, "identical arrays always yield the same guid" ); 79 diffGuid( a, b, "different arrays yield different guids" ); 80 nanGuid( a ); 81 }); 82 83 // QUESTION: do we need to hardcode the fact that hash == guid in the tests? [YK] 84 var obj1, obj2, str, arr; 85 86 module("SC.hashFor", { 87 setup: function() { 88 obj1 = {}; 89 obj2 = { 90 hash: function() { 91 return '%1234'; 92 } 93 }; 94 str = "foo"; 95 arr = ['foo', 'bar']; 96 } 97 }); 98 99 test("One argument", function() { 100 equals(SC.guidFor(obj1), SC.hashFor(obj1), "guidFor and hashFor should be equal for an obj1ect"); 101 equals(obj2.hash(), SC.hashFor(obj2), "hashFor should call the hash() function if present"); 102 equals(SC.guidFor(str), SC.hashFor(str), "guidFor and hashFor should be equal for a string"); 103 equals(SC.guidFor(arr), SC.hashFor(arr), "guidFor and hashFor should be equal for an array"); 104 }); 105 106 test("Multiple arguments", function() { 107 var h = [ 108 SC.guidFor(obj1), 109 obj2.hash(), 110 SC.guidFor(str), 111 SC.guidFor(arr) 112 ].join(''); 113 114 equals(h, SC.hashFor(obj1, obj2, str, arr), "hashFor should concatenate the arguments' hashes when there are more than one"); 115 }); 116