diff options
Diffstat (limited to 'test.zig')
| -rw-r--r-- | test.zig | 245 |
1 files changed, 0 insertions, 245 deletions
diff --git a/test.zig b/test.zig deleted file mode 100644 index 27c93c2..0000000 --- a/test.zig +++ /dev/null | |||
| @@ -1,245 +0,0 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const clap = @import("clap.zig"); | ||
| 3 | |||
| 4 | const debug = std.debug; | ||
| 5 | const mem = std.mem; | ||
| 6 | |||
| 7 | const assert = debug.assert; | ||
| 8 | |||
| 9 | const ArgSliceIterator = clap.ArgSliceIterator; | ||
| 10 | const Names = clap.Names; | ||
| 11 | const Param = clap.Param(u8); | ||
| 12 | const StreamingClap = clap.StreamingClap(u8, ArgSliceIterator.Error); | ||
| 13 | const Arg = clap.Arg(u8); | ||
| 14 | |||
| 15 | fn testNoErr(params: []const Param, args: []const []const u8, results: []const Arg) void { | ||
| 16 | var arg_iter = ArgSliceIterator.init(args); | ||
| 17 | var c = StreamingClap.init(params, &arg_iter.iter); | ||
| 18 | |||
| 19 | for (results) |res| { | ||
| 20 | const arg = (c.next() catch unreachable) orelse unreachable; | ||
| 21 | debug.assert(res.param == arg.param); | ||
| 22 | const expected_value = res.value orelse { | ||
| 23 | debug.assert(arg.value == null); | ||
| 24 | continue; | ||
| 25 | }; | ||
| 26 | const actual_value = arg.value orelse unreachable; | ||
| 27 | debug.assert(mem.eql(u8, expected_value, actual_value)); | ||
| 28 | } | ||
| 29 | |||
| 30 | if (c.next() catch unreachable) |_| { | ||
| 31 | unreachable; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | |||
| 35 | test "clap: short" { | ||
| 36 | const params = []Param{ | ||
| 37 | Param.init(0, false, Names.short('a')), | ||
| 38 | Param.init(1, false, Names.short('b')), | ||
| 39 | Param.init(2, true, Names.short('c')), | ||
| 40 | }; | ||
| 41 | |||
| 42 | const a = ¶ms[0]; | ||
| 43 | const b = ¶ms[1]; | ||
| 44 | const c = ¶ms[2]; | ||
| 45 | |||
| 46 | testNoErr( | ||
| 47 | params, | ||
| 48 | [][]const u8{ | ||
| 49 | "-a", "-b", "-ab", "-ba", | ||
| 50 | "-c", "0", "-c=0", "-ac", | ||
| 51 | "0", "-ac=0", | ||
| 52 | }, | ||
| 53 | []const Arg{ | ||
| 54 | Arg.init(a, null), | ||
| 55 | Arg.init(b, null), | ||
| 56 | Arg.init(a, null), | ||
| 57 | Arg.init(b, null), | ||
| 58 | Arg.init(b, null), | ||
| 59 | Arg.init(a, null), | ||
| 60 | Arg.init(c, "0"), | ||
| 61 | Arg.init(c, "0"), | ||
| 62 | Arg.init(a, null), | ||
| 63 | Arg.init(c, "0"), | ||
| 64 | Arg.init(a, null), | ||
| 65 | Arg.init(c, "0"), | ||
| 66 | }, | ||
| 67 | ); | ||
| 68 | } | ||
| 69 | |||
| 70 | test "clap: long" { | ||
| 71 | const params = []Param{ | ||
| 72 | Param.init(0, false, Names.long("aa")), | ||
| 73 | Param.init(1, false, Names.long("bb")), | ||
| 74 | Param.init(2, true, Names.long("cc")), | ||
| 75 | }; | ||
| 76 | |||
| 77 | const aa = ¶ms[0]; | ||
| 78 | const bb = ¶ms[1]; | ||
| 79 | const cc = ¶ms[2]; | ||
| 80 | |||
| 81 | testNoErr( | ||
| 82 | params, | ||
| 83 | [][]const u8{ | ||
| 84 | "--aa", "--bb", | ||
| 85 | "--cc", "0", | ||
| 86 | "--cc=0", | ||
| 87 | }, | ||
| 88 | []const Arg{ | ||
| 89 | Arg.init(aa, null), | ||
| 90 | Arg.init(bb, null), | ||
| 91 | Arg.init(cc, "0"), | ||
| 92 | Arg.init(cc, "0"), | ||
| 93 | }, | ||
| 94 | ); | ||
| 95 | } | ||
| 96 | |||
| 97 | test "clap: bare" { | ||
| 98 | const params = []Param{ | ||
| 99 | Param.init(0, false, Names.bare("aa")), | ||
| 100 | Param.init(1, false, Names.bare("bb")), | ||
| 101 | Param.init(2, true, Names.bare("cc")), | ||
| 102 | }; | ||
| 103 | |||
| 104 | const aa = ¶ms[0]; | ||
| 105 | const bb = ¶ms[1]; | ||
| 106 | const cc = ¶ms[2]; | ||
| 107 | |||
| 108 | testNoErr( | ||
| 109 | params, | ||
| 110 | [][]const u8{ | ||
| 111 | "aa", "bb", | ||
| 112 | "cc", "0", | ||
| 113 | "cc=0", | ||
| 114 | }, | ||
| 115 | []const Arg{ | ||
| 116 | Arg.init(aa, null), | ||
| 117 | Arg.init(bb, null), | ||
| 118 | Arg.init(cc, "0"), | ||
| 119 | Arg.init(cc, "0"), | ||
| 120 | }, | ||
| 121 | ); | ||
| 122 | } | ||
| 123 | |||
| 124 | test "clap: none" { | ||
| 125 | const params = []Param{Param.init(0, true, Names.none())}; | ||
| 126 | |||
| 127 | testNoErr( | ||
| 128 | params, | ||
| 129 | [][]const u8{ "aa", "bb" }, | ||
| 130 | []const Arg{ | ||
| 131 | Arg.init(¶ms[0], "aa"), | ||
| 132 | Arg.init(¶ms[0], "bb"), | ||
| 133 | }, | ||
| 134 | ); | ||
| 135 | } | ||
| 136 | |||
| 137 | test "clap: all" { | ||
| 138 | const params = []Param{ | ||
| 139 | Param.init(0, false, Names{ | ||
| 140 | .bare = "aa", | ||
| 141 | .short = 'a', | ||
| 142 | .long = "aa", | ||
| 143 | }), | ||
| 144 | Param.init(1, false, Names{ | ||
| 145 | .bare = "bb", | ||
| 146 | .short = 'b', | ||
| 147 | .long = "bb", | ||
| 148 | }), | ||
| 149 | Param.init(2, true, Names{ | ||
| 150 | .bare = "cc", | ||
| 151 | .short = 'c', | ||
| 152 | .long = "cc", | ||
| 153 | }), | ||
| 154 | Param.init(3, true, Names.none()), | ||
| 155 | }; | ||
| 156 | |||
| 157 | const aa = ¶ms[0]; | ||
| 158 | const bb = ¶ms[1]; | ||
| 159 | const cc = ¶ms[2]; | ||
| 160 | const bare = ¶ms[3]; | ||
| 161 | |||
| 162 | testNoErr( | ||
| 163 | params, | ||
| 164 | [][]const u8{ | ||
| 165 | "-a", "-b", "-ab", "-ba", | ||
| 166 | "-c", "0", "-c=0", "-ac", | ||
| 167 | "0", "-ac=0", "--aa", "--bb", | ||
| 168 | "--cc", "0", "--cc=0", "aa", | ||
| 169 | "bb", "cc", "0", "cc=0", | ||
| 170 | "something", | ||
| 171 | }, | ||
| 172 | []const Arg{ | ||
| 173 | Arg.init(aa, null), | ||
| 174 | Arg.init(bb, null), | ||
| 175 | Arg.init(aa, null), | ||
| 176 | Arg.init(bb, null), | ||
| 177 | Arg.init(bb, null), | ||
| 178 | Arg.init(aa, null), | ||
| 179 | Arg.init(cc, "0"), | ||
| 180 | Arg.init(cc, "0"), | ||
| 181 | Arg.init(aa, null), | ||
| 182 | Arg.init(cc, "0"), | ||
| 183 | Arg.init(aa, null), | ||
| 184 | Arg.init(cc, "0"), | ||
| 185 | Arg.init(aa, null), | ||
| 186 | Arg.init(bb, null), | ||
| 187 | Arg.init(cc, "0"), | ||
| 188 | Arg.init(cc, "0"), | ||
| 189 | Arg.init(aa, null), | ||
| 190 | Arg.init(bb, null), | ||
| 191 | Arg.init(cc, "0"), | ||
| 192 | Arg.init(cc, "0"), | ||
| 193 | Arg.init(bare, "something"), | ||
| 194 | }, | ||
| 195 | ); | ||
| 196 | } | ||
| 197 | |||
| 198 | test "clap.Example" { | ||
| 199 | // Fake program arguments. Don't mind them | ||
| 200 | const program_args = [][]const u8{ | ||
| 201 | "-h", "--help", | ||
| 202 | "-v", "--version", | ||
| 203 | "file.zig", | ||
| 204 | }; | ||
| 205 | |||
| 206 | const warn = @import("std").debug.warn; | ||
| 207 | const c = @import("clap.zig"); | ||
| 208 | |||
| 209 | // Initialize the parameters you want your program to take. | ||
| 210 | // `Param` has a type passed in, which will determin the type | ||
| 211 | // of `Param.id`. This field can then be used to identify the | ||
| 212 | // `Param`, or something else entirely. | ||
| 213 | // Example: You could have the `id` be a function, and then | ||
| 214 | // call it in the loop further down. | ||
| 215 | const params = []c.Param(u8){ | ||
| 216 | c.Param(u8).init('h', false, c.Names.prefix("help")), | ||
| 217 | c.Param(u8).init('v', false, c.Names.prefix("version")), | ||
| 218 | c.Param(u8).init('f', true, c.Names.none()), | ||
| 219 | }; | ||
| 220 | |||
| 221 | // Here, we use an `ArgSliceIterator` which iterates over | ||
| 222 | // a slice of arguments. For real program, you would probably | ||
| 223 | // use `OsArgIterator`. | ||
| 224 | var iter = &c.ArgSliceIterator.init(program_args).iter; | ||
| 225 | var parser = c.StreamingClap(u8, c.ArgSliceIterator.Error).init(params, iter); | ||
| 226 | |||
| 227 | // Iterate over all arguments passed to the program. | ||
| 228 | // In real code, you should probably handle the errors | ||
| 229 | // `parser.next` returns. | ||
| 230 | while (parser.next() catch unreachable) |arg| { | ||
| 231 | // `arg.param` is a pointer to its matching `Param` | ||
| 232 | // from the `params` array. | ||
| 233 | switch (arg.param.id) { | ||
| 234 | 'h' => warn("Help!\n"), | ||
| 235 | 'v' => warn("1.1.1\n"), | ||
| 236 | |||
| 237 | // `arg.value` is `null`, if `arg.param.takes_value` | ||
| 238 | // is `false`. Otherwise, `arg.value` is the value | ||
| 239 | // passed with the argument, such as `-a=10` or | ||
| 240 | // `-a 10`. | ||
| 241 | 'f' => warn("{}\n", arg.value.?), | ||
| 242 | else => unreachable, | ||
| 243 | } | ||
| 244 | } | ||
| 245 | } | ||