Skip to content

Commit

Permalink
feat(tests): Add comprehensive unit tests for Path enum variants
Browse files Browse the repository at this point in the history
- Organized and documented existing tests for Path enum variants (Root, Seq, Map, Alias, Unknown).
- Added missing unit tests for:
  - Map variant with different key values (including empty key).
  - Seq variant with different index values.
  - Nested Path structures involving multiple variant combinations.
- Ensured consistent docstrings and logical grouping of tests.
- Included tests for:
  - Equality and cloning of Path instances.
  - Panic scenarios for unexpected sequence and mapping ends.
  - Handling of Alias variant in Event enum.
- Improved coverage and robustness of test suite.
  • Loading branch information
sebastienrousseau committed Aug 24, 2024
1 parent 8fca524 commit fc4ae52
Showing 1 changed file with 129 additions and 127 deletions.
256 changes: 129 additions & 127 deletions tests/modules/test_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
mod tests {
use serde_yml::{de::Event, modules::path::Path};

/// Test the Path::Root variant.
/// Test the `Path::Root` variant.
///
/// This test ensures that the root path is correctly formatted as ".".
/// This test ensures that the `Root` path is correctly formatted as ".".
#[test]
fn test_path_root() {
let path = Path::Root;
assert_eq!(format!("{}", path), ".");
}

/// Test the Path::Seq variant.
/// Test the `Path::Seq` variant.
///
/// This test checks that a sequence path with a given index is correctly formatted.
#[test]
Expand All @@ -24,7 +24,7 @@ mod tests {
assert_eq!(format!("{}", path), "\\[42\\]");
}

/// Test the Path::Seq variant with a very large index.
/// Test the `Path::Seq` variant with a very large index.
///
/// This test checks that a sequence path with a large index is correctly formatted.
#[test]
Expand All @@ -40,20 +40,57 @@ mod tests {
);
}

/// Test the Path::Map variant.
/// Test the `Path::Seq` variant with a nested parent.
///
/// This test checks that a sequence path with a parent sequence is correctly formatted.
#[test]
fn test_seq_with_parent() {
let root = Path::Root;
let parent_seq = Path::Seq {
parent: &root,
index: 1,
};
let child_seq = Path::Seq {
parent: &parent_seq,
index: 42,
};
assert_eq!(format!("{}", child_seq), "\\[1\\].\\[42\\]");
}

/// Test the `Path::Seq` variant with different indices.
///
/// This test validates that various index values in the `Seq` variant are correctly formatted.
#[test]
fn test_seq_with_different_indices() {
let root = Path::Root;

let path_zero = Path::Seq {
parent: &root,
index: 0,
};
assert_eq!(format!("{}", path_zero), "\\[0\\]");

let path_large = Path::Seq {
parent: &root,
index: 100,
};
assert_eq!(format!("{}", path_large), "\\[100\\]");
}

/// Test the `Path::Map` variant.
///
/// This test checks that a map path with a given key is correctly formatted.
#[test]
fn test_path_map() {
let root = Path::Root;
let path = Path::Map {
parent: &root,
key: "key",
key: "key_name",
};
assert_eq!(format!("{}", path), "key");
assert_eq!(format!("{}", path), "key_name");
}

/// Test the Path::Map variant with an empty key.
/// Test the `Path::Map` variant with an empty key.
///
/// This test ensures that a map path with an empty key is correctly handled and formatted.
#[test]
Expand All @@ -66,7 +103,50 @@ mod tests {
assert_eq!(format!("{}", path), "");
}

/// Test the Path::Alias variant.
/// Test the `Path::Map` variant with different keys.
///
/// This test validates that various key values in the `Map` variant are correctly formatted.
#[test]
fn test_map_with_different_keys() {
let root = Path::Root;

let path_empty_key = Path::Map {
parent: &root,
key: "",
};
assert_eq!(format!("{}", path_empty_key), "");

let path_special_key = Path::Map {
parent: &root,
key: "special_key",
};
assert_eq!(format!("{}", path_special_key), "special_key");

let path_number_key = Path::Map {
parent: &root,
key: "123",
};
assert_eq!(format!("{}", path_number_key), "123");
}

/// Test the `Path::Map` variant with a parent sequence.
///
/// This test checks that the `parent` field in a `Map` variant is correctly formatted.
#[test]
fn test_map_with_parent() {
let root = Path::Root;
let parent_seq = Path::Seq {
parent: &root,
index: 1,
};
let map = Path::Map {
parent: &parent_seq,
key: "key",
};
assert_eq!(format!("{}", map), "\\[1\\].key");
}

/// Test the `Path::Alias` variant.
///
/// This test checks that an alias path is correctly formatted.
#[test]
Expand All @@ -76,7 +156,23 @@ mod tests {
assert_eq!(format!("{}", path), "");
}

/// Test the Path::Unknown variant.
/// Test the `Path::Alias` variant with a parent map.
///
/// This test checks that the `parent` field in an `Alias` variant is correctly formatted.
#[test]
fn test_alias_with_parent() {
let root = Path::Root;
let parent_map = Path::Map {
parent: &root,
key: "parent_key",
};
let alias = Path::Alias {
parent: &parent_map,
};
assert_eq!(format!("{}", alias), "parent_key.");
}

