summaryrefslogtreecommitdiff
path: root/test/parallel/test-icu-punycode.js
blob: 35dab6232f34bc0f279c4115c3e561dace367ab2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'use strict';
// Flags: --expose-internals
const common = require('../common');

if (!common.hasIntl)
  common.skip('missing Intl');

const { internalBinding } = require('internal/test/binding');
const icu = internalBinding('icu');
const assert = require('assert');

// test hasConverter method
assert(icu.hasConverter('utf-8'),
       'hasConverter should report coverter exists for utf-8');
assert(!icu.hasConverter('x'),
       'hasConverter should report coverter does not exist for x');

const tests = require('../fixtures/url-idna.js');
const fixtures = require('../common/fixtures');
const wptToASCIITests = require(
  fixtures.path('wpt', 'url', 'resources', 'toascii.json')
);

{
  for (const [i, { ascii, unicode }] of tests.entries()) {
    assert.strictEqual(ascii, icu.toASCII(unicode), `toASCII(${i + 1})`);
    assert.strictEqual(unicode, icu.toUnicode(ascii), `toUnicode(${i + 1})`);
    assert.strictEqual(ascii, icu.toASCII(icu.toUnicode(ascii)),
                       `toASCII(toUnicode(${i + 1}))`);
    assert.strictEqual(unicode, icu.toUnicode(icu.toASCII(unicode)),
                       `toUnicode(toASCII(${i + 1}))`);
  }
}

{
  const errMessage = /^Error: Cannot convert name to ASCII$/;

  for (const [i, test] of wptToASCIITests.entries()) {
    if (typeof test === 'string')
      continue; // skip comments
    const { comment, input, output } = test;
    let caseComment = `case ${i + 1}`;
    if (comment)
      caseComment += ` (${comment})`;
    if (output === null) {
      assert.throws(() => icu.toASCII(input),
                    errMessage, `ToASCII ${caseComment}`);
      icu.toASCII(input, true); // Should not throw.
    } else {
      assert.strictEqual(icu.toASCII(input), output, `ToASCII ${caseComment}`);
      assert.strictEqual(icu.toASCII(input, true), output,
                         `ToASCII ${caseComment} in lenient mode`);
    }
    icu.toUnicode(input); // Should not throw.
  }
}