var O = function (value) {
bindMethods(this); this.value = value;
};
O.prototype.func = function () { returnthis.value;
};
var o = new O("boring"); var p = {};
p.func = o.func; var func = o.func;
t.is( o.func(), "boring", "bindMethods doesn't break shit" );
t.is( p.func(), "boring", "bindMethods works on other objects" );
t.is( func(), "boring", "bindMethods works on functions" );
var p = clone(o);
t.ok( p instanceof O, "cloned correct inheritance" ); var q = clone(p);
t.ok( q instanceof O, "clone-cloned correct inheritance" );
q.foo = "bar";
t.is( p.foo, undefined, "clone-clone is copy-on-write" );
p.bar = "foo";
t.is( o.bar, undefined, "clone is copy-on-write" );
t.is( q.bar, "foo", "clone-clone has proper delegation" ); // unbind
p.func = bind(p.func, null);
t.is( p.func(), "boring", "clone function calls correct" );
q.value = "awesome";
t.is( q.func(), "awesome", "clone really does work" );
// test boring boolean funcs
t.is( isCallable(isCallable), true, "isCallable returns true on itself" );
t.is( isCallable(1), false, "isCallable returns false on numbers" );
t.is( isUndefined(null), false, "null is not undefined" );
t.is( isUndefined(""), false, "empty string is not undefined" );
t.is( isUndefined(undefined), true, "undefined is undefined" );
t.is( isUndefined({}.foo), true, "missing property is undefined" );
t.is( isUndefinedOrNull(null), true, "null is undefined or null" );
t.is( isUndefinedOrNull(""), false, "empty string is not undefined or null" );
t.is( isUndefinedOrNull(undefined), true, "undefined is undefined or null" );
t.is( isUndefinedOrNull({}.foo), true, "missing property is undefined or null" );
// test extension of arrays var a = []; var b = []; var three = [1, 2, 3];
extend(a, three, 1);
t.ok( objEqual(a, [2, 3]), "extend to an empty array" );
extend(a, three, 1)
t.ok( objEqual(a, [2, 3, 2, 3]), "extend to a non-empty array" );
extend(b, three);
t.ok( objEqual(b, three), "extend of an empty array" );
// test partial application var a = []; var func = function (a, b) { if (arguments.length != 2) { return"bad args";
} else { returnthis.value + a + b;
}
}; var self = {"value": 1, "func": func}; var self2 = {"value": 2};
t.is( self.func(2, 3), 6, "setup for test is correct" );
self.funcTwo = partial(self.func, 2);
t.is( self.funcTwo(3), 6, "partial application works" );
t.is( self.funcTwo(3), 6, "partial application works still" );
t.is( bind(self.funcTwo, self2)(3), 7, "rebinding partial works" );
self.funcTwo = bind(bind(self.funcTwo, self2), null);
t.is( self.funcTwo(3), 6, "re-unbinding partial application works" );
// nodeWalk test // ... looks a lot like a DOM tree on purpose var tree = { "id": "nodeWalkTestTree", "test:int": "1", "childNodes": [
{ "test:int": "2", "childNodes": [
{"test:int": "5"}, "ignored string",
{"ignored": "object"},
["ignored", "list"],
{ "test:skipchildren": "1", "childNodes": [{"test:int": 6}]
}
]
},
{"test:int": "3"},
{"test:int": "4"}
]
}
var visitedNodes = [];
nodeWalk(tree, function (node) { var attr = node["test:int"]; if (attr) {
visitedNodes.push(attr);
} if (node["test:skipchildren"]) { return;
} return node.childNodes;
});
t.ok( objEqual(visitedNodes, ["1", "2", "3", "4", "5"]), "nodeWalk looks like it works");
// test map var minusOne = function (x) { return x - 1; }; var res = map(minusOne, [1, 2, 3]);
t.ok( objEqual(res, [0, 1, 2]), "map works" );
t.is( isNotEmpty("foo"), true, "3 char string is not empty" );
t.is( isNotEmpty(""), false, "0 char string is empty" );
t.is( isNotEmpty([1, 2, 3]), true, "3 element list is not empty" );
t.is( isNotEmpty([]), false, "0 element list is empty" );
// test filter var greaterThanThis = function (x) { return x > this; }; var greaterThanOne = function (x) { return x > 1; }; var res = filter(greaterThanOne, [-1, 0, 1, 2, 3]);
t.ok( objEqual(res, [2, 3]), "filter works" ); var res = filter(greaterThanThis, [-1, 0, 1, 2, 3], 1);
t.ok( objEqual(res, [2, 3]), "filter self works" ); var res2 = xfilter(greaterThanOne, -1, 0, 1, 2, 3);
t.ok( objEqual(res2, res), "xfilter works" );
var v = keys(a1);
v.sort();
t.is( compare(v, ["a", "b", "c"]), 0, "keys" );
v = items(a1);
v.sort();
t.is( compare(v, [["a", 1], ["b", 2], ["c", 2]]), 0, "items" );
var StringMap = function() {};
a = new StringMap();
a.foo = "bar";
b = new StringMap();
b.foo = "bar"; try {
compare(a, b);
t.ok( false, "bad comparison registered!?" );
} catch (e) {
t.ok( e instanceof TypeError, "bad comparison raised TypeError" );
}
t.is( repr(a), "[object Object]", "default repr for StringMap" ); var isStringMap = function () { for (var i = 0; i < arguments.length; i++) { if (!(arguments[i] instanceof StringMap)) { returnfalse;
}
} returntrue;
};
// not public API
MochiKit.Base.reprRegistry.unregister("stringMap");
t.is( repr(a), "[object Object]", "default repr for StringMap" );
registerComparator("stringMap",
isStringMap, function (a, b) { // no sorted(...) in base
a = items(a);
b = items(b);
a.sort(compare);
b.sort(compare); return compare(a, b);
}
);
t.is( compare(a, b), 0, "registerComparator" );
update(a, {"foo": "bar"}, {"wibble": "baz"}, undefined, null, {"grr": 1});
t.is( a.foo, "bar", "update worked (first obj)" );
t.is( a.wibble, "baz", "update worked (second obj)" );
t.is( a.grr, 1, "update worked (skipped undefined and null)" );
t.is( compare(a, b), 1, "update worked (comparison)" );
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.