• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// Tests below are not from WPT.
4
5const common = require('../common');
6if (!common.hasIntl) {
7  // A handful of the tests fail when ICU is not included.
8  common.skip('missing Intl');
9}
10
11const assert = require('assert');
12const URL = require('url').URL;
13const { test, assert_equals } = require('../common/wpt').harness;
14const fixtures = require('../common/fixtures');
15
16// TODO(joyeecheung): we should submit these to the upstream
17const additionalTestCases =
18  require(fixtures.path('url-setter-tests-additional.js'));
19
20{
21  for (const attributeToBeSet in additionalTestCases) {
22    if (attributeToBeSet === 'comment') {
23      continue;
24    }
25    const testCases = additionalTestCases[attributeToBeSet];
26    for (const testCase of testCases) {
27      let name = `Setting <${testCase.href}>.${attributeToBeSet}` +
28                 ` = "${testCase.new_value}"`;
29      if ('comment' in testCase) {
30        name += ` ${testCase.comment}`;
31      }
32      test(function() {
33        const url = new URL(testCase.href);
34        url[attributeToBeSet] = testCase.new_value;
35        for (const attribute in testCase.expected) {
36          assert_equals(url[attribute], testCase.expected[attribute]);
37        }
38      }, `URL: ${name}`);
39    }
40  }
41}
42
43{
44  const url = new URL('http://example.com/');
45  const obj = {
46    toString() { throw new Error('toString'); },
47    valueOf() { throw new Error('valueOf'); }
48  };
49  const sym = Symbol();
50  const props = Object.getOwnPropertyDescriptors(Object.getPrototypeOf(url));
51  for (const [name, { set }] of Object.entries(props)) {
52    if (set) {
53      assert.throws(() => url[name] = obj,
54                    /^Error: toString$/,
55                    `url.${name} = { toString() { throw ... } }`);
56      assert.throws(() => url[name] = sym,
57                    /^TypeError: Cannot convert a Symbol value to a string$/,
58                    `url.${name} = ${String(sym)}`);
59    }
60  }
61}
62