Home | History | Annotate | Download | only in script-tests
      1 description("createDocument tests modeled after mozilla's testing");
      2 
      3 function stringForExceptionCode(c)
      4 {
      5     var exceptionName;
      6     switch(c) {
      7         case DOMException.INVALID_CHARACTER_ERR:
      8             exceptionName = "INVALID_CHARACTER_ERR";
      9             break;
     10         case DOMException.NAMESPACE_ERR:
     11             exceptionName = "NAMESPACE_ERR";
     12     }
     13     if (exceptionName)
     14         return exceptionName; // + "(" + c + ")";
     15     return c;
     16 }
     17 
     18 function assertEquals(actual, expect, m)
     19 {
     20     if (actual !== expect) {
     21         m += "; expected " + stringForExceptionCode(expect) + ", threw " + stringForExceptionCode(actual);
     22         testFailed(m);
     23     } else {
     24         m += "; threw " + stringForExceptionCode(actual);;
     25         testPassed(m);
     26     }
     27 }
     28 
     29 var allTests = [
     30    { args: [undefined, undefined], code: 5  },
     31    { args: [null, undefined], code: 5  },
     32    { args: [undefined, null], code: 5 },
     33    { args: [undefined, undefined, null], code: 5 },
     34    { args: [null, null], code: 5 },
     35    { args: [null, null, null], code: 5 },
     36    { args: [null, ""], code: 5 },
     37    { args: ["", null], code: 5 },
     38    { args: ["", ""], code: 5 },
     39    { args: ["a:", null, null], code: 14 },
     40    { args: [":foo", null, null], code: 14 },
     41    { args: [":", null, null], code: 14 },
     42    { args: ["foo", null, null] },
     43    { args: ["foo:bar", null, null] },
     44    { args: ["foo::bar", null, null], code: 14 },
     45    { args: ["\t:bar", null, null], code: 5 },
     46    { args: ["foo:\t", null, null], code: 5 },
     47    { args: ["foo :bar", null, null], code: 5 },
     48    { args: ["foo: bar", null, null], code: 5 },
     49    { args: ["a:b:c", null, null], code: 14, message: "valid XML name, invalid QName" },
     50 ];
     51 
     52 function sourceify(v)
     53 {
     54     switch (typeof v) {
     55         case "undefined":
     56             return v;
     57         case "string":
     58             return '"' + v.replace('"', '\\"') + '"';
     59         default:
     60             return String(v);
     61     }
     62 }
     63 
     64 function sourceifyArgs(args)
     65 {
     66     var copy = new Array(args.length);
     67     for (var i = 0, sz = args.length; i < sz; i++)
     68         copy[i] = sourceify(args[i]);
     69 
     70     return copy.join(", ");
     71 }
     72 
     73 function runTests(tests, createFunctionName)
     74 {
     75     for (var i = 0, sz = tests.length; i < sz; i++) {
     76         var test = tests[i];
     77 
     78         var code = -1;
     79         var argStr = sourceifyArgs(test.args);
     80         var msg = createFunctionName + "(" + argStr + ")";
     81         if ("message" in test)
     82             msg += "; " + test.message;
     83         try {
     84             document.implementation[createFunctionName].apply(document.implementation, test.args);
     85             if ('code' in test)
     86                 testFailed(msg + " expected exception: " + test.code);
     87             else
     88                 testPassed(msg);
     89         } catch (e) {
     90             assertEquals(e.code, test.code || "expected no exception", msg);
     91         }
     92     }
     93 }
     94 
     95 // Moz throws a "Not enough arguments" exception in these, we don't:
     96 shouldBeEqualToString("document.implementation.createDocumentType('foo').toString()", "[object DocumentType]");
     97 shouldBeEqualToString("document.implementation.createDocumentType('foo', null).toString()", "[object DocumentType]");
     98 runTests(allTests, "createDocumentType");
     99 
    100 var successfullyParsed = true;
    101