-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tutorials > Polkadot SDK > Parachains > Build Custom Pallet > Pallet …
…Unit Testing (#233) * fix: wip * fix: adding snippets * fix: typo * Apply suggestions from code review Co-authored-by: 0xLucca <95830307+0xLucca@users.noreply.github.com> * fix: updating mock runtime\ * fix: snippets comments * fix: typo * fix: closing expandable elements * fix: overflow * fix: overflow * Update tutorials/polkadot-sdk/parachains/build-custom-pallet/pallet-unit-testing.md Co-authored-by: Erin Shaben <eshaben@icloud.com> * fix: feedback * fix: clean up the add tutorial branch * Update tutorials/polkadot-sdk/parachains/build-custom-pallet/pallet-unit-testing.md Co-authored-by: Erin Shaben <eshaben@icloud.com> --------- Co-authored-by: 0xLucca <95830307+0xLucca@users.noreply.github.com> Co-authored-by: Erin Shaben <eshaben@icloud.com>
- Loading branch information
1 parent
4463223
commit 47d2f0d
Showing
14 changed files
with
522 additions
and
528 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 0 additions & 18 deletions
18
.snippets/code/tutorials/polkadot-sdk/parachains/build-custom-pallet/build-pallet/Cargo.toml
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
...code/tutorials/polkadot-sdk/parachains/build-custom-pallet/build-pallet/call_structure.rs
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
...utorials/polkadot-sdk/parachains/build-custom-pallet/build-pallet/compilation-output.html
This file was deleted.
Oops, something went wrong.
184 changes: 0 additions & 184 deletions
184
.snippets/code/tutorials/polkadot-sdk/parachains/build-custom-pallet/build-pallet/lib.rs
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
...ppets/code/tutorials/polkadot-sdk/parachains/build-custom-pallet/build-pallet/scaffold.rs
This file was deleted.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
...lkadot-sdk/parachains/build-custom-pallet/pallet-unit-testing/Cargo-dev-dependencies.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[dependencies] | ||
... | ||
|
||
[dev-dependencies] | ||
sp-core = { workspace = true, default-features = true } | ||
sp-io = { workspace = true, default-features = true } | ||
sp-runtime = { workspace = true, default-features = true } | ||
|
||
[features] | ||
... |
52 changes: 52 additions & 0 deletions
52
...ts/code/tutorials/polkadot-sdk/parachains/build-custom-pallet/pallet-unit-testing/mock.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use crate as pallet_custom; | ||
use frame_support::{derive_impl, parameter_types}; | ||
use sp_runtime::BuildStorage; | ||
|
||
type Block = frame_system::mocking::MockBlock<Test>; | ||
|
||
#[frame_support::runtime] | ||
mod runtime { | ||
#[runtime::runtime] | ||
#[runtime::derive( | ||
RuntimeCall, | ||
RuntimeEvent, | ||
RuntimeError, | ||
RuntimeOrigin, | ||
RuntimeFreezeReason, | ||
RuntimeHoldReason, | ||
RuntimeSlashReason, | ||
RuntimeLockId, | ||
RuntimeTask | ||
)] | ||
pub struct Test; | ||
|
||
#[runtime::pallet_index(0)] | ||
pub type System = frame_system::Pallet<Test>; | ||
|
||
#[runtime::pallet_index(1)] | ||
pub type CustomPallet = pallet_custom::Pallet<Test>; | ||
} | ||
|
||
// System pallet configuration | ||
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] | ||
impl frame_system::Config for Test { | ||
type Block = Block; | ||
} | ||
|
||
// Custom pallet configuration | ||
parameter_types! { | ||
pub const CounterMaxValue: u32 = 10; | ||
} | ||
|
||
impl pallet_custom::Config for Test { | ||
type RuntimeEvent = RuntimeEvent; | ||
type CounterMaxValue = CounterMaxValue; | ||
} | ||
|
||
// Test externalities initialization | ||
pub fn new_test_ext() -> sp_io::TestExternalities { | ||
frame_system::GenesisConfig::<Test>::default() | ||
.build_storage() | ||
.unwrap() | ||
.into() | ||
} |
24 changes: 24 additions & 0 deletions
24
...ode/tutorials/polkadot-sdk/parachains/build-custom-pallet/pallet-unit-testing/output.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<div id="termynal" data-termynal> | ||
<span data-ty="input"><span class="file-path"></span>cargo test --package custom-pallet</span> | ||
<pre> | ||
running 12 tests | ||
test mock::__construct_runtime_integrity_test::runtime_integrity_tests ... ok | ||
test mock::test_genesis_config_builds ... ok | ||
test test::set_counter_value_fails_for_max_value_exceeded ... ok | ||
test test::set_counter_value_fails_for_non_root ... ok | ||
test test::user_interactions_increment ... ok | ||
test test::it_works_for_increment ... ok | ||
test test::it_works_for_set_counter_value ... ok | ||
test test::it_works_for_decrement ... ok | ||
test test::increment_handles_overflow ... ok | ||
test test::decrement_fails_for_below_zero ... ok | ||
test test::increment_fails_for_max_value_exceeded ... ok | ||
test test::user_interactions_overflow ... ok | ||
test result: ok. 12 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s | ||
|
||
Doc-tests custom_pallet | ||
running 0 tests | ||
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s | ||
</pre | ||
> | ||
</div> |
Oops, something went wrong.