@@ -27,15 +27,11 @@ impl<'de> de::Deserialize<'de> for NonEmptyString {
27
27
}
28
28
}
29
29
30
- pub enum DeserializeError { }
31
-
32
- type Result < T , E = DeserializeError > = std:: result:: Result < T , E > ;
33
-
34
30
impl < ' de > Visitor < ' de > for NonEmptyStringVisitor {
35
31
type Value = NonEmptyString ;
36
32
37
33
fn expecting ( & self , formatter : & mut fmt:: Formatter ) -> fmt:: Result {
38
- formatter. write_str ( "an integer between -2^31 and 2^31 " )
34
+ formatter. write_str ( "a string with a length of more than 0 " )
39
35
}
40
36
41
37
fn visit_str < E > ( self , value : & str ) -> Result < Self :: Value , E >
@@ -55,13 +51,12 @@ impl<'de> Visitor<'de> for NonEmptyStringVisitor {
55
51
56
52
#[ cfg( test) ]
57
53
mod tests {
58
- use super :: * ;
59
54
use crate :: * ;
60
55
use assert_matches:: assert_matches;
61
56
use serde_json:: json;
62
57
63
58
#[ test]
64
- fn serialize_works ( ) {
59
+ fn serialize_non_empty_string_and_normal_string_give_the_same_result ( ) {
65
60
let value = NonEmptyString ( "abc" . to_owned ( ) ) ;
66
61
let result = serde_json:: to_string ( & value) ;
67
62
@@ -80,11 +75,40 @@ mod tests {
80
75
assert_matches ! ( e, Ok ( v) if v == expected)
81
76
}
82
77
78
+ fn deserialize_x_fails ( value : serde_json:: Value , expected_error_message : & ' static str ) {
79
+ let e: Result < NonEmptyString , _ > = serde_json:: from_value ( value) ;
80
+ assert_matches ! ( e, Err ( error) if & error. to_string( ) == expected_error_message)
81
+ }
82
+
83
83
#[ test]
84
84
fn deserialize_empty_fails ( ) {
85
- let e: Result < NonEmptyString , _ > = serde_json:: from_value ( json ! ( "" ) ) ;
85
+ deserialize_x_fails (
86
+ json ! ( "" ) ,
87
+ "invalid value: string \" \" , expected a string with a length of more than 0" ,
88
+ )
89
+ }
90
+
91
+ #[ test]
92
+ fn deserialize_number_fails ( ) {
93
+ deserialize_x_fails (
94
+ json ! ( 8 ) ,
95
+ "invalid type: integer `8`, expected a string with a length of more than 0" ,
96
+ )
97
+ }
98
+
99
+ #[ test]
100
+ fn deserialize_object_fails ( ) {
101
+ deserialize_x_fails (
102
+ json ! ( { } ) ,
103
+ "invalid type: map, expected a string with a length of more than 0" ,
104
+ )
105
+ }
86
106
87
- assert ! ( e. is_err( ) ) ;
88
- // assert_matches!(e, Ok(expected))
107
+ #[ test]
108
+ fn deserialize_sequence_fails ( ) {
109
+ deserialize_x_fails (
110
+ json ! ( [ ] ) ,
111
+ "invalid type: sequence, expected a string with a length of more than 0" ,
112
+ )
89
113
}
90
114
}
0 commit comments