github.com/xyproto/u-root@v6.0.1-0.20200302025726-5528e0c77a3c+incompatible/cmds/core/elvish/edit/location/location_test.go (about) 1 package location 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/u-root/u-root/cmds/core/elvish/edit/eddefs" 8 "github.com/u-root/u-root/cmds/core/elvish/edit/ui" 9 "github.com/u-root/u-root/cmds/core/elvish/eval" 10 "github.com/u-root/u-root/cmds/core/elvish/eval/vals" 11 "github.com/u-root/u-root/cmds/core/elvish/store/storedefs" 12 ) 13 14 var ( 15 theLocation = newProvider([]storedefs.Dir{ 16 {"/pinned", pinnedScore}, 17 {"/src/github.com/elves/elvish", 300}, 18 {"/src/home/xyz", 233}, 19 {"/home/dir", 100}, 20 {"/foo/\nbar", 77}, 21 {"/usr/elves/elvish", 6}, 22 }, "/home", nil, eval.NewEvaler(), matchDirPatternBuiltin) 23 24 locationFilterTests = []eddefs.ListingProviderFilterTest{ 25 {"", []eddefs.ListingShown{ 26 {"*", ui.Unstyled("/pinned")}, 27 {"300", ui.Unstyled("/src/github.com/elves/elvish")}, 28 {"233", ui.Unstyled("/src/home/xyz")}, 29 {"100", ui.Unstyled("~/dir")}, // home is abbreviated 30 {"77", ui.Unstyled(`"/foo/\nbar"`)}, // special char is quoted 31 {"6", ui.Unstyled("/usr/elves/elvish")}}}, 32 {"/s", []eddefs.ListingShown{ 33 {"300", ui.Unstyled("/src/github.com/elves/elvish")}, 34 {"233", ui.Unstyled("/src/home/xyz")}, 35 {"6", ui.Unstyled("/usr/elves/elvish")}}}, 36 {"/e/e", []eddefs.ListingShown{ 37 {"300", ui.Unstyled("/src/github.com/elves/elvish")}, 38 {"6", ui.Unstyled("/usr/elves/elvish")}}}, 39 {"x", []eddefs.ListingShown{{"233", ui.Unstyled("/src/home/xyz")}}}, 40 // Matchers operate on the displayed text, not the actual path. 41 // 1. Home directory is abbreviated to ~, and is matched by ~, but not by 42 // the actual path. 43 {"~", []eddefs.ListingShown{{"100", ui.Unstyled("~/dir")}}}, 44 {"home", []eddefs.ListingShown{{"233", ui.Unstyled("/src/home/xyz")}}}, 45 // 2. Special characters are quoted, and are matched by the quoted form, 46 // not by the actual form. 47 {"\n", []eddefs.ListingShown{}}, 48 {"\\n", []eddefs.ListingShown{{"77", ui.Unstyled(`"/foo/\nbar"`)}}}, 49 } 50 51 locationWithPrefixMatcher = newProvider([]storedefs.Dir{ 52 {"/pinned", pinnedScore}, 53 {"/src/github.com/elves/elvish", 300}, 54 {"/src/home/xyz", 233}, 55 {"/home/dir", 100}, 56 {"/foo/\nbar", 77}, 57 {"/usr/elves/elvish", 6}, 58 }, 59 "/home", 60 nil, 61 eval.NewEvaler(), 62 eval.NewBuiltinFn("edit:location:test:match-prefix", matchPrefix)) 63 64 locationWithPrefixMatcherTests = []eddefs.ListingProviderFilterTest{ 65 {"", []eddefs.ListingShown{ 66 {"*", ui.Unstyled("/pinned")}, 67 {"300", ui.Unstyled("/src/github.com/elves/elvish")}, 68 {"233", ui.Unstyled("/src/home/xyz")}, 69 {"100", ui.Unstyled("~/dir")}, // home is abbreviated 70 {"77", ui.Unstyled(`"/foo/\nbar"`)}, // special char is quoted 71 {"6", ui.Unstyled("/usr/elves/elvish")}}}, 72 {"/src", []eddefs.ListingShown{ 73 {"300", ui.Unstyled("/src/github.com/elves/elvish")}, 74 {"233", ui.Unstyled("/src/home/xyz")}}}, 75 {"home", []eddefs.ListingShown{}}, 76 } 77 ) 78 79 func matchPrefix(fm *eval.Frame, opts eval.RawOptions, pattern string, inputs eval.Inputs) { 80 out := fm.OutputChan() 81 inputs(func(v interface{}) { 82 out <- vals.Bool(strings.HasPrefix(v.(string), pattern)) 83 }) 84 } 85 86 func TestProvider(t *testing.T) { 87 if err := eddefs.TestListingProviderFilter( 88 "theLocation", theLocation, locationFilterTests); err != nil { 89 t.Error(err) 90 } 91 if err := eddefs.TestListingProviderFilter( 92 "locationWithPrefixMatcher", locationWithPrefixMatcher, locationWithPrefixMatcherTests); err != nil { 93 t.Error(err) 94 } 95 }