Skip to content

Commit 8dd59ad

Browse files
committed
test: IsError trait
1 parent 6dddd12 commit 8dd59ad

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

lib/llm/src/protocols/common/llm_backend.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,26 @@ pub struct EmbeddingsEngineOutput {
159159
pub prompt_tokens: u32,
160160
pub total_tokens: u32,
161161
}
162+
163+
#[cfg(test)]
164+
mod tests {
165+
use super::*;
166+
167+
#[test]
168+
fn test_is_error() {
169+
let output = LLMEngineOutput::stop();
170+
assert!(output.err().is_none());
171+
assert!(output.is_ok());
172+
assert!(!output.is_err());
173+
174+
let output = LLMEngineOutput::error("Test error".to_string());
175+
assert_eq!(format!("{}", output.err().unwrap()), "Test error");
176+
assert!(!output.is_ok());
177+
assert!(output.is_err());
178+
179+
let output = LLMEngineOutput::from_err(anyhow::Error::msg("Test error 2").into());
180+
assert_eq!(format!("{}", output.err().unwrap()), "Test error 2");
181+
assert!(!output.is_ok());
182+
assert!(output.is_err());
183+
}
184+
}

lib/runtime/src/protocols/annotated.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use super::*;
1717
use crate::{error, Result};
18+
use is_error::IsError;
1819

1920
pub trait AnnotationsProvider {
2021
fn annotations(&self) -> Option<Vec<String>>;
@@ -146,7 +147,7 @@ impl<R> Annotated<R> {
146147
}
147148
}
148149

149-
impl<R> is_error::IsError for Annotated<R>
150+
impl<R> IsError for Annotated<R>
150151
where
151152
R: for<'de> Deserialize<'de> + Serialize,
152153
{
@@ -188,3 +189,27 @@ where
188189
// Box::pin(stream)
189190
// }
190191
// }
192+
193+
#[cfg(test)]
194+
mod tests {
195+
use super::*;
196+
197+
#[test]
198+
fn test_is_error() {
199+
let annotated = Annotated::from_data("Test data".to_string());
200+
assert!(annotated.err().is_none());
201+
assert!(annotated.is_ok());
202+
assert!(!annotated.is_err());
203+
204+
let annotated = Annotated::<String>::from_error("Test error 2".to_string());
205+
assert_eq!(format!("{}", annotated.err().unwrap()), "Test error 2");
206+
assert!(!annotated.is_ok());
207+
assert!(annotated.is_err());
208+
209+
let annotated =
210+
Annotated::<String>::from_err(anyhow::Error::msg("Test error 3".to_string()).into());
211+
assert_eq!(format!("{}", annotated.err().unwrap()), "Test error 3");
212+
assert!(!annotated.is_ok());
213+
assert!(annotated.is_err());
214+
}
215+
}

lib/runtime/src/protocols/is_error.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,30 @@ pub trait IsError {
3232
self.err().is_some()
3333
}
3434
}
35+
36+
#[cfg(test)]
37+
mod tests {
38+
use super::*;
39+
40+
struct TestError {
41+
message: String,
42+
}
43+
impl IsError for TestError {
44+
fn from_err(err: Box<dyn Error>) -> Self {
45+
TestError {
46+
message: err.to_string(),
47+
}
48+
}
49+
fn err(&self) -> Option<Box<dyn Error>> {
50+
Some(anyhow::Error::msg(self.message.clone()).into())
51+
}
52+
}
53+
54+
#[test]
55+
fn test_is_error_default_implementations() {
56+
let err = TestError::from_err(anyhow::Error::msg("Test error".to_string()).into());
57+
assert_eq!(format!("{}", err.err().unwrap()), "Test error");
58+
assert!(!err.is_ok());
59+
assert!(err.is_err());
60+
}
61+
}

0 commit comments

Comments
 (0)