Skip to content

Commit

Permalink
Fix rustc_serialize unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewjasper committed Aug 14, 2020
1 parent ae7951e commit c4f91bb
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Expand Up @@ -3877,6 +3877,7 @@ name = "rustc_serialize"
version = "0.0.0"
dependencies = [
"indexmap",
"rustc_macros",
"smallvec 1.4.0",
]

Expand Down
3 changes: 3 additions & 0 deletions src/librustc_serialize/Cargo.toml
Expand Up @@ -11,3 +11,6 @@ path = "lib.rs"
[dependencies]
indexmap = "1"
smallvec = { version = "1.0", features = ["union", "may_dangle"] }

[dev-dependencies]
rustc_macros = { path = "../librustc_macros" }
21 changes: 12 additions & 9 deletions src/librustc_serialize/json.rs
Expand Up @@ -47,17 +47,17 @@
//!
//! Rust provides a mechanism for low boilerplate encoding & decoding of values to and from JSON via
//! the serialization API.
//! To be able to encode a piece of data, it must implement the `serialize::RustcEncodable` trait.
//! To be able to decode a piece of data, it must implement the `serialize::RustcDecodable` 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::Decodable` trait.
//! 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 `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.
//! 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
//!
Expand All @@ -68,10 +68,11 @@
//!
//! ```rust
//! # #![feature(rustc_private)]
//! use rustc_macros::{Decodable, Encodable};
//! use rustc_serialize::json;
//!
//! // Automatically generate `Decodable` and `Encodable` trait implementations
//! #[derive(RustcDecodable, RustcEncodable)]
//! #[derive(Decodable, Encodable)]
//! pub struct TestStruct {
//! data_int: u8,
//! data_str: String,
Expand Down Expand Up @@ -100,6 +101,7 @@
//!
//! ```rust
//! # #![feature(rustc_private)]
//! use rustc_macros::Encodable;
//! use rustc_serialize::json::{self, ToJson, Json};
//!
//! // A custom data structure
Expand All @@ -115,8 +117,8 @@
//! }
//! }
//!
//! // Only generate `RustcEncodable` trait implementation
//! #[derive(RustcEncodable)]
//! // Only generate `Encodable` trait implementation
//! #[derive(Encodable)]
//! pub struct ComplexNumRecord {
//! uid: u8,
//! dsc: String,
Expand All @@ -137,11 +139,12 @@
//!
//! ```rust
//! # #![feature(rustc_private)]
//! use rustc_macros::Decodable;
//! use std::collections::BTreeMap;
//! use rustc_serialize::json::{self, Json, ToJson};
//!
//! // Only generate `RustcDecodable` trait implementation
//! #[derive(RustcDecodable)]
//! // Only generate `Decodable` trait implementation
//! #[derive(Decodable)]
//! pub struct TestStruct {
//! data_int: u8,
//! data_str: String,
Expand Down
21 changes: 11 additions & 10 deletions src/librustc_serialize/tests/json.rs
Expand Up @@ -9,6 +9,7 @@ use json::{
from_str, DecodeResult, Decoder, DecoderError, Encoder, EncoderError, Json, JsonEvent, Parser,
StackElement,
};
use rustc_macros::{Decodable, Encodable};
use rustc_serialize::json;
use rustc_serialize::{Decodable, Encodable};

Expand All @@ -17,7 +18,7 @@ use std::io::prelude::*;
use std::string;
use Animal::*;

#[derive(RustcDecodable, Eq, PartialEq, Debug)]
#[derive(Decodable, Eq, PartialEq, Debug)]
struct OptionData {
opt: Option<usize>,
}
Expand Down Expand Up @@ -48,20 +49,20 @@ fn test_decode_option_malformed() {
);
}

#[derive(PartialEq, RustcEncodable, RustcDecodable, Debug)]
#[derive(PartialEq, Encodable, Decodable, Debug)]
enum Animal {
Dog,
Frog(string::String, isize),
}

#[derive(PartialEq, RustcEncodable, RustcDecodable, Debug)]
#[derive(PartialEq, Encodable, Decodable, Debug)]
struct Inner {
a: (),
b: usize,
c: Vec<string::String>,
}

#[derive(PartialEq, RustcEncodable, RustcDecodable, Debug)]
#[derive(PartialEq, Encodable, Decodable, Debug)]
struct Outer {
inner: Vec<Inner>,
}
Expand Down Expand Up @@ -568,7 +569,7 @@ fn test_decode_struct() {
);
}

#[derive(RustcDecodable)]
#[derive(Decodable)]
struct FloatStruct {
f: f64,
a: Vec<f64>,
Expand Down Expand Up @@ -616,20 +617,20 @@ fn test_multiline_errors() {
assert_eq!(from_str("{\n \"foo\":\n \"bar\""), Err(SyntaxError(EOFWhileParsingObject, 3, 8)));
}

#[derive(RustcDecodable)]
#[derive(Decodable)]
#[allow(dead_code)]
struct DecodeStruct {
x: f64,
y: bool,
z: string::String,
w: Vec<DecodeStruct>,
}
#[derive(RustcDecodable)]
#[derive(Decodable)]
enum DecodeEnum {
A(f64),
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) {
Err(e) => Err(ParseError(e)),
Ok(json) => Decodable::decode(&mut Decoder::new(json)),
Expand Down Expand Up @@ -933,7 +934,7 @@ fn test_prettyencoder_indent_level_param() {
#[test]
fn test_hashmap_with_enum_key() {
use std::collections::HashMap;
#[derive(RustcEncodable, Eq, Hash, PartialEq, RustcDecodable, Debug)]
#[derive(Encodable, Eq, Hash, PartialEq, Decodable, Debug)]
enum Enum {
Foo,
#[allow(dead_code)]
Expand Down Expand Up @@ -1254,7 +1255,7 @@ fn test_to_json() {
#[test]
fn test_encode_hashmap_with_arbitrary_key() {
use std::collections::HashMap;
#[derive(PartialEq, Eq, Hash, RustcEncodable)]
#[derive(PartialEq, Eq, Hash, Encodable)]
struct ArbitraryType(usize);
let mut hm: HashMap<ArbitraryType, bool> = HashMap::new();
hm.insert(ArbitraryType(1), true);
Expand Down
11 changes: 7 additions & 4 deletions src/librustc_serialize/tests/opaque.rs
@@ -1,10 +1,11 @@
#![allow(rustc::internal)]

use rustc_macros::{Decodable, Encodable};
use rustc_serialize::opaque::{Decoder, Encoder};
use rustc_serialize::{Decodable, Encodable};
use std::fmt::Debug;

#[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)]
#[derive(PartialEq, Clone, Debug, Encodable, Decodable)]
struct Struct {
a: (),
b: u8,
Expand All @@ -27,11 +28,13 @@ struct Struct {
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());

for value in &values {
Encodable::encode(&value, &mut encoder).unwrap();
Encodable::encode(value, &mut encoder).unwrap();
}

let data = encoder.into_inner();
Expand Down Expand Up @@ -225,7 +228,7 @@ fn test_struct() {
}]);
}

#[derive(PartialEq, Clone, Debug, RustcEncodable, RustcDecodable)]
#[derive(PartialEq, Clone, Debug, Encodable, Decodable)]
enum Enum {
Variant1,
Variant2(usize, f32),
Expand Down

0 comments on commit c4f91bb

Please sign in to comment.