@@ -11,6 +11,30 @@ import (
1111 "github.com/stretchr/testify/assert"
1212)
1313
14+ func TestEllipsisGuessDisplayWidth (t * testing.T ) {
15+ cases := []struct {
16+ r string
17+ want int
18+ }{
19+ {r : "a" , want : 1 },
20+ {r : "é" , want : 1 },
21+ {r : "测" , want : 2 },
22+ {r : "⚽" , want : 2 },
23+ {r : "☁️" , want : 3 }, // 2 runes, it has a mark
24+ {r : "\u200B " , want : 1 }, // ZWSP
25+ {r : "\u3000 " , want : 2 }, // ideographic space
26+ }
27+ for _ , c := range cases {
28+ t .Run (c .r , func (t * testing.T ) {
29+ w := 0
30+ for _ , r := range c .r {
31+ w += ellipsisGuessDisplayWidth (r )
32+ }
33+ assert .Equal (t , c .want , w , "hex=% x" , []byte (c .r ))
34+ })
35+ }
36+ }
37+
1438func TestEllipsisString (t * testing.T ) {
1539 cases := []struct {
1640 limit int
@@ -37,6 +61,15 @@ func TestEllipsisString(t *testing.T) {
3761 {limit : 7 , input : "测试文本" , left : "测试…" , right : "…文本" },
3862 {limit : 8 , input : "测试文本" , left : "测试文本" , right : "" },
3963 {limit : 9 , input : "测试文本" , left : "测试文本" , right : "" },
64+
65+ {limit : 6 , input : "测试abc" , left : "测…" , right : "…试abc" },
66+ {limit : 7 , input : "测试abc" , left : "测试abc" , right : "" }, // exactly 7-width
67+ {limit : 8 , input : "测试abc" , left : "测试abc" , right : "" },
68+
69+ {limit : 7 , input : "测abc试啊" , left : "测ab…" , right : "…c试啊" },
70+ {limit : 8 , input : "测abc试啊" , left : "测abc…" , right : "…试啊" },
71+ {limit : 9 , input : "测abc试啊" , left : "测abc试啊" , right : "" }, // exactly 9-width
72+ {limit : 10 , input : "测abc试啊" , left : "测abc试啊" , right : "" },
4073 }
4174 for _ , c := range cases {
4275 t .Run (fmt .Sprintf ("%s(%d)" , c .input , c .limit ), func (t * testing.T ) {
0 commit comments