locale-constructor.js 4.04 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Locale constructor can't be called as function.
assertThrows(() => Intl.Locale('sr'), TypeError);

// Non-string locale.
assertThrows(() => new Intl.Locale(5), TypeError);
10 11 12 13 14
assertThrows(() => new Intl.Locale(Symbol()), TypeError);
assertThrows(() => new Intl.Locale(null), TypeError);
assertThrows(() => new Intl.Locale(undefined), TypeError);
assertThrows(() => new Intl.Locale(false), TypeError);
assertThrows(() => new Intl.Locale(true), TypeError);
15

16 17
// Invalid locale string.
assertThrows(() => new Intl.Locale('abcdefghi'), RangeError);
18 19 20 21

// Options will be force converted into Object.
assertDoesNotThrow(() => new Intl.Locale('sr', 5));

22 23
// Regression for http://bugs.icu-project.org/trac/ticket/13417.
assertDoesNotThrow(
24 25 26 27 28 29 30 31 32 33
    () => new Intl.Locale(
        'sr-cyrl-rs-t-ja-u-ca-islamic-cu-rsd-tz-uslax-x-whatever', {
          calendar: 'buddhist',
          caseFirst: 'true',
          collation: 'phonebk',
          hourCycle: 'h23',
          caseFirst: 'upper',
          numeric: 'true',
          numberingSystem: 'roman',
        }),
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    RangeError);

// Throws only once during construction.
// Check for all getters to prevent regression.
assertThrows(
    () => new Intl.Locale('en-US', {
      get calendar() {
        throw new Error('foo');
      }
    }),
    Error);

assertThrows(
    () => new Intl.Locale('en-US', {
      get caseFirst() {
        throw new Error('foo');
      }
    }),
    Error);

assertThrows(
    () => new Intl.Locale('en-US', {
      get collation() {
        throw new Error('foo');
      }
    }),
    Error);

assertThrows(
    () => new Intl.Locale('en-US', {
      get hourCycle() {
        throw new Error('foo');
      }
    }),
    Error);

assertThrows(
    () => new Intl.Locale('en-US', {
      get numeric() {
        throw new Error('foo');
      }
    }),
    Error);

assertThrows(
    () => new Intl.Locale('en-US', {
      get numberingSystem() {
        throw new Error('foo');
      }
    }),
    Error);

86
assertThrows(
87 88 89 90 91 92 93
    () => new Intl.Locale('en-US', {
      get language() {
        throw new Error('foo');
      }
    }),
    Error);

94
assertThrows(
95 96 97 98 99 100 101
    () => new Intl.Locale('en-US', {
      get script() {
        throw new Error('foo');
      }
    }),
    Error);

102
assertThrows(
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    () => new Intl.Locale('en-US', {
      get region() {
        throw new Error('foo');
      }
    }),
    Error);

// There won't be an override for baseName so we don't expect it to throw.
assertDoesNotThrow(
    () => new Intl.Locale('en-US', {
      get baseName() {
        throw new Error('foo');
      }
    }),
    Error);
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

// Preserve the order of getter initialization.
let getCount = 0;
let calendar = -1;
let collation = -1;
let hourCycle = -1;
let caseFirst = -1;
let numeric = -1;
let numberingSystem = -1;


new Intl.Locale('en-US', {
  get calendar() {
    calendar = ++getCount;
  },
  get collation() {
    collation = ++getCount;
  },
  get hourCycle() {
    hourCycle = ++getCount;
  },
  get caseFirst() {
    caseFirst = ++getCount;
  },
  get numeric() {
    numeric = ++getCount;
  },
  get numberingSystem() {
    numberingSystem = ++getCount;
  },
});

assertEquals(1, calendar);
assertEquals(2, collation);
assertEquals(3, hourCycle);
assertEquals(4, caseFirst);
assertEquals(5, numeric);
assertEquals(6, numberingSystem);

// Check getter properties against the spec.
function checkProperties(property) {
  let desc = Object.getOwnPropertyDescriptor(Intl.Locale.prototype, property);
  assertEquals(`get ${property}`, desc.get.name);
  assertEquals('function', typeof desc.get)
  assertEquals(undefined, desc.set);
  assertFalse(desc.enumerable);
  assertTrue(desc.configurable);
}

checkProperties('language');
checkProperties('script');
checkProperties('region');
checkProperties('baseName');
checkProperties('calendar');
checkProperties('collation');
checkProperties('hourCycle');
checkProperties('caseFirst');
checkProperties('numeric');
checkProperties('numberingSystem');