@@ -177,7 +177,7 @@ impl<'a> Arg<'a> {
177177
178178impl From < Arg < ' _ > > for _zend_expected_type {
179179 fn from ( arg : Arg ) -> Self {
180- let err = match arg. r#type {
180+ let type_id = match arg. r#type {
181181 DataType :: False | DataType :: True => _zend_expected_type_Z_EXPECTED_BOOL,
182182 DataType :: Long => _zend_expected_type_Z_EXPECTED_LONG,
183183 DataType :: Double => _zend_expected_type_Z_EXPECTED_DOUBLE,
@@ -189,9 +189,9 @@ impl From<Arg<'_>> for _zend_expected_type {
189189 } ;
190190
191191 if arg. allow_null {
192- err + 1
192+ type_id + 1
193193 } else {
194- err
194+ type_id
195195 }
196196 }
197197}
@@ -302,3 +302,267 @@ impl<'a, 'b> ArgParser<'a, 'b> {
302302 Ok ( ( ) )
303303 }
304304}
305+
306+ #[ cfg( test) ]
307+ mod tests {
308+ #![ allow( clippy:: unwrap_used) ]
309+
310+ use super :: * ;
311+
312+ #[ test]
313+ fn test_new ( ) {
314+ let arg = Arg :: new ( "test" , DataType :: Long ) ;
315+ assert_eq ! ( arg. name, "test" ) ;
316+ assert_eq ! ( arg. r#type, DataType :: Long ) ;
317+ assert ! ( !arg. as_ref) ;
318+ assert ! ( !arg. allow_null) ;
319+ assert ! ( !arg. variadic) ;
320+ assert ! ( arg. default_value. is_none( ) ) ;
321+ assert ! ( arg. zval. is_none( ) ) ;
322+ assert ! ( arg. variadic_zvals. is_empty( ) ) ;
323+ }
324+
325+ #[ test]
326+ fn test_as_ref ( ) {
327+ let arg = Arg :: new ( "test" , DataType :: Long ) . as_ref ( ) ;
328+ assert ! ( arg. as_ref) ;
329+ }
330+
331+ #[ test]
332+ fn test_is_variadic ( ) {
333+ let arg = Arg :: new ( "test" , DataType :: Long ) . is_variadic ( ) ;
334+ assert ! ( arg. variadic) ;
335+ }
336+
337+ #[ test]
338+ fn test_allow_null ( ) {
339+ let arg = Arg :: new ( "test" , DataType :: Long ) . allow_null ( ) ;
340+ assert ! ( arg. allow_null) ;
341+ }
342+
343+ #[ test]
344+ fn test_default ( ) {
345+ let arg = Arg :: new ( "test" , DataType :: Long ) . default ( "default" ) ;
346+ assert_eq ! ( arg. default_value, Some ( "default" . to_string( ) ) ) ;
347+
348+ // TODO: Validate type
349+ }
350+
351+ #[ test]
352+ fn test_consume_no_value ( ) {
353+ let arg = Arg :: new ( "test" , DataType :: Long ) ;
354+ let result: Result < i32 , _ > = arg. consume ( ) ;
355+ assert ! ( result. is_err( ) ) ;
356+ assert_eq ! ( result. unwrap_err( ) . name, "test" ) ;
357+ }
358+
359+ #[ test]
360+ #[ cfg( feature = "embed" ) ]
361+ fn test_consume ( ) {
362+ let mut arg = Arg :: new ( "test" , DataType :: Long ) ;
363+ let mut zval = Zval :: from ( 42 ) ;
364+ arg. zval = Some ( & mut zval) ;
365+
366+ let result: Result < i32 , _ > = arg. consume ( ) ;
367+ assert_eq ! ( result. unwrap( ) , 42 ) ;
368+ }
369+
370+ #[ test]
371+ fn test_val_no_value ( ) {
372+ let mut arg = Arg :: new ( "test" , DataType :: Long ) ;
373+ let result: Option < i32 > = arg. val ( ) ;
374+ assert ! ( result. is_none( ) ) ;
375+ }
376+
377+ #[ test]
378+ #[ cfg( feature = "embed" ) ]
379+ fn test_val ( ) {
380+ let mut arg = Arg :: new ( "test" , DataType :: Long ) ;
381+ let mut zval = Zval :: from ( 42 ) ;
382+ arg. zval = Some ( & mut zval) ;
383+
384+ let result: Option < i32 > = arg. val ( ) ;
385+ assert_eq ! ( result. unwrap( ) , 42 ) ;
386+ }
387+
388+ #[ test]
389+ #[ cfg( feature = "embed" ) ]
390+ fn test_variadic_vals ( ) {
391+ let mut arg = Arg :: new ( "test" , DataType :: Long ) . is_variadic ( ) ;
392+ let mut zval1 = Zval :: from ( 42 ) ;
393+ let mut zval2 = Zval :: from ( 43 ) ;
394+ arg. variadic_zvals . push ( Some ( & mut zval1) ) ;
395+ arg. variadic_zvals . push ( Some ( & mut zval2) ) ;
396+
397+ let result: Vec < i32 > = arg. variadic_vals ( ) ;
398+ assert_eq ! ( result, vec![ 42 , 43 ] ) ;
399+ }
400+
401+ #[ test]
402+ fn test_zval_no_value ( ) {
403+ let mut arg = Arg :: new ( "test" , DataType :: Long ) ;
404+ let result = arg. zval ( ) ;
405+ assert ! ( result. is_none( ) ) ;
406+ }
407+
408+ #[ test]
409+ #[ cfg( feature = "embed" ) ]
410+ fn test_zval ( ) {
411+ let mut arg = Arg :: new ( "test" , DataType :: Long ) ;
412+ let mut zval = Zval :: from ( 42 ) ;
413+ arg. zval = Some ( & mut zval) ;
414+
415+ let result = arg. zval ( ) ;
416+ assert ! ( result. is_some( ) ) ;
417+ assert_eq ! ( result. unwrap( ) . dereference_mut( ) . long( ) , Some ( 42 ) ) ;
418+ }
419+
420+ #[ test]
421+ fn test_try_call_no_value ( ) {
422+ let arg = Arg :: new ( "test" , DataType :: Long ) ;
423+ let result = arg. try_call ( vec ! [ ] ) ;
424+ assert ! ( result. is_err( ) ) ;
425+ }
426+
427+ #[ test]
428+ #[ cfg( feature = "embed" ) ]
429+ fn test_try_call_not_callable ( ) {
430+ let mut arg = Arg :: new ( "test" , DataType :: Long ) ;
431+ let mut zval = Zval :: from ( 42 ) ;
432+ arg. zval = Some ( & mut zval) ;
433+
434+ let result = arg. try_call ( vec ! [ ] ) ;
435+ assert ! ( result. is_err( ) ) ;
436+ }
437+
438+ // TODO: Test the callable case
439+
440+ #[ test]
441+ #[ cfg( feature = "embed" ) ]
442+ fn test_as_arg_info ( ) {
443+ let arg = Arg :: new ( "test" , DataType :: Long ) ;
444+ let arg_info = arg. as_arg_info ( ) ;
445+ assert ! ( arg_info. is_ok( ) ) ;
446+
447+ let arg_info = arg_info. unwrap ( ) ;
448+ assert ! ( arg_info. default_value. is_null( ) ) ;
449+
450+ let r#type = arg_info. type_ ;
451+ assert_eq ! ( r#type. type_mask, 16 ) ;
452+ }
453+
454+ #[ test]
455+ #[ cfg( feature = "embed" ) ]
456+ fn test_as_arg_info_with_default ( ) {
457+ let arg = Arg :: new ( "test" , DataType :: Long ) . default ( "default" ) ;
458+ let arg_info = arg. as_arg_info ( ) ;
459+ assert ! ( arg_info. is_ok( ) ) ;
460+
461+ let arg_info = arg_info. unwrap ( ) ;
462+ assert ! ( !arg_info. default_value. is_null( ) ) ;
463+
464+ let r#type = arg_info. type_ ;
465+ assert_eq ! ( r#type. type_mask, 16 ) ;
466+ }
467+
468+ #[ test]
469+ fn test_type_from_arg ( ) {
470+ let arg = Arg :: new ( "test" , DataType :: Long ) ;
471+ let actual: _zend_expected_type = arg. into ( ) ;
472+ assert_eq ! ( actual, 0 ) ;
473+
474+ let arg = Arg :: new ( "test" , DataType :: Long ) . allow_null ( ) ;
475+ let actual: _zend_expected_type = arg. into ( ) ;
476+ assert_eq ! ( actual, 1 ) ;
477+
478+ let arg = Arg :: new ( "test" , DataType :: False ) ;
479+ let actual: _zend_expected_type = arg. into ( ) ;
480+ assert_eq ! ( actual, 2 ) ;
481+
482+ let arg = Arg :: new ( "test" , DataType :: False ) . allow_null ( ) ;
483+ let actual: _zend_expected_type = arg. into ( ) ;
484+ assert_eq ! ( actual, 3 ) ;
485+
486+ let arg = Arg :: new ( "test" , DataType :: True ) ;
487+ let actual: _zend_expected_type = arg. into ( ) ;
488+ assert_eq ! ( actual, 2 ) ;
489+
490+ let arg = Arg :: new ( "test" , DataType :: True ) . allow_null ( ) ;
491+ let actual: _zend_expected_type = arg. into ( ) ;
492+ assert_eq ! ( actual, 3 ) ;
493+
494+ let arg = Arg :: new ( "test" , DataType :: String ) ;
495+ let actual: _zend_expected_type = arg. into ( ) ;
496+ assert_eq ! ( actual, 4 ) ;
497+
498+ let arg = Arg :: new ( "test" , DataType :: String ) . allow_null ( ) ;
499+ let actual: _zend_expected_type = arg. into ( ) ;
500+ assert_eq ! ( actual, 5 ) ;
501+
502+ let arg = Arg :: new ( "test" , DataType :: Array ) ;
503+ let actual: _zend_expected_type = arg. into ( ) ;
504+ assert_eq ! ( actual, 6 ) ;
505+
506+ let arg = Arg :: new ( "test" , DataType :: Array ) . allow_null ( ) ;
507+ let actual: _zend_expected_type = arg. into ( ) ;
508+ assert_eq ! ( actual, 7 ) ;
509+
510+ let arg = Arg :: new ( "test" , DataType :: Resource ) ;
511+ let actual: _zend_expected_type = arg. into ( ) ;
512+ assert_eq ! ( actual, 14 ) ;
513+
514+ let arg = Arg :: new ( "test" , DataType :: Resource ) . allow_null ( ) ;
515+ let actual: _zend_expected_type = arg. into ( ) ;
516+ assert_eq ! ( actual, 15 ) ;
517+
518+ let arg = Arg :: new ( "test" , DataType :: Object ( None ) ) ;
519+ let actual: _zend_expected_type = arg. into ( ) ;
520+ assert_eq ! ( actual, 18 ) ;
521+
522+ let arg = Arg :: new ( "test" , DataType :: Object ( None ) ) . allow_null ( ) ;
523+ let actual: _zend_expected_type = arg. into ( ) ;
524+ assert_eq ! ( actual, 19 ) ;
525+
526+ let arg = Arg :: new ( "test" , DataType :: Double ) ;
527+ let actual: _zend_expected_type = arg. into ( ) ;
528+ assert_eq ! ( actual, 20 ) ;
529+
530+ let arg = Arg :: new ( "test" , DataType :: Double ) . allow_null ( ) ;
531+ let actual: _zend_expected_type = arg. into ( ) ;
532+ assert_eq ! ( actual, 21 ) ;
533+ }
534+
535+ #[ test]
536+ fn test_param_from_arg ( ) {
537+ let arg = Arg :: new ( "test" , DataType :: Long )
538+ . default ( "default" )
539+ . allow_null ( ) ;
540+ let param: Parameter = arg. into ( ) ;
541+ assert_eq ! ( param. name, "test" . into( ) ) ;
542+ assert_eq ! ( param. ty, abi:: Option :: Some ( DataType :: Long ) ) ;
543+ assert ! ( param. nullable) ;
544+ assert_eq ! ( param. default , abi:: Option :: Some ( "default" . into( ) ) ) ;
545+ }
546+
547+ #[ test]
548+ fn test_arg_parser_new ( ) {
549+ let arg_zvals = vec ! [ None , None ] ;
550+ let parser = ArgParser :: new ( arg_zvals) ;
551+ assert_eq ! ( parser. arg_zvals. len( ) , 2 ) ;
552+ assert ! ( parser. args. is_empty( ) ) ;
553+ assert ! ( parser. min_num_args. is_none( ) ) ;
554+ }
555+
556+ #[ test]
557+ fn test_arg_parser_arg ( ) {
558+ let arg_zvals = vec ! [ None , None ] ;
559+ let mut parser = ArgParser :: new ( arg_zvals) ;
560+ let mut arg = Arg :: new ( "test" , DataType :: Long ) ;
561+ parser = parser. arg ( & mut arg) ;
562+ assert_eq ! ( parser. args. len( ) , 1 ) ;
563+ assert_eq ! ( parser. args[ 0 ] . name, "test" ) ;
564+ assert_eq ! ( parser. args[ 0 ] . r#type, DataType :: Long ) ;
565+ }
566+
567+ // TODO: test parse
568+ }
0 commit comments