Skip to content

Commit e1dffec

Browse files
bergwolfimeoer
authored andcommitted
api: increase error.rs UT coverage
Signed-off-by: Peng Tao <bergwolf@hyper.sh>
1 parent cc62dd6 commit e1dffec

File tree

1 file changed

+148
-0
lines changed

1 file changed

+148
-0
lines changed

api/src/error.rs

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ define_error_macro!(eother, std::io::Error::new(std::io::ErrorKind::Other, ""));
8686

8787
#[cfg(test)]
8888
mod tests {
89+
use std::io::{Error, ErrorKind};
90+
8991
fn check_size(size: usize) -> std::io::Result<()> {
9092
if size > 0x1000 {
9193
return Err(einval!());
@@ -101,4 +103,150 @@ mod tests {
101103
std::io::Error::from_raw_os_error(libc::EINVAL).kind()
102104
);
103105
}
106+
107+
#[test]
108+
fn test_make_error() {
109+
let original_error = Error::new(ErrorKind::Other, "test error");
110+
let debug_info = "debug information";
111+
let file = "test.rs";
112+
let line = 42;
113+
114+
let result_error = super::make_error(original_error, debug_info, file, line);
115+
assert_eq!(result_error.kind(), ErrorKind::Other);
116+
}
117+
118+
#[test]
119+
fn test_libc_error_macros() {
120+
// Test einval macro
121+
let err = einval!();
122+
assert_eq!(err.kind(), Error::from_raw_os_error(libc::EINVAL).kind());
123+
124+
// Test enoent macro
125+
let err = enoent!();
126+
assert_eq!(err.kind(), Error::from_raw_os_error(libc::ENOENT).kind());
127+
128+
// Test ebadf macro
129+
let err = ebadf!();
130+
assert_eq!(err.kind(), Error::from_raw_os_error(libc::EBADF).kind());
131+
132+
// Test eacces macro
133+
let err = eacces!();
134+
assert_eq!(err.kind(), Error::from_raw_os_error(libc::EACCES).kind());
135+
136+
// Test enotdir macro
137+
let err = enotdir!();
138+
assert_eq!(err.kind(), Error::from_raw_os_error(libc::ENOTDIR).kind());
139+
140+
// Test eisdir macro
141+
let err = eisdir!();
142+
assert_eq!(err.kind(), Error::from_raw_os_error(libc::EISDIR).kind());
143+
144+
// Test ealready macro
145+
let err = ealready!();
146+
assert_eq!(err.kind(), Error::from_raw_os_error(libc::EALREADY).kind());
147+
148+
// Test enosys macro
149+
let err = enosys!();
150+
assert_eq!(err.kind(), Error::from_raw_os_error(libc::ENOSYS).kind());
151+
152+
// Test epipe macro
153+
let err = epipe!();
154+
assert_eq!(err.kind(), Error::from_raw_os_error(libc::EPIPE).kind());
155+
156+
// Test eio macro
157+
let err = eio!();
158+
assert_eq!(err.kind(), Error::from_raw_os_error(libc::EIO).kind());
159+
}
160+
161+
#[test]
162+
fn test_libc_error_macros_with_context() {
163+
let test_msg = "test context";
164+
165+
// Test einval macro with context
166+
let err = einval!(test_msg);
167+
assert_eq!(err.kind(), Error::from_raw_os_error(libc::EINVAL).kind());
168+
169+
// Test enoent macro with context
170+
let err = enoent!(test_msg);
171+
assert_eq!(err.kind(), Error::from_raw_os_error(libc::ENOENT).kind());
172+
173+
// Test eio macro with context
174+
let err = eio!(test_msg);
175+
assert_eq!(err.kind(), Error::from_raw_os_error(libc::EIO).kind());
176+
}
177+
178+
#[test]
179+
fn test_custom_error_macros() {
180+
// Test last_error macro
181+
let err = last_error!();
182+
// We can't predict the exact error, but we can check it's a valid error
183+
assert!(!err.to_string().is_empty());
184+
185+
// Test eother macro
186+
let err = eother!();
187+
assert_eq!(err.kind(), ErrorKind::Other);
188+
189+
// Test eother macro with context
190+
let err = eother!("custom context");
191+
assert_eq!(err.kind(), ErrorKind::Other);
192+
}
193+
194+
fn test_bail_einval_function() -> std::io::Result<()> {
195+
bail_einval!("test error message");
196+
}
197+
198+
fn test_bail_eio_function() -> std::io::Result<()> {
199+
bail_eio!("test error message");
200+
}
201+
202+
#[test]
203+
fn test_bail_macros() {
204+
// Test bail_einval macro
205+
let result = test_bail_einval_function();
206+
assert!(result.is_err());
207+
let err = result.unwrap_err();
208+
assert_eq!(err.kind(), Error::from_raw_os_error(libc::EINVAL).kind());
209+
// The error message format is controlled by the macro, so just check it's not empty
210+
assert!(!err.to_string().is_empty());
211+
212+
// Test bail_eio macro
213+
let result = test_bail_eio_function();
214+
assert!(result.is_err());
215+
let err = result.unwrap_err();
216+
assert_eq!(err.kind(), Error::from_raw_os_error(libc::EIO).kind());
217+
// The error message format is controlled by the macro, so just check it's not empty
218+
assert!(!err.to_string().is_empty());
219+
}
220+
221+
#[test]
222+
fn test_bail_macros_with_formatting() {
223+
fn test_bail_with_format(code: i32) -> std::io::Result<()> {
224+
if code == 1 {
225+
bail_einval!("error code: {}", code);
226+
} else if code == 2 {
227+
bail_eio!("I/O error with code: {}", code);
228+
}
229+
Ok(())
230+
}
231+
232+
// Test bail_einval with formatting
233+
let result = test_bail_with_format(1);
234+
assert!(result.is_err());
235+
let err = result.unwrap_err();
236+
assert_eq!(err.kind(), Error::from_raw_os_error(libc::EINVAL).kind());
237+
// The error message format is controlled by the macro, so just check it's not empty
238+
assert!(!err.to_string().is_empty());
239+
240+
// Test bail_eio with formatting
241+
let result = test_bail_with_format(2);
242+
assert!(result.is_err());
243+
let err = result.unwrap_err();
244+
assert_eq!(err.kind(), Error::from_raw_os_error(libc::EIO).kind());
245+
// The error message format is controlled by the macro, so just check it's not empty
246+
assert!(!err.to_string().is_empty());
247+
248+
// Test success case
249+
let result = test_bail_with_format(3);
250+
assert!(result.is_ok());
251+
}
104252
}

0 commit comments

Comments
 (0)