@@ -407,51 +407,146 @@ fn test_format_with_inputs_multiple_sections() {
407407}
408408
409409#[ test]
410- fn test_format_with_inputs_error_wrong_input_count ( ) {
411- // Test error when input count doesn't match template section count
410+ fn test_format_with_inputs_input_count_handling ( ) {
411+ // Test graceful handling when input count doesn't match template section count
412412 let template = MultiTemplate :: parse ( "A: {upper} B: {lower}" ) . unwrap ( ) ;
413413
414- // Too few inputs
414+ // Too few inputs - should use empty string for missing inputs
415415 let result = template. format_with_inputs ( & [ & [ "only_one" ] ] , & [ " " , " " ] ) ;
416- assert ! ( result. is_err( ) ) ;
417- assert ! (
418- result
419- . unwrap_err( )
420- . contains( "Expected 2 input slices for 2 template sections, got 1" )
421- ) ;
416+ assert ! ( result. is_ok( ) ) ;
417+ assert_eq ! ( result. unwrap( ) , "A: ONLY_ONE B: " ) ;
422418
423- // Too many inputs
419+ // Too many inputs - should truncate excess inputs
424420 let result = template. format_with_inputs ( & [ & [ "one" ] , & [ "two" ] , & [ "three" ] ] , & [ " " , " " ] ) ;
425- assert ! ( result. is_err( ) ) ;
426- assert ! (
427- result
428- . unwrap_err( )
429- . contains( "Expected 2 input slices for 2 template sections, got 3" )
421+ assert ! ( result. is_ok( ) ) ;
422+ assert_eq ! ( result. unwrap( ) , "A: ONE B: two" ) ;
423+ }
424+
425+ #[ test]
426+ fn test_format_with_inputs_excess_inputs ( ) {
427+ // Test that excess inputs are truncated gracefully
428+ let template = MultiTemplate :: parse ( "diff {} {}" ) . unwrap ( ) ;
429+ let result = template. format_with_inputs (
430+ & [
431+ & [ "file1.txt" ] ,
432+ & [ "file2.txt" ] ,
433+ & [ "file3.txt" ] , // This should be ignored
434+ & [ "file4.txt" ] , // This should also be ignored
435+ ] ,
436+ & [ " " , " " ] ,
430437 ) ;
438+
439+ assert ! ( result. is_ok( ) ) ;
440+ assert_eq ! ( result. unwrap( ) , "diff file1.txt file2.txt" ) ;
431441}
432442
433443#[ test]
434- fn test_format_with_inputs_error_wrong_separator_count ( ) {
435- // Test error when separator count doesn't match template section count
436- let template = MultiTemplate :: parse ( "A: {upper} B: {lower}" ) . unwrap ( ) ;
444+ fn test_format_with_inputs_insufficient_inputs ( ) {
445+ // Test that missing inputs are treated as empty strings
446+ let template = MultiTemplate :: parse ( "cmd {upper} {lower} {trim}" ) . unwrap ( ) ;
447+ let result = template. format_with_inputs (
448+ & [
449+ & [ "arg1" ] ,
450+ & [ "ARG2" ] ,
451+ // Missing third input
452+ ] ,
453+ & [ " " , " " , " " ] ,
454+ ) ;
437455
438- // Too few separators
439- let result = template. format_with_inputs ( & [ & [ "one" ] , & [ "two" ] ] , & [ " " ] ) ;
440- assert ! ( result. is_err( ) ) ;
441- assert ! (
442- result
443- . unwrap_err( )
444- . contains( "Expected 2 separators for 2 template sections, got 1" )
456+ assert ! ( result. is_ok( ) ) ;
457+ assert_eq ! ( result. unwrap( ) , "cmd ARG1 arg2 " ) ;
458+ }
459+
460+ #[ test]
461+ fn test_format_with_inputs_empty_inputs_array ( ) {
462+ // Test with completely empty inputs array
463+ let template = MultiTemplate :: parse ( "start {upper} middle {lower} end" ) . unwrap ( ) ;
464+ let result = template. format_with_inputs ( & [ ] , & [ " " , " " ] ) ;
465+
466+ assert ! ( result. is_ok( ) ) ;
467+ assert_eq ! ( result. unwrap( ) , "start middle end" ) ;
468+ }
469+
470+ #[ test]
471+ fn test_format_with_inputs_mixed_empty_and_filled ( ) {
472+ // Test with mix of empty slices and filled slices
473+ let template = MultiTemplate :: parse ( "A:{upper} B:{lower} C:{trim}" ) . unwrap ( ) ;
474+ let result = template. format_with_inputs (
475+ & [
476+ & [ ] , // Empty slice for first section
477+ & [ "hello" ] , // Normal input for second section
478+ & [ ] , // Empty slice for third section
479+ ] ,
480+ & [ " " , " " , " " ] ,
445481 ) ;
446482
447- // Too many separators
448- let result = template. format_with_inputs ( & [ & [ "one" ] , & [ "two" ] ] , & [ " " , " " , " " ] ) ;
449- assert ! ( result. is_err( ) ) ;
450- assert ! (
451- result
452- . unwrap_err( )
453- . contains( "Expected 2 separators for 2 template sections, got 3" )
483+ assert ! ( result. is_ok( ) ) ;
484+ assert_eq ! ( result. unwrap( ) , "A: B:hello C:" ) ;
485+ }
486+
487+ #[ test]
488+ fn test_format_with_inputs_one_to_one_mode_scenario ( ) {
489+ // Test the specific scenario from the original issue
490+ let template = MultiTemplate :: parse ( "diff {} {}" ) . unwrap ( ) ;
491+
492+ // Simulating OneToOne mode: individual slices for each input
493+ let inputs = [ "file1.txt" , "file2.txt" , "file3.txt" ] ;
494+ let input_arrays: Vec < & [ & str ] > = inputs. iter ( ) . map ( std:: slice:: from_ref) . collect ( ) ;
495+
496+ let result = template. format_with_inputs ( & input_arrays, & [ " " , " " ] ) ;
497+
498+ assert ! ( result. is_ok( ) ) ;
499+ assert_eq ! ( result. unwrap( ) , "diff file1.txt file2.txt" ) ;
500+ // file3.txt should be ignored (truncated)
501+ }
502+
503+ #[ test]
504+ fn test_format_with_inputs_separator_defaults ( ) {
505+ // Test that missing separators default to space " "
506+ let template = MultiTemplate :: parse ( "files: {} | items: {} | values: {}" ) . unwrap ( ) ;
507+
508+ // Provide separators for only first section
509+ let result = template. format_with_inputs (
510+ & [
511+ & [ "a" , "b" , "c" ] , // First section gets comma separator
512+ & [ "x" , "y" , "z" ] , // Second section gets default space separator
513+ & [ "1" , "2" , "3" ] , // Third section gets default space separator
514+ ] ,
515+ & [ "," ] ,
454516 ) ;
517+
518+ assert ! ( result. is_ok( ) ) ;
519+ assert_eq ! (
520+ result. unwrap( ) ,
521+ "files: a,b,c | items: x y z | values: 1 2 3"
522+ ) ;
523+ }
524+
525+ #[ test]
526+ fn test_format_with_inputs_no_separators_provided ( ) {
527+ // Test with no separators provided - all should default to space
528+ let template = MultiTemplate :: parse ( "A: {} B: {}" ) . unwrap ( ) ;
529+
530+ let result = template. format_with_inputs ( & [ & [ "one" , "two" , "three" ] , & [ "a" , "b" , "c" ] ] , & [ ] ) ; // No separators provided
531+
532+ assert ! ( result. is_ok( ) ) ;
533+ assert_eq ! ( result. unwrap( ) , "A: one two three B: a b c" ) ;
534+ }
535+
536+ #[ test]
537+ fn test_format_with_inputs_separator_count_handling ( ) {
538+ // Test graceful handling when separator count doesn't match template section count
539+ let template = MultiTemplate :: parse ( "A: {upper} B: {lower}" ) . unwrap ( ) ;
540+
541+ // Too few separators - should use default space for missing separators
542+ let result = template. format_with_inputs ( & [ & [ "one" ] , & [ "two" ] ] , & [ "," ] ) ;
543+ assert ! ( result. is_ok( ) ) ;
544+ assert_eq ! ( result. unwrap( ) , "A: ONE B: two" ) ;
545+
546+ // Too many separators - should truncate excess separators
547+ let result = template. format_with_inputs ( & [ & [ "one" ] , & [ "two" ] ] , & [ "," , ";" , ":" ] ) ;
548+ assert ! ( result. is_ok( ) ) ;
549+ assert_eq ! ( result. unwrap( ) , "A: ONE B: two" ) ;
455550}
456551
457552#[ test]
@@ -477,7 +572,7 @@ fn test_format_with_inputs_empty_sections() {
477572#[ test]
478573fn test_format_with_inputs_custom_separators ( ) {
479574 // Test different separators for each section
480- let template = MultiTemplate :: parse ( "List1: {join:; } | List2: {join:, }" ) . unwrap ( ) ;
575+ let template = MultiTemplate :: parse ( "List1: {} | List2: {}" ) . unwrap ( ) ;
481576 let result = template
482577 . format_with_inputs ( & [ & [ "a" , "b" , "c" ] , & [ "x" , "y" , "z" ] ] , & [ "-" , "|" ] )
483578 . unwrap ( ) ;
0 commit comments