From e7fbcc7a79b1d1c8b276a501c985aeaf1f570c2d Mon Sep 17 00:00:00 2001 From: Marcono1234 Date: Sat, 23 Dec 2023 23:40:06 +0100 Subject: [PATCH] Switch left and right `assert_eq!` args in examples This reads a bit more natural when left is the actual and right is the expected value. However, the tests still have the expected argument left, see also #29. --- README.md | 9 +++++---- src/lib.rs | 9 +++++---- src/reader/mod.rs | 43 ++++++++++++++++++++++--------------------- src/serde/de.rs | 4 ++-- src/serde/mod.rs | 9 +++++---- src/serde/ser.rs | 5 +++-- src/writer/mod.rs | 43 ++++++++++++++++++++++++++++--------------- 7 files changed, 70 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 59fc05c..eddf0d3 100644 --- a/README.md +++ b/README.md @@ -48,11 +48,11 @@ let json = r#"{"a": [1, true]}"#; let mut json_reader = JsonStreamReader::new(json.as_bytes()); json_reader.begin_object()?; -assert_eq!("a", json_reader.next_name()?); +assert_eq!(json_reader.next_name()?, "a"); json_reader.begin_array()?; -assert_eq!(1_u32, json_reader.next_number()??); -assert_eq!(true, json_reader.next_bool()?); +assert_eq!(json_reader.next_number::()??, 1); +assert_eq!(json_reader.next_bool()?, true); json_reader.end_array()?; json_reader.end_object()?; @@ -81,7 +81,8 @@ json_writer.end_object()?; // Ensures that the JSON document is complete and flushes the buffer json_writer.finish_document()?; -assert_eq!(r#"{"a":[1,true]}"#, String::from_utf8(writer)?); +let json = String::from_utf8(writer)?; +assert_eq!(json, r#"{"a":[1,true]}"#); ``` ## Serde integration diff --git a/src/lib.rs b/src/lib.rs index db4dcf9..8bb22e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -46,11 +46,11 @@ //! let mut json_reader = JsonStreamReader::new(json.as_bytes()); //! //! json_reader.begin_object()?; -//! assert_eq!("a", json_reader.next_name()?); +//! assert_eq!(json_reader.next_name()?, "a"); //! //! json_reader.begin_array()?; -//! assert_eq!(1_u32, json_reader.next_number()??); -//! assert_eq!(true, json_reader.next_bool()?); +//! assert_eq!(json_reader.next_number::()??, 1); +//! assert_eq!(json_reader.next_bool()?, true); //! json_reader.end_array()?; //! //! json_reader.end_object()?; @@ -79,7 +79,8 @@ //! // Ensures that the JSON document is complete and flushes the buffer //! json_writer.finish_document()?; //! -//! assert_eq!(r#"{"a":[1,true]}"#, String::from_utf8(writer)?); +//! let json = String::from_utf8(writer)?; +//! assert_eq!(json, r#"{"a":[1,true]}"#); //! # Ok::<(), Box>(()) //! ``` //! diff --git a/src/reader/mod.rs b/src/reader/mod.rs index 6376d4e..45d69ae 100644 --- a/src/reader/mod.rs +++ b/src/reader/mod.rs @@ -845,9 +845,9 @@ pub enum TransferError { /// assert_eq!(json_reader.next_name()?, "a"); /// /// json_reader.begin_array()?; -/// assert_eq!(1_u32, json_reader.next_number()??); +/// assert_eq!(json_reader.next_number::()??, 1); /// json_reader.skip_value()?; -/// assert_eq!(true, json_reader.next_bool()?); +/// assert_eq!(json_reader.next_bool()?, true); /// json_reader.end_array()?; /// /// json_reader.end_object()?; @@ -1078,7 +1078,7 @@ pub trait JsonReader { /// /// json_reader.skip_value()?; /// // Array does not have a next item anymore - /// assert_eq!(false, json_reader.has_next()?); + /// assert_eq!(json_reader.has_next()?, false); /// # Ok::<(), Box>(()) /// ``` /// @@ -1114,7 +1114,7 @@ pub trait JsonReader { /// # use struson::reader::*; /// let mut json_reader = JsonStreamReader::new(r#"{"a": 1}"#.as_bytes()); /// json_reader.begin_object()?; - /// assert_eq!("a", json_reader.next_name()?); + /// assert_eq!(json_reader.next_name()?, "a"); /// # Ok::<(), Box>(()) /// ``` /// @@ -1158,7 +1158,7 @@ pub trait JsonReader { /// ``` /// # use struson::reader::*; /// let mut json_reader = JsonStreamReader::new(r#""text with \"quotes\"""#.as_bytes()); - /// assert_eq!("text with \"quotes\"", json_reader.next_str()?); + /// assert_eq!(json_reader.next_str()?, "text with \"quotes\""); /// # Ok::<(), Box>(()) /// ``` /// @@ -1267,7 +1267,7 @@ pub trait JsonReader { /// ``` /// # use struson::reader::*; /// let mut json_reader = JsonStreamReader::new("12".as_bytes()); - /// assert_eq!(12_u32, json_reader.next_number()??); + /// assert_eq!(json_reader.next_number::()??, 12); /// # Ok::<(), Box>(()) /// ``` /// @@ -1320,7 +1320,7 @@ pub trait JsonReader { /// ``` /// # use struson::reader::*; /// let mut json_reader = JsonStreamReader::new("12.0e5".as_bytes()); - /// assert_eq!("12.0e5", json_reader.next_number_as_str()?); + /// assert_eq!(json_reader.next_number_as_str()?, "12.0e5"); /// # Ok::<(), Box>(()) /// ``` /// @@ -1360,7 +1360,7 @@ pub trait JsonReader { /// ``` /// # use struson::reader::*; /// let mut json_reader = JsonStreamReader::new("true".as_bytes()); - /// assert_eq!(true, json_reader.next_bool()?); + /// assert_eq!(json_reader.next_bool()?, true); /// # Ok::<(), Box>(()) /// ``` /// @@ -1450,8 +1450,8 @@ pub trait JsonReader { /// json_reader.consume_trailing_whitespace()?; /// /// assert_eq!( - /// MyStruct { text: "some text".to_owned(), number: 5 }, - /// value + /// value, + /// MyStruct { text: "some text".to_owned(), number: 5 } /// ); /// # Ok::<(), Box>(()) /// ``` @@ -1497,7 +1497,7 @@ pub trait JsonReader { /// // Skip member name "a" /// json_reader.skip_name()?; /// - /// assert_eq!("1", json_reader.next_number_as_str()?); + /// assert_eq!(json_reader.next_number_as_str()?, "1"); /// # Ok::<(), Box>(()) /// ``` /// @@ -1530,12 +1530,12 @@ pub trait JsonReader { /// let mut json_reader = JsonStreamReader::new(r#"{"a": [{}], "b": 1}"#.as_bytes()); /// json_reader.begin_object()?; /// - /// assert_eq!("a", json_reader.next_name()?); + /// assert_eq!(json_reader.next_name()?, "a"); /// /// // Skip member value [{}] /// json_reader.skip_value()?; /// - /// assert_eq!("b", json_reader.next_name()?); + /// assert_eq!(json_reader.next_name()?, "b"); /// # Ok::<(), Box>(()) /// ``` /// @@ -1561,11 +1561,11 @@ pub trait JsonReader { /// in the path. Once this method returns successfully the reader will be positioned /// before the last element specified by the path. /// - /// For example for the JSON path `foo[2]` it will start consuming a JSON object, skipping members - /// until it finds one with name "foo". Then it starts consuming the member value, expecting that - /// it is a JSON array, until right before the array item with (starting at 0) index 2. - /// If multiple members in a JSON object have the same name (for example `{"a": 1, "a": 2}`) - /// this method will seek to the first occurrence. + /// For example for the JSON path `json_path!["foo", 2]` it will start consuming a JSON object, + /// skipping members until it finds one with name "foo". Then it starts consuming the member + /// value, expecting that it is a JSON array, until right before the array item with (starting + /// at 0) index 2. If multiple members in a JSON object have the same name (for example + /// `{"a": 1, "a": 2}`) this method will seek to the first occurrence. /// /// Seeking to a specific location can be useful when parts of the processed JSON document /// are not relevant for the application processing it. @@ -1580,7 +1580,7 @@ pub trait JsonReader { /// json_reader.seek_to(&json_path!["foo", 2])?; /// /// // Can now consume the value to which the call seeked to - /// assert_eq!("c", json_reader.next_str()?); + /// assert_eq!(json_reader.next_str()?, "c"); /// # Ok::<(), Box>(()) /// ``` /// @@ -1682,7 +1682,7 @@ pub trait JsonReader { /// json_reader.seek_to(&json_path!["foo", 2])?; /// /// // Consume the value to which the call seeked to - /// assert_eq!("c", json_reader.next_str()?); + /// assert_eq!(json_reader.next_str()?, "c"); /// /// // Skip the remainder of the document /// json_reader.skip_to_top_level()?; @@ -1731,7 +1731,8 @@ pub trait JsonReader { /// json_writer.end_object()?; /// json_writer.finish_document()?; /// - /// assert_eq!(r#"{"embedded":[1,2]}"#, String::from_utf8(writer)?); + /// let json = String::from_utf8(writer)?; + /// assert_eq!(json, r#"{"embedded":[1,2]}"#); /// # Ok::<(), Box>(()) /// ``` /// diff --git a/src/serde/de.rs b/src/serde/de.rs index f18f919..6461e4b 100644 --- a/src/serde/de.rs +++ b/src/serde/de.rs @@ -145,8 +145,8 @@ impl From for DeserializerError { /// json_reader.consume_trailing_whitespace()?; /// /// assert_eq!( -/// MyStruct { text: "some text".to_owned(), number: 5 }, -/// value +/// value, +/// MyStruct { text: "some text".to_owned(), number: 5 } /// ); /// # Ok::<(), Box>(()) /// ``` diff --git a/src/serde/mod.rs b/src/serde/mod.rs index b2b1df5..9db28f8 100644 --- a/src/serde/mod.rs +++ b/src/serde/mod.rs @@ -58,9 +58,10 @@ //! // Ensures that the JSON document is complete and flushes the buffer //! json_writer.finish_document()?; //! +//! let json = String::from_utf8(writer)?; //! assert_eq!( -//! r#"{"outer":{"text":"some text","number":5}}"#, -//! String::from_utf8(writer)? +//! json, +//! r#"{"outer":{"text":"some text","number":5}}"# //! ); //! # Ok::<(), Box>(()) //! ``` @@ -93,8 +94,8 @@ //! json_reader.consume_trailing_whitespace()?; //! //! assert_eq!( -//! MyStruct { text: "some text".to_owned(), number: 5 }, -//! value +//! value, +//! MyStruct { text: "some text".to_owned(), number: 5 } //! ); //! # Ok::<(), Box>(()) //! ``` diff --git a/src/serde/ser.rs b/src/serde/ser.rs index 2815554..10a6512 100644 --- a/src/serde/ser.rs +++ b/src/serde/ser.rs @@ -99,9 +99,10 @@ impl serde::ser::Error for SerializerError { /// // Ensures that the JSON document is complete and flushes the buffer /// json_writer.finish_document()?; /// +/// let json = String::from_utf8(writer)?; /// assert_eq!( -/// r#"{"text":"some text","number":5}"#, -/// String::from_utf8(writer)? +/// json, +/// r#"{"text":"some text","number":5}"# /// ); /// # Ok::<(), Box>(()) /// ``` diff --git a/src/writer/mod.rs b/src/writer/mod.rs index 189422e..227fa6d 100644 --- a/src/writer/mod.rs +++ b/src/writer/mod.rs @@ -66,7 +66,8 @@ type IoError = std::io::Error; /// // Ensures that the JSON document is complete and flushes the buffer /// json_writer.finish_document()?; /// -/// assert_eq!(r#"{"a":[1,true]}"#, String::from_utf8(writer)?); +/// let json = String::from_utf8(writer)?; +/// assert_eq!(json, r#"{"a":[1,true]}"#); /// # Ok::<(), Box>(()) /// ``` /// @@ -107,7 +108,8 @@ pub trait JsonWriter { /// /// json_writer.finish_document()?; /// - /// assert_eq!(r#"{"a":1}"#, String::from_utf8(writer)?); + /// let json = String::from_utf8(writer)?; + /// assert_eq!(json, r#"{"a":1}"#); /// # Ok::<(), Box>(()) /// ``` /// @@ -148,7 +150,8 @@ pub trait JsonWriter { /// /// json_writer.finish_document()?; /// - /// assert_eq!("[1]", String::from_utf8(writer)?); + /// let json = String::from_utf8(writer)?; + /// assert_eq!(json, "[1]"); /// # Ok::<(), Box>(()) /// ``` /// @@ -194,7 +197,8 @@ pub trait JsonWriter { /// /// json_writer.finish_document()?; /// - /// assert_eq!(r#"{"a":1}"#, String::from_utf8(writer)?); + /// let json = String::from_utf8(writer)?; + /// assert_eq!(json, r#"{"a":1}"#); /// # Ok::<(), Box>(()) /// ``` /// @@ -218,7 +222,8 @@ pub trait JsonWriter { /// /// json_writer.finish_document()?; /// - /// assert_eq!("null", String::from_utf8(writer)?); + /// let json = String::from_utf8(writer)?; + /// assert_eq!(json, "null"); /// # Ok::<(), Box>(()) /// ``` /// @@ -241,7 +246,8 @@ pub trait JsonWriter { /// /// json_writer.finish_document()?; /// - /// assert_eq!("true", String::from_utf8(writer)?); + /// let json = String::from_utf8(writer)?; + /// assert_eq!(json, "true"); /// # Ok::<(), Box>(()) /// ``` /// @@ -274,7 +280,8 @@ pub trait JsonWriter { /// /// json_writer.finish_document()?; /// - /// assert_eq!(r#""text with \"quotes\"""#, String::from_utf8(writer)?); + /// let json = String::from_utf8(writer)?; + /// assert_eq!(json, r#""text with \"quotes\"""#); /// # Ok::<(), Box>(()) /// ``` /// @@ -316,7 +323,8 @@ pub trait JsonWriter { /// /// json_writer.finish_document()?; /// - /// assert_eq!(r#""text with \"quotes\"""#, String::from_utf8(writer)?); + /// let json = String::from_utf8(writer)?; + /// assert_eq!(json, r#""text with \"quotes\"""#); /// # Ok::<(), Box>(()) /// ``` /// @@ -357,7 +365,8 @@ pub trait JsonWriter { /// /// json_writer.finish_document()?; /// - /// assert_eq!("123.0e10", String::from_utf8(writer)?); + /// let json = String::from_utf8(writer)?; + /// assert_eq!(json, "123.0e10"); /// # Ok::<(), Box>(()) /// ``` /// @@ -395,7 +404,8 @@ pub trait JsonWriter { /// /// json_writer.finish_document()?; /// - /// assert_eq!("123", String::from_utf8(writer)?); + /// let json = String::from_utf8(writer)?; + /// assert_eq!(json, "123"); /// # Ok::<(), Box>(()) /// ``` /// @@ -424,7 +434,8 @@ pub trait JsonWriter { /// /// json_writer.finish_document()?; /// - /// assert_eq!("4.5", String::from_utf8(writer)?); + /// let json = String::from_utf8(writer)?; + /// assert_eq!(json, "4.5"); /// # Ok::<(), Box>(()) /// ``` /// @@ -440,7 +451,7 @@ pub trait JsonWriter { /// usage by the user. /* * TODO: Maybe give this method a better name? - * TODO: Maybe also support writing in scientific notation, e.g. `4.1e20`, see also https://doc.rust-lang.org/std/fmt/trait.LowerExp.html + * TODO: Maybe also support writing in scientific notation? e.g. `4.1e20`, see also https://doc.rust-lang.org/std/fmt/trait.LowerExp.html */ fn fp_number_value(&mut self, value: N) -> Result<(), JsonNumberError>; @@ -486,9 +497,10 @@ pub trait JsonWriter { /// // Ensures that the JSON document is complete and flushes the buffer /// json_writer.finish_document()?; /// + /// let json = String::from_utf8(writer)?; /// assert_eq!( - /// r#"{"outer":{"text":"some text","number":5}}"#, - /// String::from_utf8(writer)? + /// json, + /// r#"{"outer":{"text":"some text","number":5}}"# /// ); /// # Ok::<(), Box>(()) /// ``` @@ -587,7 +599,8 @@ pub trait StringValueWriter: Write { /// /// json_writer.finish_document()?; /// - /// assert_eq!("\"one, two, three and four\"", String::from_utf8(writer)?); + /// let json = String::from_utf8(writer)?; + /// assert_eq!(json, "\"one, two, three and four\""); /// # Ok::<(), Box>(()) /// ``` fn write_str(&mut self, s: &str) -> Result<(), IoError> {