v / vlib / flag / gnu_style_flags_test.v
58 lines · 50 sloc · 1.73 KB · e8eda2103827f51d13f2e7647df2ac93a62c556f
Raw
1// Test .long (GNU) parse style
2import flag
3
4const exe_and_gnu_args = ['/path/to/exe', '--f=10.2', '--mix', '--f2=2', '--test=test', '--amount=5',
5 '--version=1.2.3']
6const exe_and_gnu_args_with_tail = ['/path/to/exe', '--f2=2', '--f=10.2', '--mix', '--test=test',
7 '--amount=6', '--version=1.2.3', '/path/to/x', '/path/to/y', '/path/to/z']
8
9struct Config {
10 f f32
11 f2 f64
12 mix bool
13 some_test string = 'abc' @[long: test]
14 path string @[tail]
15 amount int = 1
16 version_str string @[long: version]
17}
18
19fn test_pure_gnu_long() {
20 config, _ := flag.to_struct[Config](exe_and_gnu_args, skip: 1, style: .long)!
21 assert config.f == 10.2
22 assert config.f2 == 2.0
23 assert config.mix == true
24 assert config.some_test == 'test'
25 assert config.path == ''
26 assert config.amount == 5
27 assert config.version_str == '1.2.3'
28}
29
30fn test_pure_gnu_long_no_exe() {
31 config, _ := flag.to_struct[Config](exe_and_gnu_args[1..], style: .long)!
32 assert config.f == 10.2
33 assert config.f2 == 2.0
34 assert config.mix == true
35 assert config.some_test == 'test'
36 assert config.path == ''
37 assert config.amount == 5
38 assert config.version_str == '1.2.3'
39}
40
41fn test_pure_gnu_long_with_tail() {
42 config, no_matches := flag.to_struct[Config](exe_and_gnu_args_with_tail, skip: 1, style: .long)!
43 assert config.path == '/path/to/x' // path is of type `string`, not `[]string`
44 assert no_matches[0] == '/path/to/y'
45 assert no_matches[1] == '/path/to/z'
46
47 assert config.amount == 6
48}
49
50fn test_pure_gnu_long_with_tail_no_exe() {
51 a := exe_and_gnu_args_with_tail[1..]
52 config, no_matches := flag.to_struct[Config](a, style: .long)!
53 assert config.path == '/path/to/x'
54 assert no_matches[0] == '/path/to/y'
55 assert no_matches[1] == '/path/to/z'
56
57 assert config.amount == 6
58}
59