<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="../../resources/js-test-pre.js"></script>
</head>
<body>
<script>

description("Tests ChildNode.before((Node or DOMString)...)");
function test(t, createChild)
{
    type = t;
    debug("<b>Test " + type + ".before((Node or DOMString)...).</b>");
    
    debug("Test with no arguments.");
    parent = document.createElement('div');
    child = createChild();
    parent.appendChild(child);
    child.before();
    shouldBe("parent.childNodes.length", "1");
    debug("");

    debug("Test with a single element.");
    parent = document.createElement('div');
    child = createChild();
    parent.appendChild(child);
    d1 = document.createElement("div");
    child.before(d1);
    shouldBe("parent.childNodes.length", "2");
    shouldBe("child.previousSibling", "d1");
    debug("");

    debug("Test with a single string.");
    parent = document.createElement('div');
    child = createChild();
    parent.appendChild(child);
    child.before('hello');
    shouldBe("parent.childNodes.length", "2");
    shouldBe("child.previousSibling.data", "'hello'");
    debug("");

    debug("Test with a both strings and elements.");
    parent = document.createElement('div');
    child = createChild();
    parent.appendChild(child);
    d1 = document.createElement("div");
    child.before('hello', d1);
    shouldBe("parent.childNodes.length", "3");
    shouldBe("child.previousSibling", "d1");
    shouldBeType("parent.childNodes[0]", "Text");
    shouldBe("parent.childNodes[0].data", "'hello'");
    shouldBeType("parent.childNodes[1]", "Element");
    shouldBe("parent.childNodes[1]", "d1");
    debug("");

    debug("Test conversion to string");
    parent = document.createElement('div');
    child = createChild();
    parent.appendChild(child);
    child.before(null, undefined, 7);
    shouldBe("parent.childNodes.length", "4");
    shouldBeType("parent.childNodes[0]", "Text");
    shouldBe("parent.childNodes[0].data", "'null'");
    shouldBeType("parent.childNodes[1]", "Text");
    shouldBe("parent.childNodes[1].data", "'undefined'");
    shouldBeType("parent.childNodes[2]", "Text");
    shouldBe("parent.childNodes[2].data", "'7'");
    shouldBeType("parent.childNodes[3]", type);
    shouldBe("parent.childNodes[3]", "child");
    debug("");

    debug("Test a child without a parent.");
    child = createChild();
    d1 = document.createElement("div");
    child.before(d1);
    shouldBeNull("child.previousSibling");
    debug("");

    debug("Test when there the previous sibling is not a viable previous sibling.");
    parent = document.createElement('div');
    child = createChild();
    d1 = document.createElement("div");
    parent.appendChild(d1);
    d2 = document.createElement("div");
    parent.appendChild(d2);
    parent.appendChild(child);
    child.before("hello", d2, "world");
    shouldBe("parent.childNodes.length", "5");
    shouldBeType("parent.childNodes[0]", "Element");
    shouldBe("parent.childNodes[0]", "d1");
    shouldBeType("parent.childNodes[1]", "Text");
    shouldBe("parent.childNodes[1].data", "'hello'");
    shouldBeType("parent.childNodes[2]", "Element");
    shouldBe("parent.childNodes[2]", "d2");
    shouldBeType("parent.childNodes[3]", "Text");
    shouldBe("parent.childNodes[3].data", "'world'");
    shouldBeType("parent.childNodes[4]", type);
    shouldBe("parent.childNodes[4]", "child");
    debug("");

    debug("Test when there is no viable previous sibling.");
    parent = document.createElement('div');
    child = createChild();
    d1 = document.createElement("div");
    parent.appendChild(d1);
    d2 = document.createElement("div");
    parent.appendChild(d2);
    parent.appendChild(child);
    child.before("hello", d1, "world", d2);
    shouldBe("parent.childNodes.length", "5");
    shouldBeType("parent.childNodes[0]", "Text");
    shouldBe("parent.childNodes[0].data", "'hello'");
    shouldBeType("parent.childNodes[1]", "Element");
    shouldBe("parent.childNodes[1]", "d1");
    shouldBeType("parent.childNodes[2]", "Text");
    shouldBe("parent.childNodes[2].data", "'world'");
    shouldBeType("parent.childNodes[3]", "Element");
    shouldBe("parent.childNodes[3]", "d2");
    shouldBeType("parent.childNodes[4]", type);
    shouldBe("parent.childNodes[4]", "child");

    debug("");
}

test("Element", function() { return document.createElement("div"); });
test("Text", function() { return document.createTextNode("text node text"); });
test("Comment", function() { return document.createComment("comment text"); });

</script>
<script src="../../resources/js-test-post.js"></script>
</body>
</html>
