diff options
| author | 2018-09-06 17:11:58 +0200 | |
|---|---|---|
| committer | 2018-09-06 17:11:58 +0200 | |
| commit | 5e1480a7a7537451f7196498ac2988bda8273a9b (patch) | |
| tree | 2c85b66feeeeec1114d9dbe2608ef64f3ef5ee76 /test.zig | |
| parent | Updated to use pass-by-value where possible (diff) | |
| download | zig-clap-5e1480a7a7537451f7196498ac2988bda8273a9b.tar.gz zig-clap-5e1480a7a7537451f7196498ac2988bda8273a9b.tar.xz zig-clap-5e1480a7a7537451f7196498ac2988bda8273a9b.zip | |
Removed the extended api. Refactored tests
Diffstat (limited to 'test.zig')
| -rw-r--r-- | test.zig | 198 |
1 files changed, 198 insertions, 0 deletions
diff --git a/test.zig b/test.zig new file mode 100644 index 0000000..1708353 --- /dev/null +++ b/test.zig | |||
| @@ -0,0 +1,198 @@ | |||
| 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 Clap = clap.Clap(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 = Clap.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", | ||
| 51 | "-ac", "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", "--cc=0", | ||
| 86 | }, | ||
| 87 | []const Arg{ | ||
| 88 | Arg.init(aa, null), | ||
| 89 | Arg.init(bb, null), | ||
| 90 | Arg.init(cc, "0"), | ||
| 91 | Arg.init(cc, "0"), | ||
| 92 | }, | ||
| 93 | ); | ||
| 94 | } | ||
| 95 | |||
| 96 | test "clap: bare" { | ||
| 97 | const params = []Param{ | ||
| 98 | Param.init(0, false, Names.bare("aa")), | ||
| 99 | Param.init(1, false, Names.bare("bb")), | ||
| 100 | Param.init(2, true, Names.bare("cc")), | ||
| 101 | }; | ||
| 102 | |||
| 103 | const aa = ¶ms[0]; | ||
| 104 | const bb = ¶ms[1]; | ||
| 105 | const cc = ¶ms[2]; | ||
| 106 | |||
| 107 | testNoErr( | ||
| 108 | params, | ||
| 109 | [][]const u8{ | ||
| 110 | "aa", "bb", | ||
| 111 | "cc", "0", "cc=0", | ||
| 112 | }, | ||
| 113 | []const Arg{ | ||
| 114 | Arg.init(aa, null), | ||
| 115 | Arg.init(bb, null), | ||
| 116 | Arg.init(cc, "0"), | ||
| 117 | Arg.init(cc, "0"), | ||
| 118 | }, | ||
| 119 | ); | ||
| 120 | } | ||
| 121 | |||
| 122 | test "clap: none" { | ||
| 123 | const params = []Param{ | ||
| 124 | Param.init(0, true, Names.none()), | ||
| 125 | }; | ||
| 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", | ||
| 167 | "-ac", "0", "-ac=0", | ||
| 168 | "--aa", "--bb", | ||
| 169 | "--cc", "0", "--cc=0", | ||
| 170 | "aa", "bb", | ||
| 171 | "cc", "0", "cc=0", | ||
| 172 | "something", | ||
| 173 | }, | ||
| 174 | []const Arg{ | ||
| 175 | Arg.init(aa, null), | ||
| 176 | Arg.init(bb, null), | ||
| 177 | Arg.init(aa, null), | ||
| 178 | Arg.init(bb, null), | ||
| 179 | Arg.init(bb, null), | ||
| 180 | Arg.init(aa, null), | ||
| 181 | Arg.init(cc, "0"), | ||
| 182 | Arg.init(cc, "0"), | ||
| 183 | Arg.init(aa, null), | ||
| 184 | Arg.init(cc, "0"), | ||
| 185 | Arg.init(aa, null), | ||
| 186 | Arg.init(cc, "0"), | ||
| 187 | Arg.init(aa, null), | ||
| 188 | Arg.init(bb, null), | ||
| 189 | Arg.init(cc, "0"), | ||
| 190 | Arg.init(cc, "0"), | ||
| 191 | Arg.init(aa, null), | ||
| 192 | Arg.init(bb, null), | ||
| 193 | Arg.init(cc, "0"), | ||
| 194 | Arg.init(cc, "0"), | ||
| 195 | Arg.init(bare, "something"), | ||
| 196 | }, | ||
| 197 | ); | ||
| 198 | } | ||