constructor-unit.js (1794B)
1 // Copyright 2019 Igalia, S.L. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 4 /*--- 5 esid: sec-initializenumberformat 6 description: Checks handling of the unit style. 7 includes: [testIntl.js] 8 features: [Intl.NumberFormat-unified] 9 ---*/ 10 11 assert.throws(TypeError, () => { 12 new Intl.NumberFormat([], { 13 style: "unit", 14 }) 15 }); 16 17 for (const unit of ["test", "MILE", "kB"]) { 18 // Throws RangeError for invalid unit identifier. 19 for (const style of [undefined, "decimal", "unit"]) { 20 assert.throws(RangeError, () => { 21 new Intl.NumberFormat([], { style, unit }) 22 }, `{ style: ${style}, unit: ${unit} }`); 23 } 24 25 const style = "currency"; 26 27 // Throws TypeError because "currency" option is missing. 28 assert.throws(TypeError, () => { 29 new Intl.NumberFormat([], { style, unit }) 30 }, `{ style: ${style}, unit: ${unit} }`); 31 32 // Throws RangeError for invalid unit identifier. 33 assert.throws(RangeError, () => { 34 new Intl.NumberFormat([], { style, unit, currency: "USD" }) 35 }, `{ style: ${style}, unit: ${unit} }`); 36 } 37 38 const nf = new Intl.NumberFormat([], { 39 style: "percent", 40 }); 41 assert.sameValue(nf.resolvedOptions().style, "percent"); 42 assert.sameValue("unit" in nf.resolvedOptions(), false); 43 assert.sameValue(nf.resolvedOptions().unit, undefined); 44 45 function check(unit) { 46 const nf = new Intl.NumberFormat([], { 47 style: "unit", 48 unit, 49 }); 50 const options = nf.resolvedOptions(); 51 assert.sameValue(options.style, "unit"); 52 assert.sameValue(options.unit, unit); 53 } 54 55 const units = allSimpleSanctionedUnits(); 56 57 for (const simpleUnit of units) { 58 check(simpleUnit); 59 for (const simpleUnit2 of units) { 60 check(simpleUnit + "-per-" + simpleUnit2); 61 check(simpleUnit2 + "-per-" + simpleUnit); 62 } 63 } 64 65 reportCompare(0, 0);