master
1
2objectPrototypeMethods = {};
3sugarEnabledMethods = [
4 'isArray','isBoolean','isDate','isFunction','isNumber','isString','isRegExp','isNaN','isObject', // Type methods
5 'keys','values','select','reject','each','merge','isEmpty','equals','clone','watch','tap','has','toQueryString', // Hash methods
6 'any','all','none','count','find','findAll','isEmpty','sum','average','min','max','least','most','map','reduce','size' // Enumerable methods
7];
8
9rememberObjectProtoypeMethods = function() {
10 for(var m in Object.prototype) {
11 if(!Object.prototype.hasOwnProperty(m)) continue;
12 objectPrototypeMethods[m] = Object.prototype[m];
13 }
14}
15
16restoreObjectPrototypeMethods = function() {
17 // Cannot iterate over Object.prototype's methods if they've been defined in a modern browser
18 // that implements defineProperty, so we'll have to set back the known ones that have been overridden.
19 sugarEnabledMethods.forEach(function(name){
20 // This is a cute one. Checking for the name in the hash isn't enough because it itself is
21 // an object that has been extended, so each and every one of the methods being held here are being
22 // perfectly shadowed!
23 if(objectPrototypeMethods.hasOwnProperty(name) && objectPrototypeMethods[name]) {
24 Object.prototype[name] = objectPrototypeMethods[name];
25 } else {
26 delete Object.prototype[name];
27 }
28 });
29}
30
31testIterateOverObject = function(obj, fn) {
32 var key;
33 for(key in obj) {
34 if(!Object.hasOwnProperty(key)) continue;
35 fn.call(obj, key, obj[key]);
36 }
37}
38
39testClassAndInstance = function(name, obj, args, expected, message) {
40 if(!testIsArray(args)) {
41 args = [args];
42 }
43 equal(Object[name].apply(obj, [obj].concat(args)), expected, message);
44 if(Object.extended) {
45 extended = Object.extended(obj);
46 equal(extended[name].apply(extended, args), expected, message + ' | On extended object');
47 }
48}
49
50assertQueryStringGenerated = function(obj, args, expected, message) {
51 expected = expected.replace(/\[/g, '%5B').replace(/\]/g, '%5D');
52 testClassAndInstance('toQueryString', obj, args, expected, message);
53}
54