forked from sei40kr/zsh-fast-alias-tips
-
Notifications
You must be signed in to change notification settings - Fork 0
/
def-matcher_test.go
132 lines (122 loc) · 2.55 KB
/
def-matcher_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package main
import (
"fmt"
"testing"
)
func TestParseDef(t *testing.T) {
mockArgs := []struct {
subject string
line string
expected Def
}{
{
subject: "when neither of the alias nor the abbreviation are quoted",
line: "dk=docker",
expected: Def{
alias: "dk",
abbr: "docker",
},
},
{
subject: "when the abbreviation is quoted",
line: "gb='git branch'",
expected: Def{
alias: "gb",
abbr: "git branch",
},
},
{
subject: "when both of the alias and the abbreviation are quoted",
line: "'g cb'='git checkout -b'",
expected: Def{
alias: "g cb",
abbr: "git checkout -b",
},
},
}
for _, mockArg := range mockArgs {
fmt.Printf("%s - ", mockArg.subject)
actual := ParseDef(mockArg.line)
expected := mockArg.expected
if actual == expected {
fmt.Println("ok")
} else {
fmt.Println("ng")
t.Fatalf("expected=%s, aParseDef(mockArg.line)ctual=%s\n", expected, actual)
}
}
}
func TestMatchDef(t *testing.T) {
mockDefs := []Def{
{
alias: "dk",
abbr: "docker",
},
{
alias: "gb",
abbr: "git branch",
},
{
alias: "gco",
abbr: "git checkout",
},
{
alias: "gcb",
abbr: "git checkout -b",
},
{
alias: "ls",
abbr: "ls -G",
},
{
alias: "ll",
abbr: "ls -lh",
},
}
mockArgs := []struct {
subject string
command string
expected *Def
}{
{
subject: "when the command has single token",
command: "docker",
expected: &Def{alias: "dk"},
},
{
subject: "when the command has multiple tokens",
command: "git branch",
expected: &Def{alias: "gb"},
},
{
subject: "when it has more than 2 matches, then return the longest one",
command: "git checkout -b",
expected: &Def{alias: "gcb"},
},
{
subject: "when it has no matches, then return a empty string",
command: "cd ..",
expected: nil,
},
{
subject: "when it was expanded recursively from >1 aliases, then reduce it fully",
command: "ls -G -lh", // ll expands to that with ls='ls -G' and ll='ls -lh' aliases defined
expected: &Def{alias: "ll"},
},
}
for _, mockArg := range mockArgs {
fmt.Printf("%s - ", mockArg.subject)
expected := mockArg.expected
actual, _ := MatchDef(mockDefs, mockArg.command)
if (actual == nil && expected == nil) || actual.alias == expected.alias {
fmt.Println("ok")
} else {
fmt.Println("ng")
if actual != nil {
t.Fatalf("expected=%s, actual=%s\n", expected.alias, actual.alias)
} else {
t.Fatalf("expected=%s, actual=nil\n", expected.alias)
}
}
}
}