@@ -241,8 +241,11 @@ def test_double_precision(self):
241
241
def test_invalid_double_precision (self , invalid_val ):
242
242
double_input = 30.12345678901234567890
243
243
expected_exception = ValueError if isinstance (invalid_val , int ) else TypeError
244
-
245
- with pytest .raises (expected_exception ):
244
+ msg = (
245
+ r"Invalid value '.*' for option 'double_precision', max is '15'|"
246
+ r"an integer is required \(got type "
247
+ )
248
+ with pytest .raises (expected_exception , match = msg ):
246
249
ujson .encode (double_input , double_precision = invalid_val )
247
250
248
251
def test_encode_string_conversion2 (self ):
@@ -447,13 +450,13 @@ class O1:
447
450
decoded_input .member = O2 ()
448
451
decoded_input .member .member = decoded_input
449
452
450
- with pytest .raises (OverflowError ):
453
+ with pytest .raises (OverflowError , match = "Maximum recursion level reached" ):
451
454
ujson .encode (decoded_input )
452
455
453
456
def test_decode_jibberish (self ):
454
457
jibberish = "fdsa sda v9sa fdsa"
455
-
456
- with pytest .raises (ValueError ):
458
+ msg = "Unexpected character found when decoding 'false'"
459
+ with pytest .raises (ValueError , match = msg ):
457
460
ujson .decode (jibberish )
458
461
459
462
@pytest .mark .parametrize (
@@ -466,12 +469,13 @@ def test_decode_jibberish(self):
466
469
],
467
470
)
468
471
def test_decode_broken_json (self , broken_json ):
469
- with pytest .raises (ValueError ):
472
+ msg = "Expected object or value"
473
+ with pytest .raises (ValueError , match = msg ):
470
474
ujson .decode (broken_json )
471
475
472
476
@pytest .mark .parametrize ("too_big_char" , ["[" , "{" ])
473
477
def test_decode_depth_too_big (self , too_big_char ):
474
- with pytest .raises (ValueError ):
478
+ with pytest .raises (ValueError , match = "Reached object decoding depth limit" ):
475
479
ujson .decode (too_big_char * (1024 * 1024 ))
476
480
477
481
@pytest .mark .parametrize (
@@ -485,13 +489,27 @@ def test_decode_depth_too_big(self, too_big_char):
485
489
],
486
490
)
487
491
def test_decode_bad_string (self , bad_string ):
488
- with pytest .raises (ValueError ):
492
+ msg = (
493
+ "Unexpected character found when decoding|"
494
+ "Unmatched ''\" ' when when decoding 'string'"
495
+ )
496
+ with pytest .raises (ValueError , match = msg ):
489
497
ujson .decode (bad_string )
490
498
491
- @pytest .mark .parametrize ("broken_json" , ['{{1337:""}}' , '{{"key":"}' , "[[[true" ])
492
- def test_decode_broken_json_leak (self , broken_json ):
499
+ @pytest .mark .parametrize (
500
+ "broken_json, err_msg" ,
501
+ [
502
+ (
503
+ '{{1337:""}}' ,
504
+ "Key name of object must be 'string' when decoding 'object'" ,
505
+ ),
506
+ ('{{"key":"}' , "Unmatched ''\" ' when when decoding 'string'" ),
507
+ ("[[[true" , "Unexpected character found when decoding array value (2)" ),
508
+ ],
509
+ )
510
+ def test_decode_broken_json_leak (self , broken_json , err_msg ):
493
511
for _ in range (1000 ):
494
- with pytest .raises (ValueError ):
512
+ with pytest .raises (ValueError , match = re . escape ( err_msg ) ):
495
513
ujson .decode (broken_json )
496
514
497
515
@pytest .mark .parametrize (
@@ -503,7 +521,12 @@ def test_decode_broken_json_leak(self, broken_json):
503
521
],
504
522
)
505
523
def test_decode_invalid_dict (self , invalid_dict ):
506
- with pytest .raises (ValueError ):
524
+ msg = (
525
+ "Key name of object must be 'string' when decoding 'object'|"
526
+ "No ':' found when decoding object value|"
527
+ "Expected object or value"
528
+ )
529
+ with pytest .raises (ValueError , match = msg ):
507
530
ujson .decode (invalid_dict )
508
531
509
532
@pytest .mark .parametrize (
@@ -567,7 +590,7 @@ def test_dumps_ints_larger_than_maxsize(self, bigNum):
567
590
assert str (bigNum ) == encoding
568
591
569
592
# GH20599
570
- with pytest .raises (ValueError ):
593
+ with pytest .raises (ValueError , match = "Value is too big" ):
571
594
assert ujson .loads (encoding ) == bigNum
572
595
573
596
@pytest .mark .parametrize (
@@ -789,21 +812,70 @@ def test_0d_array(self):
789
812
ujson .encode (np .array (1 ))
790
813
791
814
@pytest .mark .parametrize (
792
- "bad_input,exc_type,kwargs" ,
815
+ "bad_input,exc_type,err_msg, kwargs" ,
793
816
[
794
- ([{}, []], ValueError , {}),
795
- ([42 , None ], TypeError , {}),
796
- ([["a" ], 42 ], ValueError , {}),
797
- ([42 , {}, "a" ], TypeError , {}),
798
- ([42 , ["a" ], 42 ], ValueError , {}),
799
- (["a" , "b" , [], "c" ], ValueError , {}),
800
- ([{"a" : "b" }], ValueError , {"labelled" : True }),
801
- ({"a" : {"b" : {"c" : 42 }}}, ValueError , {"labelled" : True }),
802
- ([{"a" : 42 , "b" : 23 }, {"c" : 17 }], ValueError , {"labelled" : True }),
817
+ (
818
+ [{}, []],
819
+ ValueError ,
820
+ "nesting not supported for object or variable length dtypes" ,
821
+ {},
822
+ ),
823
+ (
824
+ [42 , None ],
825
+ TypeError ,
826
+ "int() argument must be a string, a bytes-like object or a number, "
827
+ "not 'NoneType'" ,
828
+ {},
829
+ ),
830
+ (
831
+ [["a" ], 42 ],
832
+ ValueError ,
833
+ "Cannot decode multidimensional arrays with variable length elements "
834
+ "to numpy" ,
835
+ {},
836
+ ),
837
+ (
838
+ [42 , {}, "a" ],
839
+ TypeError ,
840
+ "int() argument must be a string, a bytes-like object or a number, "
841
+ "not 'dict'" ,
842
+ {},
843
+ ),
844
+ (
845
+ [42 , ["a" ], 42 ],
846
+ ValueError ,
847
+ "invalid literal for int() with base 10: 'a'" ,
848
+ {},
849
+ ),
850
+ (
851
+ ["a" , "b" , [], "c" ],
852
+ ValueError ,
853
+ "nesting not supported for object or variable length dtypes" ,
854
+ {},
855
+ ),
856
+ (
857
+ [{"a" : "b" }],
858
+ ValueError ,
859
+ "Cannot decode multidimensional arrays with variable length elements "
860
+ "to numpy" ,
861
+ {"labelled" : True },
862
+ ),
863
+ (
864
+ {"a" : {"b" : {"c" : 42 }}},
865
+ ValueError ,
866
+ "labels only supported up to 2 dimensions" ,
867
+ {"labelled" : True },
868
+ ),
869
+ (
870
+ [{"a" : 42 , "b" : 23 }, {"c" : 17 }],
871
+ ValueError ,
872
+ "cannot reshape array of size 3 into shape (2,1)" ,
873
+ {"labelled" : True },
874
+ ),
803
875
],
804
876
)
805
- def test_array_numpy_except (self , bad_input , exc_type , kwargs ):
806
- with pytest .raises (exc_type ):
877
+ def test_array_numpy_except (self , bad_input , exc_type , err_msg , kwargs ):
878
+ with pytest .raises (exc_type , match = re . escape ( err_msg ) ):
807
879
ujson .decode (ujson .dumps (bad_input ), numpy = True , ** kwargs )
808
880
809
881
def test_array_numpy_labelled (self ):
@@ -1034,7 +1106,11 @@ def test_datetime_index(self):
1034
1106
],
1035
1107
)
1036
1108
def test_decode_invalid_array (self , invalid_arr ):
1037
- with pytest .raises (ValueError ):
1109
+ msg = (
1110
+ "Expected object or value|Trailing data|"
1111
+ "Unexpected character found when decoding array value"
1112
+ )
1113
+ with pytest .raises (ValueError , match = msg ):
1038
1114
ujson .decode (invalid_arr )
1039
1115
1040
1116
@pytest .mark .parametrize ("arr" , [[], [31337 ]])
@@ -1049,18 +1125,18 @@ def test_decode_extreme_numbers(self, extreme_num):
1049
1125
"too_extreme_num" , ["9223372036854775808" , "-90223372036854775809" ]
1050
1126
)
1051
1127
def test_decode_too_extreme_numbers (self , too_extreme_num ):
1052
- with pytest .raises (ValueError ):
1128
+ with pytest .raises (ValueError , match = "Value is too big|Value is too small" ):
1053
1129
ujson .decode (too_extreme_num )
1054
1130
1055
1131
def test_decode_with_trailing_whitespaces (self ):
1056
1132
assert {} == ujson .decode ("{}\n \t " )
1057
1133
1058
1134
def test_decode_with_trailing_non_whitespaces (self ):
1059
- with pytest .raises (ValueError ):
1135
+ with pytest .raises (ValueError , match = "Trailing data" ):
1060
1136
ujson .decode ("{}\n \t a" )
1061
1137
1062
1138
def test_decode_array_with_big_int (self ):
1063
- with pytest .raises (ValueError ):
1139
+ with pytest .raises (ValueError , match = "Value is too big" ):
1064
1140
ujson .loads ("[18446098363113800555]" )
1065
1141
1066
1142
@pytest .mark .parametrize (
0 commit comments