Add simple test for tagging an entry

This commit is contained in:
Matthias Beyer 2017-07-18 21:43:49 +02:00
parent 8678edb61e
commit d163ac8109
2 changed files with 74 additions and 0 deletions

View file

@ -40,3 +40,6 @@ path = "../libimagutil"
default-features = false default-features = false
features = ["testing"] features = ["testing"]
[dev-dependencies]
toml-query = "0.3.0"

View file

@ -27,8 +27,13 @@ extern crate libimagstore;
extern crate libimagrt; extern crate libimagrt;
extern crate libimagentrytag; extern crate libimagentrytag;
extern crate libimagerror; extern crate libimagerror;
#[macro_use]
extern crate libimagutil; extern crate libimagutil;
#[cfg(test)]
extern crate toml_query;
use std::path::PathBuf; use std::path::PathBuf;
use libimagrt::runtime::Runtime; use libimagrt::runtime::Runtime;
@ -169,3 +174,69 @@ fn list(id: PathBuf, rt: &Runtime) {
} }
} }
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use std::ffi::OsStr;
use toml::value::Value;
use toml_query::read::TomlValueReadExt;
use toml_query::error::Result as TomlQueryResult;
use libimagentrytag::ui::{get_add_tags, get_remove_tags};
use libimagrt::runtime::Runtime;
use libimagstore::storeid::StoreId;
use libimagstore::store::{Result as StoreResult, FileLockEntry};
use super::alter;
make_mock_app! {
app "imag-tag";
modulename mock;
version "0.3.0";
with help "imag-tag mocking app";
}
use self::mock::generate_test_runtime;
use libimagutil::testing::DEFAULT_ENTRY;
fn create_test_default_entry<'a, S: AsRef<OsStr>>(rt: &'a Runtime, name: S) -> StoreResult<StoreId> {
let mut path = PathBuf::new();
path.set_file_name(name);
let id = StoreId::new_baseless(path)?;
let mut entry = rt.store().create(id.clone())?;
entry.get_content_mut().push_str(DEFAULT_ENTRY);
Ok(id)
}
fn get_entry_tags<'a>(entry: &'a FileLockEntry<'a>) -> TomlQueryResult<Option<&'a Value>> {
entry.get_header().read(&"imag.tags".to_owned())
}
fn tags_toml_value<'a, I: IntoIterator<Item = &'static str>>(tags: I) -> Value {
Value::Array(tags.into_iter().map(|s| Value::String(s.to_owned())).collect())
}
#[test]
fn test_tag_add_adds_tag() {
let rt = generate_test_runtime(vec!["--id", "test", "--add", "foo"]).unwrap();
create_test_default_entry(&rt, "test").unwrap();
let id = PathBuf::from(String::from("test"));
let add = get_add_tags(rt.cli());
let rem = get_remove_tags(rt.cli());
alter(&rt, id.clone(), add, rem);
let test_entry = rt.store().get(id).unwrap().unwrap();
let test_tags = get_entry_tags(&test_entry).unwrap().unwrap();
assert_ne!(*test_tags, tags_toml_value(vec![]));
assert_eq!(*test_tags, tags_toml_value(vec!["foo"]));
}
}