github.com/andrewhsu/cli/v2@v2.0.1-0.20210910131313-d4b4061f5b89/pkg/text/truncate_test.go (about)

     1  package text
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestTruncate(t *testing.T) {
     8  	type args struct {
     9  		max int
    10  		s   string
    11  	}
    12  	tests := []struct {
    13  		name string
    14  		args args
    15  		want string
    16  	}{
    17  		{
    18  			name: "Short enough",
    19  			args: args{
    20  				max: 5,
    21  				s:   "short",
    22  			},
    23  			want: "short",
    24  		},
    25  		{
    26  			name: "Too short",
    27  			args: args{
    28  				max: 4,
    29  				s:   "short",
    30  			},
    31  			want: "shor",
    32  		},
    33  		{
    34  			name: "Japanese",
    35  			args: args{
    36  				max: 11,
    37  				s:   "テストテストテストテスト",
    38  			},
    39  			want: "テストテ...",
    40  		},
    41  		{
    42  			name: "Japanese filled",
    43  			args: args{
    44  				max: 11,
    45  				s:   "aテストテストテストテスト",
    46  			},
    47  			want: "aテスト... ",
    48  		},
    49  		{
    50  			name: "Chinese",
    51  			args: args{
    52  				max: 11,
    53  				s:   "幫新舉報違章工廠新增編號",
    54  			},
    55  			want: "幫新舉報...",
    56  		},
    57  		{
    58  			name: "Chinese filled",
    59  			args: args{
    60  				max: 11,
    61  				s:   "a幫新舉報違章工廠新增編號",
    62  			},
    63  			want: "a幫新舉... ",
    64  		},
    65  		{
    66  			name: "Korean",
    67  			args: args{
    68  				max: 11,
    69  				s:   "프로젝트 내의",
    70  			},
    71  			want: "프로젝트...",
    72  		},
    73  		{
    74  			name: "Korean filled",
    75  			args: args{
    76  				max: 11,
    77  				s:   "a프로젝트 내의",
    78  			},
    79  			want: "a프로젝... ",
    80  		},
    81  		{
    82  			name: "Emoji",
    83  			args: args{
    84  				max: 11,
    85  				s:   "💡💡💡💡💡💡💡💡💡💡💡💡",
    86  			},
    87  			want: "💡💡💡💡...",
    88  		},
    89  		{
    90  			name: "Accented characters",
    91  			args: args{
    92  				max: 11,
    93  				s:   "é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́",
    94  			},
    95  			want: "é́́é́́é́́é́́é́́é́́é́́é́́...",
    96  		},
    97  		{
    98  			name: "Red accented characters",
    99  			args: args{
   100  				max: 11,
   101  				s:   "\x1b[0;31mé́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́é́́\x1b[0m",
   102  			},
   103  			want: "\x1b[0;31mé́́é́́é́́é́́é́́é́́é́́é́́...\x1b[0m",
   104  		},
   105  	}
   106  	for _, tt := range tests {
   107  		t.Run(tt.name, func(t *testing.T) {
   108  			if got := Truncate(tt.args.max, tt.args.s); got != tt.want {
   109  				t.Errorf("Truncate() = %q, want %q", got, tt.want)
   110  			}
   111  		})
   112  	}
   113  }
   114  
   115  func TestTruncateColumn(t *testing.T) {
   116  	type args struct {
   117  		max int
   118  		s   string
   119  	}
   120  	tests := []struct {
   121  		name string
   122  		args args
   123  		want string
   124  	}{
   125  		{
   126  			name: "exactly minimum width",
   127  			args: args{
   128  				max: 5,
   129  				s:   "short",
   130  			},
   131  			want: "short",
   132  		},
   133  		{
   134  			name: "exactly minimum width with new line",
   135  			args: args{
   136  				max: 5,
   137  				s:   "short\n",
   138  			},
   139  			want: "sh...",
   140  		},
   141  		{
   142  			name: "less than minimum width",
   143  			args: args{
   144  				max: 4,
   145  				s:   "short",
   146  			},
   147  			want: "shor",
   148  		},
   149  		{
   150  			name: "less than minimum width with new line",
   151  			args: args{
   152  				max: 4,
   153  				s:   "short\n",
   154  			},
   155  			want: "shor",
   156  		},
   157  		{
   158  			name: "first line of multiple is short enough",
   159  			args: args{
   160  				max: 80,
   161  				s:   "short\n\nthis is a new line",
   162  			},
   163  			want: "short...",
   164  		},
   165  		{
   166  			name: "using Windows line endings",
   167  			args: args{
   168  				max: 80,
   169  				s:   "short\r\n\r\nthis is a new line",
   170  			},
   171  			want: "short...",
   172  		},
   173  		{
   174  			name: "using older MacOS line endings",
   175  			args: args{
   176  				max: 80,
   177  				s:   "short\r\rthis is a new line",
   178  			},
   179  			want: "short...",
   180  		},
   181  	}
   182  	for _, tt := range tests {
   183  		t.Run(tt.name, func(t *testing.T) {
   184  			if got := TruncateColumn(tt.args.max, tt.args.s); got != tt.want {
   185  				t.Errorf("TruncateColumn() = %q, want %q", got, tt.want)
   186  			}
   187  		})
   188  	}
   189  }
   190  
   191  func TestDisplayWidth(t *testing.T) {
   192  	tests := []struct {
   193  		name string
   194  		text string
   195  		want int
   196  	}{
   197  		{
   198  			name: "check mark",
   199  			text: `✓`,
   200  			want: 1,
   201  		},
   202  		{
   203  			name: "bullet icon",
   204  			text: `•`,
   205  			want: 1,
   206  		},
   207  		{
   208  			name: "middle dot",
   209  			text: `·`,
   210  			want: 1,
   211  		},
   212  		{
   213  			name: "ellipsis",
   214  			text: `…`,
   215  			want: 1,
   216  		},
   217  		{
   218  			name: "right arrow",
   219  			text: `→`,
   220  			want: 1,
   221  		},
   222  		{
   223  			name: "smart double quotes",
   224  			text: `“”`,
   225  			want: 2,
   226  		},
   227  		{
   228  			name: "smart single quotes",
   229  			text: `‘’`,
   230  			want: 2,
   231  		},
   232  		{
   233  			name: "em dash",
   234  			text: `—`,
   235  			want: 1,
   236  		},
   237  		{
   238  			name: "en dash",
   239  			text: `–`,
   240  			want: 1,
   241  		},
   242  		{
   243  			name: "emoji",
   244  			text: `👍`,
   245  			want: 2,
   246  		},
   247  		{
   248  			name: "accent character",
   249  			text: `é́́`,
   250  			want: 1,
   251  		},
   252  		{
   253  			name: "color codes",
   254  			text: "\x1b[0;31mred\x1b[0m",
   255  			want: 3,
   256  		},
   257  	}
   258  	for _, tt := range tests {
   259  		t.Run(tt.name, func(t *testing.T) {
   260  			if got := DisplayWidth(tt.text); got != tt.want {
   261  				t.Errorf("DisplayWidth() = %v, want %v", got, tt.want)
   262  			}
   263  		})
   264  	}
   265  }