@matheusbsbs / matheus-bagliano / commits / beb4743793
Fork 0

Add tests for parse-options.c

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Johannes Schindelin committed Oct 13, 2007 at 17:34 UTC beb474379315654566e78eea8a0e39c66ebcbb8a
4 files changed +107 -1
.gitignore
+1
@@ -154,6 +154,7 @@ test-delta
154 test-dump-cache-tree
155 test-genrandom
156 test-match-trees
157 +test-parse-options
158 test-sha1
159 common-cmds.h
160 *.tar.gz
Makefile
+1 -1
@@ -976,7 +976,7 @@ endif
976
977 ### Testing rules
978
979 -TEST_PROGRAMS = test-chmtime$X test-genrandom$X test-date$X test-delta$X test-sha1$X test-match-trees$X test-absolute-path$X
979 +TEST_PROGRAMS = test-chmtime$X test-genrandom$X test-date$X test-delta$X test-sha1$X test-match-trees$X test-absolute-path$X test-parse-options$X
980
981 all:: $(TEST_PROGRAMS)
982
t/t0040-parse-options.sh new
+70
@@ -0,0 +1,70 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2007 Johannes Schindelin
4 +#
5 +
6 +test_description='our own option parser'
7 +
8 +. ./test-lib.sh
9 +
10 +cat > expect.err << EOF
11 +usage: test-parse-options <options>
12 +
13 + -b, --boolean get a boolean
14 + -i, --integer <n> get a integer
15 + -j <n> get a integer, too
16 +
17 +string options
18 + -s, --string <string>
19 + get a string
20 + --string2 <str> get another string
21 +
22 +EOF
23 +
24 +test_expect_success 'test help' '
25 + ! test-parse-options -h > output 2> output.err &&
26 + test ! -s output &&
27 + git diff expect.err output.err
28 +'
29 +
30 +cat > expect << EOF
31 +boolean: 2
32 +integer: 1729
33 +string: 123
34 +EOF
35 +
36 +test_expect_success 'short options' '
37 + test-parse-options -s123 -b -i 1729 -b > output 2> output.err &&
38 + git diff expect output &&
39 + test ! -s output.err
40 +'
41 +cat > expect << EOF
42 +boolean: 2
43 +integer: 1729
44 +string: 321
45 +EOF
46 +
47 +test_expect_success 'long options' '
48 + test-parse-options --boolean --integer 1729 --boolean --string2=321 \
49 + > output 2> output.err &&
50 + test ! -s output.err &&
51 + git diff expect output
52 +'
53 +
54 +cat > expect << EOF
55 +boolean: 1
56 +integer: 13
57 +string: 123
58 +arg 00: a1
59 +arg 01: b1
60 +arg 02: --boolean
61 +EOF
62 +
63 +test_expect_success 'intermingled arguments' '
64 + test-parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
65 + > output 2> output.err &&
66 + test ! -s output.err &&
67 + git diff expect output
68 +'
69 +
70 +test_done
test-parse-options.c new
+35
@@ -0,0 +1,35 @@
1 +#include "cache.h"
2 +#include "parse-options.h"
3 +
4 +static int boolean = 0;
5 +static int integer = 0;
6 +static char *string = NULL;
7 +
8 +int main(int argc, const char **argv)
9 +{
10 + const char *usage[] = {
11 + "test-parse-options <options>",
12 + NULL
13 + };
14 + struct option options[] = {
15 + OPT_BOOLEAN('b', "boolean", &boolean, "get a boolean"),
16 + OPT_INTEGER('i', "integer", &integer, "get a integer"),
17 + OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
18 + OPT_GROUP("string options"),
19 + OPT_STRING('s', "string", &string, "string", "get a string"),
20 + OPT_STRING(0, "string2", &string, "str", "get another string"),
21 + OPT_END(),
22 + };
23 + int i;
24 +
25 + argc = parse_options(argc, argv, options, usage, 0);
26 +
27 + printf("boolean: %d\n", boolean);
28 + printf("integer: %d\n", integer);
29 + printf("string: %s\n", string ? string : "(not set)");
30 +
31 + for (i = 0; i < argc; i++)
32 + printf("arg %02d: %s\n", i, argv[i]);
33 +
34 + return 0;
35 +}