mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-25 16:24:46 +00:00
Fix rustc_serialize unit tests
This commit is contained in:
parent
ae7951ed43
commit
c4f91bb281
@ -3877,6 +3877,7 @@ name = "rustc_serialize"
|
|||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"indexmap",
|
"indexmap",
|
||||||
|
"rustc_macros",
|
||||||
"smallvec 1.4.0",
|
"smallvec 1.4.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -11,3 +11,6 @@ path = "lib.rs"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
indexmap = "1"
|
indexmap = "1"
|
||||||
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
|
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
rustc_macros = { path = "../librustc_macros" }
|
||||||
|
@ -47,17 +47,17 @@
|
|||||||
//!
|
//!
|
||||||
//! Rust provides a mechanism for low boilerplate encoding & decoding of values to and from JSON via
|
//! Rust provides a mechanism for low boilerplate encoding & decoding of values to and from JSON via
|
||||||
//! the serialization API.
|
//! the serialization API.
|
||||||
//! To be able to encode a piece of data, it must implement the `serialize::RustcEncodable` trait.
|
//! To be able to encode a piece of data, it must implement the `serialize::Encodable` trait.
|
||||||
//! To be able to decode a piece of data, it must implement the `serialize::RustcDecodable` trait.
|
//! To be able to decode a piece of data, it must implement the `serialize::Decodable` trait.
|
||||||
//! The Rust compiler provides an annotation to automatically generate the code for these traits:
|
//! The Rust compiler provides an annotation to automatically generate the code for these traits:
|
||||||
//! `#[derive(RustcDecodable, RustcEncodable)]`
|
//! `#[derive(Decodable, Encodable)]`
|
||||||
//!
|
//!
|
||||||
//! The JSON API provides an enum `json::Json` and a trait `ToJson` to encode objects.
|
//! The JSON API provides an enum `json::Json` and a trait `ToJson` to encode objects.
|
||||||
//! The `ToJson` trait provides a `to_json` method to convert an object into a `json::Json` value.
|
//! The `ToJson` trait provides a `to_json` method to convert an object into a `json::Json` value.
|
||||||
//! A `json::Json` value can be encoded as a string or buffer using the functions described above.
|
//! A `json::Json` value can be encoded as a string or buffer using the functions described above.
|
||||||
//! You can also use the `json::Encoder` object, which implements the `Encoder` trait.
|
//! You can also use the `json::Encoder` object, which implements the `Encoder` trait.
|
||||||
//!
|
//!
|
||||||
//! When using `ToJson` the `RustcEncodable` trait implementation is not mandatory.
|
//! When using `ToJson` the `Encodable` trait implementation is not mandatory.
|
||||||
//!
|
//!
|
||||||
//! # Examples of use
|
//! # Examples of use
|
||||||
//!
|
//!
|
||||||
@ -68,10 +68,11 @@
|
|||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! # #![feature(rustc_private)]
|
//! # #![feature(rustc_private)]
|
||||||
|
//! use rustc_macros::{Decodable, Encodable};
|
||||||
//! use rustc_serialize::json;
|
//! use rustc_serialize::json;
|
||||||
//!
|
//!
|
||||||
//! // Automatically generate `Decodable` and `Encodable` trait implementations
|
//! // Automatically generate `Decodable` and `Encodable` trait implementations
|
||||||
//! #[derive(RustcDecodable, RustcEncodable)]
|
//! #[derive(Decodable, Encodable)]
|
||||||
//! pub struct TestStruct {
|
//! pub struct TestStruct {
|
||||||
//! data_int: u8,
|
//! data_int: u8,
|
||||||
//! data_str: String,
|
//! data_str: String,
|
||||||
@ -100,6 +101,7 @@
|
|||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! # #![feature(rustc_private)]
|
//! # #![feature(rustc_private)]
|
||||||
|
//! use rustc_macros::Encodable;
|
||||||
//! use rustc_serialize::json::{self, ToJson, Json};
|
//! use rustc_serialize::json::{self, ToJson, Json};
|
||||||
//!
|
//!
|
||||||
//! // A custom data structure
|
//! // A custom data structure
|
||||||
@ -115,8 +117,8 @@
|
|||||||
//! }
|
//! }
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! // Only generate `RustcEncodable` trait implementation
|
//! // Only generate `Encodable` trait implementation
|
||||||
//! #[derive(RustcEncodable)]
|
//! #[derive(Encodable)]
|
||||||
//! pub struct ComplexNumRecord {
|
//! pub struct ComplexNumRecord {
|
||||||
//! uid: u8,
|
//! uid: u8,
|
||||||
//! dsc: String,
|
//! dsc: String,
|
||||||
@ -137,11 +139,12 @@
|
|||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! # #![feature(rustc_private)]
|
//! # #![feature(rustc_private)]
|
||||||
|
//! use rustc_macros::Decodable;
|
||||||
//! use std::collections::BTreeMap;
|
//! use std::collections::BTreeMap;
|
||||||
//! use rustc_serialize::json::{self, Json, ToJson};
|
//! use rustc_serialize::json::{self, Json, ToJson};
|
||||||
//!
|
//!
|
||||||
//! // Only generate `RustcDecodable` trait implementation
|
//! // Only generate `Decodable` trait implementation
|
||||||
//! #[derive(RustcDecodable)]
|
//! #[derive(Decodable)]
|
||||||
//! pub struct TestStruct {
|
//! pub struct TestStruct {
|
||||||
//! data_int: u8,
|
//! data_int: u8,
|
||||||
//! data_str: String,
|
//! data_str: String,
|
||||||
|
@ -9,6 +9,7 @@ use json::{
|
|||||||
from_str, DecodeResult, Decoder, DecoderError, Encoder, EncoderError, Json, JsonEvent, Parser,
|
from_str, DecodeResult, Decoder, DecoderError, Encoder, EncoderError, Json, JsonEvent, Parser,
|
||||||
StackElement,
|
StackElement,
|
||||||
};
|
};
|
||||||
|
use rustc_macros::{Decodable, Encodable};
|
||||||
use rustc_serialize::json;
|
use rustc_serialize::json;
|
||||||
use rustc_serialize::{Decodable, Encodable};
|
use rustc_serialize::{Decodable, Encodable};
|
||||||
|
|
||||||
@ -17,7 +18,7 @@ use std::io::prelude::*;
|
|||||||
use std::string;
|
use std::string;
|
||||||
use Animal::*;
|
use Animal::*;
|
||||||
|
|
||||||
#[derive(RustcDecodable, Eq, PartialEq, Debug)]
|
#[derive(Decodable, Eq, PartialEq, Debug)]
|
||||||
struct OptionData {
|
struct OptionData {
|
||||||
opt: Option<usize>,
|
opt: Option<usize>,
|
||||||
}
|
}
|
||||||
@ -48,20 +49,20 @@ fn test_decode_option_malformed() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, RustcEncodable, RustcDecodable, Debug)]
|
#[derive(PartialEq, Encodable, Decodable, Debug)]
|
||||||
enum Animal {
|
enum Animal {
|
||||||
Dog,
|
Dog,
|
||||||
Frog(string::String, isize),
|
Frog(string::String, isize),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, RustcEncodable, RustcDecodable, Debug)]
|
#[derive(PartialEq, Encodable, Decodable, Debug)]
|
||||||
struct Inner {
|
struct Inner {
|
||||||
a: (),
|
a: (),
|
||||||
b: usize,
|
b: usize,
|
||||||
c: Vec<string::String>,
|
c: Vec<string::String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, RustcEncodable, RustcDecodable, Debug)]
|
#[derive(PartialEq, Encodable, Decodable, Debug)]
|
||||||
struct Outer {
|
struct Outer {
|
||||||
inner: Vec<Inner>,
|
inner: Vec<Inner>,
|
||||||
}
|
}
|
||||||
@ -568,7 +569,7 @@ fn test_decode_struct() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(RustcDecodable)]
|
#[derive(Decodable)]
|
||||||
struct FloatStruct {
|
struct FloatStruct {
|
||||||
f: f64,
|
f: f64,
|
||||||
a: Vec<f64>,
|
a: Vec<f64>,
|
||||||
@ -616,7 +617,7 @@ fn test_multiline_errors() {
|
|||||||
assert_eq!(from_str("{\n \"foo\":\n \"bar\""), Err(SyntaxError(EOFWhileParsingObject, 3, 8)));
|
assert_eq!(from_str("{\n \"foo\":\n \"bar\""), Err(SyntaxError(EOFWhileParsingObject, 3, 8)));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(RustcDecodable)]
|
#[derive(Decodable)]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
struct DecodeStruct {
|
struct DecodeStruct {
|
||||||
x: f64,
|
x: f64,
|
||||||
@ -624,12 +625,12 @@ struct DecodeStruct {
|
|||||||
z: string::String,
|
z: string::String,
|
||||||
w: Vec<DecodeStruct>,
|
w: Vec<DecodeStruct>,
|
||||||
}
|
}
|
||||||
#[derive(RustcDecodable)]
|
#[derive(Decodable)]
|
||||||
enum DecodeEnum {
|
enum DecodeEnum {
|
||||||
A(f64),
|
A(f64),
|
||||||
B(string::String),
|
B(string::String),
|
||||||
}
|
}
|
||||||
fn check_err<T: Decodable>(to_parse: &'static str, expected: DecoderError) {
|
fn check_err<T: Decodable<Decoder>>(to_parse: &'static str, expected: DecoderError) {
|
||||||
let res: DecodeResult<T> = match from_str(to_parse) {
|
let res: DecodeResult<T> = match from_str(to_parse) {
|
||||||
Err(e) => Err(ParseError(e)),
|
Err(e) => Err(ParseError(e)),
|
||||||
Ok(json) => Decodable::decode(&mut Decoder::new(json)),
|
Ok(json) => Decodable::decode(&mut Decoder::new(json)),
|
||||||
@ -933,7 +934,7 @@ fn test_prettyencoder_indent_level_param() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_hashmap_with_enum_key() {
|
fn test_hashmap_with_enum_key() {
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
#[derive(RustcEncodable, Eq, Hash, PartialEq, RustcDecodable, Debug)]
|
#[derive(Encodable, Eq, Hash, PartialEq, Decodable, Debug)]
|
||||||
enum Enum {
|
enum Enum {
|
||||||
Foo,
|
Foo,
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
@ -1254,7 +1255,7 @@ fn test_to_json() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_encode_hashmap_with_arbitrary_key() {
|
fn test_encode_hashmap_with_arbitrary_key() {
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
#[derive(PartialEq, Eq, Hash, RustcEncodable)]
|
#[derive(PartialEq, Eq, Hash, Encodable)]
|
||||||
struct ArbitraryType(usize);
|
struct ArbitraryType(usize);
|
||||||
let mut hm: HashMap<ArbitraryType, bool> = HashMap::new();
|
let mut hm: HashMap<ArbitraryType, bool> = HashMap::new();
|
||||||
hm.insert(ArbitraryType(1), true);
|
hm.insert(ArbitraryType(1), true);
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
#![allow(rustc::internal)]
|
#![allow(rustc::internal)]
|
||||||
|
|
||||||
|
use rustc_macros::{Decodable, Encodable};
|
||||||
use rustc_serialize::opaque::{Decoder, Encoder};
|
use rustc_serialize::opaque::{Decoder, Encoder};
|
||||||
use rustc_serialize::{Decodable, Encodable};
|
use rustc_serialize::{Decodable, Encodable};
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
#[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)]
|
#[derive(PartialEq, Clone, Debug, Encodable, Decodable)]
|
||||||
struct Struct {
|
struct Struct {
|
||||||
a: (),
|
a: (),
|
||||||
b: u8,
|
b: u8,
|
||||||
@ -27,11 +28,13 @@ struct Struct {
|
|||||||
q: Option<u32>,
|
q: Option<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_round_trip<T: Encodable + Decodable + PartialEq + Debug>(values: Vec<T>) {
|
fn check_round_trip<T: Encodable<Encoder> + for<'a> Decodable<Decoder<'a>> + PartialEq + Debug>(
|
||||||
|
values: Vec<T>,
|
||||||
|
) {
|
||||||
let mut encoder = Encoder::new(Vec::new());
|
let mut encoder = Encoder::new(Vec::new());
|
||||||
|
|
||||||
for value in &values {
|
for value in &values {
|
||||||
Encodable::encode(&value, &mut encoder).unwrap();
|
Encodable::encode(value, &mut encoder).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
let data = encoder.into_inner();
|
let data = encoder.into_inner();
|
||||||
@ -225,7 +228,7 @@ fn test_struct() {
|
|||||||
}]);
|
}]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)]
|
#[derive(PartialEq, Clone, Debug, Encodable, Decodable)]
|
||||||
enum Enum {
|
enum Enum {
|
||||||
Variant1,
|
Variant1,
|
||||||
Variant2(usize, f32),
|
Variant2(usize, f32),
|
||||||
|
Loading…
Reference in New Issue
Block a user