tosource.js
Version:
A polyfill for Mozilla's Object.toSource() method.
103 lines (87 loc) • 4.22 kB
HTML
<html>
<head>
<meta charset="utf-8">
<title>QUnit .toSource()</title>
<link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.14.0.css">
</head>
<body>
<div id="qunit"></div>
<div id="qunit-fixture"></div>
<script src="http://code.jquery.com/qunit/qunit-1.14.0.js"></script>
<script src="toSource.min.js"></script>
<script>
/**
* Tests if the toSource property is enumberable for an given object (any kind of object)
*/
function toSourceIsNotEnumerableFor(Obj) {
var obj = new Obj();
var found = false;
for (var prop in obj) {
if (prop == "toSource") found = true;
}
return !found;
}
test( "Number", function() {
ok( (42).toSource() == "(new Number(42))", "Natural" );
ok( (-42).toSource() == "(new Number(-42))", "Negative" );
ok( (4.2).toSource() == "(new Number(4.2))", "Float" );
ok( (4e2).toSource() == "(new Number(400))", "Exponent" );
ok( toSourceIsNotEnumerableFor(Number), "Not enumerable" );
});
test( "Boolean", function() {
ok( (true).toSource() == "(new Boolean(true))", "true" );
ok( (false).toSource() == "(new Boolean(false))", "false" );
ok( (true == false).toSource() == "(new Boolean(false))", "Comparasion" );
ok( toSourceIsNotEnumerableFor(Boolean), "Not enumerable" );
});
test( "String", function() {
ok( ("Hello World").toSource() == "(new String(\"Hello World\"))", "Simple" );
ok( ("Hello \"World\"").toSource() == "(new String(\"Hello \\\"World\\\"\"))", "Quotes" );
ok( toSourceIsNotEnumerableFor(String), "Not enumerable" );
});
test( "Function", function() {
function hello(world) {
return "Hello, " + world + "!";
}
ok( hello.toSource() == hello.toString(), "toString()" );
ok( toSourceIsNotEnumerableFor(Function), "Not enumerable" );
});
test( "Array", function() {
ok( ([1, "2", false, {a: 1, b: 2}]).toSource() == "[1, \"2\", false, {a:1, b:2}]", "Mixed Array" );
var arr = [1, "2", false, {a: 1, b: 2}];
arr.push(arr);
ok( (arr).toSource() == "[1, \"2\", false, {a:1, b:2}, []]", "Cicular Array" );
ok( toSourceIsNotEnumerableFor(Array), "Not enumerable" );
});
test( "Object", function() {
ok( ({foo: "bar"}).toSource() == "({foo:\"bar\"})", "Valid ID" );
ok( ({"foo:bar": "baz"}).toSource() == "({'foo:bar':\"baz\"})", "Invalid ID" );
ok( ({"foo:bar": "baz", qux: "quux"}).toSource() == "({'foo:bar':\"baz\", qux:\"quux\"})", "Mixed IDs" );
ok( ({"foo": "bar", baz:{ qux: "quux"}}).toSource() == "({foo:\"bar\", baz:{qux:\"quux\"}})", "Nested Objects" );
var obj = {"foo": "bar", baz:{ qux: "quux"}};
obj.baz.obj = obj;
ok( (obj).toSource() == "({foo:\"bar\", baz:{qux:\"quux\", obj:{}}})", "Circular Object" );
ok( toSourceIsNotEnumerableFor(Object), "Not enumerable" );
});
test( "Date", function() {
var d = new Date();
ok( (d).toSource() == "(new Date(" + d.getTime() + "))", "Current Date()" );
ok( toSourceIsNotEnumerableFor(Date), "Not enumerable" );
});
test( "RegExp", function() {
ok( (/foo,bar/).toSource() == "/foo,bar/", "/foo,bar/" );
ok( toSourceIsNotEnumerableFor(RegExp), "Not enumerable" );
});
test( "Error", function() {
try {
forty_two;
} catch (err) {
var res = /\(new ReferenceError\(\"forty_two is not defined\"(, \"[^\"]*\"(, \d+)?)?\)\)/.test(err.toSource());
ok( res, "/foo,bar/" );
}
ok( toSourceIsNotEnumerableFor(Error), "Not enumerable" );
});
</script>
</body>
</html>