Skip to content

Instantly share code, notes, and snippets.

@tobie

tobie/helper.js Secret

Created March 15, 2013 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobie/3d81cd3d48859d2df1dd to your computer and use it in GitHub Desktop.
Save tobie/3d81cd3d48859d2df1dd to your computer and use it in GitHub Desktop.
Tests for structured cloning.
var structureClone = (function() {
var PROP = '' + Date.now() + Math.random(),
count = 0;
return function(obj, callback) {
var id = count++;
function handler(e) {
if (e.data[PROP] === id) {
callback(e.data.value);
window.removeEventListener('message', handler);
}
}
window.addEventListener('message', handler);
var data = {};
data[PROP] = id;
data.value = obj;
window.postMessage(data, "*");
}
})();
function assert_identical(actual, expected, description) {
return assert_true(actual === expected, description);
}
function assert_not_identical(actual, expected, description) {
return assert_true(actual !== expected, description);
}
<!DOCTYPE HTML>
<html>
<head>
<title>Structured clone</title>
</head>
<body>
<h1>Structured clone</h1>
<div id="log"></div>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="./helper.js"></script>
<script>
async_test(function(test) {
structureClone(123, test.step_func(function(actual) {
assert_equals(actual, 123);
test.done();
}));
}, "Primitive numbers are cloned.");
async_test(function(test) {
structureClone("hello world", test.step_func(function(actual) {
assert_equals(actual, "hello world");
test.done();
}));
}, "Primitive strings are cloned.");
async_test(function(test) {
structureClone(false, test.step_func(function(actual) {
assert_equals(actual, false);
test.done();
}));
}, "Primitive booleans are cloned.");
async_test(function(test) {
structureClone(null, test.step_func(function(actual) {
assert_equals(actual, null);
test.done();
}));
}, "null is cloned.");
async_test(function(test) {
var str = new String("foo");
structureClone(str, test.step_func(function(actual) {
assert_true(actual instanceof String);
assert_equals(actual.valueOf(), str.valueOf());
assert_not_identical(actual, str);
test.done();
}));
}, "String instances are cloned.");
async_test(function(test) {
var bool = new Boolean(true);
structureClone(bool, test.step_func(function(actual) {
assert_true(actual instanceof Boolean);
assert_equals(actual.valueOf(), bool.valueOf());
assert_not_identical(actual, bool);
test.done();
}));
}, "Boolean instances are cloned.");
async_test(function(test) {
var num = new Number(123);
structureClone(num, test.step_func(function(actual) {
assert_true(actual instanceof Number);
assert_equals(actual.valueOf(), num.valueOf());
assert_not_identical(actual, num);
test.done();
}));
}, "Number instances are cloned.");
async_test(function(test) {
var date = new Date();
structureClone(date, test.step_func(function(actual) {
assert_true(actual instanceof Date);
assert_equals(actual.valueOf(), date.valueOf());
assert_equals(actual.getTime(), date.getTime());
assert_not_identical(actual, date);
test.done();
}));
}, "Date instances are cloned.");
async_test(function(test) {
var regexp = /f/gim;
regexp.test("foo"); // set lastIndex to 1
structureClone(regexp, test.step_func(function(actual) {
assert_true(actual instanceof RegExp);
assert_equals(actual.global, regexp.global);
assert_equals(actual.ignoreCase, regexp.ignoreCase);
assert_equals(actual.multiline, regexp.multiline);
assert_equals(actual.source, regexp.source);
assert_not_equals(actual.lastIndex, regexp.lastIndex);
assert_not_identical(actual, regexp);
test.done();
}));
}, "RegExp instances are cloned. lastIndex property is not.");
async_test(function(test) {
test.done();
test.status = test.NOTRUN;
}, "Need to find a way to test cloning of File objects.");
async_test(function(test) {
var content = "some content",
blob = new Blob([content]);
structureClone(blob, test.step_func(function(actual) {
assert_true(actual instanceof Blob);
var reader = new FileReader();
reader.readAsText(actual);
reader.onload = function() {
assert_identical(reader.result, content);
test.done();
}
assert_not_identical(actual, blob);
}));
}, "Blob instances are cloned.");
test(function() {
assert_throws("DataCloneError", function() {
structureClone(function(){}, assert_unreached);
}, "Cloning a function throws a DataCloneError.");
assert_throws("DataCloneError", function() {
structureClone(new Error(), assert_unreached);
}, "Cloning an Error throws a DataCloneError.");
assert_throws("DataCloneError", function() {
structureClone(document.documentElement, assert_unreached);
}, "Cloning a DOM node throws a DataCloneError.");
}, "A DataCloneError is thrown when cloning Host objects, Functions and Errors.");
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment