This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargparser_test.go
95 lines (76 loc) · 1.95 KB
/
argparser_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
package argparser_test
import (
"testing"
"github.com/chardon55/go-argparser"
)
func TestArguments1(t *testing.T) {
args := []string{"pacman", "-Ss", "sl", "cowsay"}
err := argparser.NewArgParser().
AddOperation('S', "sync").
AddIncrementSwitch('s', "search").
AddLongIncrementSwitch("color").
SetExecutor(func(op argparser.Operation, args1 []string) error {
if args1[0] != args[0] {
t.Errorf("Incorrect program name. Expected: %s; actual: %s", args[0], args1[0])
return nil
}
data := op.Data()
actualLength := len(data)
if actualLength < 2 {
t.Errorf("Uncompleted data value collection. Length: %d", actualLength)
return nil
}
for i, val := range data {
if val != args[2+i] {
t.Errorf("Unmatched value. Expected: %s; actual: %s", args[2+i], val)
return nil
}
}
is := op.IncrementSwitches()
if is["s"] != 1 || is["search"] != 1 {
t.Errorf("Switch not matched")
return nil
}
return nil
}).
Complete().
Parse(args)
if err != nil {
panic(err)
}
}
func TestArguments2(t *testing.T) {
args1 := []string{"conda", "install", "pandas", "seaborn", "jupyter", "-y"}
err := argparser.NewArgParser().
AddCommand("install").
AddBooleanSwitch('y', "yes").
SetExecutor(func(op argparser.Operation, args []string) error {
if args1[0] != args[0] {
t.Errorf("Incorrect program name. Expected: %s; actual: %s", args[0], args1[0])
return nil
}
data := op.Data()
actualLength := len(data)
if actualLength < 3 {
t.Errorf("Uncompleted data value collection. Length: %d", actualLength)
return nil
}
for i, val := range data {
if val != args[2+i] {
t.Errorf("Unmatched value. Expected: %s; actual: %s", args[2+i], val)
return nil
}
}
bs := op.BooleanSwitches()
if !bs["y"] || !bs["yes"] {
t.Errorf("Switch not matched")
return nil
}
return nil
}).
Complete().
Parse(args1)
if err != nil {
panic(err)
}
}