nonzero unittests pass
parent
3531323de0
commit
644f0b4c8a
22
src/main.rs
22
src/main.rs
|
|
@ -1,4 +1,6 @@
|
||||||
use serde::{Serialize, Deserialize};
|
use serde::{Serialize, Deserialize};
|
||||||
|
use std::io::Read;
|
||||||
|
use std::fs::File;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
println!("Hello, world!");
|
println!("Hello, world!");
|
||||||
|
|
@ -17,17 +19,33 @@ struct Add {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_storage(path: String) -> Result<Storage, String> {
|
fn load_storage(path: String) -> Result<Storage, String> {
|
||||||
Err("failed".to_string())
|
match File::open(path.clone()) {
|
||||||
|
Ok(mut reader) => _load_storage(&mut reader),
|
||||||
|
Err(reason) => Err(format!("failed to read storage {}: {}", path, reason)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn _load_storage(reader: &mut dyn Read) -> Result<Storage, String> {
|
||||||
|
match serde_yaml::from_reader::<&mut dyn Read, Storage>(reader) {
|
||||||
|
Ok(storage) => Ok(storage),
|
||||||
|
Err(err) => Err(format!("failed to parse storage: {}", err)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_load_storage() {
|
||||||
|
let got = _load_storage(&mut "adds: []".as_bytes()).expect("failed to parse 'adds: []' storage");
|
||||||
|
assert_eq!(got, Storage{adds: vec![]});
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_testdata_standalone_yaml() {
|
fn test_testdata_standalone_yaml() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
load_storage("./testdata/standalone.yaml".to_string()).expect("cant load standalone.yaml"),
|
load_storage("./src/testdata/standalone.yaml".to_string()).expect("cant load standalone.yaml"),
|
||||||
Storage{adds: vec![
|
Storage{adds: vec![
|
||||||
Add{t: 1, add: "def".to_string(), tag: "abc".to_string()},
|
Add{t: 1, add: "def".to_string(), tag: "abc".to_string()},
|
||||||
Add{t: 2, add: "ghi".to_string(), tag: "".to_string()},
|
Add{t: 2, add: "ghi".to_string(), tag: "".to_string()},
|
||||||
|
|
|
||||||
|
|
@ -4,3 +4,4 @@ adds:
|
||||||
tag: abc
|
tag: abc
|
||||||
- t: 2
|
- t: 2
|
||||||
add: ghi
|
add: ghi
|
||||||
|
tag: ""
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue