Skip to content

Commit

Permalink
fix: errors
Browse files Browse the repository at this point in the history
- Handle division by zero
- Handle decimals correctly
- Add tests
  • Loading branch information
edfloreshz committed Jan 12, 2025
1 parent 379d14c commit f66f4d0
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 8 deletions.
137 changes: 129 additions & 8 deletions src/app/calculation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ impl Calculation {

pub fn on_number_press(&mut self, number: f32) {
self.display.push_str(&number.to_string());
let number = if number.fract() != 0.0 {
format!("{number}.0")
} else {
number.to_string()
};
self.expression.push_str(&number);
self.expression.push_str(&format!("{:.2}", number));
}

pub fn on_operator_press(&mut self, operator: &Operator) -> Message {
Expand All @@ -66,10 +61,16 @@ impl Calculation {

pub fn on_equals_press(&mut self) -> Result<(), Box<dyn Error>> {
let value = eval(&self.expression)?;
log::info!("Expression -> {} = {value}", self.expression);
log::info!("Expression -> {} = {:#?}", self.expression, value);
self.result = match value {
Value::Int(v) => v.to_string(),
Value::Float(v) => v.to_string(),
Value::Float(v) => {
if v.is_infinite() {
return Err(Box::new(crate::error::Error::DivisionByZero));
} else {
v.to_string()
}
}
_ => String::new(),
};
Ok(())
Expand All @@ -96,3 +97,123 @@ impl Calculation {
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_new_calculation() {
let calc = Calculation::new();
assert_eq!(calc.display, "");
assert_eq!(calc.expression, "");
assert_eq!(calc.result, "");
}

#[test]
fn test_basic_addition() {
let mut calc = Calculation::new();
calc.on_number_press(5.0);
calc.on_operator_press(&Operator::Add);
calc.on_number_press(3.0);
calc.on_equals_press().unwrap();
assert_eq!(calc.result, "8");
}

#[test]
fn test_basic_subtraction() {
let mut calc = Calculation::new();
calc.on_number_press(10.0);
calc.on_operator_press(&Operator::Subtract);
calc.on_number_press(4.0);
calc.on_equals_press().unwrap();
assert_eq!(calc.result, "6");
}

#[test]
fn test_basic_multiplication() {
let mut calc = Calculation::new();
calc.on_number_press(6.0);
calc.on_operator_press(&Operator::Multiply);
calc.on_number_press(7.0);
calc.on_equals_press().unwrap();
assert_eq!(calc.result, "42");
}

#[test]
fn test_basic_division() {
let mut calc = Calculation::new();
calc.on_number_press(15.0);
calc.on_operator_press(&Operator::Divide);
calc.on_number_press(3.0);
calc.on_equals_press().unwrap();
assert_eq!(calc.result, "5");
}

#[test]
fn test_modulus() {
let mut calc = Calculation::new();
calc.on_number_press(17.0);
calc.on_operator_press(&Operator::Modulus);
calc.on_number_press(5.0);
calc.on_equals_press().unwrap();
assert_eq!(calc.result, "2");
}

#[test]
fn test_decimal_calculation() {
let mut calc = Calculation::new();
calc.on_number_press(3.5);
calc.on_operator_press(&Operator::Multiply);
calc.on_number_press(2.0);
calc.on_equals_press().unwrap();
assert_eq!(calc.result, "7");
}

#[test]
fn test_clear() {
let mut calc = Calculation::new();
calc.on_number_press(5.0);
calc.on_operator_press(&Operator::Add);
calc.on_number_press(3.0);
calc.on_operator_press(&Operator::Clear);
assert_eq!(calc.display, "");
assert_eq!(calc.expression, "");
assert_eq!(calc.result, "");
}

#[test]
fn test_multiple_operations() {
let mut calc = Calculation::new();
calc.on_number_press(2.0);
calc.on_operator_press(&Operator::Add);
calc.on_number_press(3.0);
calc.on_operator_press(&Operator::Multiply);
calc.on_number_press(4.0);
calc.on_equals_press().unwrap();
log::info!("{}", calc.expression);
assert_eq!(calc.result, "14");
}

#[test]
fn test_division_by_zero() {
let mut calc = Calculation::new();
calc.on_number_press(5.0);
calc.on_operator_press(&Operator::Divide);
calc.on_number_press(0.0);
assert!(calc.on_equals_press().is_err());
}

#[test]
fn test_input_validation() {
let mut calc = Calculation::new();

// Valid input
calc.on_input("123+456".to_string());
assert_eq!(calc.expression, "123+456");

// Invalid input (letters)
calc.on_input("abc".to_string());
assert_eq!(calc.expression, "123+456");
}
}
16 changes: 16 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::fmt::Display;

#[derive(Debug)]
pub enum Error {
DivisionByZero,
}

impl std::error::Error for Error {}

impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::DivisionByZero => write!(f, "Attempted to divide by zero"),
}
}
}
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use app::Calculator;
mod app;
mod core;
mod error;

use app::settings;

Expand Down

0 comments on commit f66f4d0

Please sign in to comment.