Skip to content

Commit d0bde87

Browse files
authored
test: add more tests (#64)
- add more tests
1 parent 2f9e70e commit d0bde87

File tree

1 file changed

+114
-81
lines changed

1 file changed

+114
-81
lines changed

src/error.rs

Lines changed: 114 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -83,47 +83,48 @@ pub type Result<T> = std::result::Result<T, ZervError>;
8383
#[cfg(test)]
8484
mod tests {
8585
use super::*;
86+
use rstest::rstest;
8687
use std::error::Error;
8788

88-
#[test]
89-
fn test_error_display() {
90-
assert_eq!(
91-
ZervError::VcsNotFound("git".to_string()).to_string(),
92-
"VCS not found: git"
93-
);
94-
assert_eq!(
95-
ZervError::NoTagsFound.to_string(),
96-
"No tags found matching pattern"
97-
);
98-
assert_eq!(
99-
ZervError::InvalidFormat("bad".to_string()).to_string(),
100-
"Invalid version format: bad"
101-
);
102-
assert_eq!(
103-
ZervError::CommandFailed("exit 1".to_string()).to_string(),
104-
"Command execution failed: exit 1"
105-
);
106-
assert_eq!(
107-
ZervError::Regex("invalid".to_string()).to_string(),
108-
"Regex error: invalid"
109-
);
89+
#[rstest]
90+
#[case(ZervError::VcsNotFound("git".to_string()), "VCS not found: git")]
91+
#[case(ZervError::NoTagsFound, "No tags found matching pattern")]
92+
#[case(ZervError::InvalidFormat("bad".to_string()), "Invalid version format: bad")]
93+
#[case(ZervError::InvalidVersion("1.0.0-invalid".to_string()), "Invalid version: 1.0.0-invalid")]
94+
#[case(ZervError::CommandFailed("exit 1".to_string()), "Command execution failed: exit 1")]
95+
#[case(ZervError::Regex("invalid".to_string()), "Regex error: invalid")]
96+
#[case(ZervError::SchemaParseError("bad ron".to_string()), "Schema parse error: bad ron")]
97+
#[case(ZervError::UnknownSchema("unknown".to_string()), "Unknown schema: unknown")]
98+
#[case(ZervError::ConflictingSchemas("both provided".to_string()), "Conflicting schemas: both provided")]
99+
fn test_error_display(#[case] error: ZervError, #[case] expected: &str) {
100+
assert_eq!(error.to_string(), expected);
110101
}
111102

112-
#[test]
113-
fn test_io_error_conversion() {
114-
let io_err = io::Error::new(io::ErrorKind::NotFound, "file not found");
103+
#[rstest]
104+
#[case(io::ErrorKind::NotFound, "file not found")]
105+
#[case(io::ErrorKind::PermissionDenied, "access denied")]
106+
#[case(io::ErrorKind::ConnectionRefused, "connection refused")]
107+
#[case(io::ErrorKind::TimedOut, "timed out")]
108+
fn test_io_error_conversion(#[case] kind: io::ErrorKind, #[case] message: &str) {
109+
let io_err = io::Error::new(kind, message);
115110
let zerv_err: ZervError = io_err.into();
116111
assert!(matches!(zerv_err, ZervError::Io(_)));
117-
assert!(zerv_err.to_string().contains("file not found"));
112+
assert!(zerv_err.to_string().contains(message));
118113
}
119114

120-
#[test]
121-
fn test_error_source() {
122-
let io_err = io::Error::new(io::ErrorKind::PermissionDenied, "access denied");
123-
let zerv_err = ZervError::Io(io_err);
124-
assert!(zerv_err.source().is_some());
125-
126-
assert!(ZervError::NoTagsFound.source().is_none());
115+
#[rstest]
116+
#[case(ZervError::Io(io::Error::new(io::ErrorKind::NotFound, "test")), true)]
117+
#[case(ZervError::VcsNotFound("git".to_string()), false)]
118+
#[case(ZervError::NoTagsFound, false)]
119+
#[case(ZervError::InvalidFormat("bad".to_string()), false)]
120+
#[case(ZervError::InvalidVersion("bad".to_string()), false)]
121+
#[case(ZervError::CommandFailed("exit 1".to_string()), false)]
122+
#[case(ZervError::Regex("invalid".to_string()), false)]
123+
#[case(ZervError::SchemaParseError("bad".to_string()), false)]
124+
#[case(ZervError::UnknownSchema("unknown".to_string()), false)]
125+
#[case(ZervError::ConflictingSchemas("conflict".to_string()), false)]
126+
fn test_error_source(#[case] error: ZervError, #[case] has_source: bool) {
127+
assert_eq!(error.source().is_some(), has_source);
127128
}
128129

129130
#[test]
@@ -135,55 +136,73 @@ mod tests {
135136
assert!(err_result.is_err());
136137
}
137138

138-
#[test]
139-
fn test_error_equality() {
140-
// Same variants with same values should be equal
141-
assert_eq!(
142-
ZervError::VcsNotFound("git".to_string()),
143-
ZervError::VcsNotFound("git".to_string())
144-
);
145-
assert_eq!(ZervError::NoTagsFound, ZervError::NoTagsFound);
146-
assert_eq!(
147-
ZervError::InvalidFormat("bad".to_string()),
148-
ZervError::InvalidFormat("bad".to_string())
149-
);
150-
assert_eq!(
151-
ZervError::CommandFailed("exit 1".to_string()),
152-
ZervError::CommandFailed("exit 1".to_string())
153-
);
154-
assert_eq!(
155-
ZervError::Regex("invalid".to_string()),
156-
ZervError::Regex("invalid".to_string())
157-
);
158-
159-
// Same variants with different values should not be equal
160-
assert_ne!(
161-
ZervError::VcsNotFound("git".to_string()),
162-
ZervError::VcsNotFound("hg".to_string())
163-
);
164-
assert_ne!(
165-
ZervError::InvalidFormat("bad".to_string()),
166-
ZervError::InvalidFormat("worse".to_string())
167-
);
168-
assert_ne!(
169-
ZervError::CommandFailed("exit 1".to_string()),
170-
ZervError::CommandFailed("exit 2".to_string())
171-
);
172-
assert_ne!(
173-
ZervError::Regex("invalid".to_string()),
174-
ZervError::Regex("bad".to_string())
175-
);
176-
177-
// Different variants should not be equal
178-
assert_ne!(
179-
ZervError::NoTagsFound,
180-
ZervError::VcsNotFound("git".to_string())
181-
);
182-
assert_ne!(
183-
ZervError::InvalidFormat("bad".to_string()),
184-
ZervError::CommandFailed("bad".to_string())
185-
);
139+
#[rstest]
140+
#[case(
141+
ZervError::VcsNotFound("git".to_string()),
142+
ZervError::VcsNotFound("git".to_string()),
143+
true
144+
)]
145+
#[case(
146+
ZervError::VcsNotFound("git".to_string()),
147+
ZervError::VcsNotFound("hg".to_string()),
148+
false
149+
)]
150+
#[case(ZervError::NoTagsFound, ZervError::NoTagsFound, true)]
151+
#[case(
152+
ZervError::InvalidFormat("bad".to_string()),
153+
ZervError::InvalidFormat("bad".to_string()),
154+
true
155+
)]
156+
#[case(
157+
ZervError::InvalidFormat("bad".to_string()),
158+
ZervError::InvalidFormat("worse".to_string()),
159+
false
160+
)]
161+
#[case(
162+
ZervError::InvalidVersion("1.0".to_string()),
163+
ZervError::InvalidVersion("1.0".to_string()),
164+
true
165+
)]
166+
#[case(
167+
ZervError::CommandFailed("exit 1".to_string()),
168+
ZervError::CommandFailed("exit 1".to_string()),
169+
true
170+
)]
171+
#[case(
172+
ZervError::Regex("invalid".to_string()),
173+
ZervError::Regex("invalid".to_string()),
174+
true
175+
)]
176+
#[case(
177+
ZervError::SchemaParseError("bad".to_string()),
178+
ZervError::SchemaParseError("bad".to_string()),
179+
true
180+
)]
181+
#[case(
182+
ZervError::UnknownSchema("unknown".to_string()),
183+
ZervError::UnknownSchema("unknown".to_string()),
184+
true
185+
)]
186+
#[case(
187+
ZervError::ConflictingSchemas("conflict".to_string()),
188+
ZervError::ConflictingSchemas("conflict".to_string()),
189+
true
190+
)]
191+
#[case(
192+
ZervError::NoTagsFound,
193+
ZervError::VcsNotFound("git".to_string()),
194+
false
195+
)]
196+
fn test_error_equality(
197+
#[case] error1: ZervError,
198+
#[case] error2: ZervError,
199+
#[case] should_equal: bool,
200+
) {
201+
assert_eq!(error1 == error2, should_equal);
202+
}
186203

204+
#[test]
205+
fn test_io_error_equality() {
187206
// IO errors with same kind and message should be equal
188207
let io_err1 = io::Error::new(io::ErrorKind::NotFound, "file not found");
189208
let io_err2 = io::Error::new(io::ErrorKind::NotFound, "file not found");
@@ -194,4 +213,18 @@ mod tests {
194213
let io_err4 = io::Error::new(io::ErrorKind::PermissionDenied, "file not found");
195214
assert_ne!(ZervError::Io(io_err3), ZervError::Io(io_err4));
196215
}
216+
217+
#[test]
218+
fn test_debug_trait() {
219+
let error = ZervError::VcsNotFound("git".to_string());
220+
let debug_str = format!("{error:?}");
221+
assert!(debug_str.contains("VcsNotFound"));
222+
assert!(debug_str.contains("git"));
223+
}
224+
225+
#[test]
226+
fn test_error_trait_implementation() {
227+
let error = ZervError::NoTagsFound;
228+
let _: &dyn Error = &error; // Ensure Error trait is implemented
229+
}
197230
}

0 commit comments

Comments
 (0)