diff options
| author | 2018-06-01 13:37:40 +0200 | |
|---|---|---|
| committer | 2018-06-01 13:37:40 +0200 | |
| commit | 5c7016f2bc10c0f964fce5c4d9e7210db5786da7 (patch) | |
| tree | cae5744c1c76480ccdf957c1e60269bbf92d98b9 /tests | |
| parent | The arg iterator is now responsible for allocation instead of core.Clap (diff) | |
| download | zig-clap-5c7016f2bc10c0f964fce5c4d9e7210db5786da7.tar.gz zig-clap-5c7016f2bc10c0f964fce5c4d9e7210db5786da7.tar.xz zig-clap-5c7016f2bc10c0f964fce5c4d9e7210db5786da7.zip | |
Reworked extended.zig again!
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/extended.zig | 494 |
1 files changed, 305 insertions, 189 deletions
diff --git a/tests/extended.zig b/tests/extended.zig index 8f722e4..140c822 100644 --- a/tests/extended.zig +++ b/tests/extended.zig | |||
| @@ -9,226 +9,342 @@ const extended = clap.extended; | |||
| 9 | const assert = debug.assert; | 9 | const assert = debug.assert; |
| 10 | 10 | ||
| 11 | const ArgSliceIterator = core.ArgSliceIterator; | 11 | const ArgSliceIterator = core.ArgSliceIterator; |
| 12 | const Command = extended.Command; | 12 | const Names = core.Names; |
| 13 | const Clap = extended.Clap; | ||
| 13 | const Param = extended.Param; | 14 | const Param = extended.Param; |
| 14 | const Parser = extended.Parser; | 15 | const Parser = extended.Parser; |
| 15 | 16 | ||
| 16 | const Options = struct { | 17 | fn success(comptime parser: var, expect: var, args: []const []const u8) void { |
| 17 | str: []const u8, | 18 | var iter = ArgSliceIterator.init(args); |
| 18 | int: i64, | 19 | const actual = parser.parse(ArgSliceIterator.Error, &iter.iter) catch unreachable; |
| 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 | |||
| 32 | const SubOptions = struct { | ||
| 33 | a: bool, | ||
| 34 | b: u64, | ||
| 35 | qq: bool, | ||
| 36 | 20 | ||
| 37 | pub fn with(op: &const SubOptions, comptime field: []const u8, value: var) SubOptions { | 21 | const T = @typeOf(expect).Child; |
| 38 | var res = op.*; | 22 | inline for (@typeInfo(T).Struct.fields) |field| { |
| 39 | @field(res, field) = value; | 23 | assert(@field(expect, field.name) == @field(actual, field.name)); |
| 40 | return res; | ||
| 41 | } | 24 | } |
| 42 | }; | ||
| 43 | |||
| 44 | const 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 | |||
| 58 | fn 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 | } | 25 | } |
| 70 | 26 | ||
| 71 | fn testErr(comptime command: &const Command, args: []const []const u8, expected: error) void { | 27 | fn fail(comptime parser: var, expect: error, args: []const []const u8) void { |
| 72 | var arg_iter = ArgSliceIterator.init(args); | 28 | var iter = ArgSliceIterator.init(args); |
| 73 | if (command.parse(debug.global_allocator, &arg_iter.iter)) |actual| { | 29 | if (parser.parse(ArgSliceIterator.Error, &iter.iter)) |_| { |
| 74 | unreachable; | 30 | unreachable; |
| 75 | } else |err| { | 31 | } else |actual| { |
| 76 | assert(err == expected); | 32 | assert(expect == actual); |
| 77 | } | 33 | } |
| 78 | } | 34 | } |
| 79 | 35 | ||
| 80 | test "clap.extended: short" { | 36 | pub fn Test(comptime Expect: type) type { |
| 81 | const command = comptime Command.init( | 37 | return struct { |
| 82 | "", | 38 | const Self = this; |
| 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 | 39 | ||
| 106 | test "clap.extended: long" { | 40 | args: []const []const u8, |
| 107 | const command = comptime Command.init( | 41 | kind: Kind, |
| 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 | 42 | ||
| 120 | testNoErr(command, [][]const u8 { "--cc" }, default.with("cc", true)); | 43 | const Kind = union(enum) { |
| 121 | testNoErr(command, [][]const u8 { "--int", "100" }, default.with("int", 100)); | 44 | Success: Expect, |
| 122 | } | 45 | Fail: error, |
| 46 | }; | ||
| 123 | 47 | ||
| 124 | test "clap.extended: value bool" { | 48 | pub fn success(args: []const []const u8, expected: &const Expect) Self { |
| 125 | const command = comptime Command.init( | 49 | return Self{ |
| 126 | "", | 50 | .args = args, |
| 127 | Options, | 51 | .kind = Kind{ |
| 128 | default, | 52 | .Success = expected.*, |
| 129 | []Param { | 53 | }, |
| 130 | Param.smart("a"), | 54 | }; |
| 131 | }, | 55 | } |
| 132 | []Command{}, | ||
| 133 | ); | ||
| 134 | 56 | ||
| 135 | testNoErr(command, [][]const u8 { "-a" }, default.with("a", true)); | 57 | pub fn fail(args: []const []const u8, err: error) Self { |
| 136 | } | 58 | return Self{ |
| 59 | .args = args, | ||
| 60 | .kind = Kind{ | ||
| 61 | .Fail = err, | ||
| 62 | }, | ||
| 63 | }; | ||
| 64 | } | ||
| 137 | 65 | ||
| 138 | test "clap.extended: value str" { | 66 | pub fn run(t: &const Self, comptime parser: var) void { |
| 139 | const command = comptime Command.init( | 67 | var iter = ArgSliceIterator.init(t.args); |
| 140 | "", | 68 | const actual = parser.parse(ArgSliceIterator.Error, &iter.iter); |
| 141 | Options, | ||
| 142 | default, | ||
| 143 | []Param { | ||
| 144 | Param.smart("str").with("takes_value", Parser.string), | ||
| 145 | }, | ||
| 146 | []Command{}, | ||
| 147 | ); | ||
| 148 | 69 | ||
| 149 | testNoErr(command, [][]const u8 { "--str", "Hello World!" }, default.with("str", "Hello World!")); | 70 | switch (t.kind) { |
| 71 | Kind.Success => |expected| { | ||
| 72 | const actual_value = actual catch unreachable; | ||
| 73 | inline for (@typeInfo(Expect).Struct.fields) |field| { | ||
| 74 | assert(@field(expected, field.name) == @field(actual_value, field.name)); | ||
| 75 | } | ||
| 76 | }, | ||
| 77 | Kind.Fail => |expected| { | ||
| 78 | if (actual) |_| { | ||
| 79 | unreachable; | ||
| 80 | } else |actual_err| { | ||
| 81 | assert(actual_err == expected); | ||
| 82 | } | ||
| 83 | }, | ||
| 84 | } | ||
| 85 | } | ||
| 86 | }; | ||
| 150 | } | 87 | } |
| 151 | 88 | ||
| 152 | test "clap.extended: value int" { | 89 | test "clap.extended: short" { |
| 153 | const command = comptime Command.init( | 90 | const S = struct { |
| 154 | "", | 91 | a: bool, |
| 155 | Options, | 92 | b: u8, |
| 156 | default, | 93 | }; |
| 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 | 94 | ||
| 166 | test "clap.extended: position" { | 95 | const parser = comptime Clap(S){ |
| 167 | const command = comptime Command.init( | 96 | .default = S{ |
| 168 | "", | 97 | .a = false, |
| 169 | Options, | 98 | .b = 0, |
| 170 | default, | ||
| 171 | []Param { | ||
| 172 | Param.smart("a").with("position", 0), | ||
| 173 | Param.smart("b").with("position", 1), | ||
| 174 | }, | 99 | }, |
| 175 | []Command{}, | 100 | .params = []Param{ |
| 176 | ); | 101 | Param{ |
| 102 | .field = "a", | ||
| 103 | .names = Names.short('a'), | ||
| 104 | .kind = Param.Kind.Flag, | ||
| 105 | .required = true, | ||
| 106 | .position = 0, | ||
| 107 | }, | ||
| 108 | Param{ | ||
| 109 | .field = "b", | ||
| 110 | .names = Names.short('b'), | ||
| 111 | .kind = Param.Kind{ .Option = Parser.int(u8, 10) }, | ||
| 112 | .required = false, | ||
| 113 | .position = null, | ||
| 114 | }, | ||
| 115 | } | ||
| 116 | }; | ||
| 117 | |||
| 118 | const T = Test(S); | ||
| 119 | const tests = []T{ | ||
| 120 | T.success( | ||
| 121 | [][]const u8 { "-a" }, | ||
| 122 | S{ | ||
| 123 | .a = true, | ||
| 124 | .b = 0, | ||
| 125 | }, | ||
| 126 | ), | ||
| 127 | T.success( | ||
| 128 | [][]const u8 { "-a", "-b", "100" }, | ||
| 129 | S{ | ||
| 130 | .a = true, | ||
| 131 | .b = 100, | ||
| 132 | }, | ||
| 133 | ), | ||
| 134 | T.success( | ||
| 135 | [][]const u8 { "-a", "-b=100" }, | ||
| 136 | S{ | ||
| 137 | .a = true, | ||
| 138 | .b = 100, | ||
| 139 | }, | ||
| 140 | ), | ||
| 141 | T.success( | ||
| 142 | [][]const u8 { "-a", "-b100" }, | ||
| 143 | S{ | ||
| 144 | .a = true, | ||
| 145 | .b = 100, | ||
| 146 | }, | ||
| 147 | ), | ||
| 148 | T.success( | ||
| 149 | [][]const u8 { "-ab", "100" }, | ||
| 150 | S{ | ||
| 151 | .a = true, | ||
| 152 | .b = 100, | ||
| 153 | }, | ||
| 154 | ), | ||
| 155 | T.success( | ||
| 156 | [][]const u8 { "-ab=100" }, | ||
| 157 | S{ | ||
| 158 | .a = true, | ||
| 159 | .b = 100, | ||
| 160 | }, | ||
| 161 | ), | ||
| 162 | T.success( | ||
| 163 | [][]const u8 { "-ab100" }, | ||
| 164 | S{ | ||
| 165 | .a = true, | ||
| 166 | .b = 100, | ||
| 167 | }, | ||
| 168 | ), | ||
| 169 | T.fail( | ||
| 170 | [][]const u8 { "-q" }, | ||
| 171 | error.InvalidArgument, | ||
| 172 | ), | ||
| 173 | T.fail( | ||
| 174 | [][]const u8 { "--a" }, | ||
| 175 | error.InvalidArgument, | ||
| 176 | ), | ||
| 177 | T.fail( | ||
| 178 | [][]const u8 { "-b=100" }, | ||
| 179 | error.ParamNotHandled, | ||
| 180 | ), | ||
| 181 | T.fail( | ||
| 182 | [][]const u8 { "-b=100", "-a" }, | ||
| 183 | error.InvalidArgument, | ||
| 184 | ), | ||
| 185 | }; | ||
| 177 | 186 | ||
| 178 | testNoErr(command, [][]const u8 { "-a", "-b" }, default.with("a", true).with("b", true)); | 187 | for (tests) |t| { |
| 179 | testErr(command, [][]const u8 { "-b", "-a" }, error.InvalidArgument); | 188 | t.run(parser); |
| 189 | } | ||
| 180 | } | 190 | } |
| 181 | 191 | ||
| 182 | test "clap.extended: sub fields" { | 192 | test "clap.extended: long" { |
| 183 | const B = struct { | 193 | const S = struct { |
| 184 | a: bool, | 194 | a: bool, |
| 185 | }; | 195 | b: u8, |
| 186 | const A = struct { | ||
| 187 | b: B, | ||
| 188 | }; | 196 | }; |
| 189 | 197 | ||
| 190 | const command = comptime Command.init( | 198 | const parser = comptime Clap(S){ |
| 191 | "", | 199 | .default = S{ |
| 192 | A, | 200 | .a = false, |
| 193 | A { .b = B { .a = false } }, | 201 | .b = 0, |
| 194 | []Param { | ||
| 195 | Param.short('a') | ||
| 196 | .with("field", "b.a"), | ||
| 197 | }, | 202 | }, |
| 198 | []Command{}, | 203 | .params = []Param{ |
| 199 | ); | 204 | Param{ |
| 205 | .field = "a", | ||
| 206 | .names = Names.long("a"), | ||
| 207 | .kind = Param.Kind.Flag, | ||
| 208 | .required = true, | ||
| 209 | .position = 0, | ||
| 210 | }, | ||
| 211 | Param{ | ||
| 212 | .field = "b", | ||
| 213 | .names = Names.long("b"), | ||
| 214 | .kind = Param.Kind{ .Option = Parser.int(u8, 10) }, | ||
| 215 | .required = false, | ||
| 216 | .position = null, | ||
| 217 | }, | ||
| 218 | } | ||
| 219 | }; | ||
| 200 | 220 | ||
| 201 | var arg_iter = ArgSliceIterator.init([][]const u8{ "-a" }); | 221 | const T = Test(S); |
| 202 | const res = command.parse(debug.global_allocator, &arg_iter.iter) catch unreachable; | 222 | const tests = []T{ |
| 203 | debug.assert(res.b.a == true); | 223 | T.success( |
| 224 | [][]const u8 { "--a" }, | ||
| 225 | S{ | ||
| 226 | .a = true, | ||
| 227 | .b = 0, | ||
| 228 | }, | ||
| 229 | ), | ||
| 230 | T.success( | ||
| 231 | [][]const u8 { "--a", "--b", "100" }, | ||
| 232 | S{ | ||
| 233 | .a = true, | ||
| 234 | .b = 100, | ||
| 235 | }, | ||
| 236 | ), | ||
| 237 | T.success( | ||
| 238 | [][]const u8 { "--a", "--b=100" }, | ||
| 239 | S{ | ||
| 240 | .a = true, | ||
| 241 | .b = 100, | ||
| 242 | }, | ||
| 243 | ), | ||
| 244 | T.fail( | ||
| 245 | [][]const u8 { "--a=100" }, | ||
| 246 | error.DoesntTakeValue, | ||
| 247 | ), | ||
| 248 | T.fail( | ||
| 249 | [][]const u8 { "--q" }, | ||
| 250 | error.InvalidArgument, | ||
| 251 | ), | ||
| 252 | T.fail( | ||
| 253 | [][]const u8 { "-a" }, | ||
| 254 | error.InvalidArgument, | ||
| 255 | ), | ||
| 256 | T.fail( | ||
| 257 | [][]const u8 { "--b=100" }, | ||
| 258 | error.ParamNotHandled, | ||
| 259 | ), | ||
| 260 | T.fail( | ||
| 261 | [][]const u8 { "--b=100", "--a" }, | ||
| 262 | error.InvalidArgument, | ||
| 263 | ), | ||
| 264 | }; | ||
| 265 | |||
| 266 | for (tests) |t| { | ||
| 267 | t.run(parser); | ||
| 268 | } | ||
| 204 | } | 269 | } |
| 205 | 270 | ||
| 206 | test "clap.extended: sub commands" { | 271 | test "clap.extended: bare" { |
| 207 | const command = comptime Command.init( | 272 | const S = struct { |
| 208 | "", | 273 | a: bool, |
| 209 | Options, | 274 | b: u8, |
| 210 | default, | 275 | }; |
| 211 | []Param { | 276 | |
| 212 | Param.smart("a"), | 277 | const parser = comptime Clap(S){ |
| 213 | Param.smart("b"), | 278 | .default = S{ |
| 214 | }, | 279 | .a = false, |
| 215 | []Command{ | 280 | .b = 0, |
| 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 | }, | 281 | }, |
| 228 | ); | 282 | .params = []Param{ |
| 283 | Param{ | ||
| 284 | .field = "a", | ||
| 285 | .names = Names.bare("a"), | ||
| 286 | .kind = Param.Kind.Flag, | ||
| 287 | .required = true, | ||
| 288 | .position = 0, | ||
| 289 | }, | ||
| 290 | Param{ | ||
| 291 | .field = "b", | ||
| 292 | .names = Names.bare("b"), | ||
| 293 | .kind = Param.Kind{ .Option = Parser.int(u8, 10) }, | ||
| 294 | .required = false, | ||
| 295 | .position = null, | ||
| 296 | }, | ||
| 297 | } | ||
| 298 | }; | ||
| 299 | |||
| 300 | const T = Test(S); | ||
| 301 | const tests = []T{ | ||
| 302 | T.success( | ||
| 303 | [][]const u8 { "a" }, | ||
| 304 | S{ | ||
| 305 | .a = true, | ||
| 306 | .b = 0, | ||
| 307 | }, | ||
| 308 | ), | ||
| 309 | T.success( | ||
| 310 | [][]const u8 { "a", "b", "100" }, | ||
| 311 | S{ | ||
| 312 | .a = true, | ||
| 313 | .b = 100, | ||
| 314 | }, | ||
| 315 | ), | ||
| 316 | T.success( | ||
| 317 | [][]const u8 { "a", "b=100" }, | ||
| 318 | S{ | ||
| 319 | .a = true, | ||
| 320 | .b = 100, | ||
| 321 | }, | ||
| 322 | ), | ||
| 323 | T.fail( | ||
| 324 | [][]const u8 { "a=100" }, | ||
| 325 | error.DoesntTakeValue, | ||
| 326 | ), | ||
| 327 | T.fail( | ||
| 328 | [][]const u8 { "--a" }, | ||
| 329 | error.InvalidArgument, | ||
| 330 | ), | ||
| 331 | T.fail( | ||
| 332 | [][]const u8 { "-a" }, | ||
| 333 | error.InvalidArgument, | ||
| 334 | ), | ||
| 335 | T.fail( | ||
| 336 | [][]const u8 { "b=100" }, | ||
| 337 | error.ParamNotHandled, | ||
| 338 | ), | ||
| 339 | T.fail( | ||
| 340 | [][]const u8 { "b=100", "--a" }, | ||
| 341 | error.InvalidArgument, | ||
| 342 | ), | ||
| 343 | }; | ||
| 229 | 344 | ||
| 230 | testNoErr(command, [][]const u8 { "sub", "-a" }, default.with("sub", default.sub.with("a", true))); | 345 | for (tests) |t| { |
| 231 | testNoErr(command, [][]const u8 { "sub", "-b", "100" }, default.with("sub", default.sub.with("b", 100))); | 346 | t.run(parser); |
| 232 | testNoErr(command, [][]const u8 { "-a", "sub", "-a" }, default.with("a", true).with("sub", default.sub.with("a", true))); | 347 | } |
| 233 | testErr(command, [][]const u8 { "-qq", "sub" }, error.InvalidArgument); | ||
| 234 | } | 348 | } |
| 349 | |||
| 350 | // TODO: Test sub commands and sub field access | ||