forked from Sovereign-Labs/sovereign-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mocks for
sov-modules-api
(Sovereign-Labs#45)
- Loading branch information
Showing
22 changed files
with
177 additions
and
102 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
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
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
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,68 @@ | ||
use crate::Context; | ||
use sov_state::storage::{StorageKey, StorageValue}; | ||
use std::{collections::HashMap, sync::Arc}; | ||
|
||
/// Mock for Context::PublicKey, useful for testing. | ||
#[derive(borsh::BorshDeserialize, PartialEq, Eq)] | ||
pub struct MockPublicKey { | ||
pub_key: Vec<u8>, | ||
} | ||
|
||
impl MockPublicKey { | ||
pub fn new(pub_key: Vec<u8>) -> Self { | ||
Self { pub_key } | ||
} | ||
} | ||
|
||
/// Mock for Context::Signature, useful for testing. | ||
#[derive(borsh::BorshDeserialize, PartialEq, Eq)] | ||
pub struct MockSignature { | ||
sig: Vec<u8>, | ||
} | ||
|
||
impl MockSignature { | ||
pub fn new(sig: Vec<u8>) -> Self { | ||
Self { sig } | ||
} | ||
} | ||
|
||
/// Mock for Context::Storage, useful for testing. | ||
// TODO: as soon as we have JMT storage implemented, we should remove this mock and use a real db even in tests. | ||
// see https://github.com/Sovereign-Labs/sovereign/issues/40 | ||
#[derive(Clone, Default)] | ||
pub struct MockStorage { | ||
storage: HashMap<Arc<Vec<u8>>, Arc<Vec<u8>>>, | ||
} | ||
|
||
impl sov_state::Storage for MockStorage { | ||
fn get(&mut self, key: StorageKey, _version: u64) -> Option<StorageValue> { | ||
self.storage | ||
.get(&key.key) | ||
.map(|v| StorageValue { value: v.clone() }) | ||
} | ||
|
||
fn set(&mut self, key: StorageKey, _version: u64, value: StorageValue) { | ||
self.storage.insert(key.key, value.value); | ||
} | ||
|
||
fn delete(&mut self, key: StorageKey, _version: u64) { | ||
self.storage.remove(&key.key); | ||
} | ||
} | ||
|
||
/// Mock for Context, useful for testing. | ||
pub struct MockContext { | ||
sender: MockPublicKey, | ||
} | ||
|
||
impl Context for MockContext { | ||
type Storage = MockStorage; | ||
|
||
type Signature = MockSignature; | ||
|
||
type PublicKey = MockPublicKey; | ||
|
||
fn sender(&self) -> &Self::PublicKey { | ||
&self.sender | ||
} | ||
} |
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
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
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
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 |
---|---|---|
@@ -1,5 +1,3 @@ | ||
mod utils; | ||
|
||
#[test] | ||
fn tests() { | ||
let t = trybuild::TestCases::new(); | ||
|
8 changes: 4 additions & 4 deletions
8
sov-modules/sov-modules-macros/tests/derive_on_enum_not_supported.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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
mod utils; | ||
use sov_modules_api::Context; | ||
use sov_modules_macros::ModuleInfo; | ||
use utils::{Context, TestState}; | ||
use sov_state::StateMap; | ||
|
||
#[derive(ModuleInfo)] | ||
enum TestStruct<C: Context> { | ||
#[state] | ||
TestState1(TestState<C::Storage>), | ||
TestState1(StateMap<String, String, C::Storage>), | ||
|
||
#[state] | ||
TestState2(TestState<C::Storage>), | ||
TestState2(StateMap<String, String, C::Storage>), | ||
} | ||
|
||
fn main() {} |
8 changes: 4 additions & 4 deletions
8
sov-modules/sov-modules-macros/tests/field_missing_attribute.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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
mod utils; | ||
use sov_modules_api::Context; | ||
use sov_modules_macros::ModuleInfo; | ||
use utils::{Context, TestState}; | ||
use sov_state::StateMap; | ||
|
||
#[derive(ModuleInfo)] | ||
struct TestStruct<C: Context> { | ||
test_state1: TestState<C::Storage>, | ||
test_state1: StateMap<u32, u32, C::Storage>, | ||
|
||
#[state] | ||
test_state2: TestState<C::Storage>, | ||
test_state2: StateMap<Vec<u8>, u64, C::Storage>, | ||
} | ||
|
||
fn main() {} |
2 changes: 1 addition & 1 deletion
2
sov-modules/sov-modules-macros/tests/field_missing_attribute.stderr
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
error: This field is missing an attribute: add `#[module]` or `#[state]`. | ||
--> tests/field_missing_attribute.rs:7:5 | ||
| | ||
7 | test_state1: TestState<C::Storage>, | ||
7 | test_state1: StateMap<u32, u32, C::Storage>, | ||
| ^^^^^^^^^^^ |
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
8 changes: 4 additions & 4 deletions
8
sov-modules/sov-modules-macros/tests/not_supported_attribute.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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
mod utils; | ||
use sov_modules_api::Context; | ||
use sov_modules_macros::ModuleInfo; | ||
use utils::{Context, TestState}; | ||
use sov_state::StateMap; | ||
|
||
#[derive(ModuleInfo)] | ||
struct TestStruct<C: Context> { | ||
#[other] | ||
test_state1: TestState<C::Storage>, | ||
test_state1: StateMap<u32, String, C::Storage>, | ||
|
||
#[state] | ||
test_state2: TestState<C::Storage>, | ||
test_state2: StateMap<C::PublicKey, String, C::Storage>, | ||
} | ||
|
||
fn main() {} |
2 changes: 1 addition & 1 deletion
2
sov-modules/sov-modules-macros/tests/not_supported_attribute.stderr
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
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 |
---|---|---|
@@ -1,15 +1,14 @@ | ||
mod utils; | ||
|
||
use sov_modules_api::Context; | ||
use sov_modules_macros::ModuleInfo; | ||
use utils::{Context, TestState}; | ||
use sov_state::StateMap; | ||
|
||
#[derive(ModuleInfo)] | ||
struct TestStruct<C: Context> { | ||
#[state] | ||
test_state1: [usize; 22], | ||
|
||
#[state] | ||
test_state2: TestState<C::Storage>, | ||
test_state2: StateMap<u32, u32, C::Storage>, | ||
} | ||
|
||
fn main() {} |
4 changes: 2 additions & 2 deletions
4
sov-modules/sov-modules-macros/tests/not_supported_type.stderr
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
error: Type not supported by the `ModuleInfo` macro | ||
--> tests/not_supported_type.rs:9:18 | ||
--> tests/not_supported_type.rs:8:18 | ||
| | ||
9 | test_state1: [usize; 22], | ||
8 | test_state1: [usize; 22], | ||
| ^^^^^^^^^^^ |
Oops, something went wrong.