diff options
| author | Mark Rousskov <mark.simulacrum@gmail.com> | 2022-02-09 17:05:44 -0500 |
|---|---|---|
| committer | Mark Rousskov <mark.simulacrum@gmail.com> | 2022-02-20 18:58:21 -0500 |
| commit | 60b71f56e73fc139eababa00aa8f1b740cb7d445 (patch) | |
| tree | 497c5e5168d7d22c777471fbc199da6cc5c575ae /compiler/rustc_serialize/tests/json.rs | |
| parent | 45e2c2881d11324d610815bfff097e25c412199e (diff) | |
| download | rust-60b71f56e73fc139eababa00aa8f1b740cb7d445.tar.gz rust-60b71f56e73fc139eababa00aa8f1b740cb7d445.zip | |
Remove support for JSON deserialization to Rust
This is no longer used by the compiler itself, and removing this support opens the door to massively simplifying the Decodable/Decoder API by dropping the self-describing deserialization support (necessary for JSON).
Diffstat (limited to 'compiler/rustc_serialize/tests/json.rs')
| -rw-r--r-- | compiler/rustc_serialize/tests/json.rs | 311 |
1 files changed, 8 insertions, 303 deletions
diff --git a/compiler/rustc_serialize/tests/json.rs b/compiler/rustc_serialize/tests/json.rs index ede912bdfb6..944fe460632 100644 --- a/compiler/rustc_serialize/tests/json.rs +++ b/compiler/rustc_serialize/tests/json.rs @@ -4,61 +4,35 @@ use json::ErrorCode::*; use json::Json::*; use json::JsonEvent::*; use json::ParserError::*; -use json::{from_str, Decoder, Encoder, EncoderError, Json, JsonEvent, Parser, StackElement}; -use rustc_macros::{Decodable, Encodable}; +use json::{from_str, Encoder, EncoderError, Json, JsonEvent, Parser, StackElement}; +use rustc_macros::Encodable; use rustc_serialize::json; -use rustc_serialize::{Decodable, Encodable}; +use rustc_serialize::Encodable; use std::collections::BTreeMap; use std::io::prelude::*; use std::string; use Animal::*; -#[derive(Decodable, Eq, PartialEq, Debug)] +#[derive(Eq, PartialEq, Debug)] struct OptionData { opt: Option<usize>, } -#[test] -fn test_decode_option_none() { - let s = "{}"; - let obj: OptionData = json::decode(s); - assert_eq!(obj, OptionData { opt: None }); -} - -#[test] -fn test_decode_option_some() { - let s = "{ \"opt\": 10 }"; - let obj: OptionData = json::decode(s); - assert_eq!(obj, OptionData { opt: Some(10) }); -} - -#[test] -#[should_panic(expected = r#"ExpectedError("Number", "[]")"#)] -fn test_decode_option_malformed1() { - check_err::<OptionData>(r#"{ "opt": [] }"#); -} - -#[test] -#[should_panic(expected = r#"ExpectedError("Number", "false")"#)] -fn test_decode_option_malformed2() { - check_err::<OptionData>(r#"{ "opt": false }"#); -} - -#[derive(PartialEq, Encodable, Decodable, Debug)] +#[derive(PartialEq, Encodable, Debug)] enum Animal { Dog, Frog(string::String, isize), } -#[derive(PartialEq, Encodable, Decodable, Debug)] +#[derive(PartialEq, Encodable, Debug)] struct Inner { a: (), b: usize, c: Vec<string::String>, } -#[derive(PartialEq, Encodable, Decodable, Debug)] +#[derive(PartialEq, Encodable, Debug)] struct Outer { inner: Vec<Inner>, } @@ -324,18 +298,6 @@ fn test_read_identifiers() { } #[test] -fn test_decode_identifiers() { - let v: () = json::decode("null"); - assert_eq!(v, ()); - - let v: bool = json::decode("true"); - assert_eq!(v, true); - - let v: bool = json::decode("false"); - assert_eq!(v, false); -} - -#[test] fn test_read_number() { assert_eq!(from_str("+"), Err(SyntaxError(InvalidSyntax, 1, 1))); assert_eq!(from_str("."), Err(SyntaxError(InvalidSyntax, 1, 1))); @@ -364,45 +326,6 @@ fn test_read_number() { } #[test] -#[should_panic(expected = r#"ExpectedError("Integer", "765.25")"#)] -fn test_decode_numbers() { - let v: f64 = json::decode("3"); - assert_eq!(v, 3.0); - - let v: f64 = json::decode("3.1"); - assert_eq!(v, 3.1); - - let v: f64 = json::decode("-1.2"); - assert_eq!(v, -1.2); - - let v: f64 = json::decode("0.4"); - assert_eq!(v, 0.4); - - let v: f64 = json::decode("0.4e5"); - assert_eq!(v, 0.4e5); - - let v: f64 = json::decode("0.4e15"); - assert_eq!(v, 0.4e15); - - let v: f64 = json::decode("0.4e-01"); - assert_eq!(v, 0.4e-01); - - let v: u64 = json::decode("0"); - assert_eq!(v, 0); - - let v: u64 = json::decode("18446744073709551615"); - assert_eq!(v, u64::MAX); - - let v: i64 = json::decode("-9223372036854775808"); - assert_eq!(v, i64::MIN); - - let v: i64 = json::decode("9223372036854775807"); - assert_eq!(v, i64::MAX); - - json::decode::<i64>("765.25"); -} - -#[test] fn test_read_str() { assert_eq!(from_str("\""), Err(SyntaxError(EOFWhileParsingString, 1, 2))); assert_eq!(from_str("\"lol"), Err(SyntaxError(EOFWhileParsingString, 1, 5))); @@ -420,26 +343,6 @@ fn test_read_str() { } #[test] -fn test_decode_str() { - let s = [ - ("\"\"", ""), - ("\"foo\"", "foo"), - ("\"\\\"\"", "\""), - ("\"\\b\"", "\x08"), - ("\"\\n\"", "\n"), - ("\"\\r\"", "\r"), - ("\"\\t\"", "\t"), - ("\"\\u12ab\"", "\u{12ab}"), - ("\"\\uAB12\"", "\u{AB12}"), - ]; - - for (i, o) in s { - let v: string::String = json::decode(i); - assert_eq!(v, o); - } -} - -#[test] fn test_read_array() { assert_eq!(from_str("["), Err(SyntaxError(EOFWhileParsingValue, 1, 2))); assert_eq!(from_str("[1"), Err(SyntaxError(EOFWhileParsingArray, 1, 3))); @@ -458,45 +361,6 @@ fn test_read_array() { } #[test] -fn test_decode_array() { - let v: Vec<()> = json::decode("[]"); - assert_eq!(v, []); - - let v: Vec<()> = json::decode("[null]"); - assert_eq!(v, [()]); - - let v: Vec<bool> = json::decode("[true]"); - assert_eq!(v, [true]); - - let v: Vec<isize> = json::decode("[3, 1]"); - assert_eq!(v, [3, 1]); - - let v: Vec<Vec<usize>> = json::decode("[[3], [1, 2]]"); - assert_eq!(v, [vec![3], vec![1, 2]]); -} - -#[test] -fn test_decode_tuple() { - let t: (usize, usize, usize) = json::decode("[1, 2, 3]"); - assert_eq!(t, (1, 2, 3)); - - let t: (usize, string::String) = json::decode("[1, \"two\"]"); - assert_eq!(t, (1, "two".to_string())); -} - -#[test] -#[should_panic] -fn test_decode_tuple_malformed_types() { - json::decode::<(usize, string::String)>("[1, 2]"); -} - -#[test] -#[should_panic] -fn test_decode_tuple_malformed_length() { - json::decode::<(usize, usize)>("[1, 2, 3]"); -} - -#[test] fn test_read_object() { assert_eq!(from_str("{"), Err(SyntaxError(EOFWhileParsingObject, 1, 2))); assert_eq!(from_str("{ "), Err(SyntaxError(EOFWhileParsingObject, 1, 3))); @@ -553,142 +417,10 @@ fn test_read_object() { } #[test] -fn test_decode_struct() { - let s = "{ - \"inner\": [ - { \"a\": null, \"b\": 2, \"c\": [\"abc\", \"xyz\"] } - ] - }"; - - let v: Outer = json::decode(s); - assert_eq!( - v, - Outer { inner: vec![Inner { a: (), b: 2, c: vec!["abc".to_string(), "xyz".to_string()] }] } - ); -} - -#[derive(Decodable)] -struct FloatStruct { - f: f64, - a: Vec<f64>, -} -#[test] -fn test_decode_struct_with_nan() { - let s = "{\"f\":null,\"a\":[null,123]}"; - let obj: FloatStruct = json::decode(s); - assert!(obj.f.is_nan()); - assert!(obj.a[0].is_nan()); - assert_eq!(obj.a[1], 123f64); -} - -#[test] -fn test_decode_option() { - let value: Option<string::String> = json::decode("null"); - assert_eq!(value, None); - - let value: Option<string::String> = json::decode("\"jodhpurs\""); - assert_eq!(value, Some("jodhpurs".to_string())); -} - -#[test] -fn test_decode_enum() { - let value: Animal = json::decode("\"Dog\""); - assert_eq!(value, Dog); - - let s = "{\"variant\":\"Frog\",\"fields\":[\"Henry\",349]}"; - let value: Animal = json::decode(s); - assert_eq!(value, Frog("Henry".to_string(), 349)); -} - -#[test] -fn test_decode_map() { - let s = "{\"a\": \"Dog\", \"b\": {\"variant\":\"Frog\",\ - \"fields\":[\"Henry\", 349]}}"; - let mut map: BTreeMap<string::String, Animal> = json::decode(s); - - assert_eq!(map.remove(&"a".to_string()), Some(Dog)); - assert_eq!(map.remove(&"b".to_string()), Some(Frog("Henry".to_string(), 349))); -} - -#[test] fn test_multiline_errors() { assert_eq!(from_str("{\n \"foo\":\n \"bar\""), Err(SyntaxError(EOFWhileParsingObject, 3, 8))); } -#[derive(Decodable)] -#[allow(dead_code)] -struct DecodeStruct { - x: f64, - y: bool, - z: string::String, - w: Vec<DecodeStruct>, -} -#[derive(Decodable)] -enum DecodeEnum { - A(f64), - B(string::String), -} -fn check_err<T: Decodable<Decoder>>(to_parse: &str) { - let json = from_str(to_parse).unwrap(); - let _: T = Decodable::decode(&mut Decoder::new(json)); -} -#[test] -#[should_panic(expected = r#"ExpectedError("Object", "[]")"#)] -fn test_decode_errors_struct1() { - check_err::<DecodeStruct>("[]"); -} -#[test] -#[should_panic(expected = r#"ExpectedError("Number", "true")"#)] -fn test_decode_errors_struct2() { - check_err::<DecodeStruct>(r#"{"x": true, "y": true, "z": "", "w": []}"#); -} -#[test] -#[should_panic(expected = r#"ExpectedError("Boolean", "[]")"#)] -fn test_decode_errors_struct3() { - check_err::<DecodeStruct>(r#"{"x": 1, "y": [], "z": "", "w": []}"#); -} -#[test] -#[should_panic(expected = r#"ExpectedError("String", "{}")"#)] -fn test_decode_errors_struct4() { - check_err::<DecodeStruct>(r#"{"x": 1, "y": true, "z": {}, "w": []}"#); -} -#[test] -#[should_panic(expected = r#"ExpectedError("Array", "null")"#)] -fn test_decode_errors_struct5() { - check_err::<DecodeStruct>(r#"{"x": 1, "y": true, "z": "", "w": null}"#); -} -#[test] -#[should_panic(expected = r#"ExpectedError("Array", "null")"#)] -fn test_decode_errors_struct6() { - check_err::<DecodeStruct>(r#"{"x": 1, "y": true, "z": ""}"#); -} - -#[test] -#[should_panic(expected = r#"MissingFieldError("variant")"#)] -fn test_decode_errors_enum1() { - check_err::<DecodeEnum>(r#"{}"#); -} -#[test] -#[should_panic(expected = r#"ExpectedError("String", "1")"#)] -fn test_decode_errors_enum2() { - check_err::<DecodeEnum>(r#"{"variant": 1}"#); -} -#[test] -#[should_panic(expected = r#"MissingFieldError("fields")"#)] -fn test_decode_errors_enum3() { - check_err::<DecodeEnum>(r#"{"variant": "A"}"#); -} -#[test] -#[should_panic(expected = r#"ExpectedError("Array", "null")"#)] -fn test_decode_errors_enum4() { - check_err::<DecodeEnum>(r#"{"variant": "A", "fields": null}"#); -} -#[test] -#[should_panic(expected = r#"UnknownVariantError("C")"#)] -fn test_decode_errors_enum5() { - check_err::<DecodeEnum>(r#"{"variant": "C", "fields": []}"#); -} - #[test] fn test_find() { let json_value = from_str("{\"dog\" : \"cat\"}").unwrap(); @@ -938,7 +670,7 @@ fn test_prettyencoder_indent_level_param() { #[test] fn test_hashmap_with_enum_key() { use std::collections::HashMap; - #[derive(Encodable, Eq, Hash, PartialEq, Decodable, Debug)] + #[derive(Encodable, Eq, Hash, PartialEq, Debug)] enum Enum { Foo, #[allow(dead_code)] @@ -948,33 +680,6 @@ fn test_hashmap_with_enum_key() { map.insert(Enum::Foo, 0); let result = json::encode(&map).unwrap(); assert_eq!(&result[..], r#"{"Foo":0}"#); - let decoded: HashMap<Enum, _> = json::decode(&result); - assert_eq!(map, decoded); -} - -#[test] -fn test_hashmap_with_numeric_key_can_handle_double_quote_delimited_key() { - use std::collections::HashMap; - let json_str = "{\"1\":true}"; - let json_obj = match from_str(json_str) { - Err(_) => panic!("Unable to parse json_str: {:?}", json_str), - Ok(o) => o, - }; - let mut decoder = Decoder::new(json_obj); - let _hm: HashMap<usize, bool> = Decodable::decode(&mut decoder); -} - -#[test] -#[should_panic(expected = r#"ExpectedError("Number", "a")"#)] -fn test_hashmap_with_numeric_key_will_error_with_string_keys() { - use std::collections::HashMap; - let json_str = "{\"a\":true}"; - let json_obj = match from_str(json_str) { - Err(_) => panic!("Unable to parse json_str: {:?}", json_str), - Ok(o) => o, - }; - let mut decoder = Decoder::new(json_obj); - let _: HashMap<usize, bool> = Decodable::decode(&mut decoder); } fn assert_stream_equal(src: &str, expected: Vec<(JsonEvent, Vec<StackElement<'_>>)>) { |
