2017-04-13 19:34:42 -05:00
|
|
|
// Copyright 2017 Serde Developers
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2017-04-04 12:47:21 -05:00
|
|
|
#[macro_use]
|
|
|
|
extern crate serde_derive;
|
|
|
|
|
|
|
|
extern crate serde;
|
|
|
|
use serde::{Deserialize, Deserializer};
|
|
|
|
|
|
|
|
extern crate serde_test;
|
2017-04-19 13:38:57 -05:00
|
|
|
use serde_test::{Token, assert_de_tokens, assert_de_tokens_error};
|
2017-04-04 12:47:21 -05:00
|
|
|
|
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_borrowed_str() {
|
2017-04-13 14:28:23 -05:00
|
|
|
assert_de_tokens(&"borrowed", &[Token::BorrowedStr("borrowed")]);
|
2017-04-04 12:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_borrowed_str_from_string() {
|
|
|
|
assert_de_tokens_error::<&str>(
|
2017-04-13 14:28:23 -05:00
|
|
|
&[Token::String("borrowed")],
|
2017-04-19 13:38:57 -05:00
|
|
|
"invalid type: string \"borrowed\", expected a borrowed string",
|
2017-04-04 12:47:21 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_borrowed_str_from_str() {
|
|
|
|
assert_de_tokens_error::<&str>(
|
2017-04-13 14:28:23 -05:00
|
|
|
&[Token::Str("borrowed")],
|
2017-04-19 13:38:57 -05:00
|
|
|
"invalid type: string \"borrowed\", expected a borrowed string",
|
2017-04-04 12:47:21 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_string_from_borrowed_str() {
|
2017-04-13 14:28:23 -05:00
|
|
|
assert_de_tokens(&"owned".to_owned(), &[Token::BorrowedStr("owned")]);
|
2017-04-04 12:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_borrowed_bytes() {
|
2017-04-13 14:28:23 -05:00
|
|
|
assert_de_tokens(&&b"borrowed"[..], &[Token::BorrowedBytes(b"borrowed")]);
|
2017-04-04 12:47:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_borrowed_bytes_from_bytebuf() {
|
|
|
|
assert_de_tokens_error::<&[u8]>(
|
2017-04-13 14:28:23 -05:00
|
|
|
&[Token::ByteBuf(b"borrowed")],
|
2017-04-19 13:38:57 -05:00
|
|
|
"invalid type: byte array, expected a borrowed byte array",
|
2017-04-04 12:47:21 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_borrowed_bytes_from_bytes() {
|
|
|
|
assert_de_tokens_error::<&[u8]>(
|
2017-04-13 14:28:23 -05:00
|
|
|
&[Token::Bytes(b"borrowed")],
|
2017-04-19 13:38:57 -05:00
|
|
|
"invalid type: byte array, expected a borrowed byte array",
|
2017-04-04 12:47:21 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_tuple() {
|
|
|
|
assert_de_tokens(
|
|
|
|
&("str", &b"bytes"[..]),
|
|
|
|
&[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Tuple { len: 2 },
|
2017-04-04 12:47:21 -05:00
|
|
|
Token::BorrowedStr("str"),
|
|
|
|
Token::BorrowedBytes(b"bytes"),
|
|
|
|
Token::TupleEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-04-04 12:47:21 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_struct() {
|
|
|
|
#[derive(Deserialize, Debug, PartialEq)]
|
|
|
|
struct Borrowing<'a, 'b> {
|
|
|
|
bs: &'a str,
|
|
|
|
bb: &'b [u8],
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_de_tokens(
|
2017-04-13 14:28:23 -05:00
|
|
|
&Borrowing {
|
|
|
|
bs: "str",
|
|
|
|
bb: b"bytes",
|
|
|
|
},
|
2017-04-04 12:47:21 -05:00
|
|
|
&[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Struct { name: "Borrowing", len: 2 },
|
2017-04-04 12:47:21 -05:00
|
|
|
|
|
|
|
Token::BorrowedStr("bs"),
|
|
|
|
Token::BorrowedStr("str"),
|
|
|
|
|
|
|
|
Token::BorrowedStr("bb"),
|
|
|
|
Token::BorrowedBytes(b"bytes"),
|
|
|
|
|
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-04-04 12:47:21 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cow() {
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Cows<'a, 'b> {
|
|
|
|
copied: Cow<'a, str>,
|
|
|
|
|
|
|
|
#[serde(borrow)]
|
|
|
|
borrowed: Cow<'b, str>,
|
|
|
|
}
|
|
|
|
|
2017-04-05 02:22:00 -05:00
|
|
|
let tokens = &[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Struct { name: "Cows", len: 2 },
|
2017-04-04 12:47:21 -05:00
|
|
|
|
|
|
|
Token::Str("copied"),
|
|
|
|
Token::BorrowedStr("copied"),
|
|
|
|
|
|
|
|
Token::Str("borrowed"),
|
|
|
|
Token::BorrowedStr("borrowed"),
|
|
|
|
|
|
|
|
Token::StructEnd,
|
|
|
|
];
|
|
|
|
|
2017-04-05 02:22:00 -05:00
|
|
|
let mut de = serde_test::Deserializer::new(tokens);
|
2017-04-04 12:47:21 -05:00
|
|
|
let cows = Cows::deserialize(&mut de).unwrap();
|
|
|
|
|
|
|
|
match cows.copied {
|
|
|
|
Cow::Owned(ref s) if s == "copied" => {}
|
|
|
|
_ => panic!("expected a copied string"),
|
|
|
|
}
|
|
|
|
|
|
|
|
match cows.borrowed {
|
|
|
|
Cow::Borrowed("borrowed") => {}
|
|
|
|
_ => panic!("expected a borrowed string"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_lifetimes() {
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Cows<'a, 'b> {
|
|
|
|
_copied: Cow<'a, str>,
|
|
|
|
|
|
|
|
#[serde(borrow)]
|
|
|
|
_borrowed: Cow<'b, str>,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that `'de: 'a` is not required by the Deserialize impl.
|
|
|
|
fn _cows_lifetimes<'de: 'b, 'a, 'b, D>(deserializer: D) -> Cows<'a, 'b>
|
2017-04-13 14:28:23 -05:00
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
2017-04-04 12:47:21 -05:00
|
|
|
{
|
|
|
|
Deserialize::deserialize(deserializer).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Wrap<'a, 'b> {
|
|
|
|
#[serde(borrow = "'b")]
|
|
|
|
_cows: Cows<'a, 'b>,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that `'de: 'a` is not required by the Deserialize impl.
|
|
|
|
fn _wrap_lifetimes<'de: 'b, 'a, 'b, D>(deserializer: D) -> Wrap<'a, 'b>
|
2017-04-13 14:28:23 -05:00
|
|
|
where
|
|
|
|
D: Deserializer<'de>,
|
2017-04-04 12:47:21 -05:00
|
|
|
{
|
|
|
|
Deserialize::deserialize(deserializer).unwrap()
|
|
|
|
}
|
|
|
|
}
|