| 1 |
package comparison |
| 2 |
|
| 3 |
import ( |
| 4 |
"testing" |
| 5 |
|
| 6 |
"github.com/clipperhouse/displaywidth" |
| 7 |
"github.com/mattn/go-runewidth" |
| 8 |
) |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
func TestRegularEmojiAreAlwaysWidth2(t *testing.T) { |
| 13 |
|
| 14 |
|
| 15 |
emojis := []string{ |
| 16 |
"😀", "😁", "😂", "🤣", "😃", "😄", "😅", "😆", |
| 17 |
"🚀", "🎉", "🎊", "🎈", "🎁", "🎂", |
| 18 |
"👍", "👎", "👏", "🙏", |
| 19 |
"🔥", "💯", "✨", "⭐", |
| 20 |
} |
| 21 |
|
| 22 |
t.Log("Verifying that regular emojis are ALWAYS width 2:") |
| 23 |
t.Log("(Note: excludes emojis with VS16, which go-runewidth treats differently)") |
| 24 |
t.Log("") |
| 25 |
|
| 26 |
allPass := true |
| 27 |
for _, emoji := range emojis { |
| 28 |
|
| 29 |
dw := displaywidth.String(emoji) |
| 30 |
|
| 31 |
|
| 32 |
gr1 := (&runewidth.Condition{StrictEmojiNeutral: true}).StringWidth(emoji) |
| 33 |
gr2 := (&runewidth.Condition{StrictEmojiNeutral: false}).StringWidth(emoji) |
| 34 |
|
| 35 |
if dw != 2 || gr1 != 2 || gr2 != 2 { |
| 36 |
t.Errorf("%s: Expected width 2 in all cases, got displaywidth=%d, go-runewidth(strict=true)=%d, go-runewidth(strict=false)=%d", |
| 37 |
emoji, dw, gr1, gr2) |
| 38 |
allPass = false |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
if allPass { |
| 43 |
t.Log("✅ All regular emojis have width 2") |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
t.Log("") |
| 48 |
t.Log("Variation Selector Difference:") |
| 49 |
emojiWithVS16 := []string{"❤️", "✂️", "☺️"} |
| 50 |
for _, emoji := range emojiWithVS16 { |
| 51 |
dw := displaywidth.String(emoji) |
| 52 |
gr := runewidth.StringWidth(emoji) |
| 53 |
t.Logf(" %s: displaywidth=%d, go-runewidth=%d (go-runewidth treats VS16 as separate char)", emoji, dw, gr) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
func TestFlagsBehavior(t *testing.T) { |
| 59 |
flags := []string{ |
| 60 |
"🇺🇸", "🇯🇵", "🇬🇧", "🇫🇷", "🇩🇪", |
| 61 |
"🇨🇦", "🇦🇺", "🇧🇷", "🇮🇳", "🇨🇳", |
| 62 |
} |
| 63 |
|
| 64 |
t.Log("Flag behavior comparison:") |
| 65 |
t.Log("(displaywidth follows modern standards: flags are always width 2)") |
| 66 |
t.Log("") |
| 67 |
t.Log("Flag | displaywidth | go-runewidth (default) | go-runewidth (strict=false) | go-runewidth (strict=true)") |
| 68 |
t.Log("-----|-------------|------------------------|---------------------------|-------------------------") |
| 69 |
|
| 70 |
for _, flag := range flags { |
| 71 |
|
| 72 |
dw := displaywidth.String(flag) |
| 73 |
|
| 74 |
|
| 75 |
grDefault := runewidth.StringWidth(flag) |
| 76 |
gr1 := (&runewidth.Condition{StrictEmojiNeutral: false}).StringWidth(flag) |
| 77 |
gr2 := (&runewidth.Condition{StrictEmojiNeutral: true}).StringWidth(flag) |
| 78 |
|
| 79 |
t.Logf("%s | %d | %d | %d | %d", flag, dw, grDefault, gr1, gr2) |
| 80 |
|
| 81 |
|
| 82 |
if dw != 2 { |
| 83 |
t.Errorf("%s: displaywidth should always be 2, got %d", flag, dw) |
| 84 |
} |
| 85 |
|
| 86 |
|
| •87 |
if grDefault != 2 { |
| 88 |
t.Errorf("%s: go-runewidth default should be 2, got %d", flag, grDefault) |
| 89 |
} |
| •90 |
if gr1 != 2 { |
| 91 |
t.Errorf("%s: go-runewidth with strict=false should be 2, got %d", flag, gr1) |
| 92 |
} |
| •93 |
if gr2 != 2 { |
| 94 |
t.Errorf("%s: go-runewidth with strict=true should be 2, got %d", flag, gr2) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
t.Log("") |
| 99 |
t.Log("Summary:") |
| 100 |
t.Log("- displaywidth: flags are always width 2 (modern standard)") |
| 101 |
t.Log("- go-runewidth: flags are always width 1, regardless of StrictEmojiNeutral") |
| 102 |
} |