github.com/aretext/aretext@v1.3.0/cellwidth/cellwidth_test.go (about)

     1  package cellwidth
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestGraphemeClusterWidth(t *testing.T) {
    10  	testCases := []struct {
    11  		name          string
    12  		gc            []rune
    13  		offset        uint64
    14  		expectedWidth uint64
    15  	}{
    16  		{
    17  			name:          "empty",
    18  			gc:            []rune{},
    19  			expectedWidth: 0,
    20  		},
    21  		{
    22  			name:          "ascii printable",
    23  			gc:            []rune{'a'},
    24  			expectedWidth: 1,
    25  		},
    26  		{
    27  			name:          "line feed",
    28  			gc:            []rune{'\n'},
    29  			expectedWidth: 0,
    30  		},
    31  		{
    32  			name:          "carriage return and line feed",
    33  			gc:            []rune{'\r', '\n'},
    34  			expectedWidth: 0,
    35  		},
    36  		{
    37  			name:          "tab at start of line",
    38  			gc:            []rune{'\t'},
    39  			expectedWidth: 4,
    40  		},
    41  		{
    42  			name:          "tab at misaligned offset",
    43  			gc:            []rune{'\t'},
    44  			offset:        1,
    45  			expectedWidth: 3,
    46  		},
    47  		{
    48  			name:          "tab at aligned offset",
    49  			gc:            []rune{'\t'},
    50  			offset:        4,
    51  			expectedWidth: 4,
    52  		},
    53  		{
    54  			name:          "full width east-asian character",
    55  			gc:            []rune{'界'},
    56  			expectedWidth: 2,
    57  		},
    58  		{
    59  			name:          "combining accent mark",
    60  			gc:            []rune{'a', '\u0300'},
    61  			expectedWidth: 1,
    62  		},
    63  		{
    64  			name:          "trademark symbol",
    65  			gc:            []rune{'™'},
    66  			expectedWidth: 1,
    67  		},
    68  		{
    69  			name:          "left square double bracket",
    70  			gc:            []rune{'⟦'},
    71  			expectedWidth: 1,
    72  		},
    73  		{
    74  			name:          "right square double bracket",
    75  			gc:            []rune{'⟧'},
    76  			expectedWidth: 1,
    77  		},
    78  		{
    79  			name:          "left angle double bracket",
    80  			gc:            []rune{'⟪'},
    81  			expectedWidth: 1,
    82  		},
    83  		{
    84  			name:          "right angle double bracket",
    85  			gc:            []rune{'⟫'},
    86  			expectedWidth: 1,
    87  		},
    88  		{
    89  			name:          "thai",
    90  			gc:            []rune{3588, 3657, 3635},
    91  			expectedWidth: 2,
    92  		},
    93  		{
    94  			name:          "combining character (angstrom)",
    95  			gc:            []rune{'A', '\u030a'},
    96  			expectedWidth: 1,
    97  		},
    98  		{
    99  			name:          "emoticon (blowing a kiss)",
   100  			gc:            []rune{'\U0001f618'},
   101  			expectedWidth: 2,
   102  		},
   103  		{
   104  			name:          "emoji (airplane)",
   105  			gc:            []rune{'\u2708'},
   106  			expectedWidth: 1,
   107  		},
   108  		{
   109  			name:          "emoji (clover symbol)",
   110  			gc:            []rune{'\u2318'},
   111  			expectedWidth: 1,
   112  		},
   113  		{
   114  			name:          "enclosed exclamation",
   115  			gc:            []rune{'!', '\u20e3'},
   116  			expectedWidth: 1,
   117  		},
   118  		{
   119  			name:          "emoji zero-width joiner (female vampire)",
   120  			gc:            []rune{'\U0001f9db', '\u200d', '\u2640'},
   121  			expectedWidth: 2,
   122  		},
   123  		{
   124  			name:          "emoji zero-width joiner (family, woman+girl+girl)",
   125  			gc:            []rune{'\U0001f469', '\u200d', '\U0001f467', '\u200d', '\U0001f467'},
   126  			expectedWidth: 2,
   127  		},
   128  		{
   129  			name:          "region (usa)",
   130  			gc:            []rune{'\U0001f1fa', '\U0001f1f8'},
   131  			expectedWidth: 1,
   132  		},
   133  		{
   134  			name:          "emoji presentation selector",
   135  			gc:            []rune{'\u2139', '\ufe0f'},
   136  			expectedWidth: 2,
   137  		},
   138  	}
   139  
   140  	for _, tc := range testCases {
   141  		t.Run(tc.name, func(t *testing.T) {
   142  			width := GraphemeClusterWidth(tc.gc, tc.offset, 4)
   143  			assert.Equal(t, tc.expectedWidth, width)
   144  		})
   145  	}
   146  }