| 1 | package textutil |
| 2 | |
| 3 | import "testing" |
| 4 | |
| 5 | func TestFitGraphemeBytesKeepsCluster(t *testing.T) { |
| 6 | cluster := "👨👩👧👦" |
| 7 | if got := FitGraphemeBytes(cluster+"!", len(cluster)); got != cluster { |
| 8 | t.Fatalf("FitGraphemeBytes() = %q, want %q", got, cluster) |
| 9 | } |
| 10 | if got := FitGraphemeBytes(cluster+"!", 1); got != cluster { |
| 11 | t.Fatalf("FitGraphemeBytes() split oversized first cluster: %q", got) |
| 12 | } |
| 13 | } |
| 14 | |
| 15 | func TestClipGraphemesCountsSuffixInsideBudget(t *testing.T) { |
| 16 | cluster := "👨👩👧👦" |
| 17 | got := ClipGraphemes("a"+cluster+"bc", 3, "…") |
| 18 | want := "a" + cluster + "…" |
| 19 | if got != want { |
| 20 | t.Fatalf("ClipGraphemes() = %q, want %q", got, want) |
| 21 | } |
| 22 | if got := ClipGraphemes("abc", 1, "…"); got != "a" { |
| 23 | t.Fatalf("ClipGraphemes() = %q, want first grapheme without suffix", got) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | func TestTruncateGraphemesAppendsSuffixOutsideBudget(t *testing.T) { |
| 28 | cluster := "👨👩👧👦" |
| 29 | got := TruncateGraphemes("a"+cluster+"bc", 2, "...") |
| 30 | want := "a" + cluster + "..." |
| 31 | if got != want { |
| 32 | t.Fatalf("TruncateGraphemes() = %q, want %q", got, want) |
| 33 | } |
| 34 | } |
| 35 |