github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/shell/history/vars_test.go (about) 1 package history 2 3 import ( 4 "testing" 5 6 "github.com/lmorg/murex/lang" 7 _ "github.com/lmorg/murex/lang/expressions" 8 "github.com/lmorg/murex/test/count" 9 "github.com/lmorg/murex/utils/readline" 10 ) 11 12 func newReadlineInstance() *readline.Instance { 13 rl := readline.NewInstance() 14 rl.History = NewTestHistory() 15 16 return rl 17 } 18 19 func test(function func(string, *readline.Instance) (string, error), t *testing.T, tests, expected []string, rl *readline.Instance) { 20 t.Helper() 21 count.Tests(t, len(tests)) 22 23 lang.InitEnv() 24 25 for i := range tests { 26 actual, err := function(tests[i], rl) 27 if actual != expected[i] { 28 t.Errorf("Output does not match expected in test %d:", i) 29 t.Logf(" Original: %s", tests[i]) 30 t.Logf(" Expected: %s", expected[i]) 31 t.Logf(" Actual: %s", actual) 32 t.Log(" eo bytes: ", []byte(expected[i])) 33 t.Log(" ao bytes: ", []byte(actual)) 34 t.Logf(" Error: %s", err) 35 } 36 } 37 } 38 func TestBangBang(t *testing.T) { 39 rl := newReadlineInstance() 40 41 tests := []string{ 42 "^!!", 43 } 44 45 expected := []string{ 46 "out the lazy dog", 47 } 48 49 test(expandHistBangBang, t, tests, expected, rl) 50 } 51 52 func TestHistPrefix(t *testing.T) { 53 rl := newReadlineInstance() 54 55 tests := []string{ 56 "^^out", 57 "^^fox", 58 } 59 60 expected := []string{ 61 "out the lazy dog", 62 "", 63 } 64 65 test(expandHistPrefix, t, tests, expected, rl) 66 } 67 68 func TestHistIndex(t *testing.T) { 69 rl := newReadlineInstance() 70 71 tests := []string{ 72 "^-1", 73 "^0", 74 "^1", 75 "^2", 76 "^3", 77 "^4", 78 } 79 80 expected := []string{ 81 "^-1", 82 "out the quick brown #fox", 83 "out jumped over", 84 "out the lazy dog", 85 "", 86 "", 87 } 88 89 test(expandHistIndex, t, tests, expected, rl) 90 } 91 92 func TestHistRegex(t *testing.T) { 93 rl := newReadlineInstance() 94 95 tests := []string{ 96 "^m/quick/", 97 "^m/over/", 98 "^m/dog/", 99 "^m/cat/", 100 } 101 102 expected := []string{ 103 "out the quick brown #fox", 104 "out jumped over", 105 "out the lazy dog", 106 "", 107 } 108 109 test(expandHistRegex, t, tests, expected, rl) 110 } 111 112 func TestHistHashTag(t *testing.T) { 113 rl := newReadlineInstance() 114 115 tests := []string{ 116 "^#f", 117 "^#fo", 118 "^#fox", 119 "^#over", 120 "^#dog", 121 "^#cat", 122 } 123 124 expected := []string{ 125 "", 126 "", 127 "out the quick brown ", 128 "", 129 "", 130 "", 131 } 132 133 test(expandHistHashtag, t, tests, expected, rl) 134 } 135 136 /*func TestHistAllPs(t *testing.T) { 137 rl := newReadlineInstance() 138 139 tests := []string{ 140 "^[-2][-2]", 141 "^[-2][-1]", 142 "^[-2][0]", 143 "^[-2][1]", 144 "^[-2][2]", 145 "^[-2][3]", 146 "^[-1][-2]", 147 "^[-1][-1]", 148 "^[-1][0]", 149 "^[-1][1]", 150 "^[-1][2]", 151 "^[-1][3]", 152 "^[0][-2]", 153 "^[0][-1]", 154 "^[0][0]", 155 "^[0][1]", 156 "^[0][2]", 157 "^[0][3]", 158 "^[1][-2]", 159 "^[1][-1]", 160 "^[1][0]", 161 "^[1][1]", 162 "^[1][2]", 163 "^[1][3]", 164 "^[2][-2]", 165 "^[2][-1]", 166 "^[2][0]", 167 "^[2][1]", 168 "^[2][2]", 169 "^[2][3]", 170 } 171 172 expected := []string{ 173 "", 174 "", 175 "", 176 "", 177 "", 178 "", 179 "", 180 "", 181 "", 182 "", 183 "", 184 "", 185 "", 186 "", 187 "", 188 "", 189 "", 190 "", 191 "", 192 "", 193 "", 194 "", 195 "", 196 "", 197 "", 198 "", 199 "", 200 "", 201 "", 202 "", 203 } 204 205 test(expandHistAllPs, t, tests, expected, rl) 206 }*/ 207 208 func TestHistParam(t *testing.T) { 209 rl := newReadlineInstance() 210 211 tests := []string{ 212 "^[-5]", 213 "^[-4]", 214 "^[-3]", 215 "^[-2]", 216 "^[-1]", 217 "^[-0]", 218 "^[1]", 219 "^[2]", 220 "^[3]", 221 "^[4]", 222 "^[5]", 223 } 224 225 expected := []string{ 226 "", 227 "out:", 228 "the", 229 "lazy", 230 "dog", 231 "out:", 232 "the", 233 "lazy", 234 "dog", 235 "", 236 "", 237 } 238 239 test(expandHistParam, t, tests, expected, rl) 240 } 241 242 func TestHistReplace(t *testing.T) { 243 rl := newReadlineInstance() 244 245 tests := []string{ 246 "out: the quick brown fox", 247 "^s/dog/cat/", 248 "out: the quick brown fox ^s/quick/slow/", 249 "out: the quick brown fox ^s/fox/wolf/", 250 "out: the quick brown fox ^s/out/err/", 251 } 252 253 expected := []string{ 254 "out: the quick brown fox", 255 "", 256 "out: the slow brown fox ", 257 "out: the quick brown wolf ", 258 "err: the quick brown fox ", 259 } 260 261 test(expandHistReplace, t, tests, expected, rl) 262 } 263 264 /*func TestHistRepParam(t *testing.T) { 265 rl := newReadlineInstance() 266 267 tests := []string{ 268 "out: the quick brown fox", 269 "^s-1/dog/cat/", 270 "out: the quick brown fox ^s0/quick/slow/", 271 272 } 273 274 expected := []string{ 275 "out: the quick brown fox", 276 "out: the lazy cat", 277 "out: the quick brown fox out: the slow brown fox", 278 } 279 280 test(expandHistRepParam, t, tests, expected, rl) 281 }*/