"use strict";
/*global describe */
/*global it */

const assert = require("assert");
const URL = require("..").URL;
const testCases = require("./web-platform-tests/urltestdata.json");
const additionalTestCases = require("./to-upstream.json");

function testURL(expected) {
  return () => {
    let url;
    try {
      url = new URL(expected.input, expected.base);
    } catch (e) {
      if (e instanceof TypeError && expected.failure) {
        return;
      }
      throw e;
    }

    assert.equal(url.href, expected.href, "href");
    assert.equal(url.origin, expected.origin, "origin");
    assert.equal(url.protocol, expected.protocol, "protocol");
    assert.equal(url.username, expected.username, "username");
    assert.equal(url.password, expected.password, "password");
    assert.equal(url.host, expected.host, "host");
    assert.equal(url.hostname, expected.hostname, "hostname");
    assert.equal(url.port, expected.port, "port");
    assert.equal(url.pathname, expected.pathname, "pathname");
    assert.equal(url.search, expected.search, "search");
    assert.equal(url.hash, expected.hash, "hash");
  };
}

describe("Web Platform Tests", () => {
  for (const expected of testCases) {
    if (typeof expected === "string") {
      // It's a "comment"; skip it.
      continue;
    }

    it("Parsing: <" + expected.input + "> against <" + expected.base + ">", testURL(expected));
  }
});

describe("To-upstream tests", () => {
  for (const expected of additionalTestCases) {
    if (typeof expected === "string") {
      // It's a "comment"; skip it.
      continue;
    }

    it("Parsing: <" + expected.input + "> against <" + expected.base + ">", testURL(expected));
  }
});

@@ -1669,28 +1669,1360 @@
 testURL(expected));%0A  %7D%0A%7D);%0A
+%0Adescribe(%22MDN examples%22, () =%3E %7B%0A  it(%22Checking all examples on MDN pass%22, () =%3E %7B%0A    const a = new URL(%22/%22, %22https://developer.mozilla.org%22);%0A    assert.strictEqual(a.href, %22https://developer.mozilla.org/%22);%0A%0A    const b = new URL(%22https://developer.mozilla.org%22);%0A    assert.strictEqual(b.href, %22https://developer.mozilla.org/%22);%0A%0A    const c = new URL(%22en-US/docs%22, b);%0A    assert.strictEqual(c.href, %22https://developer.mozilla.org/en-US/docs%22);%0A%0A    const d = new URL(%22/en-US/docs%22, b);%0A    assert.strictEqual(d.href, %22https://developer.mozilla.org/en-US/docs%22);%0A%0A    const f = new URL(%22/en-US/docs%22, d);%0A    assert.strictEqual(f.href, %22https://developer.mozilla.org/en-US/docs%22);%0A%0A    const g = new URL(%22/en-US/docs%22, %22https://developer.mozilla.org/fr-FR/toto%22);%0A    assert.strictEqual(g.href, %22https://developer.mozilla.org/en-US/docs%22);%0A%0A    const h = new URL(%22/en-US/docs%22, a);%0A    assert.strictEqual(h.href, %22https://developer.mozilla.org/en-US/docs%22);%0A%0A    assert.throws(() =%3E new URL(%22/en-US/docs%22, %22%22));%0A    assert.throws(() =%3E new URL(%22/en-US/docs%22));%0A%0A    const k = new URL(%22http://www.example.com%22, %22https://developers.mozilla.com%22);%0A    assert.strictEqual(k.href, %22http://www.example.com/%22);%0A%0A    const l = new URL(%22http://www.example.com%22, b);%0A    assert.strictEqual(l.href, %22http://www.example.com/%22);%0A  %7D);%0A%7D);%0A

Add MDN examples to tests.