| 1 | // Test .short (POSIX) parse style |
| 2 | import flag |
| 3 | |
| 4 | const exe_and_posix_args = ['/path/to/exe', '-vv', 'vvv', '-mwindows', '-t', 'abc', '-done', '-d', |
| 5 | 'two', '-dthree', '-xyz', '2'] |
| 6 | |
| 7 | const exe_and_posix_args_with_tail = ['/path/to/exe', '-vvv', 'vvv', '-t', 'abc', '-done', '-d', |
| 8 | 'two', '-dthree', '/path/to/x', '/path/to/y', '/path/to/z'] |
| 9 | |
| 10 | const posix_multi_short_args_1 = ['-vv', 'vvv', '-done', '-d', 'two', '-yxz2'] |
| 11 | const posix_multi_short_args_2 = ['-vv', 'vvv', '-done', '-d', 'two', '-yxz', '2'] |
| 12 | const posix_multi_short_args_3 = ['-vv', 'vvv', '-xyz', '2', '-done', '-d', 'two'] |
| 13 | const posix_multi_short_args_4 = ['-yxz2', '-vv', 'vvv', '-done', '-d', 'two'] |
| 14 | const posix_multi_short_args_1_err = ['-zxy'] |
| 15 | const repeatable_cluster_args_1 = ['-mv'] |
| 16 | const repeatable_cluster_args_2 = ['-vmm'] |
| 17 | const repeatable_cluster_args_3 = ['-mmv'] |
| 18 | |
| 19 | struct Config { |
| 20 | linker_option string @[short: m] |
| 21 | test string = 'def' @[short: t] |
| 22 | device []string @[short: d] |
| 23 | paths []string @[tail] |
| 24 | verbosity int @[repeats; short: v] |
| 25 | not_mapped string = 'not changed' |
| 26 | x bool |
| 27 | b bool @[only: y] |
| 28 | u int @[short: z] |
| 29 | } |
| 30 | |
| 31 | struct RepeatableClusterConfig { |
| 32 | show_version bool @[only: v] |
| 33 | multi int @[only: m; repeats] |
| 34 | } |
| 35 | |
| 36 | fn test_pure_posix_short() { |
| 37 | config, _ := flag.to_struct[Config](exe_and_posix_args, skip: 1, style: .short)! |
| 38 | assert config.verbosity == 5 |
| 39 | assert config.test == 'abc' |
| 40 | assert 'one' in config.device |
| 41 | assert 'two' in config.device |
| 42 | assert 'three' in config.device |
| 43 | assert config.linker_option == 'windows' |
| 44 | assert config.not_mapped == 'not changed' |
| 45 | assert config.paths.len == 0 |
| 46 | assert config.x |
| 47 | assert config.b |
| 48 | assert config.u == 2 |
| 49 | } |
| 50 | |
| 51 | fn test_pure_posix_multi_short_1() { |
| 52 | config, _ := flag.to_struct[Config](posix_multi_short_args_1, style: .short)! |
| 53 | assert config.verbosity == 5 |
| 54 | assert config.test == 'def' |
| 55 | assert 'one' in config.device |
| 56 | assert 'two' in config.device |
| 57 | assert config.linker_option == '' |
| 58 | assert config.not_mapped == 'not changed' |
| 59 | assert config.paths.len == 0 |
| 60 | assert config.x |
| 61 | assert config.b |
| 62 | assert config.u == 2 |
| 63 | } |
| 64 | |
| 65 | fn test_pure_posix_multi_short_2() { |
| 66 | config, _ := flag.to_struct[Config](posix_multi_short_args_2, style: .short)! |
| 67 | assert config.verbosity == 5 |
| 68 | assert config.test == 'def' |
| 69 | assert 'one' in config.device |
| 70 | assert 'two' in config.device |
| 71 | assert config.linker_option == '' |
| 72 | assert config.not_mapped == 'not changed' |
| 73 | assert config.paths.len == 0 |
| 74 | assert config.x |
| 75 | assert config.b |
| 76 | assert config.u == 2 |
| 77 | } |
| 78 | |
| 79 | fn test_pure_posix_multi_short_3() { |
| 80 | config, _ := flag.to_struct[Config](posix_multi_short_args_3, style: .short)! |
| 81 | assert config.verbosity == 5 |
| 82 | assert config.test == 'def' |
| 83 | assert 'one' in config.device |
| 84 | assert 'two' in config.device |
| 85 | assert config.linker_option == '' |
| 86 | assert config.not_mapped == 'not changed' |
| 87 | assert config.paths.len == 0 |
| 88 | assert config.x |
| 89 | assert config.b |
| 90 | assert config.u == 2 |
| 91 | } |
| 92 | |
| 93 | fn test_pure_posix_multi_short_4() { |
| 94 | config, _ := flag.to_struct[Config](posix_multi_short_args_4, style: .short)! |
| 95 | assert config.verbosity == 5 |
| 96 | assert config.test == 'def' |
| 97 | assert 'one' in config.device |
| 98 | assert 'two' in config.device |
| 99 | assert config.linker_option == '' |
| 100 | assert config.not_mapped == 'not changed' |
| 101 | assert config.paths.len == 0 |
| 102 | assert config.x |
| 103 | assert config.b |
| 104 | assert config.u == 2 |
| 105 | } |
| 106 | |
| 107 | fn test_pure_posix_multi_short_err_1() { |
| 108 | if _, _ := flag.to_struct[Config](posix_multi_short_args_1_err, style: .short) { |
| 109 | assert false, 'flags should not have reached this assert' |
| 110 | } else { |
| 111 | assert err.msg() == 'can not assign non-integer value `xy` from flag `-zxy` to `Config.u`' |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | fn test_pure_posix_short_repeatable_cluster_1() { |
| 116 | config, _ := flag.to_struct[RepeatableClusterConfig](repeatable_cluster_args_1, style: .short)! |
| 117 | assert config.show_version |
| 118 | assert config.multi == 1 |
| 119 | } |
| 120 | |
| 121 | fn test_pure_posix_short_repeatable_cluster_2() { |
| 122 | config, _ := flag.to_struct[RepeatableClusterConfig](repeatable_cluster_args_2, style: .short)! |
| 123 | assert config.show_version |
| 124 | assert config.multi == 2 |
| 125 | } |
| 126 | |
| 127 | fn test_pure_posix_short_repeatable_cluster_3() { |
| 128 | config, _ := flag.to_struct[RepeatableClusterConfig](repeatable_cluster_args_3, style: .short)! |
| 129 | assert config.show_version |
| 130 | assert config.multi == 2 |
| 131 | } |
| 132 | |
| 133 | fn test_pure_posix_short_no_exe() { |
| 134 | config, _ := flag.to_struct[Config](exe_and_posix_args[1..], style: .short)! |
| 135 | assert config.verbosity == 5 |
| 136 | assert config.test == 'abc' |
| 137 | assert 'one' in config.device |
| 138 | assert 'two' in config.device |
| 139 | assert 'three' in config.device |
| 140 | assert config.linker_option == 'windows' |
| 141 | assert config.not_mapped == 'not changed' |
| 142 | assert config.paths.len == 0 |
| 143 | } |
| 144 | |
| 145 | fn test_pure_posix_short_with_tail() { |
| 146 | config, _ := flag.to_struct[Config](exe_and_posix_args_with_tail, skip: 1, style: .short)! |
| 147 | assert config.verbosity == 6 |
| 148 | assert config.test == 'abc' |
| 149 | assert 'one' in config.device |
| 150 | assert 'two' in config.device |
| 151 | assert 'three' in config.device |
| 152 | assert config.linker_option == '' |
| 153 | assert config.not_mapped == 'not changed' |
| 154 | assert config.paths.len == 3 |
| 155 | assert config.paths[0] == '/path/to/x' |
| 156 | assert config.paths[1] == '/path/to/y' |
| 157 | assert config.paths[2] == '/path/to/z' |
| 158 | } |
| 159 | |
| 160 | fn test_pure_posix_short_with_tail_no_exe() { |
| 161 | config, _ := flag.to_struct[Config](exe_and_posix_args_with_tail[1..], style: .short)! |
| 162 | assert config.verbosity == 6 |
| 163 | assert config.test == 'abc' |
| 164 | assert 'one' in config.device |
| 165 | assert 'two' in config.device |
| 166 | assert 'three' in config.device |
| 167 | assert config.linker_option == '' |
| 168 | assert config.not_mapped == 'not changed' |
| 169 | assert config.paths.len == 3 |
| 170 | assert config.paths[0] == '/path/to/x' |
| 171 | assert config.paths[1] == '/path/to/y' |
| 172 | assert config.paths[2] == '/path/to/z' |
| 173 | } |
| 174 | |