Skip to content

Commit

Permalink
feat: Add evolution model and comprehensive tests for evolution requi…
Browse files Browse the repository at this point in the history
…rements
  • Loading branch information
JoelVR17 committed Dec 11, 2024
1 parent 0d7ac82 commit 1b13490
Showing 1 changed file with 121 additions and 3 deletions.
124 changes: 121 additions & 3 deletions src/models/evolution.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ struct Evolution {
}

mod rules {
/// Minimum rules for beast evolution

/// Minimum level required before evolution is possible
const LEVEL_REQUIREMENT: u32 = 10;
/// Minimum number of battles required before evolution
const REQUIRED_BATTLES: u32 = 5;
/// Item ID required for evolution (special evolution stone)
const REQUIRED_ITEM: u32 = 1001;

}

#[generate_trait]
Expand Down Expand Up @@ -49,3 +46,124 @@ impl EvolutionImpl of EvolutionTrait {
}
}
}


#[cfg(test)]
mod tests {
use super::{Evolution, EvolutionTrait};
use core::result::Result;

#[test]
fn test_evolution_with_valid_requirements() {

let mut evolution = Evolution {
base_beast_id: 1_u32,
evolved_beast_id: 0_u32,
level_requirement: 15_u32,
required_battles: 10_u32,
required_item: Option::Some(1001_u32)
};

let result = evolution.evolve();

match result {
Result::Ok(evolved_id) => {
assert(evolved_id == 10_u32, 'Incorrect evolution ID');
assert(evolution.evolved_beast_id == 10_u32, 'Beast not evolved correctly');
},
Result::Err(_) => {
panic!("Evolution should have succeeded")
}
}
}

#[test]
fn test_evolution_with_invalid_level() {

let mut evolution = Evolution {
base_beast_id: 1_u32,
evolved_beast_id: 0_u32,
level_requirement: 5_u32,
required_battles: 10_u32,
required_item: Option::Some(1001_u32)
};

let result = evolution.evolve();

match result {
Result::Ok(_) => {
panic!("Evolution should have failed due to low level")
},
Result::Err(e) => {
assert(e == 'Evolution requirements not met', 'Unexpected error message');
}
}
}

#[test]
fn test_evolution_with_invalid_battles() {

let mut evolution = Evolution {
base_beast_id: 1_u32,
evolved_beast_id: 0_u32,
level_requirement: 15_u32,
required_battles: 3_u32,
required_item: Option::Some(1001_u32)
};

let result = evolution.evolve();
match result {
Result::Ok(_) => {
panic!("Evolution should have failed due to insufficient battles")
},
Result::Err(e) => {
assert(e == 'Evolution requirements not met', 'Unexpected error message');
}
}
}

#[test]
fn test_evolution_with_invalid_item() {

let mut evolution = Evolution {
base_beast_id: 1_u32,
evolved_beast_id: 0_u32,
level_requirement: 15_u32,
required_battles: 10_u32,
required_item: Option::Some(999_u32)
};

let result = evolution.evolve();
match result {
Result::Ok(_) => {
panic!("Evolution should have failed due to wrong item")
},
Result::Err(e) => {
assert(e == 'Evolution requirements not met', 'Unexpected error message');
}
}
}

#[test]
fn test_evolution_with_no_item_requirement() {

let mut evolution = Evolution {
base_beast_id: 1_u32,
evolved_beast_id: 0_u32,
level_requirement: 15_u32,
required_battles: 10_u32,
required_item: Option::None
};

let result = evolution.evolve();

match result {
Result::Ok(evolved_id) => {
assert(evolved_id == 10_u32, 'Incorrect evolution ID');
},
Result::Err(_) => {
panic!("Evolution should have succeeded without item requirement")
}
}
}
}

0 comments on commit 1b13490

Please sign in to comment.