github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/readline/unicide_test.go (about) 1 package readline 2 3 import ( 4 "testing" 5 6 "github.com/lmorg/murex/test/count" 7 ) 8 9 func TestSetRunePos(t *testing.T) { 10 tests := []struct { 11 Value string 12 Start int 13 RunePos int 14 ExpectedRPos int 15 ExpectedCPos int 16 }{ 17 { 18 Value: "hello world!", 19 Start: 5, 20 RunePos: 7, 21 ExpectedRPos: 7, 22 ExpectedCPos: 7, 23 }, 24 { 25 Value: "举手之劳就可以使办公室更加环保,比如,使用再生纸。", 26 Start: 2, 27 RunePos: 13, 28 ExpectedRPos: 13, 29 ExpectedCPos: 25, 30 }, 31 { 32 Value: "举手之劳就可以使办公室更加环保,比如,使用再生纸。", 33 Start: 2, 34 RunePos: 14, 35 ExpectedRPos: 14, 36 ExpectedCPos: 27, 37 }, 38 { 39 Value: "foo举手之劳就可以使办公室更加环保,比如,使用再生纸。", 40 Start: 2, 41 RunePos: 6, 42 ExpectedRPos: 6, 43 ExpectedCPos: 8, 44 }, 45 { 46 Value: "foo举手之劳就可以使办公室更加环保,比如,使用再生纸。", 47 Start: 2, 48 RunePos: 7, 49 ExpectedRPos: 7, 50 ExpectedCPos: 10, 51 }, 52 { 53 Value: "举手之劳就可以使办公室更加环保,比如,使用再生纸。", 54 Start: 2, 55 RunePos: 3, 56 ExpectedRPos: 3, 57 ExpectedCPos: 5, 58 }, 59 { 60 Value: "举手之劳就可以使办公室更加环保,比如,使用再生纸。", 61 Start: 2, 62 RunePos: 4, 63 ExpectedRPos: 4, 64 ExpectedCPos: 7, 65 }, 66 } 67 68 count.Tests(t, len(tests)) 69 70 for i, test := range tests { 71 u := new(UnicodeT) 72 u.Set(new(Instance), []rune(test.Value)) 73 u.SetRunePos(test.Start) 74 u.SetRunePos(test.RunePos) 75 rPos := u.RunePos() 76 cPos := u.CellPos() 77 78 if rPos != test.ExpectedRPos || cPos != test.ExpectedCPos { 79 t.Errorf("Unexpected position in test %d", i) 80 t.Logf(" Value: '%s'", test.Value) 81 t.Logf(" Start: %d", test.Start) 82 t.Logf(" SetRunePos: %d", test.RunePos) 83 t.Logf(" exp rPos: %d", test.ExpectedRPos) 84 t.Logf(" act rPos: %d", rPos) 85 t.Logf(" exp cPos: %d", test.ExpectedCPos) 86 t.Logf(" act cPos: %d", cPos) 87 } 88 } 89 } 90 91 func TestSetCellPos(t *testing.T) { 92 tests := []struct { 93 Value string 94 Start int 95 CellPos int 96 ExpectedRPos int 97 ExpectedCPos int 98 }{ 99 { 100 Value: "hello world!", 101 Start: 5, 102 CellPos: 7, 103 ExpectedRPos: 7, 104 ExpectedCPos: 7, 105 }, 106 { 107 Value: "举手之劳就可以使办公室更加环保,比如,使用再生纸。", 108 Start: 2, 109 CellPos: 25, 110 ExpectedRPos: 13, 111 ExpectedCPos: 25, 112 }, 113 { 114 Value: "举手之劳就可以使办公室更加环保,比如,使用再生纸。", 115 Start: 2, 116 CellPos: 26, 117 ExpectedRPos: 13, 118 ExpectedCPos: 25, 119 }, 120 { 121 Value: "foo举手之劳就可以使办公室更加环保,比如,使用再生纸。", 122 Start: 2, 123 CellPos: 8, 124 ExpectedRPos: 6, 125 ExpectedCPos: 8, 126 }, 127 { 128 Value: "foo举手之劳就可以使办公室更加环保,比如,使用再生纸。", 129 Start: 2, 130 CellPos: 9, 131 ExpectedRPos: 6, 132 ExpectedCPos: 8, 133 }, 134 { 135 Value: "举手之劳就可以使办公室更加环保,比如,使用再生纸。", 136 Start: 2, 137 CellPos: 5, 138 ExpectedRPos: 3, 139 ExpectedCPos: 5, 140 }, 141 { 142 Value: "举手之劳就可以使办公室更加环保,比如,使用再生纸。", 143 Start: 2, 144 CellPos: 6, 145 ExpectedRPos: 3, 146 ExpectedCPos: 5, 147 }, 148 } 149 150 count.Tests(t, len(tests)) 151 152 for i, test := range tests { 153 u := new(UnicodeT) 154 u.Set(new(Instance), []rune(test.Value)) 155 u.SetRunePos(test.Start) 156 u.SetCellPos(test.CellPos) 157 rPos := u.RunePos() 158 cPos := u.CellPos() 159 160 if rPos != test.ExpectedRPos || cPos != test.ExpectedCPos { 161 t.Errorf("Unexpected position in test %d", i) 162 t.Logf(" Value: '%s'", test.Value) 163 t.Logf(" Start: %d", test.Start) 164 t.Logf(" SetCellPos: %d", test.CellPos) 165 t.Logf(" exp rPos: %d", test.ExpectedRPos) 166 t.Logf(" act rPos: %d", rPos) 167 t.Logf(" exp cPos: %d", test.ExpectedCPos) 168 t.Logf(" act cPos: %d", cPos) 169 } 170 } 171 }