Skip to content

Commit

Permalink
chore: test lint (#769)
Browse files Browse the repository at this point in the history
~So that we can enable the `"-Wclippy::tests_outside_test_module"` flag.
Thanks @Ad96el for this.......~

I ignored the lint for pallets using the v1 benchmarking macros, and for
our emulated tests.
  • Loading branch information
ntn-x2 authored Nov 15, 2024
1 parent e86f9fb commit a27c8b6
Show file tree
Hide file tree
Showing 24 changed files with 692 additions and 610 deletions.
1 change: 1 addition & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ rustflags = [
"-Wclippy::ref_patterns",
"-Wclippy::rest_pat_in_fully_bound_structs",
"-Wclippy::suspicious_operation_groupings",
"-Wclippy::tests_outside_test_module",
"-Wclippy::type_repetition_in_bounds",
"-Wclippy::unnecessary_self_imports",
"-Wclippy::unnecessary_struct_initialization",
Expand Down
47 changes: 26 additions & 21 deletions crates/kilt-dip-primitives/src/merkle_proofs/v0/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,31 @@ impl From<Error> for u8 {
}
}

#[test]
fn error_value_never_zero() {
assert!(
enum_iterator::all::<Error>().all(|e| u8::from(e) != 0),
"One of the u8 values for the error is 0, which is not allowed."
);
}
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn error_value_never_zero() {
assert!(
enum_iterator::all::<Error>().all(|e| u8::from(e) != 0),
"One of the u8 values for the error is 0, which is not allowed."
);
}

#[test]
fn error_value_not_duplicated() {
enum_iterator::all::<Error>().fold(
sp_std::collections::btree_set::BTreeSet::<u8>::new(),
|mut values, new_value| {
let new_encoded_value = u8::from(new_value);
assert!(
values.insert(new_encoded_value),
"Failed to add unique value {:#?} for error variant",
new_encoded_value
);
values
},
);
#[test]
fn error_value_not_duplicated() {
enum_iterator::all::<Error>().fold(
sp_std::collections::btree_set::BTreeSet::<u8>::new(),
|mut values, new_value| {
let new_encoded_value = u8::from(new_value);
assert!(
values.insert(new_encoded_value),
"Failed to add unique value {:#?} for error variant",
new_encoded_value
);
values
},
);
}
}
47 changes: 26 additions & 21 deletions crates/kilt-dip-primitives/src/state_proofs/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,31 @@ impl From<MerkleProofError> for u8 {
}
}

#[test]
fn merkle_proof_error_value_never_zero() {
assert!(
enum_iterator::all::<MerkleProofError>().all(|e| u8::from(e) != 0),
"One of the u8 values for the error is 0, which is not allowed."
);
}
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn merkle_proof_error_value_never_zero() {
assert!(
enum_iterator::all::<MerkleProofError>().all(|e| u8::from(e) != 0),
"One of the u8 values for the error is 0, which is not allowed."
);
}

#[test]
fn merkle_proof_error_value_not_duplicated() {
enum_iterator::all::<MerkleProofError>().fold(
sp_std::collections::btree_set::BTreeSet::<u8>::new(),
|mut values, new_value| {
let new_encoded_value = u8::from(new_value);
assert!(
values.insert(new_encoded_value),
"Failed to add unique value {:#?} for error variant",
new_encoded_value
);
values
},
);
#[test]
fn merkle_proof_error_value_not_duplicated() {
enum_iterator::all::<MerkleProofError>().fold(
sp_std::collections::btree_set::BTreeSet::<u8>::new(),
|mut values, new_value| {
let new_encoded_value = u8::from(new_value);
assert!(
values.insert(new_encoded_value),
"Failed to add unique value {:#?} for error variant",
new_encoded_value
);
values
},
);
}
}
60 changes: 35 additions & 25 deletions crates/kilt-dip-primitives/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,30 @@ mod calculate_parachain_head_storage_key {
)
}

#[test]
fn calculate_parachain_head_storage_key_successful_spiritnet_parachain() {
assert_eq!(
calculate_parachain_head_storage_key(2_086).0,
hex_literal::hex!(
"cd710b30bd2eab0352ddcc26417aa1941b3c252fcb29d88eff4f3de5de4476c32c0cfd6c23b92a7826080000"
)
.to_vec()
);
}
#[test]
fn calculate_parachain_head_storage_key_successful_peregrine_parachain() {
assert_eq!(
calculate_parachain_head_storage_key(2_000).0,
hex_literal::hex!(
"cd710b30bd2eab0352ddcc26417aa1941b3c252fcb29d88eff4f3de5de4476c363f5a4efb16ffa83d0070000"
)
.to_vec()
);
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn calculate_parachain_head_storage_key_successful_spiritnet_parachain() {
assert_eq!(
calculate_parachain_head_storage_key(2_086).0,
hex_literal::hex!(
"cd710b30bd2eab0352ddcc26417aa1941b3c252fcb29d88eff4f3de5de4476c32c0cfd6c23b92a7826080000"
)
.to_vec()
);
}
#[test]
fn calculate_parachain_head_storage_key_successful_peregrine_parachain() {
assert_eq!(
calculate_parachain_head_storage_key(2_000).0,
hex_literal::hex!(
"cd710b30bd2eab0352ddcc26417aa1941b3c252fcb29d88eff4f3de5de4476c363f5a4efb16ffa83d0070000"
)
.to_vec()
);
}
}
}

