forked from gear-foundation/dapps-template-gear-academy
-
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.
- Loading branch information
1 parent
64735d6
commit a06e5bf
Showing
3 changed files
with
70 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,45 @@ | ||
#![no_std] | ||
|
||
#[allow(unused_imports)] | ||
use gstd::prelude::*; | ||
use gstd::{msg,exec,prelude::*}; | ||
use tamagotchi_io::*; | ||
|
||
static mut TAMAGOTCHI :Option<Tamagotchi> = None; | ||
|
||
#[no_mangle] | ||
extern fn init() { | ||
// TODO: 5️⃣ Initialize the Tamagotchi program | ||
let initname = msg::load().expect("unable to load name"); | ||
let birthdate = exec::block_timestamp(); | ||
let tmg = Tamagotchi { | ||
name:initname, | ||
date_of_birth: birthdate, | ||
}; | ||
unsafe { | ||
TAMAGOTCHI = Some(tmg); | ||
}; | ||
} | ||
|
||
#[no_mangle] | ||
extern fn handle() { | ||
// TODO: 6️⃣ Add handling of `Name` and `Age` actions | ||
let action:TmgAction = msg::load().expect("unable to load action"); | ||
let tmg = unsafe { | ||
TAMAGOTCHI.get_or_insert(Default::default()) | ||
}; | ||
match action { | ||
TmgAction::Name => { | ||
msg::reply(TmgEvent::Name(tmg.name.clone()),0).expect("Error in a reply'tamagotchi::name'"); | ||
}, | ||
TmgAction::Age => { | ||
let age = exec::block_timestamp() - tmg.date_of_birth; | ||
msg::reply(TmgEvent::Age(age),0).expect("Error in a reply'tamagotchi::age'"); | ||
}, | ||
} | ||
} | ||
|
||
#[no_mangle] | ||
extern fn state() { | ||
// TODO: 7️⃣ Return the Tamagotchi state | ||
let tmg = unsafe { TAMAGOTCHI.take().expect("Unexpected error in taking state") }; | ||
msg::reply(tmg, 0).expect("Failed to share state"); | ||
} |
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,10 +1,39 @@ | ||
use gtest::{Program, System}; | ||
|
||
|
||
use gtest::{Program, System,Log}; | ||
use tamagotchi_io::*; | ||
|
||
#[test] | ||
fn smoke_test() { | ||
let sys = System::new(); | ||
sys.init_logger(); | ||
let _program = Program::current(&sys); | ||
let program = Program::current(&sys); | ||
let result = program.send(2, String::from("Goodmoring")); | ||
assert!(!result.main_failed()); | ||
let result = program.send(2, TmgAction::Name); | ||
let log = Log::builder().dest(2).payload(TmgEvent::Name(String::from("Goodmoring"))); | ||
assert!(result.contains(&log)); | ||
let _result = program.send(2, TmgAction::Age); | ||
// let log = Log::builder().dest(2).payload(TmgEvent::Age(sys.block_timestamp())); | ||
// assert!(result.contains(&log)); | ||
|
||
//How to test the age? | ||
} | ||
|
||
#[test] | ||
fn negative_smoke_test() { | ||
let sys = System::new(); | ||
sys.init_logger(); | ||
let program = Program::current(&sys); | ||
let payload = vec![1,2,3]; | ||
let _result = program.send(2, payload); | ||
// assert!(result.main_failed()); | ||
// Why the assert is panic? | ||
|
||
// TODO: 8️⃣ Test the program initialization and message handling | ||
// let result = program.send(1, TmgAction::Name); | ||
// let log = Log::builder().dest(2).payload(TmgEvent::Name("Goodmoring".to_string())); | ||
// assert!(!result.contains(&log)); | ||
// let result = program.send(1, TmgAction::Age); | ||
// let log = Log::builder().dest(2).payload(TmgEvent::Age(sys.block_timestamp())); | ||
// assert!(!result.contains(&log)); | ||
} |