Skip to content

Commit

Permalink
test: small refactor
Browse files Browse the repository at this point in the history
Use `rstest` to reduce code repetition

Signed-off-by: Flavio Castelli <[email protected]>
  • Loading branch information
flavio committed Sep 26, 2023
1 parent 29c3288 commit d407480
Showing 1 changed file with 16 additions and 39 deletions.
55 changes: 16 additions & 39 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,11 @@ pub struct History {

#[cfg(test)]
mod tests {
use std::collections::{HashMap, HashSet};

use assert_json_diff::assert_json_eq;
use chrono::{TimeZone, Utc};
use rstest::*;
use serde_json::Value;
use std::collections::{HashMap, HashSet};

use super::{Architecture, Config, ConfigFile, History, Os, Rootfs};

Expand Down Expand Up @@ -531,45 +531,22 @@ mod tests {
}
}

#[test]
fn deserialize_example() {
let example = example_config();
let parsed: ConfigFile = serde_json::from_str(EXAMPLE_CONFIG).expect("parsed failed");
assert_eq!(example, parsed);
}

#[test]
fn deserialize_minimal() {
let example = minimal_config();
let parsed: ConfigFile = serde_json::from_str(MINIMAL_CONFIG).expect("parsed failed");
assert_eq!(example, parsed);
}

#[test]
fn deserialize_minimal2() {
let example = minimal_config2();
let parsed: ConfigFile = serde_json::from_str(MINIMAL_CONFIG2).expect("parsed failed");
assert_eq!(example, parsed);
}

#[test]
fn serialize_example() {
let serialized = serde_json::to_value(&example_config()).expect("serialize failed");
let parsed: Value = serde_json::from_str(EXAMPLE_CONFIG).expect("parsed failed");
assert_json_eq!(serialized, parsed);
}

#[test]
fn serialize_minimal() {
let serialized = serde_json::to_value(&minimal_config()).expect("serialize failed");
let parsed: Value = serde_json::from_str(MINIMAL_CONFIG).expect("parsed failed");
assert_json_eq!(serialized, parsed);
#[rstest]
#[case(example_config(), EXAMPLE_CONFIG)]
#[case(minimal_config(), MINIMAL_CONFIG)]
#[case(minimal_config2(), MINIMAL_CONFIG2)]
fn deserialize_test(#[case] config: ConfigFile, #[case] expected: &str) {
let parsed: ConfigFile = serde_json::from_str(expected).expect("parsed failed");
assert_eq!(config, parsed);
}

#[test]
fn serialize_minimal2() {
let serialized = serde_json::to_value(&minimal_config2()).expect("serialize failed");
let parsed: Value = serde_json::from_str(MINIMAL_CONFIG2).expect("parsed failed");
#[rstest]
#[case(example_config(), EXAMPLE_CONFIG)]
#[case(minimal_config(), MINIMAL_CONFIG)]
#[case(minimal_config2(), MINIMAL_CONFIG2)]
fn serialize_test(#[case] config: ConfigFile, #[case] expected: &str) {
let serialized = serde_json::to_value(&config).expect("serialize failed");
let parsed: Value = serde_json::from_str(expected).expect("parsed failed");
assert_json_eq!(serialized, parsed);
}
}

0 comments on commit d407480

Please sign in to comment.