Skip to content

Commit 5b16205

Browse files
authored
Merge pull request #2263 from CosmWasm/aw/nonzero-tests
Add tests for NonZero types
2 parents 82f3567 + 6cbf188 commit 5b16205

File tree

5 files changed

+136
-6
lines changed

5 files changed

+136
-6
lines changed

Cargo.lock

Lines changed: 68 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

contracts/nested-contracts/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ optimize = """docker run --rm -v "$(pwd)":/code \
3030
[dependencies]
3131
cosmwasm-schema = "2.1.0"
3232
cosmwasm-std = { version = "2.1.0", features = [
33-
"cosmwasm_1_4",
34-
# Enable this if you only deploy to chains that have CosmWasm 2.0 or higher
35-
# "cosmwasm_2_0",
33+
"cosmwasm_1_4",
34+
# Enable this if you only deploy to chains that have CosmWasm 2.0 or higher
35+
# "cosmwasm_2_0",
3636
] }
3737
cw-storage-plus = "2.0.0"
3838
cw2 = "2.0.0"

contracts/nested-contracts/inner-contract/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ optimize = """docker run --rm -v "$(pwd)":/code \
3030
[dependencies]
3131
cosmwasm-schema = "2.1.0"
3232
cosmwasm-std = { version = "2.1.0", features = [
33-
"cosmwasm_1_4",
34-
# Enable this if you only deploy to chains that have CosmWasm 2.0 or higher
35-
# "cosmwasm_2_0",
33+
"cosmwasm_1_4",
34+
# Enable this if you only deploy to chains that have CosmWasm 2.0 or higher
35+
# "cosmwasm_2_0",
3636
] }
3737
cw-storage-plus = "2.0.0"
3838
cw2 = "2.0.0"

packages/std/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,9 @@ chrono = { version = "0.4", default-features = false, features = [
8686
] }
8787
crc32fast = "1.3.2"
8888
hex-literal = "0.4.1"
89+
paste = "1.0.15"
90+
proptest = { version = "1.5.0", default-features = false, features = [
91+
"attr-macro",
92+
"std",
93+
] }
8994
serde_json = "1.0.81"

packages/std/src/serde.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,15 @@ where
7070
#[cfg(test)]
7171
mod tests {
7272
use super::*;
73+
use core::num::{
74+
NonZeroI128, NonZeroI16, NonZeroI32, NonZeroI64, NonZeroI8, NonZeroU128, NonZeroU16,
75+
NonZeroU32, NonZeroU64, NonZeroU8,
76+
};
77+
use proptest::{prop_assert_eq, property_test};
7378
use serde::Deserialize;
7479

80+
use crate::msgpack::{from_msgpack, to_msgpack_vec};
81+
7582
#[derive(Serialize, Deserialize, Debug, PartialEq)]
7683
#[serde(rename_all = "snake_case")]
7784
enum SomeMsg {
@@ -182,4 +189,54 @@ mod tests {
182189
r#"{"release_all":{"image":"foo","amount":42,"time":9007199254740999,"karma":-17}}"#
183190
);
184191
}
192+
193+
macro_rules! test_integer {
194+
($($ty:ty),+$(,)?) => {
195+
$(
196+
::paste::paste! {
197+
#[property_test]
198+
fn [<test_ $ty:snake:lower _encoding>](input: $ty) {
199+
let primitive = input.get();
200+
201+
// Verify that the serialization is the same as the primitive
202+
let serialized = to_json_string(&input).unwrap();
203+
let serialized_primitive = to_json_string(&primitive).unwrap();
204+
prop_assert_eq!(serialized.as_str(), serialized_primitive.as_str());
205+
206+
// Verify that the serialized primitive can be deserialized
207+
let deserialized: $ty = from_json(serialized_primitive).unwrap();
208+
assert_eq!(deserialized, input);
209+
210+
// Verify that zero is not allowed
211+
assert!(from_json::<$ty>("0").is_err());
212+
213+
// Verify that the msgpack encoding is the same as the primitive
214+
let serialized = to_msgpack_vec(&input).unwrap();
215+
let serialized_primitive = to_msgpack_vec(&primitive).unwrap();
216+
prop_assert_eq!(serialized.as_slice(), serialized_primitive.as_slice());
217+
218+
// Verify that the serialized primitive can be deserialized
219+
let deserialized: $ty = from_msgpack(&serialized_primitive).unwrap();
220+
prop_assert_eq!(deserialized, input);
221+
}
222+
}
223+
)+
224+
};
225+
}
226+
227+
test_integer! {
228+
NonZeroU8,
229+
NonZeroU16,
230+
NonZeroU32,
231+
NonZeroU64,
232+
NonZeroU128,
233+
}
234+
235+
test_integer! {
236+
NonZeroI8,
237+
NonZeroI16,
238+
NonZeroI32,
239+
NonZeroI64,
240+
NonZeroI128,
241+
}
185242
}

0 commit comments

Comments
 (0)