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

Add tests for NonZero types #2263

Merged
merged 4 commits into from
Sep 23, 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
68 changes: 68 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions contracts/nested-contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ optimize = """docker run --rm -v "$(pwd)":/code \
[dependencies]
cosmwasm-schema = "2.1.0"
cosmwasm-std = { version = "2.1.0", features = [
"cosmwasm_1_4",
# Enable this if you only deploy to chains that have CosmWasm 2.0 or higher
# "cosmwasm_2_0",
"cosmwasm_1_4",
# Enable this if you only deploy to chains that have CosmWasm 2.0 or higher
# "cosmwasm_2_0",
] }
cw-storage-plus = "2.0.0"
cw2 = "2.0.0"
Expand Down
6 changes: 3 additions & 3 deletions contracts/nested-contracts/inner-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ optimize = """docker run --rm -v "$(pwd)":/code \
[dependencies]
cosmwasm-schema = "2.1.0"
cosmwasm-std = { version = "2.1.0", features = [
"cosmwasm_1_4",
# Enable this if you only deploy to chains that have CosmWasm 2.0 or higher
# "cosmwasm_2_0",
"cosmwasm_1_4",
# Enable this if you only deploy to chains that have CosmWasm 2.0 or higher
# "cosmwasm_2_0",
] }
cw-storage-plus = "2.0.0"
cw2 = "2.0.0"
Expand Down
5 changes: 5 additions & 0 deletions packages/std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,9 @@ chrono = { version = "0.4", default-features = false, features = [
] }
crc32fast = "1.3.2"
hex-literal = "0.4.1"
paste = "1.0.15"
proptest = { version = "1.5.0", default-features = false, features = [
"attr-macro",
"std",
] }
serde_json = "1.0.81"
57 changes: 57 additions & 0 deletions packages/std/src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,15 @@ where
#[cfg(test)]
mod tests {
use super::*;
use core::num::{
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroU128, NonZeroU16,
NonZeroU32, NonZeroU64, NonZeroU8,
};
use proptest::{prop_assert_eq, property_test};
use serde::Deserialize;

use crate::msgpack::{from_msgpack, to_msgpack_vec};

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
enum SomeMsg {
Expand Down Expand Up @@ -182,4 +189,54 @@ mod tests {
r#"{"release_all":{"image":"foo","amount":42,"time":9007199254740999,"karma":-17}}"#
);
}

macro_rules! test_integer {
($($ty:ty),+$(,)?) => {
$(
::paste::paste! {
#[property_test]
fn [<test_ $ty:snake:lower _encoding>](input: $ty) {
let primitive = input.get();

// Verify that the serialization is the same as the primitive
let serialized = to_json_string(&input).unwrap();
let serialized_primitive = to_json_string(&primitive).unwrap();
prop_assert_eq!(serialized.as_str(), serialized_primitive.as_str());

// Verify that the serialized primitive can be deserialized
let deserialized: $ty = from_json(serialized_primitive).unwrap();
assert_eq!(deserialized, input);

// Verify that zero is not allowed
assert!(from_json::<$ty>("0").is_err());

// Verify that the msgpack encoding is the same as the primitive
let serialized = to_msgpack_vec(&input).unwrap();
let serialized_primitive = to_msgpack_vec(&primitive).unwrap();
prop_assert_eq!(serialized.as_slice(), serialized_primitive.as_slice());

// Verify that the serialized primitive can be deserialized
let deserialized: $ty = from_msgpack(&serialized_primitive).unwrap();
prop_assert_eq!(deserialized, input);
}
}
)+
};
}

test_integer! {
NonZeroU8,
NonZeroU16,
NonZeroU32,
NonZeroU64,
NonZeroU128,
}

test_integer! {
NonZeroI8,
NonZeroI16,
NonZeroI32,
NonZeroI64,
NonZeroI128,
}
}
Loading