@@ -27,15 +27,11 @@ impl<'de> de::Deserialize<'de> for NonEmptyString {
2727 }
2828}
2929
30- pub enum DeserializeError { }
31-
32- type Result < T , E = DeserializeError > = std:: result:: Result < T , E > ;
33-
3430impl < ' de > Visitor < ' de > for NonEmptyStringVisitor {
3531 type Value = NonEmptyString ;
3632
3733 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 " )
3935 }
4036
4137 fn visit_str < E > ( self , value : & str ) -> Result < Self :: Value , E >
@@ -55,13 +51,12 @@ impl<'de> Visitor<'de> for NonEmptyStringVisitor {
5551
5652#[ cfg( test) ]
5753mod tests {
58- use super :: * ;
5954 use crate :: * ;
6055 use assert_matches:: assert_matches;
6156 use serde_json:: json;
6257
6358 #[ test]
64- fn serialize_works ( ) {
59+ fn serialize_non_empty_string_and_normal_string_give_the_same_result ( ) {
6560 let value = NonEmptyString ( "abc" . to_owned ( ) ) ;
6661 let result = serde_json:: to_string ( & value) ;
6762
@@ -80,11 +75,40 @@ mod tests {
8075 assert_matches ! ( e, Ok ( v) if v == expected)
8176 }
8277
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+
8383 #[ test]
8484 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+ }
86106
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+ )
89113 }
90114}
0 commit comments