summaryrefslogtreecommitdiffstats
path: root/third_party/rust/serde_bytes/tests/test_serde.rs
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/rust/serde_bytes/tests/test_serde.rs')
-rw-r--r--third_party/rust/serde_bytes/tests/test_serde.rs59
1 files changed, 59 insertions, 0 deletions
diff --git a/third_party/rust/serde_bytes/tests/test_serde.rs b/third_party/rust/serde_bytes/tests/test_serde.rs
new file mode 100644
index 0000000000..d48e3fd434
--- /dev/null
+++ b/third_party/rust/serde_bytes/tests/test_serde.rs
@@ -0,0 +1,59 @@
+use serde_bytes::{ByteBuf, Bytes};
+use serde_test::{assert_de_tokens, assert_ser_tokens, assert_tokens, Token};
+
+#[test]
+fn test_bytes() {
+ let empty = Bytes::new(&[]);
+ assert_tokens(&empty, &[Token::BorrowedBytes(b"")]);
+ assert_ser_tokens(&empty, &[Token::Bytes(b"")]);
+ assert_ser_tokens(&empty, &[Token::ByteBuf(b"")]);
+ assert_de_tokens(&empty, &[Token::BorrowedStr("")]);
+
+ let buf = vec![65, 66, 67];
+ let bytes = Bytes::new(&buf);
+ assert_tokens(&bytes, &[Token::BorrowedBytes(b"ABC")]);
+ assert_ser_tokens(&bytes, &[Token::Bytes(b"ABC")]);
+ assert_ser_tokens(&bytes, &[Token::ByteBuf(b"ABC")]);
+ assert_de_tokens(&bytes, &[Token::BorrowedStr("ABC")]);
+}
+
+#[test]
+fn test_byte_buf() {
+ let empty = ByteBuf::new();
+ assert_tokens(&empty, &[Token::BorrowedBytes(b"")]);
+ assert_tokens(&empty, &[Token::Bytes(b"")]);
+ assert_tokens(&empty, &[Token::ByteBuf(b"")]);
+ assert_de_tokens(&empty, &[Token::BorrowedStr("")]);
+ assert_de_tokens(&empty, &[Token::Str("")]);
+ assert_de_tokens(&empty, &[Token::String("")]);
+ assert_de_tokens(&empty, &[Token::Seq { len: None }, Token::SeqEnd]);
+ assert_de_tokens(&empty, &[Token::Seq { len: Some(0) }, Token::SeqEnd]);
+
+ let buf = ByteBuf::from(vec![65, 66, 67]);
+ assert_tokens(&buf, &[Token::BorrowedBytes(b"ABC")]);
+ assert_tokens(&buf, &[Token::Bytes(b"ABC")]);
+ assert_tokens(&buf, &[Token::ByteBuf(b"ABC")]);
+ assert_de_tokens(&buf, &[Token::BorrowedStr("ABC")]);
+ assert_de_tokens(&buf, &[Token::Str("ABC")]);
+ assert_de_tokens(&buf, &[Token::String("ABC")]);
+ assert_de_tokens(
+ &buf,
+ &[
+ Token::Seq { len: None },
+ Token::U8(65),
+ Token::U8(66),
+ Token::U8(67),
+ Token::SeqEnd,
+ ],
+ );
+ assert_de_tokens(
+ &buf,
+ &[
+ Token::Seq { len: Some(3) },
+ Token::U8(65),
+ Token::U8(66),
+ Token::U8(67),
+ Token::SeqEnd,
+ ],
+ );
+}