Segmenter.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. describe("errors", () => {
  2. test("called without new", () => {
  3. expect(() => {
  4. Intl.Segmenter();
  5. }).toThrowWithMessage(TypeError, "Intl.Segmenter constructor must be called with 'new'");
  6. });
  7. test("structurally invalid tag", () => {
  8. expect(() => {
  9. new Intl.Segmenter("root");
  10. }).toThrowWithMessage(RangeError, "root is not a structurally valid language tag");
  11. expect(() => {
  12. new Intl.Segmenter("en-");
  13. }).toThrowWithMessage(RangeError, "en- is not a structurally valid language tag");
  14. expect(() => {
  15. new Intl.Segmenter("Latn");
  16. }).toThrowWithMessage(RangeError, "Latn is not a structurally valid language tag");
  17. expect(() => {
  18. new Intl.Segmenter("en-u-aa-U-aa");
  19. }).toThrowWithMessage(RangeError, "en-u-aa-U-aa is not a structurally valid language tag");
  20. });
  21. test("options is an invalid type", () => {
  22. expect(() => {
  23. new Intl.Segmenter("en", null);
  24. }).toThrowWithMessage(TypeError, "Options is not an object");
  25. });
  26. test("localeMatcher option is invalid", () => {
  27. expect(() => {
  28. new Intl.Segmenter("en", { localeMatcher: "hello!" });
  29. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option localeMatcher");
  30. });
  31. test("granularity option is invalid", () => {
  32. expect(() => {
  33. new Intl.Segmenter("en", { granularity: "hello!" });
  34. }).toThrowWithMessage(RangeError, "hello! is not a valid value for option granularity");
  35. });
  36. });
  37. describe("normal behavior", () => {
  38. test("length is 0", () => {
  39. expect(Intl.Segmenter).toHaveLength(0);
  40. });
  41. test("all valid localeMatcher options", () => {
  42. ["lookup", "best fit"].forEach(localeMatcher => {
  43. expect(() => {
  44. new Intl.Segmenter("en", { localeMatcher: localeMatcher });
  45. }).not.toThrow();
  46. });
  47. });
  48. test("all valid granularity options", () => {
  49. ["grapheme", "word", "sentence"].forEach(granularity => {
  50. expect(() => {
  51. new Intl.Segmenter("en", { granularity: granularity });
  52. }).not.toThrow();
  53. });
  54. });
  55. });