Expand All @@ -74,16 +79,21 @@ mod calculate_dip_identity_commitment_storage_key_for_runtime {
))
}

#[test]
fn calculate_dip_identity_commitment_storage_key_for_runtime_successful_peregrine_parachain() {
use did::DidIdentifierOf;
use peregrine_runtime::Runtime as PeregrineRuntime;
use sp_core::crypto::Ss58Codec;
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn calculate_dip_identity_commitment_storage_key_for_runtime_successful_peregrine_parachain() {
use did::DidIdentifierOf;
use peregrine_runtime::Runtime as PeregrineRuntime;
use sp_core::crypto::Ss58Codec;

assert_eq!(
assert_eq!(
calculate_dip_identity_commitment_storage_key_for_runtime::<PeregrineRuntime>(&DidIdentifierOf::<PeregrineRuntime>::from_ss58check("4s3jpR7pzrUdhVUqHHdWoBN6oNQHBC7WRo7zsXdjAzQPT7Cf").unwrap(), 0).0,
hex_literal::hex!("b375edf06348b4330d1e88564111cb3d5bf19e4ed2927982e234d989e812f3f314c9211b34c8b43b2a18d67d5c96de9cb6caebbe9e3adeaaf693a2d198f2881d0b504fc72ed4ac0a7ed24a025fc228ce01a12dfa1fa4ab9a0000")
.to_vec()
);
}
}
}
57 changes: 31 additions & 26 deletions crates/kilt-dip-primitives/src/verifier/parachain/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,36 @@ where
}
}

#[test]
fn dip_parachain_state_proof_verifier_error_value_never_zero() {
assert!(
enum_iterator::all::<DipParachainStateProofVerifierError<u8>>().all(|e| u16::from(e) != 0),
"One of the u8 values for the error is 0, which is not allowed."
);
}
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn dip_parachain_state_proof_verifier_error_value_not_duplicated() {
enum_iterator::all::<DipParachainStateProofVerifierError<u8>>().fold(
sp_std::collections::btree_set::BTreeSet::<u16>::new(),
|mut values, new_value| {
let new_encoded_value = u16::from(new_value);
// DidOriginError is generic, and we cannot test its constraints in this unit
// test, so we skip it.
if new_encoded_value == u8::MAX as u16 * 3 {
return values;
}
assert!(
values.insert(new_encoded_value),
"Failed to add unique value {:#?} for error variant",
new_encoded_value
);
values
},
);
#[test]
fn dip_parachain_state_proof_verifier_error_value_never_zero() {
assert!(
enum_iterator::all::<DipParachainStateProofVerifierError<u8>>().all(|e| u16::from(e) != 0),
"One of the u8 values for the error is 0, which is not allowed."
);
}

#[test]
fn dip_parachain_state_proof_verifier_error_value_not_duplicated() {
enum_iterator::all::<DipParachainStateProofVerifierError<u8>>().fold(
sp_std::collections::btree_set::BTreeSet::<u16>::new(),
|mut values, new_value| {
let new_encoded_value = u16::from(new_value);
// DidOriginError is generic, and we cannot test its constraints in this unit
// test, so we skip it.
if new_encoded_value == u8::MAX as u16 * 3 {
return values;
}
assert!(
values.insert(new_encoded_value),
"Failed to add unique value {:#?} for error variant",
new_encoded_value
);
values
},
);
}
}
57 changes: 31 additions & 26 deletions crates/kilt-dip-primitives/src/verifier/relaychain/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,36 @@ where
}
}

#[test]
fn dip_relaychain_state_proof_verifier_error_value_never_zero() {
assert!(
enum_iterator::all::<DipRelaychainStateProofVerifierError<u8>>().all(|e| u16::from(e) != 0),
"One of the u8 values for the error is 0, which is not allowed."
);
}
#[cfg(test)]
mod tests {
use super::*;

#[test]
fn dip_relaychain_state_proof_verifier_error_value_not_duplicated() {
enum_iterator::all::<DipRelaychainStateProofVerifierError<u8>>().fold(
sp_std::collections::btree_set::BTreeSet::<u16>::new(),
|mut values, new_value| {
let new_encoded_value = u16::from(new_value);
// DidOriginError is generic, and we cannot test its constraints in this unit
// test, so we skip it.
if new_encoded_value == u8::MAX as u16 * 3 {
return values;
}
assert!(
values.insert(new_encoded_value),
"Failed to add unique value {:#?} for error variant",
new_encoded_value
);
values
},
);
#[test]
fn dip_relaychain_state_proof_verifier_error_value_never_zero() {
assert!(
enum_iterator::all::<DipRelaychainStateProofVerifierError<u8>>().all(|e| u16::from(e) != 0),
"One of the u8 values for the error is 0, which is not allowed."
);
}

#[test]
fn dip_relaychain_state_proof_verifier_error_value_not_duplicated() {
enum_iterator::all::<DipRelaychainStateProofVerifierError<u8>>().fold(
sp_std::collections::btree_set::BTreeSet::<u16>::new(),
|mut values, new_value| {
let new_encoded_value = u16::from(new_value);
// DidOriginError is generic, and we cannot test its constraints in this unit
// test, so we skip it.
if new_encoded_value == u8::MAX as u16 * 3 {
return values;
}
assert!(
values.insert(new_encoded_value),
"Failed to add unique value {:#?} for error variant",
new_encoded_value
);
values
},
);
}
}
Loading

0 comments on commit a27c8b6

Please sign in to comment.