Skip to content

Commit

Permalink
Some brief work on getting tests setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
BurntSushi committed Jun 30, 2014
1 parent c08aff0 commit 2ebf7f7
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUSTTESTFLAGS ?=
LIB_NAME = docopt
LIB ?= $(BUILD)/.libregex.timestamp
LIB_FILES = src/lib.rs src/parse.rs src/synonym.rs
TEST_FILES = src/test.rs
TEST_FILES = src/test.rs src/testcases.rs
ARGS ?=

all: $(LIB)
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

extern crate debug;
extern crate libc;
#[cfg(test)]
#[phase(plugin, link)]
extern crate log;
extern crate regex;
#[phase(plugin)] extern crate regex_macros;

Expand Down Expand Up @@ -135,6 +138,10 @@ impl<'k> Map<&'k str, Value> for ValueMap {

impl fmt::Show for ValueMap {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.is_empty() {
return write!(f, "{{EMPTY}}");
}

// This is a little crazy, but we want to group synonyms with
// their keys and sort them for predictable output.
let reverse: HashMap<&String, &String> =
Expand Down Expand Up @@ -213,4 +220,6 @@ mod parse;
mod synonym;
#[cfg(test)]
mod test;
#[cfg(test)]
mod testcases;

100 changes: 100 additions & 0 deletions src/testcases.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
use std::collections::HashMap;
use {Docopt, Config, ValueMap};
use {Value, Switch, Counted, Plain, List};

static conf: Config = Config {
options_first: false,
help: true,
version: None,
};

fn get_args(doc: &str, argv: &[&'static str]) -> ValueMap {
let dopt =
match Docopt::new(doc, conf.clone()) {
Err(err) => fail!("Invalid usage: {}", err),
Ok(dopt) => dopt,
};
match dopt.argv(argv) {
Err(err) => fail!("{}", err),
Ok(vals) => vals,
}
}

fn map_from_alist(alist: Vec<(&'static str, Value)>) -> HashMap<String, Value> {
alist.move_iter().map(|(k, v)| (k.to_string(), v)).collect()
}

fn same_args(expected: &HashMap<String, Value>, got: &ValueMap) {
// debug!("GOT: {}", got);
// debug!("EXPECTED: {}", expected);
// debug!("---------------");
for (k, v) in expected.iter() {
assert!(v == got.map.get(k), "EXPECTED KEY: {}", k);
}
for (k, v) in got.map.iter() {
assert!(v == expected.get(k), "GOT KEY: {}", k);
}
}

#[test]
fn test_1() {
let doc = "
Usage: prog
";
let args = &[];
let vals = get_args(doc, args);

let expected = map_from_alist(vec!());
same_args(&expected, &vals);
}

#[test]
#[should_fail]
fn test_2() {
let doc = "
Usage: prog
";
let args = &["--xxx"];
let vals = get_args(doc, args);
}

#[test]
fn test_3() {
let doc = "
Usage: prog [options]
Options:
-a All.
";
let args = &[];
let vals = get_args(doc, args);
let expected = map_from_alist(vec!(("-a", Switch(false))));
same_args(&expected, &vals);
}

#[test]
fn test_4() {
let doc = "
Usage: prog [options]
Options:
-a All.
";
let args = &["-a"];
let vals = get_args(doc, args);
let expected = map_from_alist(vec!(("-a", Switch(true))));
same_args(&expected, &vals);
}

#[test]
#[should_fail]
fn test_5() {
let doc = "
Usage: prog [options]
Options:
-a All.
";
let args = &["-x"];
let vals = get_args(doc, args);
}

0 comments on commit 2ebf7f7

Please sign in to comment.