tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

rounding-priority.js (3162B)


      1 // |reftest| skip-if(!this.hasOwnProperty("Intl"))
      2 
      3 const tests = [
      4  // Rounding conflict with maximum fraction/significand digits.
      5  {
      6    value: 4.321,
      7    options: {
      8      maximumFractionDigits: 2,
      9      maximumSignificantDigits: 2,
     10    },
     11    roundingPriorities: {
     12      auto: "4.3",
     13      lessPrecision: "4.3",
     14      morePrecision: "4.32",
     15    },
     16  },
     17  {
     18    value: 4.321,
     19    options: {
     20      maximumFractionDigits: 2,
     21      minimumFractionDigits: 2,
     22      maximumSignificantDigits: 2,
     23    },
     24    roundingPriorities: {
     25      auto: "4.3",
     26      lessPrecision: "4.3",
     27      morePrecision: "4.32",
     28    },
     29  },
     30  {
     31    value: 4.321,
     32    options: {
     33      maximumFractionDigits: 2,
     34      maximumSignificantDigits: 2,
     35      minimumSignificantDigits: 2,
     36    },
     37    roundingPriorities: {
     38      auto: "4.3",
     39      lessPrecision: "4.3",
     40      morePrecision: "4.32",
     41    },
     42  },
     43  {
     44    value: 4.321,
     45    options: {
     46      maximumFractionDigits: 2,
     47      minimumFractionDigits: 2,
     48      maximumSignificantDigits: 2,
     49      minimumSignificantDigits: 2,
     50    },
     51    roundingPriorities: {
     52      auto: "4.3",
     53      lessPrecision: "4.3",
     54      morePrecision: "4.32",
     55    },
     56  },
     57 
     58  // Rounding conflict with minimum fraction/significand digits.
     59  {
     60    value: 1.0,
     61    options: {
     62      minimumFractionDigits: 2,
     63      minimumSignificantDigits: 2,
     64    },
     65    roundingPriorities: {
     66      auto: "1.0",
     67      // Returning "1.00" for both options seems unexpected. Also filed at
     68      // <https://github.com/tc39/proposal-intl-numberformat-v3/issues/52>.
     69      lessPrecision: "1.00",
     70      morePrecision: "1.0",
     71    },
     72  },
     73  {
     74    value: 1.0,
     75    options: {
     76      minimumFractionDigits: 2,
     77      maximumFractionDigits: 2,
     78      minimumSignificantDigits: 2,
     79    },
     80    roundingPriorities: {
     81      auto: "1.0",
     82      lessPrecision: "1.00",
     83      morePrecision: "1.0",
     84    },
     85  },
     86  {
     87    value: 1.0,
     88    options: {
     89      minimumFractionDigits: 2,
     90      minimumSignificantDigits: 2,
     91      maximumSignificantDigits: 2,
     92    },
     93    roundingPriorities: {
     94      auto: "1.0",
     95      lessPrecision: "1.0",
     96      morePrecision: "1.00",
     97    },
     98  },
     99  {
    100    value: 1.0,
    101    options: {
    102      minimumFractionDigits: 2,
    103      maximumFractionDigits: 2,
    104      minimumSignificantDigits: 2,
    105      maximumSignificantDigits: 2,
    106    },
    107    roundingPriorities: {
    108      auto: "1.0",
    109      lessPrecision: "1.0",
    110      morePrecision: "1.00",
    111    },
    112  },
    113 ];
    114 
    115 for (let {value, options, roundingPriorities} of tests) {
    116  for (let [roundingPriority, expected] of Object.entries(roundingPriorities)) {
    117    let nf = new Intl.NumberFormat("en", {...options, roundingPriority});
    118    assertEq(nf.resolvedOptions().roundingPriority, roundingPriority);
    119    assertEq(nf.format(value), expected, `value=${value}, roundingPriority=${roundingPriority}`);
    120  }
    121 }
    122 
    123 // Default value of "auto".
    124 assertEq(new Intl.NumberFormat().resolvedOptions().roundingPriority, "auto");
    125 
    126 // Invalid values.
    127 for (let roundingPriority of ["", null, "more", "less", "never"]){
    128  assertThrowsInstanceOf(() => new Intl.NumberFormat("en", {roundingPriority}), RangeError);
    129 }
    130 
    131 if (typeof reportCompare === "function")
    132  reportCompare(true, true);