command_parser: add tests

Run with: cargo test --target=x86_64-unknown-linux-gnu
This commit is contained in:
Astro 2019-09-19 14:56:34 +02:00
parent 35dfba99e1
commit 6f36c682cd
2 changed files with 99 additions and 6 deletions

View File

@ -12,7 +12,7 @@ use nom::{
use lexical_core as lexical; use lexical_core as lexical;
#[derive(Clone, Debug)] #[derive(Clone, Debug, PartialEq)]
pub enum Error { pub enum Error {
Parser(ErrorKind), Parser(ErrorKind),
Incomplete, Incomplete,
@ -60,7 +60,7 @@ impl fmt::Display for Error {
} }
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq)]
pub enum ShowCommand { pub enum ShowCommand {
Input, Input,
Reporting, Reporting,
@ -69,7 +69,7 @@ pub enum ShowCommand {
PostFilter, PostFilter,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq)]
pub enum PidParameter { pub enum PidParameter {
Target, Target,
KP, KP,
@ -81,7 +81,7 @@ pub enum PidParameter {
IntegralMax, IntegralMax,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq)]
pub enum PwmMode { pub enum PwmMode {
Manual { Manual {
width: u32, width: u32,
@ -90,7 +90,7 @@ pub enum PwmMode {
Pid, Pid,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone, PartialEq)]
pub enum Command { pub enum Command {
Quit, Quit,
Show(ShowCommand), Show(ShowCommand),
@ -294,3 +294,94 @@ impl Command {
} }
} }
} }
#[cfg(test)]
mod test {
use super::*;
#[test]
fn parse_quit() {
let command = Command::parse(b"quit");
assert_eq!(command, Ok(Command::Quit));
}
#[test]
fn parse_report() {
let command = Command::parse(b"report");
assert_eq!(command, Ok(Command::Show(ShowCommand::Input)));
}
#[test]
fn parse_report_mode() {
let command = Command::parse(b"report mode");
assert_eq!(command, Ok(Command::Show(ShowCommand::Reporting)));
}
#[test]
fn parse_report_mode_on() {
let command = Command::parse(b"report mode on");
assert_eq!(command, Ok(Command::Reporting(true)));
}
#[test]
fn parse_report_mode_off() {
let command = Command::parse(b"report mode off");
assert_eq!(command, Ok(Command::Reporting(false)));
}
#[test]
fn parse_pwm_manual() {
let command = Command::parse(b"pwm 1 16383 65535");
assert_eq!(command, Ok(Command::Pwm {
channel: 1,
mode: PwmMode::Manual {
width: 16383,
total: 65535,
},
}));
}
#[test]
fn parse_pwm_pid() {
let command = Command::parse(b"pwm 0 pid");
assert_eq!(command, Ok(Command::Pwm {
channel: 0,
mode: PwmMode::Pid,
}));
}
#[test]
fn parse_pid() {
let command = Command::parse(b"pid");
assert_eq!(command, Ok(Command::Show(ShowCommand::Pid)));
}
#[test]
fn parse_pid_target() {
let command = Command::parse(b"pid 0 target 36.5");
assert_eq!(command, Ok(Command::Pid {
channel: 0,
parameter: PidParameter::Target,
value: 36.5,
}));
}
#[test]
fn parse_pid_integral_max() {
let command = Command::parse(b"pid 1 integral_max 2000");
assert_eq!(command, Ok(Command::Pid {
channel: 1,
parameter: PidParameter::IntegralMax,
value: 2000.0,
}));
}
#[test]
fn parse_postfilter_rate() {
let command = Command::parse(b"postfilter 0 rate 21");
assert_eq!(command, Ok(Command::PostFilter {
channel: 0,
rate: 21.0,
}));
}
}

View File

@ -1,6 +1,6 @@
#![feature(const_fn, proc_macro_hygiene)] #![feature(const_fn, proc_macro_hygiene)]
#![no_std] #![no_std]
#![no_main] #![cfg_attr(not(test), no_main)]
use cortex_m_rt::entry; use cortex_m_rt::entry;
use core::fmt::{self, Write}; use core::fmt::{self, Write};
@ -24,6 +24,7 @@ macro_rules! println {
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*)); ($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
} }
#[cfg(not(test))]
#[no_mangle] // https://github.com/rust-lang/rust/issues/{38281,51647} #[no_mangle] // https://github.com/rust-lang/rust/issues/{38281,51647}
#[panic_handler] #[panic_handler]
pub fn panic_fmt(info: &core::panic::PanicInfo) -> ! { pub fn panic_fmt(info: &core::panic::PanicInfo) -> ! {
@ -97,6 +98,7 @@ struct ControlState {
pid: pid::Controller, pid: pid::Controller,
} }
#[cfg(not(test))]
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
let mut stdout = hio::hstdout().unwrap(); let mut stdout = hio::hstdout().unwrap();