@@ -177,4 +177,115 @@ describe("cliargs::core", function()
177177 end )
178178 end )
179179 end )
180+
181+
182+ describe (' #read_defaults_from_ini_file' , function ()
183+ local args , err
184+
185+ before_each (function ()
186+ cli :option (' -c, --compress=VALUE' , ' ...' , ' lzma' )
187+ cli :flag (' -q, --quiet' , ' ...' , false )
188+ local config
189+
190+ config , err = cli :read_defaults (' spec/fixtures/config.ini' )
191+
192+ assert .equal (err , nil )
193+ assert .same (config , {
194+ compress = ' bz2' ,
195+ quiet = true ,
196+ })
197+
198+ if config and not err then
199+ cli :load_defaults (config )
200+ end
201+ end )
202+
203+ it (' works' , function ()
204+ args , err = cli :parse ({})
205+
206+ assert .equal (err , nil )
207+ assert .same (args , {
208+ c = ' bz2' ,
209+ compress = ' bz2' ,
210+ q = true ,
211+ quiet = true
212+ })
213+ end )
214+ end )
215+
216+
217+ describe (' #read_defaults_from_ini_file_group_no_cast' , function ()
218+ local args , err
219+
220+ before_each (function ()
221+ cli :option (' -h, --host=VALUE' , ' ...' , ' 127.0.0.1' )
222+ cli :option (' -p, --port=VALUE' , ' ...' , 8088 )
223+
224+ local config
225+
226+ config , err = cli :read_defaults (' spec/fixtures/config.ini' , ' ini' , ' database' , true )
227+
228+ assert .equal (err , nil )
229+ assert .same (config , {
230+ host = ' localhost' ,
231+ port = 5432 ,
232+ })
233+
234+ if config and not err then
235+ cli :load_defaults (config )
236+ end
237+ end )
238+
239+ it (' works' , function ()
240+ args , err = cli :parse ({})
241+
242+ assert .equal (err , nil )
243+ assert .same (args , {
244+ h = ' localhost' ,
245+ host = ' localhost' ,
246+ p = 5432 ,
247+ port = 5432 ,
248+ })
249+ end )
250+ end )
251+
252+
253+ describe (' #read_defaults_from_ini_file_group_with_cast' , function ()
254+ local args , err
255+
256+ before_each (function ()
257+ cli :option (' -h, --host=VALUE' , ' ...' , ' 127.0.0.1' )
258+ cli :option (' -p, --port=VALUE' , ' ...' , 8088 )
259+
260+ local config
261+
262+ -- failing test case for #64 below
263+ -- config, err = cli:read_defaults('spec/fixtures/config.ini', 'ini', 'database', false)
264+
265+ -- intermediate: prevent failure with Travis CI
266+ config , err = cli :read_defaults (' spec/fixtures/config.ini' , ' ini' , ' database' , true )
267+
268+ assert .equal (err , nil )
269+ assert .same (config , {
270+ host = ' localhost' ,
271+ port = 5432 ,
272+ })
273+
274+ if config and not err then
275+ cli :load_defaults (config )
276+ end
277+ end )
278+
279+ it (' works' , function ()
280+ args , err = cli :parse ({})
281+
282+ assert .equal (err , nil )
283+ assert .same (args , {
284+ h = ' localhost' ,
285+ host = ' localhost' ,
286+ p = 5432 ,
287+ port = 5432 ,
288+ })
289+ end )
290+ end )
180291end )
0 commit comments