Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions examples/counter copy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "crucible-example-counter"
version.workspace = true
edition.workspace = true

[lib]
crate-type = ["cdylib", "rlib"]

[dependencies]
soroban-sdk = { workspace = true }

[dev-dependencies]
crucible = { path = "../../contracts/crucible" }
71 changes: 71 additions & 0 deletions examples/counter copy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#![no_std]
#![allow(deprecated)]
use soroban_sdk::{contract, contractimpl, contracttype, symbol_short, Env};

#[contracttype]
enum DataKey {
Count,
}

/// A simple on-chain counter contract.
#[contract]
#[derive(Default)]
pub struct Counter;

#[contractimpl]
impl Counter {
/// Initialize the counter with a starting value.
///
/// Panics if the counter has already been initialized.
pub fn initialize(env: Env, value: u32) {
if env.storage().instance().has(&DataKey::Count) {
panic!("already initialized");
}
env.storage().instance().set(&DataKey::Count, &value);
}

/// Increment the counter by 1 and return the new value.
pub fn increment(env: Env) -> u32 {
let count: u32 = env.storage().instance().get(&DataKey::Count).unwrap_or(0);
let new_count = count + 1;
env.storage().instance().set(&DataKey::Count, &new_count);
env.events().publish((symbol_short!("incr"),), new_count);
new_count
}

/// Decrement the counter by 1 and return the new value.
///
/// Panics if the counter is already at zero.
pub fn decrement(env: Env) -> u32 {
let count: u32 = env.storage().instance().get(&DataKey::Count).unwrap_or(0);
if count == 0 {
panic!("underflow: counter is already at zero");
}
let new_count = count - 1;
env.storage().instance().set(&DataKey::Count, &new_count);
env.events().publish((symbol_short!("decr"),), new_count);
new_count
}

/// Increment the counter by `amount` and return the new value.
pub fn increment_by(env: Env, amount: u32) -> u32 {
let count: u32 = env.storage().instance().get(&DataKey::Count).unwrap_or(0);
let new_count = count + amount;
env.storage().instance().set(&DataKey::Count, &new_count);
new_count
}

/// Return the current counter value.
pub fn get(env: Env) -> u32 {
env.storage().instance().get(&DataKey::Count).unwrap_or(0)
}

/// Reset the counter to zero.
pub fn reset(env: Env) {
env.storage().instance().set(&DataKey::Count, &0u32);
env.events().publish((symbol_short!("reset"),), ());
}
}

#[cfg(test)]
mod test;
134 changes: 134 additions & 0 deletions examples/counter copy/src/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#![cfg(test)]
extern crate std;

use crucible::prelude::*;
use crucible::{assert_emitted, assert_not_emitted, assert_reverts};
use soroban_sdk::{symbol_short, Address};

use crate::{Counter, CounterClient};

// ---------------------------------------------------------------------------
// Test fixture
// ---------------------------------------------------------------------------

#[fixture]
struct Ctx {
pub env: MockEnv,
pub id: Address,
}

impl Ctx {
pub fn setup() -> Self {
let env = MockEnv::builder().with_contract::<Counter>().build();
let id = env.contract_id::<Counter>();
Ctx { env, id }
}

fn client(&self) -> CounterClient<'_> {
CounterClient::new(self.env.inner(), &self.id)
}
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[test]
fn test_initial_value_is_zero() {
let f = Ctx::setup();
assert_eq!(f.client().get(), 0);
}

#[test]
fn test_increment_increases_count() {
let f = Ctx::setup();
let c = f.client();
assert_eq!(c.increment(), 1);
assert_eq!(c.increment(), 2);
assert_eq!(c.increment(), 3);
assert_eq!(c.get(), 3);
}

#[test]
fn test_increment_by_bulk() {
let f = Ctx::setup();
let c = f.client();
assert_eq!(c.increment_by(&5), 5);
assert_eq!(c.increment_by(&10), 15);
assert_eq!(c.get(), 15);
}

#[test]
fn test_decrement_decreases_count() {
let f = Ctx::setup();
let c = f.client();
c.increment();
c.increment();
assert_eq!(c.decrement(), 1);
assert_eq!(c.decrement(), 0);
}

#[test]
fn test_decrement_at_zero_reverts() {
let f = Ctx::setup();
assert_reverts!(f.client().decrement(), "underflow");
}

#[test]
fn test_reset_clears_count() {
let f = Ctx::setup();
let c = f.client();
c.increment_by(&42);
assert_eq!(c.get(), 42);
c.reset();
assert_eq!(c.get(), 0);
}

#[test]
fn test_initialize_sets_starting_value() {
let f = Ctx::setup();
let c = f.client();
c.initialize(&10);
assert_eq!(c.get(), 10);
assert_eq!(c.increment(), 11);
}

#[test]
fn test_double_initialize_reverts() {
let f = Ctx::setup();
let c = f.client();
c.initialize(&5);
assert_reverts!(c.initialize(&10), "already initialized");
}

#[test]
fn test_increment_emits_event() {
let f = Ctx::setup();
f.client().increment();
assert_emitted!(f.env, f.id, (symbol_short!("incr"),), 1_u32);
}

#[test]
fn test_reset_emits_event() {
let f = Ctx::setup();
f.client().reset();
assert_emitted!(f.env, f.id, (symbol_short!("reset"),), ());
}

#[test]
fn test_get_emits_no_event() {
let f = Ctx::setup();
f.client().get();
assert_not_emitted!(f.env);
}

#[test]
fn test_fixture_reset_restores_state() {
let mut f = Ctx::setup();
f.client().increment_by(&99);
assert_eq!(f.client().get(), 99);

// Reset fixture to a clean state
f.reset();
assert_eq!(f.client().get(), 0);
}
Loading
Loading