src

Go monorepo.
git clone git://code.dwrz.net/src
Log | Files | Refs

line_test.go (2066B)


      1 package line
      2 
      3 import (
      4 	"testing"
      5 )
      6 
      7 type GlyphIndexTest struct {
      8 	data   string
      9 	glyph  int
     10 	index  int
     11 	target int
     12 }
     13 
     14 func TestGlyphIndex(t *testing.T) {
     15 	var tests = []GlyphIndexTest{
     16 		{
     17 			data:   "",
     18 			index:  0,
     19 			glyph:  0,
     20 			target: 0,
     21 		},
     22 		{
     23 			data:   "\n",
     24 			index:  0,
     25 			glyph:  0,
     26 			target: 0,
     27 		},
     28 		{
     29 			data:   "\tšŸ¤”",
     30 			index:  0,
     31 			glyph:  0,
     32 			target: 0,
     33 		},
     34 		{
     35 			data:   "\tšŸ¤”",
     36 			index:  0,
     37 			glyph:  0,
     38 			target: 1,
     39 		},
     40 		{
     41 			data:   "\tšŸ¤”",
     42 			index:  1,
     43 			glyph:  8,
     44 			target: 8,
     45 		},
     46 		{
     47 			data:   "\tšŸ¤”**",
     48 			index:  5,  // \t = 0, šŸ¤” = 1-4, * = 5
     49 			glyph:  10, // \t = 0, šŸ¤” = 8, * = 10.
     50 			target: 10, // *
     51 		},
     52 		{
     53 			data:   "\tšŸ¤”*",
     54 			index:  6,  // \t = 0, šŸ¤” = 1-4, * = 5
     55 			glyph:  11, // \t = 0, šŸ¤” = 8, * = 10.
     56 			target: 11, // *
     57 		},
     58 
     59 		{
     60 			data:   "ä½ ę˜Æ谁ļ¼Ÿ",
     61 			index:  3, // ä½  size is 3 bytes.
     62 			glyph:  2, // ä½  width is 2.
     63 			target: 2, // ę˜Æ
     64 		},
     65 		{
     66 			data:   "ē£Øęµęˆé’ˆ",
     67 			index:  12, // 针
     68 			glyph:  8,  // 针
     69 			target: 8,  // Doesn't exist; return end of last rune.
     70 		},
     71 		{
     72 			data:   "你儽ļ¼",
     73 			index:  6, // ļ¼
     74 			glyph:  4, // ļ¼
     75 			target: 5,
     76 		},
     77 	}
     78 
     79 	for n, test := range tests {
     80 		i, g := New(test.data).FindGlyphIndex(test.target)
     81 		if i != test.index {
     82 			t.Errorf(
     83 				"#%d: expected index %d, but got %d",
     84 				n, test.index, i,
     85 			)
     86 		}
     87 		if g != test.glyph {
     88 			t.Errorf(
     89 				"#%d: expected glyph %d, but got %d",
     90 				n, test.glyph, g,
     91 			)
     92 		}
     93 	}
     94 }
     95 
     96 type WidthTest struct {
     97 	data     string
     98 	expected int
     99 }
    100 
    101 func TestWidth(t *testing.T) {
    102 	var tests = []WidthTest{
    103 		{
    104 			data:     "",
    105 			expected: 0,
    106 		},
    107 		{
    108 			data:     "\n",
    109 			expected: 0,
    110 		},
    111 		{
    112 			data:     "\t",
    113 			expected: 8,
    114 		},
    115 		{
    116 			data:     "Hello, World!",
    117 			expected: 13,
    118 		},
    119 		{
    120 			data:     "Hello, äø–ē•Œ!",
    121 			expected: 12,
    122 		},
    123 		{
    124 			data:     "šŸ’»šŸ’©",
    125 			expected: 4,
    126 		},
    127 	}
    128 
    129 	for _, test := range tests {
    130 		if w := New(test.data).Width(); w != test.expected {
    131 			t.Errorf(
    132 				"expected width %d, but got %d",
    133 				test.expected, w,
    134 			)
    135 		}
    136 	}
    137 }