diff --git a/Taskfile.yml b/Taskfile.yml index a9f4943..80a9248 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -14,6 +14,11 @@ tasks: cmds: - cmd: cargo clean + cov: + desc: Generates code coverage + cmds: + - cmd: ./coverage.sh + clippy: desc: Runs clippy for all targets cmds: diff --git a/tests/loading/data/data01.kivi b/tests/loading/data/data01.kivi new file mode 100644 index 0000000..4d583f5 --- /dev/null +++ b/tests/loading/data/data01.kivi @@ -0,0 +1,8 @@ +host +127.0.0.1 + +port +54321 + +timeout +12ms diff --git a/tests/loading/mod.rs b/tests/loading/mod.rs index dffb3d0..8aef529 100644 --- a/tests/loading/mod.rs +++ b/tests/loading/mod.rs @@ -1,3 +1,4 @@ +mod test_load_from_file; mod test_load_from_string; use kivi::load_from_string; diff --git a/tests/loading/test_load_from_file.rs b/tests/loading/test_load_from_file.rs new file mode 100644 index 0000000..7c40248 --- /dev/null +++ b/tests/loading/test_load_from_file.rs @@ -0,0 +1,11 @@ +use kivi::load_from_file; + +#[test] +fn loading_from_file_should_work() { + let kvp = load_from_file("./tests/loading/data/data01.kivi").unwrap(); + assert!(!kvp.is_empty()); + assert_eq!(3, kvp.len()); + assert_eq!("127.0.0.1", kvp.get("host").unwrap()); + assert_eq!("54321", kvp.get("port").unwrap()); + assert_eq!("12ms", kvp.get("timeout").unwrap()); +} diff --git a/tests/test_api.rs b/tests/test_api.rs new file mode 100644 index 0000000..c67a437 --- /dev/null +++ b/tests/test_api.rs @@ -0,0 +1,41 @@ +use kivi::load_from_string; + +#[test] +fn getting_empty_value_should_work() { + let kvp = load_from_string(""); + assert!(kvp.is_empty()); + assert_eq!(0, kvp.len()); + assert_eq!(None, kvp.get("a")); +} + +#[test] +fn getting_non_empty_value_should_work() { + let kvp = load_from_string("a\nb\n"); + assert!(!kvp.is_empty()); + assert_eq!(1, kvp.len()); + assert_eq!("b", kvp.get("a").unwrap()); +} + +#[test] +fn getting_keys_should_work() { + let kvp = load_from_string("a\nb\nc\nd\n"); + assert!(!kvp.is_empty()); + assert_eq!(2, kvp.len()); + assert_eq!("b", kvp.get("a").unwrap()); + assert_eq!("d", kvp.get("c").unwrap()); + let mut keys = kvp.keys().map(|s| s.to_owned()).collect::>(); + keys.sort(); + assert_eq!("a,c", keys.join(",")); +} + +#[test] +fn getting_values_should_work() { + let kvp = load_from_string("a\nb\nc\nd\n"); + assert!(!kvp.is_empty()); + assert_eq!(2, kvp.len()); + assert_eq!("b", kvp.get("a").unwrap()); + assert_eq!("d", kvp.get("c").unwrap()); + let mut values = kvp.values().map(|s| s.to_owned()).collect::>(); + values.sort(); + assert_eq!("b,d", values.join(",")); +}