Skip to content

Commit

Permalink
improved test coverage of my study program.
Browse files Browse the repository at this point in the history
  • Loading branch information
hidekuno committed May 26, 2024
1 parent ae0a329 commit d235687
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 95 deletions.
163 changes: 107 additions & 56 deletions zlearning/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ pub mod walker;

#[cfg(test)]
mod tests {
extern "C" {
fn close(fd: u32) -> u32;
}
use crate::param::Config;
use crate::path::create_tree;
use crate::path::Path;
use crate::tree::Cache;
use crate::visitor::LineVisitor;
use crate::visitor::SimpleVisitor;
use crate::visitor::TestVisitor;
Expand All @@ -30,12 +36,7 @@ mod tests {
let mut cursor =
io::Cursor::new(String::from("fj.news\nfj.news.reader\nfj.news.server\n").into_bytes());
let cache = Path::create_tree::<io::Cursor<Vec<u8>>>(&mut cursor, '.', 10);

if let Some(top) = cache.top {
assert_eq!(top.borrow().get_name(), "fj");
} else {
panic!("test failure");
}
assert_eq!(cache.top.unwrap().borrow().get_name(), "fj");
}
#[test]
fn test_visitor() {
Expand All @@ -44,20 +45,17 @@ mod tests {
let cache = Path::create_tree::<Cursor<Vec<u8>>>(&mut cursor, '.', 10);
let cursor = Cursor::new(Vec::new());

if let Some(top) = cache.top {
assert_eq!(top.borrow().get_name(), "fj");
let mut test = TestVisitor::new(Box::new(cursor));
top.borrow().accept(&mut test);

let mut iter = test.get_items().iter();
assert_eq!(iter.next(), Some(&String::from("fj")));
assert_eq!(iter.next(), Some(&String::from("news")));
assert_eq!(iter.next(), Some(&String::from("reader")));
assert_eq!(iter.next(), Some(&String::from("server")));
assert_eq!(iter.next(), None);
} else {
panic!("test failure");
}
let top = cache.top.unwrap();
assert_eq!(top.borrow().get_name(), "fj");
let mut test = TestVisitor::new(Box::new(cursor));
top.borrow().accept(&mut test);

let mut iter = test.get_items().iter();
assert_eq!(iter.next(), Some(&String::from("fj")));
assert_eq!(iter.next(), Some(&String::from("news")));
assert_eq!(iter.next(), Some(&String::from("reader")));
assert_eq!(iter.next(), Some(&String::from("server")));
assert_eq!(iter.next(), None);
}
#[test]
fn test_item_visitor() {
Expand All @@ -67,33 +65,42 @@ mod tests {
let cache = Path::create_tree::<Cursor<Vec<u8>>>(&mut cursor, '.', 10);
let mut cursor = Cursor::new(Vec::new());

if let Some(top) = cache.top {
let mut v = SimpleVisitor::new(&mut cursor);
top.borrow().accept(&mut v);
} else {
panic!("test failure");
}
let top = cache.top.unwrap();
let mut v = SimpleVisitor::new(&mut cursor);
top.borrow().accept(&mut v);

assert_eq!(
Ok("fj\n news\n reader\n server\n"),
from_utf8(cursor.get_ref())
);
}
#[test]
fn test_item_visitor_line() {
let mut cursor =
Cursor::new(String::from("fj.news\nfj.news.reader\nfj.news.server\n").into_bytes());

let test_data = [
"fj",
"fj.news",
"fj.news.b",
"fj.news.config",
"fj.news.group",
"fj.news.group.archives",
"fj.news.group.comp",
"fj.news.group.soc",
"fj.news.usage",
"fj.org",
"fj.org.ieee",
"fj.org.jus",
];
let mut cursor = Cursor::new(test_data.join("\n").into_bytes());
let cache = Path::create_tree::<Cursor<Vec<u8>>>(&mut cursor, '.', 10);

let mut cursor = Cursor::new(Vec::new());

if let Some(top) = cache.top {
let mut v = LineVisitor::new(&mut cursor, " ", "| ", "`--", "|--");
top.borrow().accept(&mut v);
} else {
panic!("test failure");
}
let top = cache.top.unwrap();
let mut v = LineVisitor::new(&mut cursor, " ", "| ", "`--", "|--");
top.borrow().accept(&mut v);

assert_eq!(
Ok("fj\n`--news\n |--reader\n `--server\n"),
Ok("fj\n|--news\n| |--b\n| |--config\n| |--group\n| | |--archives\n| | |--comp\n| | `--soc\n| `--usage\n`--org\n |--ieee\n `--jus\n"),
from_utf8(cursor.get_ref())
);
}
Expand All @@ -105,19 +112,16 @@ mod tests {
let cache = Path::create_tree::<Cursor<Vec<u8>>>(&mut cursor, '.', 10);
let cursor = Cursor::new(Vec::new());

if let Some(top) = cache.top {
let mut c = create_test_walker(Box::new(cursor));
let vec = c(top);

let mut iter = vec.iter();
assert_eq!(iter.next(), Some(&String::from("fj")));
assert_eq!(iter.next(), Some(&String::from("news")));
assert_eq!(iter.next(), Some(&String::from("reader")));
assert_eq!(iter.next(), Some(&String::from("server")));
assert_eq!(iter.next(), None);
} else {
panic!("test failure");
}
let top = cache.top.unwrap();
let mut c = create_test_walker(Box::new(cursor));
let vec = c(top);

let mut iter = vec.iter();
assert_eq!(iter.next(), Some(&String::from("fj")));
assert_eq!(iter.next(), Some(&String::from("news")));
assert_eq!(iter.next(), Some(&String::from("reader")));
assert_eq!(iter.next(), Some(&String::from("server")));
assert_eq!(iter.next(), None);
}
#[test]
fn test_walker() {
Expand All @@ -130,8 +134,6 @@ mod tests {
if let Some(top) = cache.top {
let mut c = create_walker();
c(top, &mut cursor);
} else {
panic!("test failure");
}
// cursor.seek(SeekFrom::Start(0)).unwrap();
cursor.rewind().unwrap();
Expand All @@ -142,22 +144,71 @@ mod tests {
}
#[test]
fn test_walker_line() {
let mut cursor =
Cursor::new(String::from("fj.news\nfj.news.reader\nfj.news.server\n").into_bytes());
let test_data = [
"fj",
"fj.news",
"fj.news.b",
"fj.news.config",
"fj.news.group",
"fj.news.group.archives",
"fj.news.group.comp",
"fj.news.group.soc",
"fj.news.usage",
"fj.org",
"fj.org.ieee",
"fj.org.jus",
];
let mut cursor = Cursor::new(test_data.join("\n").into_bytes());
let cache = Path::create_tree::<Cursor<Vec<u8>>>(&mut cursor, '.', 10);

let mut cursor = Cursor::new(Vec::new());

if let Some(top) = cache.top {
let mut c = create_line_walker(" ", "| ", "`--", "|--");
c(top, &mut cursor);
} else {
panic!("test failure");
}
cursor.rewind().unwrap();
assert_eq!(
Ok("fj\n`--news\n |--reader\n `--server\n"),
Ok("fj\n|--news\n| |--b\n| |--config\n| |--group\n| | |--archives\n| | |--comp\n| | `--soc\n| `--usage\n`--org\n |--ieee\n `--jus\n"),
from_utf8(cursor.get_ref())
);
}
#[test]
fn test_level_limit() {
let test_data = [
"fj",
"fj.news",
"fj.news.b",
"fj.news.config",
"fj.news.group",
"fj.news.group.archives",
];
let mut cursor = Cursor::new(test_data.join("\n").into_bytes());
let cache = Path::create_tree::<Cursor<Vec<u8>>>(&mut cursor, '.', 2);

let mut cursor = Cursor::new(Vec::new());

if let Some(top) = cache.top {
let mut c = create_line_walker(" ", "| ", "`--", "|--");
c(top, &mut cursor);
}
cursor.rewind().unwrap();
assert_eq!(
Ok("fj\n`--news\n |--b\n |--config\n `--group\n"),
from_utf8(cursor.get_ref())
);
}
#[test]
fn test_default() {
let _: Cache<Path> = Default::default();
let _: Config = Default::default();
}
#[test]
fn test_stdin() {
unsafe {
close(0);
}
let config = Config::new();
let _ = create_tree(&config);
}
}
76 changes: 50 additions & 26 deletions zlearning/src/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub enum DisplayMode {
MultiCharLine,
BoldMultiCharLine,
}
#[derive(PartialEq)]
enum ParamParse {
DelimiterOn,
FilenameOn,
Expand Down Expand Up @@ -70,7 +71,7 @@ pub struct Config {
level: i32,
}
impl Config {
fn new() -> Self {
pub fn new() -> Self {
Config {
mode: DisplayMode::Space,
filename: None,
Expand All @@ -91,11 +92,16 @@ impl Config {
self.level
}
}
impl Default for Config {
fn default() -> Self {
Self::new()
}
}
pub fn parse_arg(args: &[String]) -> Result<Config, Box<dyn Error>> {
let mut parse = ParamParse::Off;
let mut config = Config::new();

for arg in args {
for arg in args.iter() {
match parse {
ParamParse::Off => {
if arg == SINGLE_CHAR_PARAM {
Expand Down Expand Up @@ -137,6 +143,9 @@ pub fn parse_arg(args: &[String]) -> Result<Config, Box<dyn Error>> {
}
}
}
if parse != ParamParse::Off {
return Err(Box::new(InvalidOptionError {}));
}
Ok(config)
}
#[test]
Expand All @@ -148,6 +157,7 @@ fn test_parse_arg_01() {
assert_eq!(config.mode, DisplayMode::Space);
assert_eq!(config.filename, None);
assert_eq!(config.level, MAX_LEVEL);
assert_eq!(config.mode(), &DisplayMode::Space);
}
#[test]
fn test_parse_arg_02() {
Expand Down Expand Up @@ -235,50 +245,64 @@ fn test_parse_arg_09() {
fn test_parse_arg_err_01() {
let args = ["-f", "-d"];

match parse_arg(&args.iter().map(|s| s.to_string()).collect::<Vec<String>>()) {
Ok(_) => panic!("test fail"),
Err(e) => assert_eq!(e.to_string(), "invalid option"),
}
let _ = parse_arg(&args.iter().map(|s| s.to_string()).collect::<Vec<String>>())
.map_err(|e| assert_eq!(e.to_string(), "invalid option"));
}
#[test]
fn test_parse_arg_err_02() {
let args = ["-d", ""];
match parse_arg(&args.iter().map(|s| s.to_string()).collect::<Vec<String>>()) {
Ok(_) => panic!("test fail"),
Err(e) => assert_eq!(e.to_string(), "invalid option"),
}
let _ = parse_arg(&args.iter().map(|s| s.to_string()).collect::<Vec<String>>())
.map_err(|e| assert_eq!(e.to_string(), "invalid option"));
}
#[test]
fn test_parse_arg_err_03() {
let args = ["-d", "123"];
match parse_arg(&args.iter().map(|s| s.to_string()).collect::<Vec<String>>()) {
Ok(_) => panic!("test fail"),
Err(e) => assert_eq!(e.to_string(), "invalid option"),
}
let _ = parse_arg(&args.iter().map(|s| s.to_string()).collect::<Vec<String>>())
.map_err(|e| assert_eq!(e.to_string(), "invalid option"));
}
#[test]
fn test_parse_arg_err_04() {
let args = ["10", "123"];
match parse_arg(&args.iter().map(|s| s.to_string()).collect::<Vec<String>>()) {
Ok(_) => panic!("test fail"),
Err(e) => assert_eq!(e.to_string(), "invalid option"),
}

let _ = parse_arg(&args.iter().map(|s| s.to_string()).collect::<Vec<String>>())
.map_err(|e| assert_eq!(e.to_string(), "invalid option"));
}
#[test]
fn test_parse_arg_err_05() {
let args = ["-n", "abc"];

match parse_arg(&args.iter().map(|s| s.to_string()).collect::<Vec<String>>()) {
Ok(_) => panic!("test fail"),
Err(e) => assert_eq!(e.to_string(), "invalid option"),
}
let _ = parse_arg(&args.iter().map(|s| s.to_string()).collect::<Vec<String>>())
.map_err(|e| assert_eq!(e.to_string(), "invalid option"));
}
#[test]
fn test_parse_arg_err_06() {
let args = ["-n", "-1"];

match parse_arg(&args.iter().map(|s| s.to_string()).collect::<Vec<String>>()) {
Ok(_) => panic!("test fail"),
Err(e) => assert_eq!(e.to_string(), "invalid option"),
}
let _ = parse_arg(&args.iter().map(|s| s.to_string()).collect::<Vec<String>>())
.map_err(|e| assert_eq!(e.to_string(), "invalid option"));
}
#[test]
fn test_parse_arg_err_07() {
let args = ["-n", "-f"];

let _ = parse_arg(&args.iter().map(|s| s.to_string()).collect::<Vec<String>>())
.map_err(|e| assert_eq!(e.to_string(), "invalid option"));
}
#[test]
fn test_parse_arg_err_08() {
let args = ["-n"];

let _ = parse_arg(&args.iter().map(|s| s.to_string()).collect::<Vec<String>>()).map_err(|e| {
let _ = e.source();
assert_eq!(e.to_string(), "invalid option");
});
}
#[test]
fn test_parse_arg_err_09() {
let args = ["-f"];

let _ = parse_arg(&args.iter().map(|s| s.to_string()).collect::<Vec<String>>()).map_err(|e| {
let _ = e.source();
assert_eq!(e.to_string(), "invalid option");
});
}
Loading

0 comments on commit d235687

Please sign in to comment.