Skip to content

Commit

Permalink
move tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Mar 18, 2024
1 parent 748b310 commit a2d964d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 81 deletions.
81 changes: 0 additions & 81 deletions lib/speedy-uuid/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,84 +294,3 @@ mod serde_impl {
}
}
}

#[cfg(test)]
mod test {
use crate::Uuid;
use serde_test::Token;
use std::str::FromStr;

const UUID_1: &str = "38058daf-b2cd-4832-902a-83583ac07e28";
const UUID_1_BYTES: [u8; 16] = [
0x38, 0x05, 0x8d, 0xaf, 0xb2, 0xcd, 0x48, 0x32, 0x90, 0x2a, 0x83, 0x58, 0x3a, 0xc0, 0x7e,
0x28,
];

#[test]
fn parse_1() {
let uuid = Uuid::from_str(UUID_1).unwrap();
assert_eq!(UUID_1, uuid.to_string());
}

#[test]
#[cfg(feature = "serde")]
fn deserialize_str() {
use serde_test::Configure;

let uuid = Uuid::from_str(UUID_1).unwrap().readable();
serde_test::assert_de_tokens(&uuid, &[Token::Str(UUID_1)]);
}

#[test]
#[cfg(feature = "serde")]
fn deserialize_bytes() {
use serde_test::Configure;

let uuid = Uuid::from_slice(&UUID_1_BYTES).unwrap();
serde_test::assert_de_tokens(&uuid.compact(), &[Token::Bytes(&UUID_1_BYTES)]);
serde_test::assert_de_tokens(&uuid.readable(), &[Token::Bytes(&UUID_1_BYTES)]);
}

#[test]
#[cfg(feature = "serde")]
fn deserialize_byte_array() {
use serde_test::{Compact, Configure};

let uuid = Uuid::from_slice(&UUID_1_BYTES).unwrap();
serde_test::assert_de_tokens(
&uuid.readable(),
&[
Token::Seq { len: Some(16) },
Token::U8(UUID_1_BYTES[0]),
Token::U8(UUID_1_BYTES[1]),
Token::U8(UUID_1_BYTES[2]),
Token::U8(UUID_1_BYTES[3]),
Token::U8(UUID_1_BYTES[4]),
Token::U8(UUID_1_BYTES[5]),
Token::U8(UUID_1_BYTES[6]),
Token::U8(UUID_1_BYTES[7]),
Token::U8(UUID_1_BYTES[8]),
Token::U8(UUID_1_BYTES[9]),
Token::U8(UUID_1_BYTES[10]),
Token::U8(UUID_1_BYTES[11]),
Token::U8(UUID_1_BYTES[12]),
Token::U8(UUID_1_BYTES[13]),
Token::U8(UUID_1_BYTES[14]),
Token::U8(UUID_1_BYTES[15]),
Token::SeqEnd,
],
);

serde_test::assert_de_tokens_error::<Compact<Uuid>>(
&[Token::Seq { len: Some(16) }],
"invalid type: sequence, expected bytes",
);
}

#[test]
#[cfg(feature = "serde")]
fn serialize_uuid() {
let uuid = Uuid::from_slice(&UUID_1_BYTES).unwrap();
serde_test::assert_ser_tokens(&uuid, &[Token::Str(UUID_1)]);
}
}
10 changes: 10 additions & 0 deletions lib/speedy-uuid/tests/basic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use speedy_uuid::Uuid;
use std::str::FromStr;

const UUID: &str = "38058daf-b2cd-4832-902a-83583ac07e28";

#[test]
fn parse() {
let uuid = Uuid::from_str(UUID).unwrap();
assert_eq!(UUID, uuid.to_string());
}
62 changes: 62 additions & 0 deletions lib/speedy-uuid/tests/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#![cfg(feature = "serde")]

use serde_test::{Compact, Configure, Token};
use speedy_uuid::Uuid;
use std::str::FromStr;

const UUID: &str = "38058daf-b2cd-4832-902a-83583ac07e28";
const UUID_BYTES: [u8; 16] = [
0x38, 0x05, 0x8d, 0xaf, 0xb2, 0xcd, 0x48, 0x32, 0x90, 0x2a, 0x83, 0x58, 0x3a, 0xc0, 0x7e, 0x28,
];

#[test]
fn deserialize_str() {
let uuid = Uuid::from_str(UUID).unwrap().readable();
serde_test::assert_de_tokens(&uuid, &[Token::Str(UUID)]);
}

#[test]
fn deserialize_bytes() {
let uuid = Uuid::from_slice(&UUID_BYTES).unwrap();
serde_test::assert_de_tokens(&uuid.compact(), &[Token::Bytes(&UUID_BYTES)]);
serde_test::assert_de_tokens(&uuid.readable(), &[Token::Bytes(&UUID_BYTES)]);
}

#[test]
fn deserialize_byte_array() {
let uuid = Uuid::from_slice(&UUID_BYTES).unwrap();
serde_test::assert_de_tokens(
&uuid.readable(),
&[
Token::Seq { len: Some(16) },
Token::U8(UUID_BYTES[0]),
Token::U8(UUID_BYTES[1]),
Token::U8(UUID_BYTES[2]),
Token::U8(UUID_BYTES[3]),
Token::U8(UUID_BYTES[4]),
Token::U8(UUID_BYTES[5]),
Token::U8(UUID_BYTES[6]),
Token::U8(UUID_BYTES[7]),
Token::U8(UUID_BYTES[8]),
Token::U8(UUID_BYTES[9]),
Token::U8(UUID_BYTES[10]),
Token::U8(UUID_BYTES[11]),
Token::U8(UUID_BYTES[12]),
Token::U8(UUID_BYTES[13]),
Token::U8(UUID_BYTES[14]),
Token::U8(UUID_BYTES[15]),
Token::SeqEnd,
],
);

serde_test::assert_de_tokens_error::<Compact<Uuid>>(
&[Token::Seq { len: Some(16) }],
"invalid type: sequence, expected bytes",
);
}

#[test]
fn serialize_uuid() {
let uuid = Uuid::from_slice(&UUID_BYTES).unwrap();
serde_test::assert_ser_tokens(&uuid, &[Token::Str(UUID)]);
}

0 comments on commit a2d964d

Please sign in to comment.