/// Test the `Path::Unknown` variant.
///
/// This test checks that an unknown path is correctly formatted.
#[test]
Expand All @@ -86,9 +182,25 @@ mod tests {
assert_eq!(format!("{}", path), "?");
}

/// Test equality of two Path instances with the same structure.
/// Test the `Path::Unknown` variant with a parent map.
///
/// This test checks that the `parent` field in an `Unknown` variant is correctly formatted.
#[test]
fn test_unknown_with_parent() {
let root = Path::Root;
let parent_map = Path::Map {
parent: &root,
key: "parent_key",
};
let unknown = Path::Unknown {
parent: &parent_map,
};
assert_eq!(format!("{}", unknown), "parent_key.?");
}

/// Test equality of two `Path` instances with the same structure.
///
/// This test checks that two instances of Path with identical structures are considered equal.
/// This test checks that two instances of `Path` with identical structures are considered equal.
#[test]
fn test_path_equality() {
let root = Path::Root;
Expand All @@ -103,9 +215,9 @@ mod tests {
assert_eq!(seq1, seq2);
}

/// Test cloning and copying a Path instance.
/// Test cloning and copying a `Path` instance.
///
/// This test checks that copying a Path instance results in an identical path.
/// This test checks that copying a `Path` instance results in an identical path.
#[test]
fn test_path_clone_and_copy() {
let root = Path::Root;
Expand Down Expand Up @@ -169,7 +281,7 @@ mod tests {
);
}

/// Test a complex nested structure with a Seq inside a Map inside another Seq.
/// Test a complex nested structure with a `Seq` inside a `Map` inside another `Seq`.
///
/// This test checks the formatting of a complex nested structure involving sequences and maps.
#[test]
Expand All @@ -190,118 +302,6 @@ mod tests {
assert_eq!(format!("{}", seq2), "\\[1\\].key.\\[99\\]");
}

/// Test handling of the `parent` field in the `Seq` variant.
///
/// This test checks that the `parent` field in a `Seq` variant is correctly formatted.
#[test]
fn test_seq_with_parent() {
let root = Path::Root;
let parent_seq = Path::Seq {
parent: &root,
index: 1,
};
let child_seq = Path::Seq {
parent: &parent_seq,
index: 42,
};
assert_eq!(format!("{}", child_seq), "\\[1\\].\\[42\\]");
}

/// Test handling of the `index` field in the `Seq` variant.
///
/// This test validates that various index values in the `Seq` variant are correctly formatted.
#[test]
fn test_seq_with_different_indices() {
let root = Path::Root;

let path_zero = Path::Seq {
parent: &root,
index: 0,
};
assert_eq!(format!("{}", path_zero), "\\[0\\]");

let path_large = Path::Seq {
parent: &root,
index: 100,
};
assert_eq!(format!("{}", path_large), "\\[100\\]");
}

/// Test handling of the `parent` field in the `Map` variant.
///
/// This test checks that the `parent` field in a `Map` variant is correctly formatted.
#[test]
fn test_map_with_parent() {
let root = Path::Root;
let parent_seq = Path::Seq {
parent: &root,
index: 1,
};
let map = Path::Map {
parent: &parent_seq,
key: "key",
};
assert_eq!(format!("{}", map), "\\[1\\].key");
}

/// Test handling of the `key` field in the `Map` variant.
///
/// This test validates that various key values in the `Map` variant are correctly formatted.
#[test]
fn test_map_with_different_keys() {
let root = Path::Root;

let path_empty_key = Path::Map {
parent: &root,
key: "",
};
assert_eq!(format!("{}", path_empty_key), "");

let path_special_key = Path::Map {
parent: &root,
key: "special_key",
};
assert_eq!(format!("{}", path_special_key), "special_key");

let path_number_key = Path::Map {
parent: &root,
key: "123",
};
assert_eq!(format!("{}", path_number_key), "123");
}

/// Test handling of the `parent` field in the `Alias` variant.
///
/// This test checks that the `parent` field in an `Alias` variant is correctly formatted.
#[test]
fn test_alias_with_parent() {
let root = Path::Root;
let parent_map = Path::Map {
parent: &root,
key: "parent_key",
};
let alias = Path::Alias {
parent: &parent_map,
};
assert_eq!(format!("{}", alias), "parent_key.");
}

/// Test handling of the `parent` field in the `Unknown` variant.
///
/// This test checks that the `parent` field in an `Unknown` variant is correctly formatted.
#[test]
fn test_unknown_with_parent() {
let root = Path::Root;
let parent_map = Path::Map {
parent: &root,
key: "parent_key",
};
let unknown = Path::Unknown {
parent: &parent_map,
};
assert_eq!(format!("{}", unknown), "parent_key.?");
}

/// Test panic for unexpected end of sequence.
#[test]
#[should_panic(expected = "unexpected end of sequence")]
Expand All @@ -317,6 +317,8 @@ mod tests {
}

/// Test deserialization of the `Alias` variant in the `Event` enum.
///
/// This test checks that the `Alias` variant is correctly handled in the `Event` enum.
#[test]
fn test_event_alias_variant() {
let alias_index: usize = 42; // Example index
Expand Down

0 comments on commit fc4ae52

Please sign in to comment.