Add test to _everything_

This commit is contained in:
Ticki 2016-03-09 17:18:31 +01:00
parent 7269b5f07d
commit 5d4826d4ff
6 changed files with 194 additions and 11 deletions

View File

@ -1,21 +1,21 @@
extern crate libterm; extern crate libterm;
use libterm::{TermRead, TermWrite, IntoRawMode, Color, Style, Key}; use libterm::{TermRead, TermWrite, IntoRawMode, Key};
use std::io::{Read, Write, stdout, stdin}; use std::io::{Write, stdout, stdin};
fn main() { fn main() {
let stdin = stdin(); let stdin = stdin();
let mut stdout = stdout().into_raw_mode().unwrap(); let mut stdout = stdout().into_raw_mode().unwrap();
stdout.clear(); stdout.clear().unwrap();
stdout.goto(0, 0); stdout.goto(0, 0).unwrap();
stdout.write(b"q to exit. Type stuff, use alt, and so on."); stdout.write(b"q to exit. Type stuff, use alt, and so on.").unwrap();
stdout.hide_cursor(); stdout.hide_cursor().unwrap();
stdout.flush(); stdout.flush().unwrap();
for c in stdin.keys() { for c in stdin.keys() {
stdout.goto(5, 5); stdout.goto(5, 5).unwrap();
stdout.clear_line(); stdout.clear_line().unwrap();
match c { match c {
Key::Char('q') => break, Key::Char('q') => break,
Key::Char(c) => println!("{}", c), Key::Char(c) => println!("{}", c),
@ -28,8 +28,8 @@ fn main() {
Key::Invalid => println!("???"), Key::Invalid => println!("???"),
Key::Error => println!("ERROR"), Key::Error => println!("ERROR"),
} }
stdout.flush(); stdout.flush().unwrap();
} }
stdout.show_cursor(); stdout.show_cursor().unwrap();
} }

View File

@ -89,3 +89,51 @@ impl Color {
} }
} }
} }
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_rgb() {
assert_eq!(Color::Rgb(2, 3, 4).to_ansi_val(), 110);
assert_eq!(Color::Rgb(2, 1, 4).to_ansi_val(), 98);
assert_eq!(Color::Rgb(5, 1, 4).to_ansi_val(), 206);
}
#[test]
fn test_grayscale() {
assert_eq!(Color::Grayscale(2).to_ansi_val(), 234);
assert_eq!(Color::Grayscale(5).to_ansi_val(), 237);
}
#[test]
fn test_normal() {
assert_eq!(Color::Black.to_ansi_val(), 0);
assert_eq!(Color::Green.to_ansi_val(), 2);
assert_eq!(Color::White.to_ansi_val(), 7);
}
#[test]
fn test_hi() {
assert_eq!(Color::LightRed.to_ansi_val(), 9);
assert_eq!(Color::LightCyan.to_ansi_val(), 0xE);
assert_eq!(Color::LightWhite.to_ansi_val(), 0xF);
}
#[cfg(debug)]
#[should_panic]
#[test]
fn test_bound_check_rgb() {
Color::Rgb(3, 9, 1).debug_check();
}
#[cfg(debug)]
#[should_panic]
#[test]
fn test_bound_check_rgb_2() {
Color::Rgb(3, 6, 1).debug_check();
}
#[cfg(debug)]
#[should_panic]
#[test]
fn test_bound_check_grayscale() {
Color::Grayscale(25).debug_check();
}
}

View File

