@@ -2661,7 +2661,6 @@ public static IEnumerable<object[]> Format_Valid_TestData()
2661
2661
yield return new object [ ] { null , "Foo }}{0}" , new object [ ] { 1 } , "Foo }1" } ; // Escaped closed curly braces
2662
2662
yield return new object [ ] { null , "Foo {0} {{0}}" , new object [ ] { 1 } , "Foo 1 {0}" } ; // Escaped placeholder
2663
2663
2664
-
2665
2664
yield return new object [ ] { null , "Foo {0}" , new object [ ] { null } , "Foo " } ; // Values has null only
2666
2665
yield return new object [ ] { null , "Foo {0} {1} {2}" , new object [ ] { "Bar" , null , "Baz" } , "Foo Bar Baz" } ; // Values has null
2667
2666
@@ -2670,6 +2669,29 @@ public static IEnumerable<object[]> Format_Valid_TestData()
2670
2669
yield return new object [ ] { new CustomFormatter ( ) , "{0}" , new object [ ] { 1.2 } , "abc" } ; // Custom format provider
2671
2670
yield return new object [ ] { new CustomFormatter ( ) , "{0:0}" , new object [ ] { 1.2 } , "abc" } ; // Custom format provider
2672
2671
2672
+ // More arguments than needed
2673
+ yield return new object [ ] { null , "{0}" , new object [ ] { 1 , 2 } , "1" } ;
2674
+ yield return new object [ ] { null , "{0}" , new object [ ] { 1 , 2 , 3 } , "1" } ;
2675
+ yield return new object [ ] { null , "{0}" , new object [ ] { 1 , 2 , 3 , 4 } , "1" } ;
2676
+ yield return new object [ ] { null , "{0}{1}" , new object [ ] { 1 , 2 , 3 } , "12" } ;
2677
+ yield return new object [ ] { null , "{0}{1}" , new object [ ] { 1 , 2 , 3 , 4 } , "12" } ;
2678
+ yield return new object [ ] { null , "{0}{1}" , new object [ ] { 1 , 2 , 3 , 4 , 5 } , "12" } ;
2679
+ yield return new object [ ] { null , "{0}{1}{2}" , new object [ ] { 1 , 2 , 3 , 4 } , "123" } ;
2680
+ yield return new object [ ] { null , "{0}{1}{2}" , new object [ ] { 1 , 2 , 3 , 4 , 5 } , "123" } ;
2681
+ yield return new object [ ] { null , "{0}{1}{2}" , new object [ ] { 1 , 2 , 3 , 4 , 5 , 6 } , "123" } ;
2682
+ yield return new object [ ] { null , "{0}{1}{2}{3}" , new object [ ] { 1 , 2 , 3 , 4 , 5 } , "1234" } ;
2683
+ yield return new object [ ] { null , "{0}{1}{2}{3}" , new object [ ] { 1 , 2 , 3 , 4 , 5 , 6 } , "1234" } ;
2684
+ yield return new object [ ] { null , "{0}{1}{2}{3}" , new object [ ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 } , "1234" } ;
2685
+ yield return new object [ ] { null , "{0}{1}{2}{3}{4}" , new object [ ] { 1 , 2 , 3 , 4 , 5 , 6 } , "12345" } ;
2686
+ yield return new object [ ] { null , "{0}{1}{2}{3}{4}" , new object [ ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 } , "12345" } ;
2687
+ yield return new object [ ] { null , "{0}{1}{2}{3}{4}" , new object [ ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } , "12345" } ;
2688
+
2689
+ // Out of order
2690
+ yield return new object [ ] { null , "{1}{0}" , new object [ ] { 1 , 2 } , "21" } ;
2691
+ yield return new object [ ] { null , "{2}{1}{0}" , new object [ ] { 1 , 2 , 3 } , "321" } ;
2692
+ yield return new object [ ] { null , "{3}{2}{1}{0}" , new object [ ] { 1 , 2 , 3 , 4 } , "4321" } ;
2693
+ yield return new object [ ] { null , "{4}{3}{2}{1}{0}" , new object [ ] { 1 , 2 , 3 , 4 , 5 } , "54321" } ;
2694
+
2673
2695
// Longer inputs
2674
2696
yield return new object [ ] { null , "0 = {0} 1 = {1} 2 = {2} 3 = {3} 4 = {4}" , new object [ ] { "zero" , "one" , "two" , "three" , "four" } , "0 = zero 1 = one 2 = two 3 = three 4 = four" } ;
2675
2697
yield return new object [ ] { new TestFormatter ( ) , "0 = {0} 1 = {1} 2 = {2} 3 = {3} 4 = {4}" , new object [ ] { "zero" , "one" , "two" , "three" , "four" } , "0 = Test: : zero 1 = Test: : one 2 = Test: : two 3 = Test: : three 4 = Test: : four" } ;
@@ -2737,7 +2759,7 @@ public static void Format_Valid(IFormatProvider provider, string format, object[
2737
2759
}
2738
2760
2739
2761
[ Fact ]
2740
- public static void Format_Invalid ( )
2762
+ public static void Format_Invalid_ArgumentException ( )
2741
2763
{
2742
2764
var formatter = new TestFormatter ( ) ;
2743
2765
var obj1 = new object ( ) ;
@@ -2751,66 +2773,107 @@ public static void Format_Invalid()
2751
2773
AssertExtensions . Throws < ArgumentNullException > ( "format" , ( ) => string . Format ( null , obj1 , obj2 , obj3 ) ) ;
2752
2774
AssertExtensions . Throws < ArgumentNullException > ( "format" , ( ) => string . Format ( null , obj1 , obj2 , obj3 , obj4 ) ) ;
2753
2775
2754
- AssertExtensions . Throws < ArgumentNullException > ( "format" , ( ) => string . Format ( formatter , null , obj1 ) ) ;
2755
- AssertExtensions . Throws < ArgumentNullException > ( "format" , ( ) => string . Format ( formatter , null , obj1 , obj2 ) ) ;
2756
- AssertExtensions . Throws < ArgumentNullException > ( "format" , ( ) => string . Format ( formatter , null , obj1 , obj2 , obj3 ) ) ;
2776
+ AssertExtensions . Throws < ArgumentNullException > ( "format" , ( ) => string . Format ( formatter , ( string ) null , obj1 ) ) ;
2777
+ AssertExtensions . Throws < ArgumentNullException > ( "format" , ( ) => string . Format ( formatter , ( string ) null , obj1 , obj2 ) ) ;
2778
+ AssertExtensions . Throws < ArgumentNullException > ( "format" , ( ) => string . Format ( formatter , ( string ) null , obj1 , obj2 , obj3 ) ) ;
2757
2779
2758
2780
// Args is null
2759
2781
AssertExtensions . Throws < ArgumentNullException > ( "args" , ( ) => string . Format ( "" , null ) ) ;
2760
2782
AssertExtensions . Throws < ArgumentNullException > ( "args" , ( ) => string . Format ( formatter , "" , null ) ) ;
2761
2783
2762
2784
// Args and format are null
2763
2785
AssertExtensions . Throws < ArgumentNullException > ( "format" , ( ) => string . Format ( null , ( object [ ] ) null ) ) ;
2764
- AssertExtensions . Throws < ArgumentNullException > ( "format" , ( ) => string . Format ( formatter , null , null ) ) ;
2765
-
2766
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{-1}" , obj1 ) ) ; // Format has value < 0
2767
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{-1}" , obj1 , obj2 ) ) ; // Format has value < 0
2768
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{-1}" , obj1 , obj2 , obj3 ) ) ; // Format has value < 0
2769
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{-1}" , obj1 , obj2 , obj3 , obj4 ) ) ; // Format has value < 0
2770
- Assert . Throws < FormatException > ( ( ) => string . Format ( formatter , "{-1}" , obj1 ) ) ; // Format has value < 0
2771
- Assert . Throws < FormatException > ( ( ) => string . Format ( formatter , "{-1}" , obj1 , obj2 ) ) ; // Format has value < 0
2772
- Assert . Throws < FormatException > ( ( ) => string . Format ( formatter , "{-1}" , obj1 , obj2 , obj3 ) ) ; // Format has value < 0
2773
- Assert . Throws < FormatException > ( ( ) => string . Format ( formatter , "{-1}" , obj1 , obj2 , obj3 , obj4 ) ) ; // Format has value < 0
2774
-
2775
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{1}" , obj1 ) ) ; // Format has value >= 1
2776
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{2}" , obj1 , obj2 ) ) ; // Format has value >= 2
2777
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{3}" , obj1 , obj2 , obj3 ) ) ; // Format has value >= 3
2778
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{4}" , obj1 , obj2 , obj3 , obj4 ) ) ; // Format has value >= 4
2779
- Assert . Throws < FormatException > ( ( ) => string . Format ( formatter , "{1}" , obj1 ) ) ; // Format has value >= 1
2780
- Assert . Throws < FormatException > ( ( ) => string . Format ( formatter , "{2}" , obj1 , obj2 ) ) ; // Format has value >= 2
2781
- Assert . Throws < FormatException > ( ( ) => string . Format ( formatter , "{3}" , obj1 , obj2 , obj3 ) ) ; // Format has value >= 3
2782
- Assert . Throws < FormatException > ( ( ) => string . Format ( formatter , "{4}" , obj1 , obj2 , obj3 , obj4 ) ) ; // Format has value >= 4
2783
-
2784
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{" , "" ) ) ; // Format has unescaped {
2785
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{a" , "" ) ) ; // Format has unescaped {
2786
-
2787
- Assert . Throws < FormatException > ( ( ) => string . Format ( "}" , "" ) ) ; // Format has unescaped }
2788
- Assert . Throws < FormatException > ( ( ) => string . Format ( "}a" , "" ) ) ; // Format has unescaped }
2789
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{0:}}" , "" ) ) ; // Format has unescaped }
2790
-
2791
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{\0 " , "" ) ) ; // Format has invalid character after {
2792
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{a" , "" ) ) ; // Format has invalid character after {
2793
-
2794
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{0 " , "" ) ) ; // Format with index and spaces is not closed
2795
-
2796
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{1000000" , new string [ 10 ] ) ) ; // Format index is too long
2797
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{10000000}" , new string [ 10 ] ) ) ; // Format index is too long
2786
+ AssertExtensions . Throws < ArgumentNullException > ( "format" , ( ) => string . Format ( formatter , ( string ) null , null ) ) ;
2787
+ }
2798
2788
2799
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{0," , "" ) ) ; // Format with comma is not closed
2800
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{0, " , "" ) ) ; // Format with comma and spaces is not closed
2801
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{0,-" , "" ) ) ; // Format with comma and minus sign is not closed
2789
+ public static IEnumerable < object [ ] > Format_Invalid_FormatExceptionFromFormat_MemberData ( )
2790
+ {
2791
+ var obj1 = new object ( ) ;
2792
+ var obj2 = new object ( ) ;
2793
+ var obj3 = new object ( ) ;
2794
+ var obj4 = new object ( ) ;
2802
2795
2803
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{0,-\0 " , "" ) ) ; // Format has invalid character after minus sign
2804
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{0,-a" , "" ) ) ; // Format has invalid character after minus sign
2796
+ foreach ( IFormatProvider provider in new [ ] { null , new TestFormatter ( ) } )
2797
+ {
2798
+ yield return new object [ ] { provider , "{-1}" , new object [ ] { obj1 } } ; // Format has value < 0
2799
+ yield return new object [ ] { provider , "{-1}" , new object [ ] { obj1 , obj2 } } ; // Format has value < 0
2800
+ yield return new object [ ] { provider , "{-1}" , new object [ ] { obj1 , obj2 , obj3 } } ; // Format has value < 0
2801
+ yield return new object [ ] { provider , "{-1}" , new object [ ] { obj1 , obj2 , obj3 , obj4 } } ; // Format has value < 0
2802
+ yield return new object [ ] { provider , "{" , new object [ ] { "" } } ; // Format has unescaped {
2803
+ yield return new object [ ] { provider , "{a" , new object [ ] { "" } } ; // Format has unescaped {
2804
+ yield return new object [ ] { provider , "}" , new object [ ] { "" } } ; // Format has unescaped }
2805
+ yield return new object [ ] { provider , "}a" , new object [ ] { "" } } ; // Format has unescaped }
2806
+ yield return new object [ ] { provider , "{0:}}" , new object [ ] { "" } } ; // Format has unescaped }
2807
+ yield return new object [ ] { provider , "{\0 " , new object [ ] { "" } } ; // Format has invalid character after {
2808
+ yield return new object [ ] { provider , "{a" , new object [ ] { "" } } ; // Format has invalid character after {
2809
+ yield return new object [ ] { provider , "{0 " , new object [ ] { "" } } ; // Format with index and spaces is not closed
2810
+ yield return new object [ ] { provider , "{1000000" , new object [ 10 ] } ; // Format has missing closing brace
2811
+ yield return new object [ ] { provider , "{0," , new object [ ] { "" } } ; // Format with comma is not closed
2812
+ yield return new object [ ] { provider , "{0, " , new object [ ] { "" } } ; // Format with comma and spaces is not closed
2813
+ yield return new object [ ] { provider , "{0,-" , new object [ ] { "" } } ; // Format with comma and minus sign is not closed
2814
+ yield return new object [ ] { provider , "{0,-\0 " , new object [ ] { "" } } ; // Format has invalid character after minus sign
2815
+ yield return new object [ ] { provider , "{0,-a" , new object [ ] { "" } } ; // Format has invalid character after minus sign
2816
+ yield return new object [ ] { provider , "{0,1000000" , new string [ 10 ] } ; // Format length is too long
2817
+ yield return new object [ ] { provider , "{0:" , new string [ 10 ] } ; // Format with colon is not closed
2818
+ yield return new object [ ] { provider , "{0: " , new string [ 10 ] } ; // Format with colon and spaces is not closed
2819
+ yield return new object [ ] { provider , "{0:{" , new string [ 10 ] } ; // Format with custom format contains unescaped {
2820
+ yield return new object [ ] { provider , "{0:{}" , new string [ 10 ] } ; // Format with custom format contains unescaped {
2821
+ }
2822
+ }
2823
+
2824
+ public static IEnumerable < object [ ] > Format_Invalid_FormatExceptionFromArgs_MemberData ( )
2825
+ {
2826
+ var obj1 = new object ( ) ;
2827
+ var obj2 = new object ( ) ;
2828
+ var obj3 = new object ( ) ;
2829
+ var obj4 = new object ( ) ;
2805
2830
2806
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{0,1000000" , new string [ 10 ] ) ) ; // Format length is too long
2807
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{0,10000000}" , new string [ 10 ] ) ) ; // Format length is too long
2831
+ foreach ( IFormatProvider provider in new [ ] { null , new TestFormatter ( ) } )
2832
+ {
2833
+ yield return new object [ ] { provider , "{1}" , new object [ ] { obj1 } } ; // Format has value >= 1
2834
+ yield return new object [ ] { provider , "{2}" , new object [ ] { obj1 , obj2 } } ; // Format has value >= 2
2835
+ yield return new object [ ] { provider , "{3}" , new object [ ] { obj1 , obj2 , obj3 } } ; // Format has value >= 3
2836
+ yield return new object [ ] { provider , "{4}" , new object [ ] { obj1 , obj2 , obj3 , obj4 } } ; // Format has value >= 4
2837
+ }
2838
+ }
2808
2839
2809
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{0:" , new string [ 10 ] ) ) ; // Format with colon is not closed
2810
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{0: " , new string [ 10 ] ) ) ; // Format with colon and spaces is not closed
2840
+ [ Theory ]
2841
+ [ MemberData ( nameof ( Format_Invalid_FormatExceptionFromFormat_MemberData ) ) ]
2842
+ [ MemberData ( nameof ( Format_Invalid_FormatExceptionFromArgs_MemberData ) ) ]
2843
+ [ InlineData ( null , "{10000000}" , new object [ ] { null } ) ]
2844
+ [ InlineData ( null , "{0,10000000}" , new string [ ] { null } ) ]
2845
+ public static void Format_Invalid_FormatExceptionFromFormatOrArgs ( IFormatProvider provider , string format , object [ ] args )
2846
+ {
2847
+ if ( provider is null )
2848
+ {
2849
+ Assert . Throws < FormatException > ( ( ) => string . Format ( format , args ) ) ;
2850
+ switch ( args . Length )
2851
+ {
2852
+ case 1 :
2853
+ Assert . Throws < FormatException > ( ( ) => string . Format ( format , args [ 0 ] ) ) ;
2854
+ break ;
2855
+ case 2 :
2856
+ Assert . Throws < FormatException > ( ( ) => string . Format ( format , args [ 0 ] , args [ 1 ] ) ) ;
2857
+ break ;
2858
+ case 3 :
2859
+ Assert . Throws < FormatException > ( ( ) => string . Format ( format , args [ 0 ] , args [ 1 ] , args [ 2 ] ) ) ;
2860
+ break ;
2861
+ }
2862
+ }
2811
2863
2812
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{0:{" , new string [ 10 ] ) ) ; // Format with custom format contains unescaped {
2813
- Assert . Throws < FormatException > ( ( ) => string . Format ( "{0:{}" , new string [ 10 ] ) ) ; // Format with custom format contains unescaped {
2864
+ Assert . Throws < FormatException > ( ( ) => string . Format ( provider , format , args ) ) ;
2865
+ switch ( args . Length )
2866
+ {
2867
+ case 1 :
2868
+ Assert . Throws < FormatException > ( ( ) => string . Format ( provider , format , args [ 0 ] ) ) ;
2869
+ break ;
2870
+ case 2 :
2871
+ Assert . Throws < FormatException > ( ( ) => string . Format ( provider , format , args [ 0 ] , args [ 1 ] ) ) ;
2872
+ break ;
2873
+ case 3 :
2874
+ Assert . Throws < FormatException > ( ( ) => string . Format ( provider , format , args [ 0 ] , args [ 1 ] , args [ 2 ] ) ) ;
2875
+ break ;
2876
+ }
2814
2877
}
2815
2878
2816
2879
[ ConditionalTheory ]
0 commit comments