summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorGravatar Jimmi HC2018-05-31 16:00:44 +0200
committerGravatar Jimmi HC2018-05-31 16:00:44 +0200
commitf4e53cd6149ed4dcbfd8b81a6427b1b652d0a472 (patch)
tree7ca591977efac33ebc9aa51aab7356a4faa97d46 /tests
parentReworked the core. (diff)
downloadzig-clap-f4e53cd6149ed4dcbfd8b81a6427b1b652d0a472.tar.gz
zig-clap-f4e53cd6149ed4dcbfd8b81a6427b1b652d0a472.tar.xz
zig-clap-f4e53cd6149ed4dcbfd8b81a6427b1b652d0a472.zip
Started work on the proper structure for the lib
Diffstat (limited to '')
-rw-r--r--tests/core.zig134
-rw-r--r--tests/extended.zig234
2 files changed, 368 insertions, 0 deletions
diff --git a/tests/core.zig b/tests/core.zig
new file mode 100644
index 0000000..a6a705e
--- /dev/null
+++ b/tests/core.zig
@@ -0,0 +1,134 @@
1const std = @import("std");
2const clap = @import("../index.zig");
3
4const debug = std.debug;
5const mem = std.mem;
6const core = clap.core;
7
8const assert = debug.assert;
9
10const ArgSliceIterator = core.ArgSliceIterator;
11const Names = core.Names;
12const Param = core.Param;
13const Clap = core.Clap;
14
15fn testNoErr(params: []const Param(u8), args: []const []const u8, ids: []const u8, values: []const ?[]const u8) void {
16 var arg_iter = ArgSliceIterator.init(args);
17 var iter = Clap(u8).init(params, &arg_iter.iter, debug.global_allocator);
18
19 var i: usize = 0;
20 while (iter.next() catch unreachable) |arg| : (i += 1) {
21 debug.assert(ids[i] == arg.id);
22 const expected_value = values[i] ?? {
23 debug.assert(arg.value == null);
24 continue;
25 };
26 const actual_value = arg.value ?? unreachable;
27
28 debug.assert(mem.eql(u8, expected_value, actual_value));
29 }
30}
31
32test "clap.core: short" {
33 const params = []Param(u8) {
34 Param(u8).init(0, false, Names.short('a')),
35 Param(u8).init(1, false, Names.short('b')),
36 Param(u8).init(2, true, Names.short('c')),
37 };
38
39 testNoErr(params, [][]const u8 { "-a" }, []u8{0}, []?[]const u8{null});
40 testNoErr(params, [][]const u8 { "-a", "-b" }, []u8{0,1}, []?[]const u8{null,null});
41 testNoErr(params, [][]const u8 { "-ab" }, []u8{0,1}, []?[]const u8{null,null});
42 testNoErr(params, [][]const u8 { "-c=100" }, []u8{2}, []?[]const u8{"100"});
43 testNoErr(params, [][]const u8 { "-c100" }, []u8{2}, []?[]const u8{"100"});
44 testNoErr(params, [][]const u8 { "-c", "100" }, []u8{2}, []?[]const u8{"100"});
45 testNoErr(params, [][]const u8 { "-abc", "100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"});
46 testNoErr(params, [][]const u8 { "-abc=100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"});
47 testNoErr(params, [][]const u8 { "-abc100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"});
48}
49
50test "clap.core: long" {
51 const params = []Param(u8) {
52 Param(u8).init(0, false, Names.long("aa")),
53 Param(u8).init(1, false, Names.long("bb")),
54 Param(u8).init(2, true, Names.long("cc")),
55 };
56
57 testNoErr(params, [][]const u8 { "--aa" }, []u8{0}, []?[]const u8{null});
58 testNoErr(params, [][]const u8 { "--aa", "--bb" }, []u8{0,1}, []?[]const u8{null,null});
59 testNoErr(params, [][]const u8 { "--cc=100" }, []u8{2}, []?[]const u8{"100"});
60 testNoErr(params, [][]const u8 { "--cc", "100" }, []u8{2}, []?[]const u8{"100"});
61}
62
63test "clap.core: bare" {
64 const params = []Param(u8) {
65 Param(u8).init(0, false, Names.bare("aa")),
66 Param(u8).init(1, false, Names.bare("bb")),
67 Param(u8).init(2, true, Names.bare("cc")),
68 };
69
70 testNoErr(params, [][]const u8 { "aa" }, []u8{0}, []?[]const u8{null});
71 testNoErr(params, [][]const u8 { "aa", "bb" }, []u8{0,1}, []?[]const u8{null,null});
72 testNoErr(params, [][]const u8 { "cc=100" }, []u8{2}, []?[]const u8{"100"});
73 testNoErr(params, [][]const u8 { "cc", "100" }, []u8{2}, []?[]const u8{"100"});
74}
75
76test "clap.core: none" {
77 const params = []Param(u8) {
78 Param(u8).init(0, true, Names.none()),
79 };
80
81 testNoErr(params, [][]const u8 { "aa" }, []u8{0}, []?[]const u8{"aa"});
82}
83
84test "clap.core: all" {
85 const params = []Param(u8) {
86 Param(u8).init(
87 0,
88 false,
89 Names{
90 .bare = "aa",
91 .short = 'a',
92 .long = "aa",
93 }
94 ),
95 Param(u8).init(
96 1,
97 false,
98 Names{
99 .bare = "bb",
100 .short = 'b',
101 .long = "bb",
102 }
103 ),
104 Param(u8).init(
105 2,
106 true,
107 Names{
108 .bare = "cc",
109 .short = 'c',
110 .long = "cc",
111 }
112 ),
113 Param(u8).init(3, true, Names.none()),
114 };
115
116 testNoErr(params, [][]const u8 { "-a" }, []u8{0}, []?[]const u8{null});
117 testNoErr(params, [][]const u8 { "-a", "-b" }, []u8{0,1}, []?[]const u8{null,null});
118 testNoErr(params, [][]const u8 { "-ab" }, []u8{0,1}, []?[]const u8{null,null});
119 testNoErr(params, [][]const u8 { "-c=100" }, []u8{2}, []?[]const u8{"100"});
120 testNoErr(params, [][]const u8 { "-c100" }, []u8{2}, []?[]const u8{"100"});
121 testNoErr(params, [][]const u8 { "-c", "100" }, []u8{2}, []?[]const u8{"100"});
122 testNoErr(params, [][]const u8 { "-abc", "100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"});
123 testNoErr(params, [][]const u8 { "-abc=100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"});
124 testNoErr(params, [][]const u8 { "-abc100" }, []u8{0,1,2}, []?[]const u8{null,null,"100"});
125 testNoErr(params, [][]const u8 { "--aa" }, []u8{0}, []?[]const u8{null});
126 testNoErr(params, [][]const u8 { "--aa", "--bb" }, []u8{0,1}, []?[]const u8{null,null});
127 testNoErr(params, [][]const u8 { "--cc=100" }, []u8{2}, []?[]const u8{"100"});
128 testNoErr(params, [][]const u8 { "--cc", "100" }, []u8{2}, []?[]const u8{"100"});
129 testNoErr(params, [][]const u8 { "aa" }, []u8{0}, []?[]const u8{null});
130 testNoErr(params, [][]const u8 { "aa", "bb" }, []u8{0,1}, []?[]const u8{null,null});
131 testNoErr(params, [][]const u8 { "cc=100" }, []u8{2}, []?[]const u8{"100"});
132 testNoErr(params, [][]const u8 { "cc", "100" }, []u8{2}, []?[]const u8{"100"});
133 testNoErr(params, [][]const u8 { "dd" }, []u8{3}, []?[]const u8{"dd"});
134}
diff --git a/tests/extended.zig b/tests/extended.zig
new file mode 100644
index 0000000..8f722e4
--- /dev/null
+++ b/tests/extended.zig
@@ -0,0 +1,234 @@
1const std = @import("std");
2const clap = @import("../index.zig");
3
4const debug = std.debug;
5const mem = std.mem;
6const core = clap.core;
7const extended = clap.extended;
8
9const assert = debug.assert;
10
11const ArgSliceIterator = core.ArgSliceIterator;
12const Command = extended.Command;
13const Param = extended.Param;
14const Parser = extended.Parser;
15
16const Options = struct {
17 str: []const u8,
18 int: i64,
19 uint: u64,
20 a: bool,
21 b: bool,
22 cc: bool,
23 sub: &const SubOptions,
24
25 pub fn with(op: &const Options, comptime field: []const u8, value: var) Options {
26 var res = op.*;
27 @field(res, field) = value;
28 return res;
29 }
30};
31
32const SubOptions = struct {
33 a: bool,
34 b: u64,
35 qq: bool,
36
37 pub fn with(op: &const SubOptions, comptime field: []const u8, value: var) SubOptions {
38 var res = op.*;
39 @field(res, field) = value;
40 return res;
41 }
42};
43
44const default = Options {
45 .str = "",
46 .int = 0,
47 .uint = 0,
48 .a = false,
49 .b = false,
50 .cc = false,
51 .sub = SubOptions{
52 .a = false,
53 .b = 0,
54 .qq = false,
55 },
56};
57
58fn testNoErr(comptime command: &const Command, args: []const []const u8, expected: &const command.Result) void {
59 var arg_iter = ArgSliceIterator.init(args);
60 const actual = command.parse(debug.global_allocator, &arg_iter.iter) catch unreachable;
61 assert(mem.eql(u8, expected.str, actual.str));
62 assert(expected.int == actual.int);
63 assert(expected.uint == actual.uint);
64 assert(expected.a == actual.a);
65 assert(expected.b == actual.b);
66 assert(expected.cc == actual.cc);
67 assert(expected.sub.a == actual.sub.a);
68 assert(expected.sub.b == actual.sub.b);
69}
70
71fn testErr(comptime command: &const Command, args: []const []const u8, expected: error) void {
72 var arg_iter = ArgSliceIterator.init(args);
73 if (command.parse(debug.global_allocator, &arg_iter.iter)) |actual| {
74 unreachable;
75 } else |err| {
76 assert(err == expected);
77 }
78}
79
80test "clap.extended: short" {
81 const command = comptime Command.init(
82 "",
83 Options,
84 default,
85 []Param {
86 Param.smart("a"),
87 Param.smart("b"),
88 Param.smart("int")
89 .with("short", 'i')
90 .with("takes_value", Parser.int(i64, 10)),
91 },
92 []Command{},
93 );
94
95 testNoErr(command, [][]const u8 { "-a" }, default.with("a", true));
96 testNoErr(command, [][]const u8 { "-a", "-b" }, default.with("a", true).with("b", true));
97 testNoErr(command, [][]const u8 { "-i=100" }, default.with("int", 100));
98 testNoErr(command, [][]const u8 { "-i100" }, default.with("int", 100));
99 testNoErr(command, [][]const u8 { "-i", "100" }, default.with("int", 100));
100 testNoErr(command, [][]const u8 { "-ab" }, default.with("a", true).with("b", true));
101 testNoErr(command, [][]const u8 { "-abi", "100" }, default.with("a", true).with("b", true).with("int", 100));
102 testNoErr(command, [][]const u8 { "-abi=100" }, default.with("a", true).with("b", true).with("int", 100));
103 testNoErr(command, [][]const u8 { "-abi100" }, default.with("a", true).with("b", true).with("int", 100));
104}
105
106test "clap.extended: long" {
107 const command = comptime Command.init(
108 "",
109 Options,
110 default,
111 []Param {
112 Param.smart("cc"),
113 Param.smart("int").with("takes_value", Parser.int(i64, 10)),
114 Param.smart("uint").with("takes_value", Parser.int(u64, 10)),
115 Param.smart("str").with("takes_value", Parser.string),
116 },
117 []Command{},
118 );
119
120 testNoErr(command, [][]const u8 { "--cc" }, default.with("cc", true));
121 testNoErr(command, [][]const u8 { "--int", "100" }, default.with("int", 100));
122}
123
124test "clap.extended: value bool" {
125 const command = comptime Command.init(
126 "",
127 Options,
128 default,
129 []Param {
130 Param.smart("a"),
131 },
132 []Command{},
133 );
134
135 testNoErr(command, [][]const u8 { "-a" }, default.with("a", true));
136}
137
138test "clap.extended: value str" {
139 const command = comptime Command.init(
140 "",
141 Options,
142 default,
143 []Param {
144 Param.smart("str").with("takes_value", Parser.string),
145 },
146 []Command{},
147 );
148
149 testNoErr(command, [][]const u8 { "--str", "Hello World!" }, default.with("str", "Hello World!"));
150}
151
152test "clap.extended: value int" {
153 const command = comptime Command.init(
154 "",
155 Options,
156 default,
157 []Param {
158 Param.smart("int").with("takes_value", Parser.int(i64, 10)),
159 },
160 []Command{},
161 );
162
163 testNoErr(command, [][]const u8 { "--int", "100" }, default.with("int", 100));
164}
165
166test "clap.extended: position" {
167 const command = comptime Command.init(
168 "",
169 Options,
170 default,
171 []Param {
172 Param.smart("a").with("position", 0),
173 Param.smart("b").with("position", 1),
174 },
175 []Command{},
176 );
177
178 testNoErr(command, [][]const u8 { "-a", "-b" }, default.with("a", true).with("b", true));
179 testErr(command, [][]const u8 { "-b", "-a" }, error.InvalidArgument);
180}
181
182test "clap.extended: sub fields" {
183 const B = struct {
184 a: bool,
185 };
186 const A = struct {
187 b: B,
188 };
189
190 const command = comptime Command.init(
191 "",
192 A,
193 A { .b = B { .a = false } },
194 []Param {
195 Param.short('a')
196 .with("field", "b.a"),
197 },
198 []Command{},
199 );
200
201 var arg_iter = ArgSliceIterator.init([][]const u8{ "-a" });
202 const res = command.parse(debug.global_allocator, &arg_iter.iter) catch unreachable;
203 debug.assert(res.b.a == true);
204}
205
206test "clap.extended: sub commands" {
207 const command = comptime Command.init(
208 "",
209 Options,
210 default,
211 []Param {
212 Param.smart("a"),
213 Param.smart("b"),
214 },
215 []Command{
216 Command.init(
217 "sub",
218 SubOptions,
219 default.sub,
220 []Param {
221 Param.smart("a"),
222 Param.smart("b")
223 .with("takes_value", Parser.int(u64, 10)),
224 },
225 []Command{},
226 ),
227 },
228 );
229
230 testNoErr(command, [][]const u8 { "sub", "-a" }, default.with("sub", default.sub.with("a", true)));
231 testNoErr(command, [][]const u8 { "sub", "-b", "100" }, default.with("sub", default.sub.with("b", 100)));
232 testNoErr(command, [][]const u8 { "-a", "sub", "-a" }, default.with("a", true).with("sub", default.sub.with("a", true)));
233 testErr(command, [][]const u8 { "-qq", "sub" }, error.InvalidArgument);
234}