@cryptotaxi247 / kubo / commits / 922f84878

commands/cli: Added argument parse tests

Matt Bell committed Nov 19, 2014 at 18:30 UTC 922f84878caea85c26b7dd11578bd502bd1763ca
1 file changed +105
commands/cli/parse_test.go
+105
@@ -49,3 +49,108 @@ func TestOptionParsing(t *testing.T) {
49 t.Errorf("Returned command was different than expected")
50 }
51 }
52 +
53 +func TestArgumentParsing(t *testing.T) {
54 + rootCmd := &commands.Command{
55 + Subcommands: map[string]*commands.Command{
56 + "noarg": &commands.Command{},
57 + "onearg": &commands.Command{
58 + Arguments: []commands.Argument{
59 + commands.StringArg("a", true, false, "some arg"),
60 + },
61 + },
62 + "twoargs": &commands.Command{
63 + Arguments: []commands.Argument{
64 + commands.StringArg("a", true, false, "some arg"),
65 + commands.StringArg("b", true, false, "another arg"),
66 + },
67 + },
68 + "variadic": &commands.Command{
69 + Arguments: []commands.Argument{
70 + commands.StringArg("a", true, true, "some arg"),
71 + },
72 + },
73 + "optional": &commands.Command{
74 + Arguments: []commands.Argument{
75 + commands.StringArg("b", false, true, "another arg"),
76 + },
77 + },
78 + "reversedoptional": &commands.Command{
79 + Arguments: []commands.Argument{
80 + commands.StringArg("a", false, false, "some arg"),
81 + commands.StringArg("b", true, false, "another arg"),
82 + },
83 + },
84 + },
85 + }
86 +
87 + _, _, _, err := Parse([]string{"noarg"}, nil, rootCmd)
88 + if err != nil {
89 + t.Error("Should have passed")
90 + }
91 + _, _, _, err = Parse([]string{"noarg", "value!"}, nil, rootCmd)
92 + if err == nil {
93 + t.Error("Should have failed (provided an arg, but command didn't define any)")
94 + }
95 +
96 + _, _, _, err = Parse([]string{"onearg", "value!"}, nil, rootCmd)
97 + if err != nil {
98 + t.Error("Should have passed")
99 + }
100 + _, _, _, err = Parse([]string{"onearg"}, nil, rootCmd)
101 + if err == nil {
102 + t.Error("Should have failed (didn't provide any args, arg is required)")
103 + }
104 +
105 + _, _, _, err = Parse([]string{"twoargs", "value1", "value2"}, nil, rootCmd)
106 + if err != nil {
107 + t.Error("Should have passed")
108 + }
109 + _, _, _, err = Parse([]string{"twoargs", "value!"}, nil, rootCmd)
110 + if err == nil {
111 + t.Error("Should have failed (only provided 1 arg, needs 2)")
112 + }
113 + _, _, _, err = Parse([]string{"twoargs"}, nil, rootCmd)
114 + if err == nil {
115 + t.Error("Should have failed (didn't provide any args, 2 required)")
116 + }
117 +
118 + _, _, _, err = Parse([]string{"variadic", "value!"}, nil, rootCmd)
119 + if err != nil {
120 + t.Error("Should have passed")
121 + }
122 + _, _, _, err = Parse([]string{"variadic", "value1", "value2", "value3"}, nil, rootCmd)
123 + if err != nil {
124 + t.Error("Should have passed")
125 + }
126 + _, _, _, err = Parse([]string{"variadic"}, nil, rootCmd)
127 + if err == nil {
128 + t.Error("Should have failed (didn't provide any args, 1 required)")
129 + }
130 +
131 + _, _, _, err = Parse([]string{"optional", "value!"}, nil, rootCmd)
132 + if err != nil {
133 + t.Error("Should have passed")
134 + }
135 + _, _, _, err = Parse([]string{"optional"}, nil, rootCmd)
136 + if err != nil {
137 + t.Error("Should have passed")
138 + }
139 +
140 + _, _, _, err = Parse([]string{"reversedoptional", "value1", "value2"}, nil, rootCmd)
141 + if err != nil {
142 + t.Error("Should have passed")
143 + }
144 + _, _, _, err = Parse([]string{"reversedoptional", "value!"}, nil, rootCmd)
145 + if err != nil {
146 + t.Error("Should have passed")
147 + }
148 + _, _, _, err = Parse([]string{"reversedoptional"}, nil, rootCmd)
149 + if err == nil {
150 + t.Error("Should have failed (didn't provide any args, 1 required)")
151 + }
152 + _, _, _, err = Parse([]string{"reversedoptional", "value1", "value2", "value3"}, nil, rootCmd)
153 + if err == nil {
154 + t.Error("Should have failed (provided too many args, only takes 1)")
155 + }
156 +}