Skip to content

Consider helpers for formatToParts assertions #4451

Open
@gibson042

Description

@gibson042

(originally posted by @gibson042 in #4447 (comment))

I wonder if a harness file should define some formatToParts assertion helpers:

function assertFormatPart(part, type, value, label) {
  var suffix = label ? ', ' + label : '';
  assert.sameValue(part.type, type, 'part type' + suffix);
  if (value === undefined) return;
  assert.sameValue(part.value, value, 'part value' + suffix);
}

function assertFormatParts(parts, expectations, label) {
  var suffix = label ? ', ' + label : '';
  assert.sameValue(parts.length, expectations.length, 'parts count' + suffix);
  for (var i = 0; i < parts.length; i++) {
    assertFormatPart(parts[i], expectations[i][0], expectations[i][1],
      suffix && (suffix + ' part ' + i));
  }
}

for use in files like test/intl402/DateTimeFormat/prototype/formatToParts/dayPeriod-long-en.js:

for (var h = 0; h < 24; h++) {
  var parts = long.formatToParts(inputs[h]);
  assertFormatParts(parts, [['dayPeriod']], 'hour ' + h + ' long dayPeriod');
  
  var partsNumeric = longNumeric.formatToParts(inputs[h]);
  assertFormatParts(partsNumeric,
    [['hour', String((h % 12) || 12)], ['literal'], ['dayPeriod']],
    'numeric hour ' + h + ' must precede long dayPeriod');

  counts[i]++;
  prevDayPeriod = dayPeriod;
}

Similar functions already exist in various files, with names like

  • assertParts
  • assertSameParts
  • compare
  • compareFormatToPartsSnapshot
  • formatAsString
  • partsEquals
  • reduce
  • serializeTypesAndLiteral
  • verifyFormatParts
  • etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions