Skip to content
Snippets Groups Projects
Commit 1185b528 authored by Ms2ger's avatar Ms2ger Committed by Rick Waldron
Browse files

Intl.Segmenter: Submit remaining V8 tests.

parent 815b36a4
No related branches found
No related tags found
No related merge requests found
Showing
with 130 additions and 822 deletions
// 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.
// Flags: --harmony-intl-segmenter
// Segmenter constructor can't be called as function.
assertThrows(() => Intl.Segmenter(["sr"]), TypeError);
// Invalid locale string.
assertThrows(() => new Intl.Segmenter(["abcdefghi"]), RangeError);
assertDoesNotThrow(() => new Intl.Segmenter(["sr"], {}), TypeError);
assertDoesNotThrow(() => new Intl.Segmenter([], {}));
assertDoesNotThrow(() => new Intl.Segmenter(["fr", "ar"], {}));
assertDoesNotThrow(() => new Intl.Segmenter({ 0: "ja", 1: "fr" }, {}));
assertDoesNotThrow(() => new Intl.Segmenter({ 1: "ja", 2: "fr" }, {}));
assertDoesNotThrow(() => new Intl.Segmenter(["sr"]));
assertDoesNotThrow(() => new Intl.Segmenter());
assertDoesNotThrow(
() =>
new Intl.Segmenter(["sr"], {
lineBreakStyle: "strict",
granularity: "grapheme"
})
);
assertDoesNotThrow(
() => new Intl.Segmenter(["sr"], { granularity: "sentence" })
);
assertDoesNotThrow(() => new Intl.Segmenter(["sr"], { granularity: "word" }));
assertDoesNotThrow(
() => new Intl.Segmenter(["sr"], { granularity: "grapheme" })
);
assertDoesNotThrow(() => new Intl.Segmenter(["sr"], { granularity: "line" }));
assertThrows(
() => new Intl.Segmenter(["sr"], { granularity: "standard" }),
RangeError
);
assertDoesNotThrow(
() => new Intl.Segmenter(["sr"], { lineBreakStyle: "normal" })
);
assertDoesNotThrow(
() => new Intl.Segmenter(["sr"], { lineBreakStyle: "strict" })
);
assertDoesNotThrow(
() => new Intl.Segmenter(["sr"], { lineBreakStyle: "loose" })
);
assertThrows(
() => new Intl.Segmenter(["sr"], { lineBreakStyle: "giant" }),
RangeError
);
assertDoesNotThrow(
() =>
new Intl.Segmenter(["sr"], {
granularity: "sentence",
lineBreakStyle: "normal"
})
);
assertDoesNotThrow(
() =>
new Intl.Segmenter(["sr"], {
granularity: "sentence",
lineBreakStyle: "strict"
})
);
assertDoesNotThrow(
() =>
new Intl.Segmenter(["sr"], {
granularity: "sentence",
lineBreakStyle: "loose"
})
);
assertDoesNotThrow(
() =>
new Intl.Segmenter(["sr"], {
granularity: "word",
lineBreakStyle: "normal"
})
);
assertDoesNotThrow(
() =>
new Intl.Segmenter(["sr"], {
granularity: "word",
lineBreakStyle: "strict"
})
);
assertDoesNotThrow(
() =>
new Intl.Segmenter(["sr"], {
granularity: "word",
lineBreakStyle: "loose"
})
);
assertDoesNotThrow(
() =>
new Intl.Segmenter(["sr"], {
granularity: "grapheme",
lineBreakStyle: "normal"
})
);
assertDoesNotThrow(
() =>
new Intl.Segmenter(["sr"], {
granularity: "grapheme",
lineBreakStyle: "strict"
})
);
assertDoesNotThrow(
() =>
new Intl.Segmenter(["sr"], {
granularity: "grapheme",
lineBreakStyle: "loose"
})
);
assertDoesNotThrow(
() =>
new Intl.Segmenter(["sr"], {
granularity: "line",
lineBreakStyle: "loose"
})
);
assertDoesNotThrow(
() =>
new Intl.Segmenter(["sr"], {
granularity: "line",
lineBreakStyle: "normal"
})
);
assertDoesNotThrow(
() =>
new Intl.Segmenter(["sr"], {
granularity: "line",
lineBreakStyle: "strict"
})
);
// propagate exception from getter
assertThrows(
() =>
new Intl.Segmenter(undefined, {
get localeMatcher() {
throw new TypeError("");
}
}),
TypeError
);
assertThrows(
() =>
new Intl.Segmenter(undefined, {
get lineBreakStyle() {
throw new TypeError("");
}
}),
TypeError
);
assertThrows(
() =>
new Intl.Segmenter(undefined, {
get granularity() {
throw new TypeError("");
}
}),
TypeError
);
// Throws only once during construction.
// Check for all getters to prevent regression.
// Preserve the order of getter initialization.
let getCount = 0;
let localeMatcher = -1;
let lineBreakStyle = -1;
let granularity = -1;
new Intl.Segmenter(["en-US"], {
get localeMatcher() {
localeMatcher = ++getCount;
},
get lineBreakStyle() {
lineBreakStyle = ++getCount;
},
get granularity() {
granularity = ++getCount;
}
});
assertEquals(1, localeMatcher);
assertEquals(2, lineBreakStyle);
assertEquals(3, granularity);
// 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.
// Flags: --harmony-intl-segmenter
let segmenter = new Intl.Segmenter([], { granularity: "line" });
// The default lineBreakStyle is 'normal'
assertEquals("normal", segmenter.resolvedOptions().lineBreakStyle);
segmenter = new Intl.Segmenter();
assertEquals(undefined, segmenter.resolvedOptions().lineBreakStyle);
// The default granularity is 'grapheme'
assertEquals("grapheme", segmenter.resolvedOptions().granularity);
assertEquals(
undefined,
new Intl.Segmenter(["sr"], { lineBreakStyle: "strict" }).resolvedOptions()
.lineBreakStyle
);
assertEquals(
"grapheme",
new Intl.Segmenter(["sr"], { lineBreakStyle: "strict" }).resolvedOptions()
.granularity
);
assertEquals(
undefined,
new Intl.Segmenter(["sr"], { lineBreakStyle: "normal" }).resolvedOptions()
.lineBreakStyle
);
assertEquals(
"grapheme",
new Intl.Segmenter(["sr"], { lineBreakStyle: "normal" }).resolvedOptions()
.granularity
);
assertEquals(
undefined,
new Intl.Segmenter(["sr"], { lineBreakStyle: "loose" }).resolvedOptions()
.lineBreakStyle
);
assertEquals(
"grapheme",
new Intl.Segmenter(["sr"], { lineBreakStyle: "loose" }).resolvedOptions()
.granularity
);
assertEquals(
"word",
new Intl.Segmenter(["sr"], { granularity: "word" }).resolvedOptions()
.granularity
);
assertEquals(
undefined,
new Intl.Segmenter(["sr"], { granularity: "word" }).resolvedOptions()
.lineBreakStyle
);
assertEquals(
"grapheme",
new Intl.Segmenter(["sr"], { granularity: "grapheme" }).resolvedOptions()
.granularity
);
assertEquals(
undefined,
new Intl.Segmenter(["sr"], { granularity: "grapheme" }).resolvedOptions()
.lineBreakStyle
);
assertEquals(
"sentence",
new Intl.Segmenter(["sr"], { granularity: "sentence" }).resolvedOptions()
.granularity
);
assertEquals(
undefined,
new Intl.Segmenter(["sr"], { granularity: "sentence" }).resolvedOptions()
.lineBreakStyle
);
assertEquals(
"line",
new Intl.Segmenter(["sr"], { granularity: "line" }).resolvedOptions()
.granularity
);
assertEquals(
"normal",
new Intl.Segmenter(["sr"], { granularity: "line" }).resolvedOptions()
.lineBreakStyle
);
assertEquals(
"grapheme",
new Intl.Segmenter(["sr"], {
lineBreakStyle: "loose",
granularity: "grapheme"
}).resolvedOptions().granularity
);
assertEquals(
undefined,
new Intl.Segmenter(["sr"], {
lineBreakStyle: "loose",
granularity: "grapheme"
}).resolvedOptions().lineBreakStyle
);
assertEquals(
"grapheme",
new Intl.Segmenter(["sr"], {
lineBreakStyle: "strict",
granularity: "grapheme"
}).resolvedOptions().granularity
);
assertEquals(
undefined,
new Intl.Segmenter(["sr"], {
lineBreakStyle: "strict",
granularity: "grapheme"
}).resolvedOptions().lineBreakStyle
);
assertEquals(
"grapheme",
new Intl.Segmenter(["sr"], {
lineBreakStyle: "normal",
granularity: "grapheme"
}).resolvedOptions().granularity
);
assertEquals(
undefined,
new Intl.Segmenter(["sr"], {
lineBreakStyle: "normal",
granularity: "grapheme"
}).resolvedOptions().lineBreakStyle
);
assertEquals(
"word",
new Intl.Segmenter(["sr"], {
lineBreakStyle: "loose",
granularity: "word"
}).resolvedOptions().granularity
);
assertEquals(
undefined,
new Intl.Segmenter(["sr"], {
lineBreakStyle: "loose",
granularity: "word"
}).resolvedOptions().lineBreakStyle
);
assertEquals(
"word",
new Intl.Segmenter(["sr"], {
lineBreakStyle: "strict",
granularity: "word"
}).resolvedOptions().granularity
);
assertEquals(
undefined,
new Intl.Segmenter(["sr"], {
lineBreakStyle: "strict",
granularity: "word"
}).resolvedOptions().lineBreakStyle
);
assertEquals(
"word",
new Intl.Segmenter(["sr"], {
lineBreakStyle: "normal",
granularity: "word"
}).resolvedOptions().granularity
);
assertEquals(
undefined,
new Intl.Segmenter(["sr"], {
lineBreakStyle: "normal",
granularity: "word"
}).resolvedOptions().lineBreakStyle
);
assertEquals(
"sentence",
new Intl.Segmenter(["sr"], {
lineBreakStyle: "loose",
granularity: "sentence"
}).resolvedOptions().granularity
);
assertEquals(
undefined,
new Intl.Segmenter(["sr"], {
lineBreakStyle: "loose",
granularity: "sentence"
}).resolvedOptions().lineBreakStyle
);
assertEquals(
"sentence",
new Intl.Segmenter(["sr"], {
lineBreakStyle: "strict",
granularity: "sentence"
}).resolvedOptions().granularity
);
assertEquals(
undefined,
new Intl.Segmenter(["sr"], {
lineBreakStyle: "strict",
granularity: "sentence"
}).resolvedOptions().lineBreakStyle
);
assertEquals(
"sentence",
new Intl.Segmenter(["sr"], {
lineBreakStyle: "normal",
granularity: "sentence"
}).resolvedOptions().granularity
);
assertEquals(
"normal",
new Intl.Segmenter(["sr"], {
lineBreakStyle: "normal",
granularity: "line"
}).resolvedOptions().lineBreakStyle
);
assertEquals(
"line",
new Intl.Segmenter(["sr"], {
lineBreakStyle: "loose",
granularity: "line"
}).resolvedOptions().granularity
);
assertEquals(
"loose",
new Intl.Segmenter(["sr"], {
lineBreakStyle: "loose",
granularity: "line"
}).resolvedOptions().lineBreakStyle
);
assertEquals(
"line",
new Intl.Segmenter(["sr"], {
lineBreakStyle: "strict",
granularity: "line"
}).resolvedOptions().granularity
);
assertEquals(
"strict",
new Intl.Segmenter(["sr"], {
lineBreakStyle: "strict",
granularity: "line"
}).resolvedOptions().lineBreakStyle
);
assertEquals(
"line",
new Intl.Segmenter(["sr"], {
lineBreakStyle: "normal",
granularity: "line"
}).resolvedOptions().granularity
);
assertEquals(
"normal",
new Intl.Segmenter(["sr"], {
lineBreakStyle: "normal",
granularity: "line"
}).resolvedOptions().lineBreakStyle
);
assertEquals("ar", new Intl.Segmenter(["ar"]).resolvedOptions().locale);
assertEquals("ar", new Intl.Segmenter(["ar", "en"]).resolvedOptions().locale);
assertEquals("fr", new Intl.Segmenter(["fr", "en"]).resolvedOptions().locale);
assertEquals("ar", new Intl.Segmenter(["xyz", "ar"]).resolvedOptions().locale);
// 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.
// Flags: --harmony-intl-segmenter
const seg = new Intl.Segmenter([], {granularity: "grapheme"})
for (const text of [
"Hello world!", // English
" Hello world! ", // English with space before/after
" Hello world? Foo bar!", // English
"Jedovatou mambu objevila žena v zahrádkářské kolonii.", // Czech
"Việt Nam: Nhất thể hóa sẽ khác Trung Quốc?", // Vietnamese
"Σοβαρές ενστάσεις Κομισιόν για τον προϋπολογισμό της Ιταλίας", // Greek
"Решение Индии о покупке российских С-400 расценили как вызов США", // Russian
"הרופא שהציל נשים והנערה ששועבדה ע", // Hebrew,
"ترامب للملك سلمان: أنا جاد للغاية.. عليك دفع المزيد", // Arabic
"भारत की एस 400 मिसाइल के मुकाबले पाक की थाड, जानें कौन कितना ताकतवर", // Hindi
"ரெட் அலர்ட் எச்சரிக்கை; புதுச்சேரியில் நாளை அரசு விடுமுறை!", // Tamil
"'ఉత్తర్వులు అందే వరకు ఓటర్ల తుది జాబితాను వెబ్‌సైట్లో పెట్టవద్దు'", // Telugu
"台北》抹黑柯P失敗?朱學恒酸:姚文智氣pupu嗆大老闆", // Chinese
"วัดไทรตีระฆังเบาลงช่วงเข้าพรรษา เจ้าอาวาสเผยคนร้องเรียนรับผลกรรมแล้ว", // Thai
"九州北部の一部が暴風域に入りました(日直予報士 2018年10月06日) - 日本気象協会 tenki.jp", // Japanese
"법원 “다스 지분 처분권·수익권 모두 MB가 보유”", // Korean
]) {
const iter = seg.segment(text);
let segments = [];
let oldPos = -1;
for (let result = iter.next(); !result.done; result = iter.next()) {
const v = result.value;
assertEquals(undefined, v.breakType);
assertEquals("string", typeof v.segment);
assertTrue(v.segment.length > 0);
segments.push(v.segment);
assertEquals("number", typeof v.position);
assertTrue(oldPos < v.position);
oldPos = v.position;
}
assertEquals(text, segments.join(''));
}
// 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.
// Flags: --harmony-intl-segmenter
let breakCounts = {};
for (const locale of ["en", "fr", "ja", "zh", "ko"]) {
for (const lineBreakStyle of ["strict", "normal", "loose"]) {
const seg = new Intl.Segmenter(
[locale], {granularity: "line", lineBreakStyle: lineBreakStyle});
let opportunity = 0;
for (const text of [
// We know the following data caused different line break results between
// different modes.
// https://www.w3.org/TR/css-text-3/#propdef-line-break
// Japanese small kana or the Katakana-Hiragana prolonged sound mark
"あぁーぃーあーいーぁーぃー",
// hyphens:
// ‐ U+2010, – U+2013, 〜 U+301C, ゠ U+30A0
"ABC‐DEF–GHI〜JKL゠MNO",
// iteration marks:
// 々 U+3005, 〻 U+303B, ゝ U+309D, ゞ U+309E, ヽ U+30FD, ヾ U+30FE
"あ々あ〻あゝあゞあヽあヾあ",
// centered punctuation marks:
// ・ U+30FB, : U+FF1A, ; U+FF1B, ・ U+FF65, ‼ U+203C
"ABC・DEF:GHI;JKL・MNO‼PQR",
// centered punctuation marks:
// ⁇ U+2047, ⁈ U+2048, ⁉ U+2049, ! U+FF01, ? U+FF1F
"ABC⁇DEF⁈GHI⁉JKL!MNO?PQR",
]) {
const iter = seg.segment(text);
while (!iter.following()) {
opportunity++;
}
}
breakCounts[locale + "-" + lineBreakStyle] = opportunity;
}
}
// In Japanese
// Just test the break count in loose mode is greater than normal mode.
assertTrue(breakCounts["ja-loose"] > breakCounts["ja-normal"]);
// and test the break count in normal mode is greater than strict mode.
assertTrue(breakCounts["ja-normal"] > breakCounts["ja-strict"]);
// In Chinese
// Just test the break count in loose mode is greater than normal mode.
assertTrue(breakCounts["zh-loose"] > breakCounts["zh-normal"]);
// and test the break count in normal mode is greater than strict mode.
assertTrue(breakCounts["zh-normal"] > breakCounts["zh-strict"]);
// In English, French and Korean
assertTrue(breakCounts["en-loose"] >= breakCounts["en-normal"]);
assertTrue(breakCounts["fr-loose"] >= breakCounts["fr-normal"]);
assertTrue(breakCounts["ko-loose"] >= breakCounts["ko-normal"]);
// and test the break count in normal mode is greater than strict mode.
assertTrue(breakCounts["en-normal"] > breakCounts["en-strict"]);
assertTrue(breakCounts["fr-normal"] > breakCounts["fr-strict"]);
assertTrue(breakCounts["ko-normal"] > breakCounts["ko-strict"]);
// 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.
// Flags: --harmony-intl-segmenter
const seg = new Intl.Segmenter([], {granularity: "line"})
for (const text of [
"Hello world!", // English
" Hello world! ", // English with space before/after
" Hello world? Foo bar!", // English
"Jedovatou mambu objevila žena v zahrádkářské kolonii.", // Czech
"Việt Nam: Nhất thể hóa sẽ khác Trung Quốc?", // Vietnamese
"Σοβαρές ενστάσεις Κομισιόν για τον προϋπολογισμό της Ιταλίας", // Greek
"Решение Индии о покупке российских С-400 расценили как вызов США", // Russian
"הרופא שהציל נשים והנערה ששועבדה ע", // Hebrew,
"ترامب للملك سلمان: أنا جاد للغاية.. عليك دفع المزيد", // Arabic
"भारत की एस 400 मिसाइल के मुकाबले पाक की थाड, जानें कौन कितना ताकतवर", // Hindi
"ரெட் அலர்ட் எச்சரிக்கை; புதுச்சேரியில் நாளை அரசு விடுமுறை!", // Tamil
"'ఉత్తర్వులు అందే వరకు ఓటర్ల తుది జాబితాను వెబ్‌సైట్లో పెట్టవద్దు'", // Telugu
"台北》抹黑柯P失敗?朱學恒酸:姚文智氣pupu嗆大老闆", // Chinese
"วัดไทรตีระฆังเบาลงช่วงเข้าพรรษา เจ้าอาวาสเผยคนร้องเรียนรับผลกรรมแล้ว", // Thai
"九州北部の一部が暴風域に入りました(日直予報士 2018年10月06日) - 日本気象協会 tenki.jp", // Japanese
"법원 “다스 지분 처분권·수익권 모두 MB가 보유”", // Korean
]) {
const iter = seg.segment(text);
let segments = [];
let oldPos = -1;
for (let result = iter.next(); !result.done; result = iter.next()) {
const v = result.value;
assertTrue(["soft", "hard"].includes(iter.breakType), iter.breakType);
assertEquals("string", typeof v.segment);
assertTrue(v.segment.length > 0);
segments.push(v.segment);
assertEquals("number", typeof v.position);
assertTrue(oldPos < v.position);
oldPos = v.position;
}
assertEquals(text, segments.join(''));
}
// 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.
// Flags: --harmony-intl-segmenter
const seg = new Intl.Segmenter([], {granularity: "sentence"})
for (const text of [
"Hello world!", // English
" Hello world! ", // English with space before/after
" Hello world? Foo bar!", // English
"Jedovatou mambu objevila žena v zahrádkářské kolonii.", // Czech
"Việt Nam: Nhất thể hóa sẽ khác Trung Quốc?", // Vietnamese
"Σοβαρές ενστάσεις Κομισιόν για τον προϋπολογισμό της Ιταλίας", // Greek
"Решение Индии о покупке российских С-400 расценили как вызов США", // Russian
"הרופא שהציל נשים והנערה ששועבדה ע", // Hebrew,
"ترامب للملك سلمان: أنا جاد للغاية.. عليك دفع المزيد", // Arabic
"भारत की एस 400 मिसाइल के मुकाबले पाक की थाड, जानें कौन कितना ताकतवर", // Hindi
"ரெட் அலர்ட் எச்சரிக்கை; புதுச்சேரியில் நாளை அரசு விடுமுறை!", // Tamil
"'ఉత్తర్వులు అందే వరకు ఓటర్ల తుది జాబితాను వెబ్‌సైట్లో పెట్టవద్దు'", // Telugu
"台北》抹黑柯P失敗?朱學恒酸:姚文智氣pupu嗆大老闆", // Chinese
"วัดไทรตีระฆังเบาลงช่วงเข้าพรรษา เจ้าอาวาสเผยคนร้องเรียนรับผลกรรมแล้ว", // Thai
"九州北部の一部が暴風域に入りました(日直予報士 2018年10月06日) - 日本気象協会 tenki.jp", // Japanese
"법원 “다스 지분 처분권·수익권 모두 MB가 보유”", // Korean
]) {
const iter = seg.segment(text);
let segments = [];
let oldPos = -1;
for (let result = iter.next(); !result.done; result = iter.next()) {
const v = result.value;
assertTrue(["sep", "term"].includes(iter.breakType), iter.breakType);
assertEquals("string", typeof v.segment);
assertTrue(v.segment.length > 0);
segments.push(v.segment);
assertEquals("number", typeof v.position);
assertTrue(oldPos < v.position);
oldPos = v.position;
}
assertEquals(text, segments.join(''));
}
// 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.
// Flags: --harmony-intl-segmenter
const seg = new Intl.Segmenter([], {granularity: "word"})
for (const text of [
"Hello world!", // English
" Hello world! ", // English with space before/after
" Hello world? Foo bar!", // English
"Jedovatou mambu objevila žena v zahrádkářské kolonii.", // Czech
"Việt Nam: Nhất thể hóa sẽ khác Trung Quốc?", // Vietnamese
"Σοβαρές ενστάσεις Κομισιόν για τον προϋπολογισμό της Ιταλίας", // Greek
"Решение Индии о покупке российских С-400 расценили как вызов США", // Russian
"הרופא שהציל נשים והנערה ששועבדה ע", // Hebrew,
"ترامب للملك سلمان: أنا جاد للغاية.. عليك دفع المزيد", // Arabic
"भारत की एस 400 मिसाइल के मुकाबले पाक की थाड, जानें कौन कितना ताकतवर", // Hindi
"ரெட் அலர்ட் எச்சரிக்கை; புதுச்சேரியில் நாளை அரசு விடுமுறை!", // Tamil
"'ఉత్తర్వులు అందే వరకు ఓటర్ల తుది జాబితాను వెబ్‌సైట్లో పెట్టవద్దు'", // Telugu
"台北》抹黑柯P失敗?朱學恒酸:姚文智氣pupu嗆大老闆", // Chinese
"วัดไทรตีระฆังเบาลงช่วงเข้าพรรษา เจ้าอาวาสเผยคนร้องเรียนรับผลกรรมแล้ว", // Thai
"九州北部の一部が暴風域に入りました(日直予報士 2018年10月06日) - 日本気象協会 tenki.jp", // Japanese
"법원 “다스 지분 처분권·수익권 모두 MB가 보유”", // Korean
]) {
const iter = seg.segment(text);
let segments = [];
let oldPos = -1;
for (let result = iter.next(); !result.done; result = iter.next()) {
const v = result.value;
assertTrue(["word", "none"].includes(iter.breakType), iter.breakType);
assertEquals("string", typeof v.segment);
assertTrue(v.segment.length > 0);
segments.push(v.segment);
assertEquals("number", typeof v.position);
assertTrue(oldPos < v.position);
oldPos = v.position;
}
assertEquals(text, segments.join(''));
}
// 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.
// Flags: --harmony-intl-segmenter
assertEquals("function", typeof Intl.Segmenter.prototype.segment);
assertEquals(1, Intl.Segmenter.prototype.segment.length);
let seg = new Intl.Segmenter("en", {granularity: "word"})
let res;
// test with 0 args
assertDoesNotThrow(() => res = seg.segment())
// test with 1 arg
assertDoesNotThrow(() => res = seg.segment("hello"))
assertEquals("hello", res.next().value.segment);
// test with 2 args
assertDoesNotThrow(() => res = seg.segment("hello world"))
assertEquals("hello", res.next().value.segment);
// test with other types
assertDoesNotThrow(() => res = seg.segment(undefined))
assertEquals("undefined", res.next().value.segment);
assertDoesNotThrow(() => res = seg.segment(null))
assertEquals("null", res.next().value.segment);
assertDoesNotThrow(() => res = seg.segment(true))
assertEquals("true", res.next().value.segment);
assertDoesNotThrow(() => res = seg.segment(false))
assertEquals("false", res.next().value.segment);
assertDoesNotThrow(() => res = seg.segment(1234))
assertEquals("1234", res.next().value.segment);
assertDoesNotThrow(() => res = seg.segment(3.1415926))
assertEquals("3.1415926", res.next().value.segment);
assertDoesNotThrow(() => res = seg.segment(["hello","world"]))
assertEquals("hello", res.next().value.segment);
assertDoesNotThrow(() => res = seg.segment({k: 'v'}))
assertEquals("[", res.next().value.segment);
assertThrows(() => res = seg.segment(Symbol()), TypeError)
// 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.
// Flags: --harmony-intl-segmenter
// Test subclassing of Segmenter
class CustomSegmenter extends Intl.Segmenter {
constructor(locales, options) {
super(locales, options);
this.isCustom = true;
}
}
const seg = new CustomSegmenter("zh");
assertEquals(true, seg.isCustom, "Custom property");
assertEquals(Object.getPrototypeOf(seg), CustomSegmenter.prototype, "Prototype");
// 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.
// Flags: --harmony-intl-segmenter
assertEquals(
typeof Intl.Segmenter.supportedLocalesOf,
"function",
"Intl.Segmenter.supportedLocalesOf should be a function"
);
const undef = Intl.Segmenter.supportedLocalesOf();
assertEquals([], undef);
const empty = Intl.Segmenter.supportedLocalesOf([]);
assertEquals([], empty);
const strLocale = Intl.Segmenter.supportedLocalesOf("sr");
assertEquals("sr", strLocale[0]);
const multiLocale = ["sr-Thai-RS", "de", "zh-CN"];
assertEquals(multiLocale, Intl.Segmenter.supportedLocalesOf(multiLocale));
...@@ -15,20 +15,26 @@ features: [Intl.Segmenter] ...@@ -15,20 +15,26 @@ features: [Intl.Segmenter]
const defaultLocale = new Intl.Segmenter().resolvedOptions().locale; const defaultLocale = new Intl.Segmenter().resolvedOptions().locale;
const tests = [ const tests = [
[undefined, defaultLocale, "undefined"], [undefined, [defaultLocale], "undefined"],
["EN", "en", "Single value"], ["EN", ["en"], "Single value"],
[[], defaultLocale, "Empty array"], [[], [defaultLocale], "Empty array"],
[["en-GB-oed"], "en-GB", "Grandfathered"], [["sr"], ["sr"], "Single-element array"],
[["x-private"], defaultLocale, "Private", ["lookup"]], [["fr", "ar"], ["fr", "ar"], "Two-element array"],
[["en", "EN"], "en", "Duplicate value (canonical first)"], [["xyz", "ar"], ["ar"], "Two-element array with unknown code"],
[["EN", "en"], "en", "Duplicate value (canonical last)"], [["en-GB-oed"], ["en-GB"], "Grandfathered"],
[{ 0: "DE", length: 0 }, defaultLocale, "Object with zero length"], [["x-private"], [defaultLocale], "Private", ["lookup"]],
[{ 0: "DE", length: 1 }, "de", "Object with length"], [["en", "EN"], ["en"], "Duplicate value (canonical first)"],
[["EN", "en"], ["en"], "Duplicate value (canonical last)"],
[{ 0: "DE", length: 0 }, [defaultLocale], "Object with zero length"],
[{ 0: "DE", length: 1 }, ["de"], "Object with length"],
[{ 0: "ja", 1: "fr" }, [defaultLocale], "Object without length, indexed from 0"],
[{ 1: "ja", 2: "fr" }, [defaultLocale], "Object without length, indexed from 1"],
]; ];
for (const [locales, expected, name, matchers = ["best fit", "lookup"]] of tests) { for (const [locales, expected, name, matchers = ["best fit", "lookup"]] of tests) {
for (const localeMatcher of matchers) { for (const localeMatcher of matchers) {
const segmenter = new Intl.Segmenter(locales, { localeMatcher }); const segmenter = new Intl.Segmenter(locales, { localeMatcher });
assert.sameValue(segmenter.resolvedOptions().locale, expected, name); const actual = segmenter.resolvedOptions().locale;
assert(expected.includes(actual), `${name}: expected one of ${expected}, found ${actual}`);
} }
} }
...@@ -16,6 +16,7 @@ const invalidOptions = [ ...@@ -16,6 +16,7 @@ const invalidOptions = [
null, null,
1, 1,
"", "",
"standard",
"Grapheme", "Grapheme",
"GRAPHEME", "GRAPHEME",
"grapheme\0", "grapheme\0",
......
...@@ -17,6 +17,7 @@ const invalidOptions = [ ...@@ -17,6 +17,7 @@ const invalidOptions = [
null, null,
1, 1,
"", "",
"giant",
"Strict", "Strict",
"STRICT", "STRICT",
"strict\0", "strict\0",
......
...@@ -26,6 +26,7 @@ const optionsArguments = [ ...@@ -26,6 +26,7 @@ const optionsArguments = [
"test", "test",
7, 7,
Symbol(), Symbol(),
{},
]; ];
for (const options of optionsArguments) { for (const options of optionsArguments) {
......
...@@ -17,6 +17,7 @@ const optionsArguments = [ ...@@ -17,6 +17,7 @@ const optionsArguments = [
"test", "test",
7, 7,
Symbol(), Symbol(),
{},
]; ];
for (const options of optionsArguments) { for (const options of optionsArguments) {
......
// Copyright 2018 the V8 project authors, Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.Segmenter
description: Checks handling of valid values for the granularity option to the Segmenter constructor.
info: |
Intl.Segmenter ([ locales [ , options ]])
9. Let lineBreakStyle be ? GetOption(options, "lineBreakStyle", "string", « "strict", "normal", "loose" », "normal").
13. Let granularity be ? GetOption(options, "granularity", "string", « "grapheme", "word", "sentence", "line" », "grapheme").
14. Set segmenter.[[SegmenterGranularity]] to granularity.
15. If granularity is "line",
a. Set segmenter.[[SegmenterLineBreakStyle]] to r.[[lb]].
features: [Intl.Segmenter]
---*/
const lineBreakStyleOptions = ["strict", "normal", "loose"];
const granularityOptions = ["grapheme", "word", "sentence", "line"];
const combinations = [];
combinations.push([
{},
"grapheme",
undefined,
]);
for (const lineBreakStyle of lineBreakStyleOptions) {
combinations.push([
{ lineBreakStyle },
"grapheme",
undefined,
]);
}
for (const granularity of granularityOptions) {
combinations.push([
{ granularity },
granularity,
granularity === "line" ? "normal" : undefined,
]);
}
for (const lineBreakStyle of lineBreakStyleOptions) {
for (const granularity of granularityOptions) {
combinations.push([
{ granularity, lineBreakStyle },
granularity,
granularity === "line" ? lineBreakStyle : undefined,
]);
}
}
for (const [input, granularity, lineBreakStyle] of combinations) {
const segmenter = new Intl.Segmenter([], input);
const resolvedOptions = segmenter.resolvedOptions();
assert.sameValue(resolvedOptions.granularity, granularity);
assert.sameValue(resolvedOptions.lineBreakStyle, lineBreakStyle);
}
// Copyright 2018 the V8 project authors, Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.Segmenter.supportedLocalesOf
description: Checks handling of an empty locales argument to the supportedLocalesOf function.
info: |
Intl.Segmenter.supportedLocalesOf ( locales [, options ])
3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
includes: [compareArray.js]
features: [Intl.Segmenter]
---*/
assert.sameValue(typeof Intl.Segmenter.supportedLocalesOf, "function",
"Should support Intl.Segmenter.supportedLocalesOf.");
assert.compareArray(Intl.Segmenter.supportedLocalesOf(), []);
assert.compareArray(Intl.Segmenter.supportedLocalesOf([]), []);
// Copyright 2018 the V8 project authors, Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-Intl.Segmenter.supportedLocalesOf
description: Checks handling of specific locales arguments to the supportedLocalesOf function.
info: |
Intl.Segmenter.supportedLocalesOf ( locales [, options ])
3. Return ? SupportedLocales(availableLocales, requestedLocales, options).
includes: [compareArray.js]
locale: [sr, sr-Thai-RS, de, zh-CN]
features: [Intl.Segmenter]
---*/
assert.sameValue(typeof Intl.Segmenter.supportedLocalesOf, "function",
"Should support Intl.Segmenter.supportedLocalesOf.");
assert.compareArray(Intl.Segmenter.supportedLocalesOf("sr"), ["sr"]);
const multiLocale = ["sr-Thai-RS", "de", "zh-CN"];
assert.compareArray(Intl.Segmenter.supportedLocalesOf(multiLocale), multiLocale);
...@@ -30,12 +30,16 @@ for (const text of [ ...@@ -30,12 +30,16 @@ for (const text of [
]) { ]) {
const iter = seg.segment(text); const iter = seg.segment(text);
let segments = []; let segments = [];
let oldPos = -1;
for (let result = iter.next(); !result.done; result = iter.next()) { for (let result = iter.next(); !result.done; result = iter.next()) {
const v = result.value; const v = result.value;
assert.sameValue(undefined, v.breakType); assert.sameValue(undefined, v.breakType);
assert.sameValue("string", typeof v.segment); assert.sameValue("string", typeof v.segment);
assert(v.segment.length > 0); assert(v.segment.length > 0);
segments.push(v.segment); segments.push(v.segment);
assert.sameValue(typeof v.position, "number");
assert(oldPos < v.position);
oldPos = v.position;
} }
assert.sameValue(text, segments.join('')); assert.sameValue(text, segments.join(''));
} }
...@@ -30,12 +30,16 @@ for (const text of [ ...@@ -30,12 +30,16 @@ for (const text of [
]) { ]) {
const iter = seg.segment(text); const iter = seg.segment(text);
let segments = []; let segments = [];
let oldPos = -1;
for (let result = iter.next(); !result.done; result = iter.next()) { for (let result = iter.next(); !result.done; result = iter.next()) {
const v = result.value; const v = result.value;
assert(["soft", "hard"].includes(iter.breakType), iter.breakType); assert(["soft", "hard"].includes(iter.breakType), iter.breakType);
assert.sameValue("string", typeof v.segment); assert.sameValue("string", typeof v.segment);
assert(v.segment.length > 0); assert(v.segment.length > 0);
segments.push(v.segment); segments.push(v.segment);
} assert.sameValue(typeof v.position, "number");
assert.sameValue(text, segments.join('')); assert(oldPos < v.position);
oldPos = v.position;
}
assert.sameValue(text, segments.join(''));
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment