Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move tests around #140

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo build --release -p tests --test recursive_models
- run: cargo build --release -p musli --test recursive_models

clippy:
runs-on: ubuntu-latest
Expand All @@ -122,7 +122,7 @@ jobs:
- uses: dtolnay/rust-toolchain@1.76
with:
components: clippy
- run: cargo clippy --all-targets -- -D warnings
- run: cargo clippy --all-targets --features test -- -D warnings

rustfmt:
runs-on: ubuntu-latest
Expand Down
7 changes: 5 additions & 2 deletions crates/musli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,11 @@ lexical = { version = "6.1.1", optional = true, default-features = false, featur
serde = { version = "1.0.198", optional = true }

[dev-dependencies]
musli = { path = ".", features = ["test", "storage", "wire", "descriptive", "json", "parse-full", "value", "serde"] }
tests = { path = "../../tests" }

rand = "0.8.5"
serde = { version = "1.0.198", features = ["derive"] }
url = { version = "2.5.0", features = ["serde"] }

musli = { path = ".", features = ["storage", "wire", "descriptive", "json", "parse-full", "value", "serde"] }
trybuild = "1.0.90"
bstr = "1.9.1"
5 changes: 2 additions & 3 deletions crates/musli/src/descriptive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,5 @@ pub use self::error::Error;

/// The maximum length that can be inlined in the tag without adding additional
/// data to the wire format.
#[cfg(feature = "test")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "test")))]
pub const MAX_INLINE_LEN: usize = (self::tag::DATA_MASK - 1) as usize;
#[cfg(test)]
pub(crate) const MAX_INLINE_LEN: usize = (self::tag::DATA_MASK - 1) as usize;
6 changes: 3 additions & 3 deletions crates/musli/src/int/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::reader::Reader;
use crate::writer::Writer;

/// Trait that encodes common behaviors of unsigned numbers.
pub trait Unsigned:
pub(crate) trait Unsigned:
Copy
+ Shr<u32, Output = Self>
+ Shl<u32, Output = Self>
Expand Down Expand Up @@ -59,7 +59,7 @@ pub trait Unsigned:
}

/// Helper trait for performing I/O over [Unsigned] types.
pub trait UnsignedOps: Unsigned {
pub(crate) trait UnsignedOps: Unsigned {
/// Write the current byte array to the given writer in little-endian
/// encoding.
fn write_bytes<C, W>(self, cx: &C, writer: W, byte_order: ByteOrder) -> Result<(), C::Error>
Expand All @@ -75,7 +75,7 @@ pub trait UnsignedOps: Unsigned {
}

/// Trait that encodes common behaviors of signed numbers.
pub trait Signed:
pub(crate) trait Signed:
Copy
+ Neg<Output = Self>
+ Shr<u32, Output = Self>
Expand Down
6 changes: 5 additions & 1 deletion crates/musli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,11 @@ extern crate alloc;
extern crate std;

#[macro_use]
mod macros;
#[doc(hidden)]
pub mod macros;

#[cfg(test)]
mod tests;

pub mod help;

Expand Down
124 changes: 124 additions & 0 deletions crates/musli/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,127 @@ macro_rules! test_fns {
}
}
}

#[cfg(all(feature = "test", feature = "alloc"))]
#[doc(hidden)]
pub mod support {
use crate::mode::Binary;
use crate::value::{self, Value};
use crate::{Decode, Encode};

pub use alloc::vec::Vec;

#[track_caller]
pub fn musli_value_rt<T>(expected: T)
where
T: Encode<Binary> + for<'de> Decode<'de, Binary>,
T: PartialEq + core::fmt::Debug,
{
let value: Value = value::encode(&expected).expect("value: Encoding should succeed");
let actual: T = value::decode(&value).expect("value: Decoding should succeed");
assert_eq!(
actual, expected,
"value: roundtripped value does not match expected"
);
}
}

/// Roundtrip the given expression through all supported formats.
#[cfg(feature = "test")]
#[macro_export]
#[doc(hidden)]
macro_rules! rt {
($what:ident, $expr:expr $(, $($extra:tt)*)?) => {{
let expected = $expr;

macro_rules! rt {
($name:ident) => {{
assert_eq!(
musli::$name::test::rt($expr), expected,
"{}: roundtripped value does not match expected",
stringify!($name),
);
}}
}

$crate::test_matrix!($what, rt);
$crate::macros::support::musli_value_rt($expr);
$crate::extra!($expr $(, $($extra)*)*);
expected
}};
}

/// This is used to test when there is a decode assymmetry, such as the decoded
/// value does not match the encoded one due to things such as skipped fields.
#[cfg(feature = "test")]
#[macro_export]
macro_rules! assert_decode_eq {
($what:ident, $expr:expr, $expected:expr $(, $($extra:tt)*)?) => {{
let mut bytes = $crate::macros::support::Vec::<u8>::new();

macro_rules! decode {
($name:ident) => {{
$crate::$name::test::decode($expr, &mut bytes, &$expected);
}}
}

$crate::test_matrix!($what, decode);
$crate::extra!($expr $(, $($extra)*)*);
}};
}

#[cfg(feature = "test")]
#[macro_export]
macro_rules! extra {
($expr:expr $(,)?) => {};

($expr:expr, json = $json_expected:expr $(, $($tt:tt)*)?) => {{
let json = $crate::json::test::to_vec($expr);
let string = ::std::string::String::from_utf8(json).expect("Encoded JSON is not valid utf-8");

assert_eq!(
string, $json_expected,
"json: encoded json does not match expected value"
);

$crate::extra!($expr $(, $($tt)*)*);
}};
}

