Skip to content

Commit

Permalink
test(axelar-std): add derive macro unit tests (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbayindirli authored Jan 22, 2025
1 parent b672e2f commit f52dded
Show file tree
Hide file tree
Showing 17 changed files with 224 additions and 1 deletion.
1 change: 1 addition & 0 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 packages/axelar-std-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub fn derive_upgradable(input: TokenStream) -> TokenStream {
/// # mod test {
/// use core::fmt::Debug;
/// use stellar_axelar_std::events::Event;
/// use stellar_axelar_std_derive::IntoEvent;
/// use stellar_axelar_std::IntoEvent;
/// use soroban_sdk::{Address, contract, contractimpl, Env, String};
///
/// #[derive(Debug, PartialEq, IntoEvent)]
Expand Down
1 change: 1 addition & 0 deletions packages/axelar-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ stellar-axelar-std-derive = { workspace = true, optional = true }
goldie = { workspace = true }
paste = { workspace = true }
soroban-sdk = { workspace = true, features = ["testutils"] }
stellar-axelar-std = { workspace = true, features = ["testutils"] }
stellar-axelar-std-derive = { workspace = true, features = ["testutils"] }

[features]
Expand Down
182 changes: 182 additions & 0 deletions packages/axelar-std/tests/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
use soroban_sdk::testutils::Address as _;
use soroban_sdk::{contract, contractimpl, Address, Env, String};
use stellar_axelar_std::events::{fmt_last_emitted_event, Event};
use stellar_axelar_std::IntoEvent;

#[contract]
pub struct Contract;

#[derive(Debug, PartialEq, Eq, IntoEvent)]
struct EmptyEvent;

#[derive(Debug, PartialEq, Eq, IntoEvent)]
struct SingleDataEvent {
topic1: Address,
topic2: Address,
#[data]
data: String,
}

#[derive(Debug, PartialEq, Eq, IntoEvent)]
#[event_name("custom_name")]
struct NamedEvent {
topic: Address,
#[data]
data: String,
}

#[derive(Debug, PartialEq, Eq, IntoEvent)]
struct MultiDataEvent {
topic1: String,
topic2: Address,
#[data]
data1: String,
#[data]
data2: String,
}

#[derive(Debug, PartialEq, Eq, IntoEvent)]
struct NoDataEvent {
topic1: String,
topic2: Address,
}

#[derive(Debug, PartialEq, Eq, IntoEvent)]
struct NoTopicEvent {
#[data]
data1: String,
#[data]
data2: Address,
#[data]
data3: String,
}

#[contractimpl]
impl Contract {
pub const fn __constructor() {}

pub fn empty(env: &Env) {
EmptyEvent.emit(env);
}

pub fn single_data(env: &Env, topic1: Address, topic2: Address, data: String) {
SingleDataEvent {
topic1,
topic2,
data,
}
.emit(env);
}

pub fn named(env: &Env, topic: Address, data: String) {
NamedEvent { topic, data }.emit(env);
}

pub fn multi_data(env: &Env, topic1: String, topic2: Address, data1: String, data2: String) {
MultiDataEvent {
topic1,
topic2,
data1,
data2,
}
.emit(env);
}

pub fn no_data(env: &Env, topic1: String, topic2: Address) {
NoDataEvent { topic1, topic2 }.emit(env);
}

pub fn no_topic(env: &Env, data1: String, data2: Address, data3: String) {
NoTopicEvent {
data1,
data2,
data3,
}
.emit(env);
}
}

#[test]
fn event_empty_succeeds() {
let env = &Env::default();

let contract_id = env.register(Contract, ());
let client = ContractClient::new(env, &contract_id);

client.empty();
goldie::assert!(fmt_last_emitted_event::<EmptyEvent>(env));
}

#[test]
fn single_data_event_emitted() {
let env = &Env::default();

let contract_id = env.register(Contract, ());
let client = ContractClient::new(env, &contract_id);

let topic1 = Address::generate(env);
let topic2 = Address::generate(env);
let data = String::from_str(env, "100");

client.single_data(&topic1, &topic2, &data);
goldie::assert!(fmt_last_emitted_event::<SingleDataEvent>(env));
}

#[test]
fn named_event_emitted() {
let env = &Env::default();

let contract_id = env.register(Contract, ());
let client = ContractClient::new(env, &contract_id);

let topic = Address::generate(env);
let data = String::from_str(env, "100");

client.named(&topic, &data);
goldie::assert!(fmt_last_emitted_event::<NamedEvent>(env));
}

#[test]
fn multi_data_event_emitted() {
let env = &Env::default();

let contract_id = env.register(Contract, ());
let client = ContractClient::new(env, &contract_id);

let topic1 = String::from_str(env, "topic-1");
let topic2 = Address::generate(env);
let data1 = String::from_str(env, "data-1");
let data2 = String::from_str(env, "data-2");

client.multi_data(&topic1, &topic2, &data1, &data2);
goldie::assert!(fmt_last_emitted_event::<MultiDataEvent>(env));
}

#[test]
fn no_data_event_emitted() {
let env = &Env::default();

let contract_id = env.register(Contract, ());
let client = ContractClient::new(env, &contract_id);

let topic1 = String::from_str(env, "topic-1");
let topic2 = Address::generate(env);

client.no_data(&topic1, &topic2);
goldie::assert!(fmt_last_emitted_event::<NoDataEvent>(env));
}

#[test]
fn no_topic_event_emitted() {
let env = &Env::default();

let contract_id = env.register(Contract, ());
let client = ContractClient::new(env, &contract_id);

let data1 = String::from_str(env, "data-1");
let data2 = Address::generate(env);
let data3 = String::from_str(env, "data-3");

client.no_topic(&data1, &data2, &data3);
goldie::assert!(fmt_last_emitted_event::<NoTopicEvent>(env));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract: Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4)
topics: (Symbol(emitted_data), String(topic-1), Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHK3M))
data: (String(data-1), String(data-2))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract: Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM)
topics: (Symbol(empty))
data: ()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract: Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM)
topics: (Symbol(multi_data), String(topic-1), Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4))
data: (String(data-1), String(data-2))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract: Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM)
topics: (Symbol(custom_name), Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4))
data: (String(100))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract: Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM)
topics: (Symbol(no_data), String(topic-1), Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4))
data: ()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract: Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM)
topics: (Symbol(no_topic))
data: (String(data-1), Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4), String(data-3))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract: Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM)
topics: (Symbol(single_data), Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4), Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHK3M))
data: (String(100))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract: Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4)
topics: (Symbol(transferred), Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHK3M), Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAITA4))
data: (String(100))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract: Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM)
topics: (Symbol(multi_data), String(topic-1), Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4))
data: (String(data-1), String(data-2))
3 changes: 3 additions & 0 deletions packages/axelar-std/tests/testdata/named_event_emitted.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract: Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM)
topics: (Symbol(custom_name), Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4))
data: (String(100))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract: Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM)
topics: (Symbol(no_data), String(topic-1), Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4))
data: ()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract: Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM)
topics: (Symbol(no_topic))
data: (String(data-1), Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4), String(data-3))
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract: Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM)
topics: (Symbol(single_data), Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFCT4), Contract(CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHK3M))
data: (String(100))

0 comments on commit f52dded

Please sign in to comment.