summaryrefslogtreecommitdiff
path: root/match
diff options
context:
space:
mode:
authorEyal Posener <[email protected]>2017-05-07 19:53:55 +0300
committerEyal Posener <[email protected]>2017-05-07 19:57:41 +0300
commit6fb4875efa1f536813da29972a0c3250bde8b5eb (patch)
treec7ee97d75ed8d1d6c734256e3ff789402571bcca /match
parente8f6dfad584cb1e3082bc7ea82f8c0551dd944b3 (diff)
Move match to a separate package
Diffstat (limited to 'match')
-rw-r--r--match/file.go30
-rw-r--r--match/match.go9
-rw-r--r--match/match_test.go114
-rw-r--r--match/prefix.go15
4 files changed, 168 insertions, 0 deletions
diff --git a/match/file.go b/match/file.go
new file mode 100644
index 0000000..c972ce0
--- /dev/null
+++ b/match/file.go
@@ -0,0 +1,30 @@
+package match
+
+import (
+ "path/filepath"
+ "strings"
+)
+
+// File is a file name Matcher, if the last word can prefix the
+// File path, there is a possible match
+type File string
+
+func (a File) String() string {
+ return string(a)
+}
+
+// Match returns true if prefix's abs path prefixes a's abs path
+func (a File) Match(prefix string) bool {
+ full, err := filepath.Abs(string(a))
+ if err != nil {
+ return false
+ }
+ prefixFull, err := filepath.Abs(prefix)
+ if err != nil {
+ return false
+ }
+
+ // if the file has the prefix as prefix,
+ // but we don't want to show too many files, so, if it is in a deeper directory - omit it.
+ return strings.HasPrefix(full, prefixFull) && (full == prefixFull || !strings.Contains(full[len(prefixFull)+1:], "/"))
+}
diff --git a/match/match.go b/match/match.go
new file mode 100644
index 0000000..ffd7eb8
--- /dev/null
+++ b/match/match.go
@@ -0,0 +1,9 @@
+package match
+
+// Matcher matches itself to a string
+// it is used for comparing a given argument to the last typed
+// word, and see if it is a possible auto complete option.
+type Matcher interface {
+ String() string
+ Match(prefix string) bool
+}
diff --git a/match/match_test.go b/match/match_test.go
new file mode 100644
index 0000000..f9afd46
--- /dev/null
+++ b/match/match_test.go
@@ -0,0 +1,114 @@
+package match
+
+import (
+ "os"
+ "testing"
+)
+
+func TestMatch(t *testing.T) {
+ t.Parallel()
+
+ // Change to tests directory for testing completion of
+ // files and directories
+ err := os.Chdir("../tests")
+ if err != nil {
+ panic(err)
+ }
+
+ tests := []struct {
+ m Matcher
+ prefix string
+ want bool
+ }{
+ {
+ m: Prefix("abcd"),
+ prefix: "",
+ want: true,
+ },
+ {
+ m: Prefix("abcd"),
+ prefix: "ab",
+ want: true,
+ },
+ {
+ m: Prefix("abcd"),
+ prefix: "ac",
+ want: false,
+ },
+ {
+ m: Prefix(""),
+ prefix: "ac",
+ want: false,
+ },
+ {
+ m: Prefix(""),
+ prefix: "",
+ want: true,
+ },
+ {
+ m: File("file.txt"),
+ prefix: "",
+ want: true,
+ },
+ {
+ m: File("./file.txt"),
+ prefix: "",
+ want: true,
+ },
+ {
+ m: File("./file.txt"),
+ prefix: "f",
+ want: true,
+ },
+ {
+ m: File("./file.txt"),
+ prefix: "file.",
+ want: true,
+ },
+ {
+ m: File("./file.txt"),
+ prefix: "./f",
+ want: true,
+ },
+ {
+ m: File("./file.txt"),
+ prefix: "other.txt",
+ want: false,
+ },
+ {
+ m: File("./file.txt"),
+ prefix: "/file.txt",
+ want: false,
+ },
+ {
+ m: File("/file.txt"),
+ prefix: "file.txt",
+ want: false,
+ },
+ {
+ m: File("/file.txt"),
+ prefix: "./file.txt",
+ want: false,
+ },
+ {
+ m: File("/file.txt"),
+ prefix: "/file.txt",
+ want: true,
+ },
+ {
+ m: File("/file.txt"),
+ prefix: "/fil",
+ want: true,
+ },
+ }
+
+ for _, tt := range tests {
+ name := tt.m.String() + "/" + tt.prefix
+ t.Run(name, func(t *testing.T) {
+ got := tt.m.Match(tt.prefix)
+ if got != tt.want {
+ t.Errorf("Failed %s: got = %t, want: %t", name, got, tt.want)
+ }
+ })
+ }
+}
diff --git a/match/prefix.go b/match/prefix.go
new file mode 100644
index 0000000..d54902d
--- /dev/null
+++ b/match/prefix.go
@@ -0,0 +1,15 @@
+package match
+
+import "strings"
+
+// Prefix is a simple Matcher, if the word is it's prefix, there is a match
+type Prefix string
+
+func (a Prefix) String() string {
+ return string(a)
+}
+
+// Match returns true if a has the prefix as prefix
+func (a Prefix) Match(prefix string) bool {
+ return strings.HasPrefix(string(a), prefix)
+}