#[cfg(feature = "test")]
#[macro_export]
macro_rules! test_matrix {
(full, $call:path) => {
$call!(storage);
$call!(wire);
$call!(descriptive);
$call!(json);
};

(no_json, $call:path) => {
$call!(storage);
$call!(wire);
$call!(descriptive);
};

(descriptive, $call:path) => {
$call!(descriptive);
$call!(json);
};

// TODO: Deprecate this in favor of only using `upgrade_stable`.
(wire_only, $call:path) => {
$call!(wire);
};

(upgrade_stable, $call:path) => {
$call!(wire);
$call!(descriptive);
$call!(json);
};

(upgrade_stable_no_text, $call:path) => {
$call!(wire);
$call!(descriptive);
};
}
1 change: 1 addition & 0 deletions crates/musli/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod pack_compat;
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
//! This is a test that ensures that arbitrary packs of data can be successfully skipped over.

#![cfg(feature = "test")]

use musli::{Decode, Encode};
use crate::{Decode, Encode};

#[derive(Debug, PartialEq, Encode, Decode)]
pub struct Inner;

#[derive(Debug, PartialEq, Encode, Decode)]
#[musli(packed)]
#[musli(crate, packed)]
struct Packed<const N: usize> {
header: u32,
#[musli(bytes)]
values: [u8; N],
}

#[derive(Debug, PartialEq, Encode, Decode)]
#[musli(crate)]
struct PackedCompat<const N: usize, const L: usize> {
prefix: u32,
small: Packed<N>,
Expand All @@ -24,6 +23,7 @@ struct PackedCompat<const N: usize, const L: usize> {
}

#[derive(Debug, PartialEq, Encode, Decode)]
#[musli(crate)]
struct IgnoreLarge<const N: usize> {
prefix: u32,
#[musli(mode = Binary, name = 1)]
Expand All @@ -33,6 +33,7 @@ struct IgnoreLarge<const N: usize> {
}

#[derive(Debug, PartialEq, Encode, Decode)]
#[musli(crate)]
struct IgnoreSmall<const L: usize> {
prefix: u32,
#[musli(mode = Binary, name = 2)]
Expand All @@ -42,6 +43,7 @@ struct IgnoreSmall<const L: usize> {
}

#[derive(Debug, PartialEq, Encode, Decode)]
#[musli(crate)]
struct IgnoreBoth {
prefix: u32,
#[musli(mode = Binary, name = 3)]
Expand All @@ -61,7 +63,7 @@ const fn array<const N: usize>() -> [u8; N] {
}

fn test_length<const N: usize, const L: usize>() {
tests::rt! {
crate::rt! {
upgrade_stable,
PackedCompat {
prefix: 42,
Expand All @@ -71,7 +73,7 @@ fn test_length<const N: usize, const L: usize>() {
}
};

tests::assert_decode_eq! {
crate::assert_decode_eq! {
upgrade_stable,
PackedCompat {
prefix: 42,
Expand All @@ -86,7 +88,7 @@ fn test_length<const N: usize, const L: usize>() {
}
};

tests::assert_decode_eq! {
crate::assert_decode_eq! {
upgrade_stable,
PackedCompat {
prefix: 42,
Expand All @@ -101,7 +103,7 @@ fn test_length<const N: usize, const L: usize>() {
}
};

tests::assert_decode_eq! {
crate::assert_decode_eq! {
upgrade_stable,
PackedCompat {
prefix: 42,
Expand All @@ -118,6 +120,6 @@ fn test_length<const N: usize, const L: usize>() {

#[test]
fn test_lengths() {
test_length::<{ musli::wire::MAX_INLINE_LEN - 4 }, 256>();
test_length::<{ musli::descriptive::MAX_INLINE_LEN - 4 }, 256>();
test_length::<{ crate::wire::MAX_INLINE_LEN - 4 }, 256>();
test_length::<{ crate::descriptive::MAX_INLINE_LEN - 4 }, 256>();
}
5 changes: 2 additions & 3 deletions crates/musli/src/wire/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,5 @@ pub use self::error::Error;

/// The maximum length that can be inlined in the tag without adding additional
/// data to the wire format.
#[cfg(feature = "test")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "test")))]
pub const MAX_INLINE_LEN: usize = (self::tag::DATA_MASK - 1) as usize;
#[cfg(test)]
pub(crate) const MAX_INLINE_LEN: usize = (self::tag::DATA_MASK - 1) as usize;
4 changes: 2 additions & 2 deletions tests/tests/bytes.rs → crates/musli/tests/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct Container {

#[test]
fn container() {
tests::rt!(
musli::rt!(
full,
Container {
vec: vec![0, 1, 2, 3],
Expand All @@ -39,7 +39,7 @@ pub struct BytesCompat {

#[test]
fn bytes_compat() {
tests::rt!(
musli::rt!(
full,
BytesCompat {
empty_bytes: Bytes([]),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn signed_to_unsigned() {
f: $ty,
}

tests::assert_decode_eq! {
musli::assert_decode_eq! {
descriptive,
SignedIntegers {
a: 2,
Expand Down Expand Up @@ -80,7 +80,7 @@ fn unsigned_to_signed() {
f: $ty,
}

tests::assert_decode_eq! {
musli::assert_decode_eq! {
descriptive,
UnsignedIntegers {
a: 2,
Expand Down
Loading