From 2dd90af92a0beddf9c0d97d7a48b1e93f815fb41 Mon Sep 17 00:00:00 2001 From: Joseph Crail Date: Wed, 3 Jun 2015 01:38:59 -0400 Subject: [PATCH] Add initial tests. --- Makefile | 3 +- test/fixtures/wc/alice_in_wonderland.txt | 5 +++ test/fixtures/wc/lorem_ipsum.txt | 13 ++++++ test/fixtures/wc/moby_dick.txt | 18 ++++++++ test/wc.rs | 57 ++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/wc/alice_in_wonderland.txt create mode 100644 test/fixtures/wc/lorem_ipsum.txt create mode 100644 test/fixtures/wc/moby_dick.txt create mode 100644 test/wc.rs diff --git a/Makefile b/Makefile index 74b6a694e..b4e463020 100644 --- a/Makefile +++ b/Makefile @@ -188,7 +188,8 @@ TEST_PROGS := \ truncate \ tsort \ unlink \ - unexpand + unexpand \ + wc TEST ?= $(TEST_PROGS) diff --git a/test/fixtures/wc/alice_in_wonderland.txt b/test/fixtures/wc/alice_in_wonderland.txt new file mode 100644 index 000000000..a95562a1c --- /dev/null +++ b/test/fixtures/wc/alice_in_wonderland.txt @@ -0,0 +1,5 @@ +Alice was beginning to get very tired of sitting by +her sister on the bank, and of having nothing to do: once or twice +she had peeped into the book her sister was reading, but it had no +pictures or conversations in it, "and what is the use of a book," +thought Alice "without pictures or conversation?" diff --git a/test/fixtures/wc/lorem_ipsum.txt b/test/fixtures/wc/lorem_ipsum.txt new file mode 100644 index 000000000..16752446c --- /dev/null +++ b/test/fixtures/wc/lorem_ipsum.txt @@ -0,0 +1,13 @@ +Lorem ipsum dolor sit amet, consectetur adipiscing +elit. Nunc interdum suscipit sem vel ornare. Proin euismod, justo +sed mollis dictum, eros urna ultricies augue, eu pharetra mi ex id +ante. Duis convallis porttitor aliquam. Nunc vitae tincidunt ex. +Suspendisse iaculis ligula ac diam consectetur lacinia. Donec vel +velit dui. Etiam fringilla, dolor quis tempor vehicula, lacus +turpis bibendum velit, et pellentesque elit odio a magna. Cras +vulputate tortor non libero vehicula euismod. Aliquam tincidunt +nisl eget enim cursus, viverra sagittis magna commodo. Cras rhoncus +egestas leo nec blandit. Suspendisse potenti. Etiam ullamcorper +leo vel lacus vestibulum, cursus semper eros efficitur. In hac +habitasse platea dictumst. Phasellus scelerisque vehicula +fringilla. diff --git a/test/fixtures/wc/moby_dick.txt b/test/fixtures/wc/moby_dick.txt new file mode 100644 index 000000000..193dc8e35 --- /dev/null +++ b/test/fixtures/wc/moby_dick.txt @@ -0,0 +1,18 @@ +Call me Ishmael. Some years ago - never mind how long +precisely - having little or no money in my purse, and nothing +particular to interest me on shore, I thought I would sail about a +little and see the watery part of the world. It is a way I have of +driving off the spleen and regulating the circulation. Whenever I +find myself growing grim about the mouth; whenever it is a damp, +drizzly November in my soul; whenever I find myself involuntarily +pausing before coffin warehouses, and bringing up the rear of every +funeral I meet; and especially whenever my hypos get such an upper +hand of me, that it requires a strong moral principle to prevent me +from deliberately stepping into the street, and methodically +knocking people's hats off - then, I account it high time to get to +sea as soon as I can. This is my substitute for pistol and ball. +With a philosophical flourish Cato throws himself upon his sword; I +quietly take to the ship. There is nothing surprising in this. If +they but knew it, almost all men in their degree, some time or +other, cherish very nearly the same feelings towards the ocean with +me. diff --git a/test/wc.rs b/test/wc.rs new file mode 100644 index 000000000..5cb8f3b59 --- /dev/null +++ b/test/wc.rs @@ -0,0 +1,57 @@ +use std::process::Command; +use util::*; + +static PROGNAME: &'static str = "./wc"; + +#[path = "common/util.rs"] +#[macro_use] +mod util; + +#[test] +fn test_stdin_default() { + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd, get_file_contents("lorem_ipsum.txt")); + assert_eq!(result.stdout, " 13 109 772\n"); +} + +#[test] +fn test_stdin_only_bytes() { + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["-c"]), get_file_contents("lorem_ipsum.txt")); + assert_eq!(result.stdout, " 772\n"); +} + +#[test] +fn test_stdin_all_counts() { + let mut cmd = Command::new(PROGNAME); + let result = run_piped_stdin(&mut cmd.args(&["-c", "-m", "-l", "-L", "-w"]), get_file_contents("alice_in_wonderland.txt")); + assert_eq!(result.stdout, " 5 57 302 302 66\n"); +} + +#[test] +fn test_single_default() { + let mut cmd = Command::new(PROGNAME); + let result = run(&mut cmd.arg("moby_dick.txt")); + assert_eq!(result.stdout, " 18 204 1115 moby_dick.txt\n"); +} + +#[test] +fn test_single_only_lines() { + let mut cmd = Command::new(PROGNAME); + let result = run(&mut cmd.args(&["-l", "moby_dick.txt"])); + assert_eq!(result.stdout, " 18 moby_dick.txt\n"); +} + +#[test] +fn test_single_all_counts() { + let mut cmd = Command::new(PROGNAME); + let result = run(&mut cmd.args(&["-c", "-l", "-L", "-m", "-w", "alice_in_wonderland.txt"])); + assert_eq!(result.stdout, " 5 57 302 302 66 alice_in_wonderland.txt\n"); +} + +#[test] +fn test_multiple_default() { + let mut cmd = Command::new(PROGNAME); + let result = run(&mut cmd.args(&["lorem_ipsum.txt", "moby_dick.txt", "alice_in_wonderland.txt"])); + assert_eq!(result.stdout, " 13 109 772 lorem_ipsum.txt\n 18 204 1115 moby_dick.txt\n 5 57 302 alice_in_wonderland.txt\n 36 370 2189 total\n"); +}