@@ -31,6 +31,8 @@ struct Args {
31
31
file : String ,
32
32
}
33
33
34
+ const META_FILE : & str = "META.json" ;
35
+
34
36
fn parse_args < I > ( out : & mut impl Write , args : I ) -> Result < Args , Box < dyn Error > >
35
37
where
36
38
I : IntoIterator ,
39
41
use lexopt:: prelude:: * ;
40
42
let mut res = Args {
41
43
exit : false ,
42
- file : String :: from ( "META.json" ) ,
44
+ file : String :: from ( META_FILE ) ,
43
45
} ;
44
46
let mut parser = lexopt:: Parser :: from_iter ( args) ;
45
47
@@ -101,3 +103,106 @@ fn docs(out: &mut impl Write) -> Result<(), Box<dyn Error>> {
101
103
writeln ! ( out, "Docs" ) ?;
102
104
Ok ( ( ) )
103
105
}
106
+
107
+ #[ cfg( test) ]
108
+ mod tests {
109
+ use super :: * ;
110
+ use core:: panic;
111
+ use std:: str;
112
+
113
+ struct TC < ' a > {
114
+ name : & ' a str ,
115
+ args : & ' a [ & ' a str ] ,
116
+ exit : bool ,
117
+ file : & ' a str ,
118
+ out : & ' a str ,
119
+ }
120
+
121
+ #[ test]
122
+ fn test_parse_args ( ) -> Result < ( ) , Box < dyn Error > > {
123
+ for tc in [
124
+ TC {
125
+ name : "no args" ,
126
+ args : & [ "meta" ] ,
127
+ exit : false ,
128
+ file : META_FILE ,
129
+ out : "" ,
130
+ } ,
131
+ TC {
132
+ name : "short help" ,
133
+ args : & [ "meta" , "-h" ] ,
134
+ exit : true ,
135
+ file : META_FILE ,
136
+ out : "Usage: meta [--help | h] [--version | -v] [<path>]\n \n \
137
+ Options:\n \
138
+ \x20 -h --help Print this usage statement and exit\n \
139
+ \x20 -v --version Print the version number and exit\n ",
140
+ } ,
141
+ TC {
142
+ name : "long help" ,
143
+ args : & [ "meta" , "--help" ] ,
144
+ exit : true ,
145
+ file : META_FILE ,
146
+ out : "Usage: meta [--help | h] [--version | -v] [<path>]\n \n \
147
+ Options:\n \
148
+ \x20 -h --help Print this usage statement and exit\n \
149
+ \x20 -v --version Print the version number and exit\n ",
150
+ } ,
151
+ TC {
152
+ name : "short version" ,
153
+ args : & [ "meta" , "-v" ] ,
154
+ exit : true ,
155
+ file : META_FILE ,
156
+ out : concat ! ( "meta " , env!( "CARGO_PKG_VERSION" ) , "\n " ) ,
157
+ } ,
158
+ TC {
159
+ name : "long version" ,
160
+ args : & [ "meta" , "--version" ] ,
161
+ exit : true ,
162
+ file : META_FILE ,
163
+ out : concat ! ( "meta " , env!( "CARGO_PKG_VERSION" ) , "\n " ) ,
164
+ } ,
165
+ TC {
166
+ name : "short man" ,
167
+ args : & [ "meta" , "-m" ] ,
168
+ exit : true ,
169
+ file : META_FILE ,
170
+ out : "Docs\n " ,
171
+ } ,
172
+ TC {
173
+ name : "long man" ,
174
+ args : & [ "meta" , "--man" ] ,
175
+ exit : true ,
176
+ file : META_FILE ,
177
+ out : "Docs\n " ,
178
+ } ,
179
+ TC {
180
+ name : "file name" ,
181
+ args : & [ "meta" , "hello.json" ] ,
182
+ exit : false ,
183
+ file : "hello.json" ,
184
+ out : "" ,
185
+ } ,
186
+ ] {
187
+ let mut file: Vec < u8 > = Vec :: new ( ) ;
188
+ match parse_args ( & mut file, tc. args ) {
189
+ Err ( e) => panic ! ( "test {} failed: {e}" , tc. name) ,
190
+ Ok ( res) => {
191
+ assert_eq ! ( res. exit, tc. exit) ;
192
+ assert_eq ! ( res. file, tc. file) ;
193
+ assert_eq ! ( str :: from_utf8( & file) ?, tc. out) ;
194
+ }
195
+ }
196
+ }
197
+
198
+ let mut file: Vec < u8 > = Vec :: new ( ) ;
199
+ match parse_args ( & mut file, [ "hi" , "-x" ] ) {
200
+ Ok ( _) => panic ! ( "Should have failed on -x but did not" ) ,
201
+ Err ( e) => {
202
+ assert_eq ! ( e. to_string( ) , "invalid option '-x'" ) ;
203
+ }
204
+ }
205
+
206
+ Ok ( ( ) )
207
+ }
208
+ }
0 commit comments