Skip to content

Commit beb4743

Browse files
dschogitster
authored andcommitted
Add tests for parse-options.c
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
1 parent d7a38c5 commit beb4743

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ test-delta
154154
test-dump-cache-tree
155155
test-genrandom
156156
test-match-trees
157+
test-parse-options
157158
test-sha1
158159
common-cmds.h
159160
*.tar.gz

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,7 @@ endif
976976

977977
### Testing rules
978978

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
980980

981981
all:: $(TEST_PROGRAMS)
982982

t/t0040-parse-options.sh

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
 (0)