Skip to content

Commit 2baef14

Browse files
committed
Add testcase
1 parent 33a1847 commit 2baef14

2 files changed

Lines changed: 85 additions & 0 deletions

File tree

crates/vespera_macro/src/openapi_generator.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,4 +1281,22 @@ pub fn get_user() -> User {
12811281
let value = utils_get_type_default(&ty);
12821282
assert!(value.is_none());
12831283
}
1284+
1285+
#[test]
1286+
fn test_generate_openapi_with_unparseable_definition() {
1287+
// Test line 42: syn::parse_str fails with invalid Rust syntax
1288+
// This triggers the `continue` branch when parsing fails
1289+
let mut metadata = CollectedMetadata::new();
1290+
metadata.structs.push(StructMetadata {
1291+
name: "Invalid".to_string(),
1292+
// Invalid Rust syntax - cannot be parsed by syn
1293+
definition: "struct { invalid syntax {{{{".to_string(),
1294+
include_in_openapi: true,
1295+
});
1296+
1297+
// Should gracefully skip unparseable definitions
1298+
let doc = generate_openapi_doc_with_metadata(None, None, None, &metadata);
1299+
// The unparseable definition should be skipped
1300+
assert!(doc.components.is_none() || doc.components.as_ref().unwrap().schemas.is_none());
1301+
}
12841302
}

crates/vespera_macro/src/parse_utils.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,4 +353,71 @@ mod tests {
353353
let result = parser.parse2(tokens);
354354
assert!(result.is_err());
355355
}
356+
357+
#[test]
358+
fn test_parse_bracketed_list_empty() {
359+
// Test parse_bracketed_list with empty brackets
360+
let parser = |input: ParseStream| {
361+
parse_bracketed_list(input, |input| {
362+
input.parse::<LitStr>().map(|lit| lit.value())
363+
})
364+
};
365+
366+
let tokens = quote::quote!([]);
367+
let result = parser.parse2(tokens);
368+
assert!(result.is_ok());
369+
let items: Vec<String> = result.unwrap();
370+
assert!(items.is_empty());
371+
}
372+
373+
#[test]
374+
fn test_parse_bracketed_list_single_item() {
375+
// Test parse_bracketed_list with single item
376+
let parser = |input: ParseStream| {
377+
parse_bracketed_list(input, |input| {
378+
input.parse::<LitStr>().map(|lit| lit.value())
379+
})
380+
};
381+
382+
let tokens = quote::quote!(["single"]);
383+
let result = parser.parse2(tokens);
384+
assert!(result.is_ok());
385+
let items: Vec<String> = result.unwrap();
386+
assert_eq!(items, vec!["single"]);
387+
}
388+
389+
#[test]
390+
fn test_parse_bracketed_list_with_trailing_comma() {
391+
// Test parse_bracketed_list with trailing comma
392+
let parser = |input: ParseStream| {
393+
parse_bracketed_list(input, |input| {
394+
input.parse::<LitStr>().map(|lit| lit.value())
395+
})
396+
};
397+
398+
let tokens = quote::quote!(["a", "b",]);
399+
let result = parser.parse2(tokens);
400+
assert!(result.is_ok());
401+
let items: Vec<String> = result.unwrap();
402+
assert_eq!(items, vec!["a", "b"]);
403+
}
404+
405+
#[test]
406+
fn test_parse_bracketed_list_integers() {
407+
// Test parse_bracketed_list with integer literals
408+
use syn::LitInt;
409+
let parser = |input: ParseStream| {
410+
parse_bracketed_list(input, |input| {
411+
input
412+
.parse::<LitInt>()
413+
.and_then(|lit| lit.base10_parse::<i32>())
414+
})
415+
};
416+
417+
let tokens = quote::quote!([1, 2, 3]);
418+
let result = parser.parse2(tokens);
419+
assert!(result.is_ok());
420+
let items: Vec<i32> = result.unwrap();
421+
assert_eq!(items, vec![1, 2, 3]);
422+
}
356423
}

0 commit comments

Comments
 (0)