options_test.go
This file
Path
src/options_test.go
448 lines · 12.4 KiB · Go
Last changed by
Add support for text styling using --colorJunegunn Choibefore adoption11841f6
History of this fileTake it
1package fzf23import (4 "fmt"5 "io/ioutil"6 "testing"78 "github.com/junegunn/fzf/src/tui"9)1011func TestDelimiterRegex(t *testing.T) {12 // Valid regex13 delim := delimiterRegexp(".")14 if delim.regex == nil || delim.str != nil {15 t.Error(delim)16 }17 // Broken regex -> string18 delim = delimiterRegexp("[0-9")19 if delim.regex != nil || *delim.str != "[0-9" {20 t.Error(delim)21 }22 // Valid regex23 delim = delimiterRegexp("[0-9]")24 if delim.regex.String() != "[0-9]" || delim.str != nil {25 t.Error(delim)26 }27 // Tab character28 delim = delimiterRegexp("\t")29 if delim.regex != nil || *delim.str != "\t" {30 t.Error(delim)31 }32 // Tab expression33 delim = delimiterRegexp("\\t")34 if delim.regex != nil || *delim.str != "\t" {35 t.Error(delim)36 }37 // Tabs -> regex38 delim = delimiterRegexp("\t+")39 if delim.regex == nil || delim.str != nil {40 t.Error(delim)41 }42}4344func TestDelimiterRegexString(t *testing.T) {45 delim := delimiterRegexp("*")46 tokens := Tokenize("-*--*---**---", delim)47 if delim.regex != nil ||48 tokens[0].text.ToString() != "-*" ||49 tokens[1].text.ToString() != "--*" ||50 tokens[2].text.ToString() != "---*" ||51 tokens[3].text.ToString() != "*" ||52 tokens[4].text.ToString() != "---" {53 t.Errorf("%s %v %d", delim, tokens, len(tokens))54 }55}5657func TestDelimiterRegexRegex(t *testing.T) {58 delim := delimiterRegexp("--\\*")59 tokens := Tokenize("-*--*---**---", delim)60 if delim.str != nil ||61 tokens[0].text.ToString() != "-*--*" ||62 tokens[1].text.ToString() != "---*" ||63 tokens[2].text.ToString() != "*---" {64 t.Errorf("%s %d", tokens, len(tokens))65 }66}6768func TestSplitNth(t *testing.T) {69 {70 ranges := splitNth("..")71 if len(ranges) != 1 ||72 ranges[0].begin != rangeEllipsis ||73 ranges[0].end != rangeEllipsis {74 t.Errorf("%v", ranges)75 }76 }77 {78 ranges := splitNth("..3,1..,2..3,4..-1,-3..-2,..,2,-2,2..-2,1..-1")79 if len(ranges) != 10 ||80 ranges[0].begin != rangeEllipsis || ranges[0].end != 3 ||81 ranges[1].begin != rangeEllipsis || ranges[1].end != rangeEllipsis ||82 ranges[2].begin != 2 || ranges[2].end != 3 ||83 ranges[3].begin != 4 || ranges[3].end != rangeEllipsis ||84 ranges[4].begin != -3 || ranges[4].end != -2 ||85 ranges[5].begin != rangeEllipsis || ranges[5].end != rangeEllipsis ||86 ranges[6].begin != 2 || ranges[6].end != 2 ||87 ranges[7].begin != -2 || ranges[7].end != -2 ||88 ranges[8].begin != 2 || ranges[8].end != -2 ||89 ranges[9].begin != rangeEllipsis || ranges[9].end != rangeEllipsis {90 t.Errorf("%v", ranges)91 }92 }93}9495func TestIrrelevantNth(t *testing.T) {96 {97 opts := defaultOptions()98 words := []string{"--nth", "..", "-x"}99 parseOptions(opts, words)100 postProcessOptions(opts)101 if len(opts.Nth) != 0 {102 t.Errorf("nth should be empty: %v", opts.Nth)103 }104 }105 for _, words := range [][]string{[]string{"--nth", "..,3", "+x"}, []string{"--nth", "3,1..", "+x"}, []string{"--nth", "..-1,1", "+x"}} {106 {107 opts := defaultOptions()108 parseOptions(opts, words)109 postProcessOptions(opts)110 if len(opts.Nth) != 0 {111 t.Errorf("nth should be empty: %v", opts.Nth)112 }113 }114 {115 opts := defaultOptions()116 words = append(words, "-x")117 parseOptions(opts, words)118 postProcessOptions(opts)119 if len(opts.Nth) != 2 {120 t.Errorf("nth should not be empty: %v", opts.Nth)121 }122 }123 }124}125126func TestParseKeys(t *testing.T) {127 pairs := parseKeyChords("ctrl-z,alt-z,f2,@,Alt-a,!,ctrl-G,J,g,ctrl-alt-a,ALT-enter,alt-SPACE", "")128 check := func(i int, s string) {129 if pairs[i] != s {130 t.Errorf("%s != %s", pairs[i], s)131 }132 }133 if len(pairs) != 12 {134 t.Error(12)135 }136 check(tui.CtrlZ, "ctrl-z")137 check(tui.AltZ, "alt-z")138 check(tui.F2, "f2")139 check(tui.AltZ+'@', "@")140 check(tui.AltA, "Alt-a")141 check(tui.AltZ+'!', "!")142 check(tui.CtrlA+'g'-'a', "ctrl-G")143 check(tui.AltZ+'J', "J")144 check(tui.AltZ+'g', "g")145 check(tui.CtrlAltA, "ctrl-alt-a")146 check(tui.CtrlAltM, "ALT-enter")147 check(tui.AltSpace, "alt-SPACE")148149 // Synonyms150 pairs = parseKeyChords("enter,Return,space,tab,btab,esc,up,down,left,right", "")151 if len(pairs) != 9 {152 t.Error(9)153 }154 check(tui.CtrlM, "Return")155 check(tui.AltZ+' ', "space")156 check(tui.Tab, "tab")157 check(tui.BTab, "btab")158 check(tui.ESC, "esc")159 check(tui.Up, "up")160 check(tui.Down, "down")161 check(tui.Left, "left")162 check(tui.Right, "right")163164 pairs = parseKeyChords("Tab,Ctrl-I,PgUp,page-up,pgdn,Page-Down,Home,End,Alt-BS,Alt-BSpace,shift-left,shift-right,btab,shift-tab,return,Enter,bspace", "")165 if len(pairs) != 11 {166 t.Error(11)167 }168 check(tui.Tab, "Ctrl-I")169 check(tui.PgUp, "page-up")170 check(tui.PgDn, "Page-Down")171 check(tui.Home, "Home")172 check(tui.End, "End")173 check(tui.AltBS, "Alt-BSpace")174 check(tui.SLeft, "shift-left")175 check(tui.SRight, "shift-right")176 check(tui.BTab, "shift-tab")177 check(tui.CtrlM, "Enter")178 check(tui.BSpace, "bspace")179}180181func TestParseKeysWithComma(t *testing.T) {182 checkN := func(a int, b int) {183 if a != b {184 t.Errorf("%d != %d", a, b)185 }186 }187 check := func(pairs map[int]string, i int, s string) {188 if pairs[i] != s {189 t.Errorf("%s != %s", pairs[i], s)190 }191 }192193 pairs := parseKeyChords(",", "")194 checkN(len(pairs), 1)195 check(pairs, tui.AltZ+',', ",")196197 pairs = parseKeyChords(",,a,b", "")198 checkN(len(pairs), 3)199 check(pairs, tui.AltZ+'a', "a")200 check(pairs, tui.AltZ+'b', "b")201 check(pairs, tui.AltZ+',', ",")202203 pairs = parseKeyChords("a,b,,", "")204 checkN(len(pairs), 3)205 check(pairs, tui.AltZ+'a', "a")206 check(pairs, tui.AltZ+'b', "b")207 check(pairs, tui.AltZ+',', ",")208209 pairs = parseKeyChords("a,,,b", "")210 checkN(len(pairs), 3)211 check(pairs, tui.AltZ+'a', "a")212 check(pairs, tui.AltZ+'b', "b")213 check(pairs, tui.AltZ+',', ",")214215 pairs = parseKeyChords("a,,,b,c", "")216 checkN(len(pairs), 4)217 check(pairs, tui.AltZ+'a', "a")218 check(pairs, tui.AltZ+'b', "b")219 check(pairs, tui.AltZ+'c', "c")220 check(pairs, tui.AltZ+',', ",")221222 pairs = parseKeyChords(",,,", "")223 checkN(len(pairs), 1)224 check(pairs, tui.AltZ+',', ",")225}226227func TestBind(t *testing.T) {228 keymap := defaultKeymap()229 check := func(keyName int, arg1 string, types ...actionType) {230 if len(keymap[keyName]) != len(types) {231 t.Errorf("invalid number of actions (%d != %d)", len(types), len(keymap[keyName]))232 return233 }234 for idx, action := range keymap[keyName] {235 if types[idx] != action.t {236 t.Errorf("invalid action type (%d != %d)", types[idx], action.t)237 }238 }239 if len(arg1) > 0 && keymap[keyName][0].a != arg1 {240 t.Errorf("invalid action argument: (%s != %s)", arg1, keymap[keyName][0].a)241 }242 }243 check(tui.CtrlA, "", actBeginningOfLine)244 parseKeymap(keymap,245 "ctrl-a:kill-line,ctrl-b:toggle-sort+up+down,c:page-up,alt-z:page-down,"+246 "f1:execute(ls {+})+abort+execute(echo {+})+select-all,f2:execute/echo {}, {}, {}/,f3:execute[echo '({})'],f4:execute;less {};,"+247 "alt-a:execute-Multi@echo (,),[,],/,:,;,%,{}@,alt-b:execute;echo (,),[,],/,:,@,%,{};,"+248 "x:Execute(foo+bar),X:execute/bar+baz/"+249 ",f1:+top,f1:+top"+250 ",,:abort,::accept,+:execute:++\nfoobar,Y:execute(baz)+up")251 check(tui.CtrlA, "", actKillLine)252 check(tui.CtrlB, "", actToggleSort, actUp, actDown)253 check(tui.AltZ+'c', "", actPageUp)254 check(tui.AltZ+',', "", actAbort)255 check(tui.AltZ+':', "", actAccept)256 check(tui.AltZ, "", actPageDown)257 check(tui.F1, "ls {+}", actExecute, actAbort, actExecute, actSelectAll, actTop, actTop)258 check(tui.F2, "echo {}, {}, {}", actExecute)259 check(tui.F3, "echo '({})'", actExecute)260 check(tui.F4, "less {}", actExecute)261 check(tui.AltZ+'x', "foo+bar", actExecute)262 check(tui.AltZ+'X', "bar+baz", actExecute)263 check(tui.AltA, "echo (,),[,],/,:,;,%,{}", actExecuteMulti)264 check(tui.AltB, "echo (,),[,],/,:,@,%,{}", actExecute)265 check(tui.AltZ+'+', "++\nfoobar,Y:execute(baz)+up", actExecute)266267 for idx, char := range []rune{'~', '!', '@', '#', '$', '%', '^', '&', '*', '|', ';', '/'} {268 parseKeymap(keymap, fmt.Sprintf("%d:execute%cfoobar%c", idx%10, char, char))269 check(tui.AltZ+int([]rune(fmt.Sprintf("%d", idx%10))[0]), "foobar", actExecute)270 }271272 parseKeymap(keymap, "f1:abort")273 check(tui.F1, "", actAbort)274}275276func TestColorSpec(t *testing.T) {277 theme := tui.Dark256278 dark := parseTheme(theme, "dark")279 if *dark != *theme {280 t.Errorf("colors should be equivalent")281 }282 if dark == theme {283 t.Errorf("point should not be equivalent")284 }285286 light := parseTheme(theme, "dark,light")287 if *light == *theme {288 t.Errorf("should not be equivalent")289 }290 if *light != *tui.Light256 {291 t.Errorf("colors should be equivalent")292 }293 if light == theme {294 t.Errorf("point should not be equivalent")295 }296297 customized := parseTheme(theme, "fg:231,bg:232")298 if customized.Fg.Color != 231 || customized.Bg.Color != 232 {299 t.Errorf("color not customized")300 }301 if *tui.Dark256 == *customized {302 t.Errorf("colors should not be equivalent")303 }304 customized.Fg = tui.Dark256.Fg305 customized.Bg = tui.Dark256.Bg306 if *tui.Dark256 != *customized {307 t.Errorf("colors should now be equivalent: %v, %v", tui.Dark256, customized)308 }309310 customized = parseTheme(theme, "fg:231,dark,bg:232")311 if customized.Fg != tui.Dark256.Fg || customized.Bg == tui.Dark256.Bg {312 t.Errorf("color not customized")313 }314}315316func TestDefaultCtrlNP(t *testing.T) {317 check := func(words []string, key int, expected actionType) {318 opts := defaultOptions()319 parseOptions(opts, words)320 postProcessOptions(opts)321 if opts.Keymap[key][0].t != expected {322 t.Error()323 }324 }325 check([]string{}, tui.CtrlN, actDown)326 check([]string{}, tui.CtrlP, actUp)327328 check([]string{"--bind=ctrl-n:accept"}, tui.CtrlN, actAccept)329 check([]string{"--bind=ctrl-p:accept"}, tui.CtrlP, actAccept)330331 f, _ := ioutil.TempFile("", "fzf-history")332 f.Close()333 hist := "--history=" + f.Name()334 check([]string{hist}, tui.CtrlN, actNextHistory)335 check([]string{hist}, tui.CtrlP, actPreviousHistory)336337 check([]string{hist, "--bind=ctrl-n:accept"}, tui.CtrlN, actAccept)338 check([]string{hist, "--bind=ctrl-n:accept"}, tui.CtrlP, actPreviousHistory)339340 check([]string{hist, "--bind=ctrl-p:accept"}, tui.CtrlN, actNextHistory)341 check([]string{hist, "--bind=ctrl-p:accept"}, tui.CtrlP, actAccept)342}343344func optsFor(words ...string) *Options {345 opts := defaultOptions()346 parseOptions(opts, words)347 postProcessOptions(opts)348 return opts349}350351func TestToggle(t *testing.T) {352 opts := optsFor()353 if opts.ToggleSort {354 t.Error()355 }356357 opts = optsFor("--bind=a:toggle-sort")358 if !opts.ToggleSort {359 t.Error()360 }361362 opts = optsFor("--bind=a:toggle-sort", "--bind=a:up")363 if opts.ToggleSort {364 t.Error()365 }366}367368func TestPreviewOpts(t *testing.T) {369 opts := optsFor()370 if !(opts.Preview.command == "" &&371 opts.Preview.hidden == false &&372 opts.Preview.wrap == false &&373 opts.Preview.position == posRight &&374 opts.Preview.size.percent == true &&375 opts.Preview.size.size == 50) {376 t.Error()377 }378 opts = optsFor("--preview", "cat {}", "--preview-window=left:15:hidden:wrap:+{1}-/2")379 if !(opts.Preview.command == "cat {}" &&380 opts.Preview.hidden == true &&381 opts.Preview.wrap == true &&382 opts.Preview.position == posLeft &&383 opts.Preview.scroll == "{1}-/2" &&384 opts.Preview.size.percent == false &&385 opts.Preview.size.size == 15) {386 t.Error(opts.Preview)387 }388 opts = optsFor("--preview-window=up:15:wrap:hidden:+{1}-/2", "--preview-window=down", "--preview-window=cycle")389 if !(opts.Preview.command == "" &&390 opts.Preview.hidden == true &&391 opts.Preview.wrap == true &&392 opts.Preview.cycle == true &&393 opts.Preview.position == posDown &&394 opts.Preview.scroll == "{1}-/2" &&395 opts.Preview.size.percent == false &&396 opts.Preview.size.size == 15) {397 t.Error(opts.Preview.size.size)398 }399 opts = optsFor("--preview-window=up:15:wrap:hidden")400 if !(opts.Preview.command == "" &&401 opts.Preview.hidden == true &&402 opts.Preview.wrap == true &&403 opts.Preview.position == posUp &&404 opts.Preview.size.percent == false &&405 opts.Preview.size.size == 15) {406 t.Error(opts.Preview)407 }408 opts = optsFor("--preview=foo", "--preview-window=up", "--preview-window=default:70%")409 if !(opts.Preview.command == "foo" &&410 opts.Preview.position == posRight &&411 opts.Preview.size.percent == true &&412 opts.Preview.size.size == 70) {413 t.Error(opts.Preview)414 }415}416417func TestAdditiveExpect(t *testing.T) {418 opts := optsFor("--expect=a", "--expect", "b", "--expect=c")419 if len(opts.Expect) != 3 {420 t.Error(opts.Expect)421 }422}423424func TestValidateSign(t *testing.T) {425 testCases := []struct {426 inputSign string427 isValid bool428 }{429 {"> ", true},430 {"아", true},431 {"😀", true},432 {"", false},433 {">>>", false},434 {"\n", false},435 {"\t", false},436 }437438 for _, testCase := range testCases {439 err := validateSign(testCase.inputSign, "")440 if testCase.isValid && err != nil {441 t.Errorf("Input sign `%s` caused error", testCase.inputSign)442 }443444 if !testCase.isValid && err == nil {445 t.Errorf("Input sign `%s` did not cause error", testCase.inputSign)446 }447 }448}