use crate::alu::*;
// use crate::message::*;
// use p101_is::Register;
use std::sync::mpsc;
// use std::time::Duration;
use rust_decimal::Decimal;

#[test]
fn when_a_numeric_key_is_pressed_then_the_m_register_should_be_updated() {

    // Arrange
    
    let (sender, _system) = mpsc::channel();
    let mut alu = DefaultAlu::new(sender);

    // Act

    let result = alu.write_to_m(2.into());
    
    // Assert

    assert_eq!(Ok(()), result);
    assert_eq!(Decimal::new(2, 0), alu.memory.m.read());
    assert_eq!(None, alu.decimals);
}

#[test]
fn when_several_numeric_keys_are_pressed_then_the_m_register_should_be_updated_accordingly() {

    // Arrange
    
    let (sender, _system) = mpsc::channel();
    let mut alu = DefaultAlu::new(sender);

    // Act

    let _ = alu.write_to_m(1.into());
    let _ = alu.write_to_m(2.into());
    let _ = alu.write_to_m(3.into());
    
    // Assert

    assert_eq!(Decimal::new(123, 0), alu.memory.m.read());
    assert_eq!(None, alu.decimals);
}

#[test]
fn when_decimals_are_entered_then_the_m_register_should_be_updated_accordingly() {

    // Arrange
    
    let (sender, _system) = mpsc::channel();
    let mut alu = DefaultAlu::new(sender);

    // Act

    let _ = alu.write_to_m(1.into());
    let _ = alu.write_to_m(2.into());
    alu.entering_decimals(true);
    let _ = alu.write_to_m(3.into());
    let _ = alu.write_to_m(4.into());
    let _ = alu.write_to_m(5.into());
    
    // Assert

    assert_eq!(Decimal::new(12345, 3), alu.memory.m.read());
}

/*
 * TODO: Find out what this test would prove.
 *
 
#[test]
fn for_any_pressed_numeric_key_a_print_message_should_be_emitted() {

    // Arrange
    
    let (sender, system) = mpsc::channel();
    let mut alu = DefaultAlu::new(sender);

    // Act

    let _ = alu.write_to_m(1.into());
    let _ = alu.write_to_m(2.into());
    alu.entering_decimals(true);
    let _ = alu.write_to_m(3.into());
    
    // Assert

    let mut msg = system.recv_timeout(Duration::from_millis(1));
    assert_eq!(Ok(Message::Print(Decimal::new(1, 0).into())), msg);

    msg = system.recv_timeout(Duration::from_millis(1));
    assert_eq!(Ok(Message::Print(Register::M.into())), msg);

    msg = system.recv_timeout(Duration::from_millis(1));
    assert_eq!(Ok(Message::Print(Decimal::new(12, 0).into())), msg);

    msg = system.recv_timeout(Duration::from_millis(1));
    assert_eq!(Ok(Message::Print(Register::M.into())), msg);

    msg = system.recv_timeout(Duration::from_millis(1));
    assert_eq!(Ok(Message::Print(Decimal::new(123, 1).into())), msg);

    msg = system.recv_timeout(Duration::from_millis(1));
    assert_eq!(Ok(Message::Print(Decimal::new(12345, 3), Register::M, Operand::), msg);
}

*/

