Skip to content

Commit

Permalink
feat: add the grip unit test crate (#32)
Browse files Browse the repository at this point in the history
Adds the `#[grip::test]` proc macro to hide implementation details from
users and make writing unit tests seamless.
  • Loading branch information
alexfertel authored Apr 4, 2024
1 parent 3df4d8a commit 1934d3d
Show file tree
Hide file tree
Showing 18 changed files with 525 additions and 287 deletions.
40 changes: 25 additions & 15 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["contracts", "lib/*"]
members = ["contracts", "lib/crypto", "lib/grip", "lib/grip-proc"]
# Explicitly set the resolver to version 2, which is the default for packages
# with edition >= 2021.
# https://doc.rust-lang.org/edition-guide/rust-2021/default-cargo-resolver.html
Expand Down
2 changes: 1 addition & 1 deletion contracts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ stylus-proc = { version = "0.4.3", default-features = false }
mini-alloc = "0.4.2"

[dev-dependencies]
wavm-shims = { path = "../lib/wavm-shims" }
grip = { path = "../lib/grip" }

[features]
default = []
Expand Down
84 changes: 39 additions & 45 deletions contracts/src/erc20/extensions/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,6 @@ mod tests {
use stylus_sdk::storage::{StorageBool, StorageString, StorageType};

use super::{Metadata, DEFAULT_DECIMALS};
#[allow(unused_imports)]
use crate::test_utils;

impl Default for Metadata {
fn default() -> Self {
Expand All @@ -111,50 +109,46 @@ mod tests {
}
}

#[test]
fn constructs() {
test_utils::with_storage::<Metadata>(|meta| {
let name = meta.name();
let symbol = meta.symbol();
let decimals = meta.decimals();
let initialized = meta._initialized.get();
assert_eq!(name, "");
assert_eq!(symbol, "");
assert_eq!(decimals, DEFAULT_DECIMALS);
assert_eq!(initialized, false);

const NAME: &str = "Meta";
const SYMBOL: &str = "Symbol";
meta.constructor(NAME.to_owned(), SYMBOL.to_owned());

let name = meta.name();
let symbol = meta.symbol();
let decimals = meta.decimals();
let initialized = meta._initialized.get();
assert_eq!(name, NAME);
assert_eq!(symbol, SYMBOL);
assert_eq!(decimals, DEFAULT_DECIMALS);
assert_eq!(initialized, true);
})
#[grip::test]
fn constructs(meta: Metadata) {
let name = meta.name();
let symbol = meta.symbol();
let decimals = meta.decimals();
let initialized = meta._initialized.get();
assert_eq!(name, "");
assert_eq!(symbol, "");
assert_eq!(decimals, DEFAULT_DECIMALS);
assert_eq!(initialized, false);

const NAME: &str = "Meta";
const SYMBOL: &str = "Symbol";
meta.constructor(NAME.to_owned(), SYMBOL.to_owned());

let name = meta.name();
let symbol = meta.symbol();
let decimals = meta.decimals();
let initialized = meta._initialized.get();
assert_eq!(name, NAME);
assert_eq!(symbol, SYMBOL);
assert_eq!(decimals, DEFAULT_DECIMALS);
assert_eq!(initialized, true);
}

#[test]
fn constructs_only_once() {
test_utils::with_storage::<Metadata>(|meta| {
const NAME: &str = "Meta";
const SYMBOL: &str = "Symbol";
meta.constructor(NAME.to_owned(), SYMBOL.to_owned());

meta.constructor("Invalid".to_owned(), "Invalid".to_owned());

let name = meta.name();
let symbol = meta.symbol();
let decimals = meta.decimals();
let initialized = meta._initialized.get();
assert_eq!(name, NAME);
assert_eq!(symbol, SYMBOL);
assert_eq!(decimals, DEFAULT_DECIMALS);
assert_eq!(initialized, true);
})
#[grip::test]
fn constructs_only_once(meta: Metadata) {
const NAME: &str = "Meta";
const SYMBOL: &str = "Symbol";
meta.constructor(NAME.to_owned(), SYMBOL.to_owned());

meta.constructor("Invalid".to_owned(), "Invalid".to_owned());

let name = meta.name();
let symbol = meta.symbol();
let decimals = meta.decimals();
let initialized = meta._initialized.get();
assert_eq!(name, NAME);
assert_eq!(symbol, SYMBOL);
assert_eq!(decimals, DEFAULT_DECIMALS);
assert_eq!(initialized, true);
}
}
Loading

0 comments on commit 1934d3d

Please sign in to comment.