Skip to content

Commit

Permalink
Merge pull request #31 from tinythings/isbm-init-tests
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
isbm authored Oct 28, 2024
2 parents 9d87c03 + 442ecda commit a9dfc52
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
25 changes: 25 additions & 0 deletions .github/workflows/all_the_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Unit/Integration Testing

on:
push:
branches:
- "master"
pull_request:

env:
CARGO_TERM_COLOR: always
# RUSTFLAGS: -D warnings

jobs:
unit_tests:
name: Unit and Static Type tests
runs-on: ubuntu-latest

steps:
- name: Install latest nextest release
uses: taiki-e/install-action@nextest

- uses: actions/checkout@v2
- name: Run test target
run: |
make test
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ log = "0.4.22"
sysinfo = { version = "0.31.4", features = ["linux-tmpfs"] }

[workspace]
members = ["modules/sys/*"]
resolver = "2"
members = [
"modules/sys/*",
"libsysinspect",
]

[profile.release]
strip = true
Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ build:
man:
pandoc --standalone --to man docs/manpages/sysinspect.8.md -o docs/manpages/sysinspect.8

test:
cargo nextest run --workspace

tar:
# Cleanup
rm -rf package/${ARC_NAME}
Expand Down
92 changes: 92 additions & 0 deletions libsysinspect/tests/utl_dataconv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#[cfg(test)]
mod dataconv_test {
use libsysinspect::util::dataconv;
use serde_yaml::{self, Value};
use std::collections::HashMap;

#[test]
fn test_dataconv_int() {
let m = serde_yaml::from_str::<HashMap<String, Value>>("foo: 1").unwrap();
let v = m.get("foo").unwrap();
let i = dataconv::as_int(Some(v).cloned());
assert!(i == 1);
}

#[test]
fn test_dataconv_bool() {
let m = serde_yaml::from_str::<HashMap<String, Value>>("foo: true").unwrap();
let v = m.get("foo").unwrap();
let i = dataconv::as_bool(Some(v).cloned());
assert!(i);
}

#[test]
fn test_dataconv_str() {
let m = serde_yaml::from_str::<HashMap<String, Value>>("foo: \"Darth Vader\"").unwrap();
let v = m.get("foo").unwrap();
let i = dataconv::as_str(Some(v).cloned());
assert!(i.eq("Darth Vader"));
}

#[test]
fn test_dataconv_str_opt() {
let m = serde_yaml::from_str::<HashMap<String, Value>>("foo: \"Darth Vader\"").unwrap();
let v = m.get("foo").unwrap();
let i = dataconv::as_str_opt(Some(v).cloned());
assert!(i.is_some(), "Data must contain something");
assert!(i.unwrap_or_default().eq("Darth Vader"), "Data must be a Luke's father");
}

#[test]
fn test_dataconv_bool_opt() {
let m = serde_yaml::from_str::<HashMap<String, Value>>("foo: true").unwrap();
let v = m.get("foo").unwrap();
let i = dataconv::as_bool_opt(Some(v).cloned());
assert!(i.is_some(), "Data must contain something");
assert!(i.unwrap_or(false), "Data must be true");
}

#[test]
fn test_dataconv_int_opt() {
let m = serde_yaml::from_str::<HashMap<String, Value>>("foo: 1").unwrap();
let v = m.get("foo").unwrap();
let i = dataconv::as_int_opt(Some(v).cloned());
assert!(i.is_some(), "Data must contain something");
assert!(i.unwrap_or(0) == 1, "Data must be 1");
}

#[test]
fn test_dataconv_str_list() {
let m = serde_yaml::from_str::<HashMap<String, Value>>("foo: \"bar,spam,baz,toto\"").unwrap();
let v = m.get("foo").unwrap();
let l = dataconv::as_str_list(Some(v).cloned());
assert!(l.len() == 4, "Data length must be 4");
assert!(
l == vec!["bar".to_string(), "spam".to_string(), "baz".to_string(), "toto".to_string()],
"Vector must be the same"
);
}

#[test]
fn test_dataconv_str_list_opt() {
let m = serde_yaml::from_str::<HashMap<String, Value>>("foo: \"bar,spam,baz,toto\"").unwrap();
let v = m.get("foo").unwrap();
let l = dataconv::as_str_list_opt(Some(v).cloned());
assert!(l.is_some(), "Data must contain something");
assert!(l.clone().unwrap().len() == 4, "Data length must be 4");
assert!(
l.unwrap() == vec!["bar".to_string(), "spam".to_string(), "baz".to_string(), "toto".to_string()],
"Vector must be the same"
);
}

#[test]
fn test_dataconv_to_str() {
let m = serde_yaml::from_str::<HashMap<String, Value>>("foo: \"bar,spam,baz,toto\"").unwrap();
let v = m.get("foo").unwrap();
let s = dataconv::to_string(Some(v).cloned());
assert!(s.is_some(), "Data must contain something");
let s = s.unwrap();
assert!(s.eq("bar,spam,baz,toto"));
}
}

0 comments on commit a9dfc52

Please sign in to comment.