use super::*;
#[test]
fn remove_preserving_order_keeps_remaining_entries_in_original_order() {
let mut map: serde_json::Map<String, serde_json::Value> = [
("z", serde_json::json!(1)),
("m", serde_json::json!({"nested": [2, 3]})),
("a", serde_json::Value::Null),
("y", serde_json::json!(4)),
]
.into_iter()
.map(|(key, value)| (key.to_owned(), value))
.collect();
let expected: Vec<_> = map
.iter()
.filter(|(key, _)| key.as_str() != "m")
.map(|(key, value)| (key.clone(), value.clone()))
.collect();
assert_eq!(
remove_preserving_order(&mut map, "m"),
Some(serde_json::json!({"nested": [2, 3]}))
);
assert_eq!(map.into_iter().collect::<Vec<_>>(), expected);
}
#[test]
fn remove_preserving_order_distinguishes_missing_and_null_values() {
let mut map = serde_json::Map::new();
map.insert("z".to_owned(), serde_json::json!(1));
map.insert("a".to_owned(), serde_json::Value::Null);
let before: Vec<_> = map.clone().into_iter().collect();
assert_eq!(remove_preserving_order(&mut map, "missing"), None);
assert_eq!(map.clone().into_iter().collect::<Vec<_>>(), before);
assert_eq!(
remove_preserving_order(&mut map, "a"),
Some(serde_json::Value::Null)
);
assert_eq!(remove_preserving_order(&mut map, "a"), None);
}
#[test]
fn text_that_states_no_object_has_none() {
for text in ["not json", "[1, 2", "\"str", "12"] {
assert_eq!(parse_partial_object(text), None, "{text}");
}
}
mod partial_arguments {
use super::super::parse_partial_arguments;
use serde_json::{Value, json};
fn parsed(text: &str) -> Value {
Value::Object(parse_partial_arguments(text))
}
#[test]
fn cut_off_text_keeps_what_it_states() {
for (text, expected) in [
("", json!({})),
(" ", json!({})),
("{", json!({})),
(r#"{"path"#, json!({})),
(r#"{"path":"#, json!({})),
(r#"{"path": "no"#, json!({"path": "no"})),
(r#"{"path": "notes.md","#, json!({"path": "notes.md"})),
(r#"{"path": "notes.md", "con"#, json!({"path": "notes.md"})),
(r#"{"a": {"b": [1, 2"#, json!({"a": {"b": [1, 2]}})),
(r#"{"a": [{"b": "c"}, {"d"#, json!({"a": [{"b": "c"}, {}]})),
(r#"{"a": 1, "b": 2,"#, json!({"a": 1, "b": 2})),
(r#"{"line": "one\ntw"#, json!({"line": "one\ntw"})),
(r#"{"quote": "say \"hi"#, json!({"quote": "say \"hi"})),
] {
assert_eq!(parsed(text), expected, "{text}");
}
}
#[test]
fn escapes_cut_short_keep_the_string_before_them() {
for (text, expected) in [
(r#"{"a": "x\"#, json!({"a": "x"})),
(r#"{"a": "x\u"#, json!({"a": "x"})),
(r#"{"a": "x\u00"#, json!({"a": "x"})),
(r#"{"a": "x\u00e"#, json!({"a": "x"})),
(r#"{"a": "x\u00e9"#, json!({"a": "xé"})),
(r#"{"a": "x\\u00"#, json!({"a": "x\\u00"})),
] {
assert_eq!(parsed(text), expected, "{text}");
}
}
#[test]
fn numbers_and_literals_cut_short_are_dropped_until_valid() {
for (text, expected) in [
(r#"{"n": 1"#, json!({"n": 1})),
(r#"{"n": 1."#, json!({})),
(r#"{"n": -"#, json!({})),
(r#"{"n": 1e"#, json!({})),
(r#"{"n": 1.5"#, json!({"n": 1.5})),
(r#"{"b": tru"#, json!({})),
(r#"{"b": true"#, json!({"b": true})),
(r#"{"v": nul"#, json!({})),
(r#"{"a": 1, "v": nul"#, json!({"a": 1})),
] {
assert_eq!(parsed(text), expected, "{text}");
}
}
#[test]
fn raw_control_characters_and_stray_backslashes_are_repaired() {
assert_eq!(
parsed("{\"text\": \"line one\nline two\ttabbed\"}"),
json!({"text": "line one\nline two\ttabbed"})
);
assert_eq!(
parsed(r#"{"path": "C:\Users\me"}"#),
json!({"path": "C:\\Users\\me"})
);
assert_eq!(
parsed("{\"text\": \"cut\nshort"),
json!({"text": "cut\nshort"})
);
}
#[test]
fn a_complete_object_parses_as_serde_does() {
let text = r#"{"path": "a.md", "content": "x\ny \"q\" \u00e9", "n": [1, 2.5, -3e2], "o": {"t": true, "f": null}}"#;
assert_eq!(
parsed(text),
serde_json::from_str::<Value>(text).expect("valid json")
);
}
#[test]
fn a_top_level_that_is_not_an_object_is_empty() {
for text in [
"[1, 2]", "[1, 2", "\"str\"", "\"str", "12", "true", "null", "not json",
] {
assert_eq!(parsed(text), json!({}), "{text}");
}
}
#[test]
fn every_prefix_parses_and_the_whole_text_parses_exactly() {
for whole in [
json!({"path": "notes/today.md", "content": "Line one.\nLine \"two\" \\ é ✓\n\tIndented."}),
json!({"x": 1, "y": -2.5, "z": 3e10, "flags": [true, false, null], "nested": {"a": [{"b": "c"}]}}),
json!({"query": "", "limit": 0, "filters": {}, "tags": []}),
] {
for text in [
whole.to_string(),
serde_json::to_string_pretty(&whole).expect("json"),
] {
for (cut, _) in text.char_indices() {
let _ = parse_partial_arguments(&text[..cut]);
}
assert_eq!(parsed(&text), whole);
}
}
}
}
#[test]
fn json_len_counts_the_serialized_bytes_and_is_zero_on_failure() {
let value = serde_json::json!({"text": "héllo", "list": [1, 2.5, null]});
assert_eq!(json_len(&value), value.to_string().len());
let unserializable: std::collections::BTreeMap<(u8, u8), u8> = [((1, 2), 3)].into();
assert_eq!(json_len(&unserializable), 0);
}
struct Binary(f64);
impl<'de> Deserializer<'de> for Binary {
type Error = serde::de::value::Error;
fn deserialize_any<V: serde::de::Visitor<'de>>(self, _: V) -> Result<V::Value, Self::Error> {
Err(serde::de::Error::custom("not self-describing"))
}
fn deserialize_f64<V: serde::de::Visitor<'de>>(
self,
visitor: V,
) -> Result<V::Value, Self::Error> {
visitor.visit_f64(self.0)
}
fn is_human_readable(&self) -> bool {
false
}
serde::forward_to_deserialize_any! {
bool i8 i16 i32 i64 i128 u8 u16 u32 u64 u128 f32 char str string bytes
byte_buf option unit unit_struct newtype_struct seq tuple tuple_struct
map struct enum identifier ignored_any
}
}
#[test]
fn floats_reads_a_binary_format_directly() {
let read: Result<f64, _> = floats(Binary(0.5));
assert_eq!(read.ok(), Some(0.5));
}