v / vlib / flag / flag_from_test.v
55 lines · 49 sloc · 1.57 KB · c51d30bf5309653c6b573ec815268e69a78ea8cc
Raw
1import flag
2
3const some_args_1 = ['--mix', '-m', 'ok', '-d', 'one', '--test=abc', '-d', 'two', '/path/to/a',
4 'path/to/b']
5
6struct Config {
7 am string @[only: m]
8 def_test string = 'def' @[long: test; short: t]
9 device []string @[short: d]
10 paths []string @[tail]
11mut:
12 amount int = 1
13 mix bool
14 mix_hard bool = true
15}
16
17fn test_using() {
18 mut config := Config{
19 mix_hard: false
20 amount: 8
21 }
22
23 config, _ = flag.using[Config](config, some_args_1)!
24 assert config.mix
25 assert config.mix_hard == false
26 assert config.am == 'ok'
27 assert config.def_test == 'abc'
28 assert config.device[0] == 'one'
29 assert config.device[1] == 'two'
30 assert config.amount == 8
31 assert config.paths.len == 2
32 assert config.paths[0] == '/path/to/a'
33 assert config.paths[1] == 'path/to/b'
34
35 config.mix = false // is changed to true via `--mix`
36 config.mix_hard = true // should be kept as `true`, since no flags changed it
37 config.amount = 888
38
39 config2, _ := flag.using[Config](config, some_args_1)!
40 assert config2.mix
41 assert config2.mix_hard
42 assert config2.am == 'ok'
43 assert config2.def_test == 'abc'
44 assert config2.device[0] == 'one'
45 assert config2.device[1] == 'two'
46 assert config2.device[2] == 'one' // `config` already had items pushed from `some_args_1` so this grows when using `using[T](struct,...)`
47 assert config2.device[3] == 'two'
48 assert config2.device.len == 4
49 assert config2.amount == 888
50 assert config2.paths.len == 4
51 assert config2.paths[0] == '/path/to/a'
52 assert config2.paths[1] == 'path/to/b'
53 assert config2.paths[2] == '/path/to/a'
54 assert config2.paths[3] == 'path/to/b'
55}
56