github.com/elves/elvish@v0.15.0/pkg/edit/config_api_test.go (about) 1 package edit 2 3 import ( 4 "testing" 5 6 "github.com/elves/elvish/pkg/cli/term" 7 ) 8 9 func TestBeforeReadline(t *testing.T) { 10 f := setup(rc( 11 `called = 0`, 12 `edit:before-readline = [ { called = (+ $called 1) } ]`)) 13 defer f.Cleanup() 14 15 // Wait for UI to stabilize so that we can be sure that before-readline hooks 16 // have been called. 17 f.TestTTY(t, "~> ", term.DotHere) 18 19 testGlobal(t, f.Evaler, "called", 1.0) 20 } 21 22 func TestAfterReadline(t *testing.T) { 23 f := setup() 24 defer f.Cleanup() 25 evals(f.Evaler, 26 `called = 0`, 27 `called-with = ''`, 28 `edit:after-readline = [ 29 [code]{ called = (+ $called 1); called-with = $code } ]`) 30 31 // Wait for UI to stabilize so that we can be sure that after-readline hooks 32 // are *not* called. 33 f.TestTTY(t, "~> ", term.DotHere) 34 testGlobal(t, f.Evaler, "called", "0") 35 36 // Input "test code", press Enter and wait until the editor is done. 37 feedInput(f.TTYCtrl, "test code\n") 38 f.Wait() 39 40 testGlobals(t, f.Evaler, map[string]interface{}{ 41 "called": 1.0, 42 "called-with": "test code", 43 }) 44 } 45 46 func TestAddCmdFilters(t *testing.T) { 47 cases := []struct { 48 name string 49 rc string 50 input string 51 wantHistory []string 52 }{ 53 // TODO: Enable the following two tests once error output can 54 // be tested. 55 // { 56 // name: "non callable item", 57 // rc: "edit:add-cmd-filters = [$false]", 58 // input: "echo\n", 59 // wantHistory: []string{"echo"}, 60 // }, 61 // { 62 // name: "callback outputs nothing", 63 // rc: "edit:add-cmd-filters = [[_]{}]", 64 // input: "echo\n", 65 // wantHistory: []string{"echo"}, 66 // }, 67 { 68 name: "callback outputs true", 69 rc: "edit:add-cmd-filters = [[_]{ put $true }]", 70 input: "echo\n", 71 wantHistory: []string{"echo"}, 72 }, 73 { 74 name: "callback outputs false", 75 rc: "edit:add-cmd-filters = [[_]{ put $false }]", 76 input: "echo\n", 77 wantHistory: nil, 78 }, 79 { 80 name: "false-true chain", 81 rc: "edit:add-cmd-filters = [[_]{ put $false } [_]{ put $true }]", 82 input: "echo\n", 83 wantHistory: nil, 84 }, 85 { 86 name: "true-false chain", 87 rc: "edit:add-cmd-filters = [[_]{ put $true } [_]{ put $false }]", 88 input: "echo\n", 89 wantHistory: nil, 90 }, 91 { 92 name: "positive", 93 rc: "edit:add-cmd-filters = [[cmd]{ ==s $cmd echo }]", 94 input: "echo\n", 95 wantHistory: []string{"echo"}, 96 }, 97 { 98 name: "negative", 99 rc: "edit:add-cmd-filters = [[cmd]{ ==s $cmd echo }]", 100 input: "echo x\n", 101 wantHistory: nil, 102 }, 103 { 104 name: "default value", 105 rc: "", 106 input: " echo\n", 107 wantHistory: nil, 108 }, 109 } 110 111 for _, c := range cases { 112 t.Run(c.name, func(t *testing.T) { 113 f := setup(rc(c.rc)) 114 defer f.Cleanup() 115 116 feedInput(f.TTYCtrl, c.input) 117 f.Wait() 118 119 testCommands(t, f.Store, c.wantHistory...) 120 }) 121 } 122 } 123 124 func TestAddCmdFilters_SkipsRemainingOnFalse(t *testing.T) { 125 f := setup(rc( 126 `called = $false`, 127 `@edit:add-cmd-filters = [_]{ put $false } [_]{ called = $true; put $true }`, 128 )) 129 defer f.Cleanup() 130 131 feedInput(f.TTYCtrl, "echo\n") 132 f.Wait() 133 testCommands(t, f.Store) 134 testGlobal(t, f.Evaler, "called", false) 135 }