summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/intl402/Segmenter/prototype/segment/containing/breakable-input.js
blob: d1f50c5b9c0cf2334d8b85d164b4e335fd862707 (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
// |reftest| skip-if(!Intl.Segmenter) -- Intl.Segmenter is not enabled unconditionally
// Copyright 2020 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-%segmentsprototype%.containing
description: Verifies the cases which the input is breakable.
info: |
    %Segments.prototype%.containing ( index )

    8. Let startIndex be ! FindBoundary(segmenter, string, n, before).
    9. Let endIndex be ! FindBoundary(segmenter, string, n, after).

features: [Intl.Segmenter]
---*/

// The inputs are breakable for "grapheme" and "word" but not for "sentence"
const granularities = [undefined, "grapheme", "word"];
// The following all contains more than one segments in either "grapheme" or "word"
// granularity.
const inputs = [
    "123 ",
    "a ",
    " a",
    " \ud800\udc00", // SPACE + surrogate
    "\ud800\udc00 ", // surrogate + SPACE
    "\udc00\ud800", // incorrect surrogate- tail + leading
    "\ud800 ", // only leading surrogate + SPACE
    "\udc00 ", // only trailing surrogate + SPACE
    " \ud800", // SPACE + only leading surrogate
    " \udc00", // SPACE + only trailing surrogate
    " 台", // SPACE + a Han character
    "台 ", // a Han character + SPACE
    "\u0301 ", // a modifier + SPACE
];

granularities.forEach(
    function(granularity) {
      const segmenter = new Intl.Segmenter(undefined, {granularity});
      inputs.forEach(function(input) {
        const segment = segmenter.segment(input);
        let msg = `granularity: ${granularity} input: ${input}`;
        const first = segment.containing(0);
        assert.sameValue(0, first.index, `${msg} containing(0) index`);
        assert.sameValue(input, first.input, `${msg} containing(0) input`);
        assert.sameValue(false, first.segment == input,
            `${msg} containing(0) segment`);
        const last = segment.containing(input.length - 1);
        msg += ` containing(${input.length - 1}) `
        assert.sameValue(true, last.index > 0, `${msg} index > 0`);
        assert.sameValue(true, last.index < input.length, `${msg} index`);
        assert.sameValue(input, last.input, `${msg} input`);
        assert.sameValue(false, last.segment == input, `${msg} segment`);
      });
    });

reportCompare(0, 0);