@ -135,3 +135,90 @@ impl<W: Write> TermWrite for W {
Ok(try!(self.write(b"\x1BP")) + try!(self.write(b))) Ok(try!(self.write(b"\x1BP")) + try!(self.write(b)))
} }
} }
#[cfg(test)]
mod test {
use super::*;
use std::io::Cursor;
#[test]
fn test_csi() {
let mut buf = Cursor::new(Vec::new());
buf.csi(b"bluh").unwrap();
assert_eq!(buf.get_ref(), b"\x1B[bluh");
buf.csi(b"blah").unwrap();
assert_eq!(buf.get_ref(), b"\x1B[bluh\x1B[blah");
}
#[test]
fn test_csi_partial() {
let mut buf = [0; 3];
let mut buf = &mut buf[..];
assert_eq!(buf.csi(b"blu").unwrap(), 3);
assert_eq!(buf.csi(b"").unwrap(), 0);
assert_eq!(buf.csi(b"nooooo").unwrap(), 0);
}
#[test]
fn test_osc() {
let mut buf = Cursor::new(Vec::new());
buf.osc(b"bluh").unwrap();
assert_eq!(buf.get_ref(), b"\x1B]bluh");
buf.osc(b"blah").unwrap();
assert_eq!(buf.get_ref(), b"\x1B]bluh\x1B]blah");
}
#[test]
fn test_osc_partial() {
let mut buf = [0; 3];
let mut buf = &mut buf[..];
assert_eq!(buf.osc(b"blu").unwrap(), 3);
assert_eq!(buf.osc(b"").unwrap(), 0);
assert_eq!(buf.osc(b"nooooo").unwrap(), 0);
}
#[test]
fn test_dsc() {
let mut buf = Cursor::new(Vec::new());
buf.dsc(b"bluh").unwrap();
assert_eq!(buf.get_ref(), b"\x1BPbluh");
buf.dsc(b"blah").unwrap();
assert_eq!(buf.get_ref(), b"\x1BPbluh\x1BPblah");
}
#[test]
fn test_dsc_partial() {
let mut buf = [0; 3];
let mut buf = &mut buf[..];
assert_eq!(buf.dsc(b"blu").unwrap(), 3);
assert_eq!(buf.dsc(b"").unwrap(), 0);
assert_eq!(buf.dsc(b"nooooo").unwrap(), 0);
}
#[test]
fn test_clear() {
let mut buf = Cursor::new(Vec::new());
buf.clear().unwrap();
assert_eq!(buf.get_ref(), b"\x1B[2J");
buf.clear().unwrap();
assert_eq!(buf.get_ref(), b"\x1B[2J\x1B[2J");
}
#[test]
fn test_goto() {
let mut buf = Cursor::new(Vec::new());
buf.goto(34, 43).unwrap();
assert_eq!(buf.get_ref(), b"\x1B[00044;00035H");
buf.goto(24, 45).unwrap();
assert_eq!(buf.get_ref(), b"\x1B[00044;00035H\x1B[00046;00025H");
}
#[test]
fn test_style() {
use Style;
let mut buf = Cursor::new(Vec::new());
buf.style(Style::Bold).unwrap();
assert_eq!(buf.get_ref(), b"\x1B[001m");
buf.style(Style::Italic).unwrap();
assert_eq!(buf.get_ref(), b"\x1B[001m\x1B[003m");
}
}

View File

@ -98,3 +98,21 @@ impl<R: Read> TermRead for R {
Ok(Some(passwd)) Ok(Some(passwd))
} }
} }
#[cfg(test)]
mod test {
use super::*;
#[cfg(feature = "nightly")]
#[test]
fn test_keys() {
let mut i = b"\x1Bayo\x7F\x1B[D".keys();
assert_eq!(i.next(), Some(Key::Alt('a')));
assert_eq!(i.next(), Some(Key::Char('y')));
assert_eq!(i.next(), Some(Key::Char('o')));
assert_eq!(i.next(), Some(Key::Backspace));
assert_eq!(i.next(), Some(Key::Left));
assert_eq!(i.next(), None);
}
}

View File

@ -96,3 +96,16 @@ impl<W: Write> IntoRawMode for W {
} }
} }
} }
#[cfg(test)]
mod test {
use super::*;
use std::io::{Write, stdout};
#[test]
fn test_into_raw_mode() {
let mut out = stdout().into_raw_mode().unwrap();
out.write(b"this is a test, muahhahahah").unwrap();
}
}

View File

@ -48,3 +48,20 @@ pub fn set_terminal_attr(ios: *mut Termios) -> c_int {
tcsetattr(0, 0, ios) tcsetattr(0, 0, ios)
} }
} }
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_get_terminal_attr() {
get_terminal_attr();
get_terminal_attr();
get_terminal_attr();
}
#[test]
fn test_set_terminal_attr() {
let mut ios = get_terminal_attr().0;
set_terminal_attr(&mut ios as *mut _);
}
}