Skip to content
Open
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
77 changes: 77 additions & 0 deletions tests/test_ownable_counter.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use starknet::{ContractAddress};

use snforge_std::{declare, ContractClassTrait, DeclareResultTrait};
use cairo_bootcamp_3::ownable_counter::{IOwnableCounterDispatcher};

pub mod Accounts {
use starknet::ContractAddress;
use starknet::contract_address_const;
use core::traits::TryInto;

pub fn zero() -> ContractAddress {
0x0000000000000000000000000000000000000000.try_into().unwrap()
}

pub fn admin() -> ContractAddress {
contract_address_const::<'admin'>()
}

pub fn owner() -> ContractAddress {
contract_address_const::<'owner'>()
}
}

fn deploy_contract(name: ByteArray) -> ContractAddress {
let contract = declare(name).unwrap().contract_class();
let constructor_args = array![Accounts::admin().into()];
let (contract_address, _) = contract.deploy(@constructor_args).unwrap();
contract_address
}

#[test]
fn test_increase_count() {
let contract_address = deploy_contract("OwnableCounter");

let dispatcher = IOwnableCounterDispatcher { contract_address };

let count = dispatcher.increase_count(234);

assert(count == 234, 'count not increased');
}

#[test]
fn test_get_count() {
let contract_address = deploy_contract("OwnableCounter");

let dispatcher = IOwnableCounterDispatcher { contract_address };

let count = dispatcher.increase_count(234);

assert(count == 20, 'count not increased');

let count = dispatcher.get_count();
assert(count, 'count not retrieved');
}

#[test]
fn test_add_owner() {
let contract_address = deploy_contract("OwnableCounter");

let dispatcher = IOwnableCounterDispatcher { contract_address };

let owner = dispatcher.add_owner(Accounts::owner());
assert(owner == Accounts::owner(), 'owner not added');
}

#[test]
fn test_get_owner() {
let contract_address = deploy_contract("OwnableCounter");

let dispatcher = IOwnableCounterDispatcher { contract_address };

let owner = dispatcher.add_owner(Accounts::owner());
assert(owner == Accounts::owner(), 'owner not added');

let owner = dispatcher.get_owner();
assert(owner, 'owner not retrieved');
}
102 changes: 101 additions & 1 deletion tests/test_student_registry.cairo
Original file line number Diff line number Diff line change
@@ -1 +1,101 @@
// Write test for the StudentRegistry contract here
use starknet::{ContractAddress};

use snforge_std::{declare, ContractClassTrait, DeclareResultTrait};
use cairo_bootcamp_3::student_registry::{
IStudentRegistryDispatcher, IStudentRegistryDispatcherTrait
};

pub mod Accounts {
use starknet::ContractAddress;
use starknet::contract_address_const;
use core::traits::TryInto;

pub fn zero() -> ContractAddress {
0x0000000000000000000000000000000000000000.try_into().unwrap()
}

pub fn admin() -> ContractAddress {
contract_address_const::<'admin'>()
}

pub fn student1() -> ContractAddress {
contract_address_const::<'student1'>()
}

pub fn student2() -> ContractAddress {
contract_address_const::<'student2'>()
}
}

fn deploy_contract(name: ByteArray) -> ContractAddress {
let contract = declare(name).unwrap().contract_class();
let constructor_args = array![Accounts::admin().into()];
let (contract_address, _) = contract.deploy(@constructor_args).unwrap();
contract_address
}

#[test]
fn test_add_student() {
let contract_address = deploy_contract("StudentRegistry");

let dispatcher = IStudentRegistryDispatcher { contract_address };

let student = dispatcher.add_student('Paul', Accounts::student1(), 18, 40, true);

assert(student == true, 'Student not added');
}

#[test]
fn test_get_student() {
// add student
let contract_address = deploy_contract("StudentRegistry");

let dispatcher = IStudentRegistryDispatcher { contract_address };

let student = dispatcher.add_student('Paul', Accounts::student1(), 18, 40, true);
assert(student == true, 'Student not added');

// get added student
let (name, account, age, xp, is_active) = dispatcher.get_student(Accounts::student1());
assert(name == 'Paul', 'name should match');
assert(account == Accounts::student1(), 'account should match');
assert(age == 18, 'age should match');
assert(xp == 40, 'xp should match');
assert(is_active == true, 'is_active should match');
}

#[test]
fn test_update_student() {
let contract_address = deploy_contract("StudentRegistry");

let dispatcher = IStudentRegistryDispatcher { contract_address };

let student = dispatcher.add_student('Paul', Accounts::student1(), 19, 40, true);
assert(student == true, 'student not updated');

let student0 = dispatcher.update_student('Joseph', Accounts::student1(), 19, 35, true);
assert(student0 == true, 'student not updated');
let (name, account, age, xp, is_active) = dispatcher.get_student(Accounts::student1());

assert(name == 'Joseph', 'name should match');
assert(account == Accounts::student1(), 'account should match');
assert(age == 19, 'age should match');
assert(xp == 35, 'xp should match');
assert(is_active == true, 'is_active should match');
}


#[test]
#[should_panic(expected: ('ZERO ADDRESS!',))]
fn test_not_update_zero_address() {
let contract_address = deploy_contract("StudentRegistry");

let dispatcher = IStudentRegistryDispatcher { contract_address };

let student = dispatcher.add_student('Paul', Accounts::student1(), 18, 40, true);

assert(student == true, 'Student not created');

let student = dispatcher.update_student('Joseph', Accounts::zero(), 19, 35, true);
assert(student == false, 'cannot update zero address');
}