use crate::prelude::*;
#[derive(Clone, Debug, PartialEq)]
pub enum Creature {
Bulbasaur,
Ivysaur,
Venusaur,
Charmander,
Charmaleon,
Charizard,
Squirtle,
Wartortle,
Blastoise,
}
#[derive(Debug, PartialEq, Eq)]
pub enum Command {
Evolute,
Devolute,
Close,
}
pub fn pokemon_op_sys(stack: &mut Stack<Creature>, operator: &Command) {
use Creature::*;
let last_creature = stack.pop().unwrap();
match operator {
Command::Evolute => stack.push(match last_creature {
Bulbasaur => Ivysaur,
Ivysaur => Venusaur,
Charmander => Charmaleon,
Charmaleon => Charizard,
Squirtle => Wartortle,
Wartortle => Blastoise,
any_other => any_other,
}),
Command::Devolute => stack.push(match last_creature {
Ivysaur => Bulbasaur,
Venusaur => Ivysaur,
Charmaleon => Charmander,
Charizard => Charmaleon,
Wartortle => Squirtle,
Blastoise => Wartortle,
any_other => any_other,
}),
Command::Close => {}
}
}
#[cfg(test)]
mod tests {
use crate::op_systems::pokemon::{pokemon_op_sys, Command::*, Creature::*};
use crate::prelude::*;
#[test]
fn test_evolution() {
let mut machine = Machine::new(&pokemon_op_sys);
let my_creature = Charmander;
let result = machine.operate(&Item::Value(my_creature)).unwrap();
assert_eq!(result, &Charmander);
assert_eq!(machine.stack_length(), 1);
let result = machine.operate(&Item::Operator(Evolute)).unwrap();
assert_eq!(result, &Charmaleon);
assert_eq!(machine.stack_length(), 1);
let result = machine.operate(&Item::Operator(Evolute)).unwrap();
assert_eq!(result, &Charizard);
let result = machine.operate(&Item::Operator(Evolute)).unwrap();
assert_eq!(result, &Charizard);
machine.operate(&Item::Operator(Close));
assert_eq!(machine.stack_length(), 0);
}
#[test]
fn test_devolution() {
let mut machine = Machine::new(&pokemon_op_sys);
let my_creature = Blastoise;
let result = machine.operate(&Item::Value(my_creature)).unwrap();
assert_eq!(result, &Blastoise);
assert_eq!(machine.stack_length(), 1);
let result = machine.operate(&Item::Operator(Devolute)).unwrap();
assert_eq!(result, &Wartortle);
assert_eq!(machine.stack_length(), 1);
let result = machine.operate(&Item::Operator(Devolute)).unwrap();
assert_eq!(result, &Squirtle);
let result = machine.operate(&Item::Operator(Devolute)).unwrap();
assert_eq!(result, &Squirtle);
machine.operate(&Item::Operator(Close));
assert_eq!(machine.stack_length(), 0);
}
}