Skip to content

Commit

Permalink
Make tests without features pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
zrho committed Jun 24, 2024
1 parent e81f376 commit eba1c65
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 37 deletions.
75 changes: 40 additions & 35 deletions hugr-sexpr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,41 +50,46 @@
//! In particular, the [`output::Output`] and [`input::Input`] traits can be derived
//! automatically for structs with named fields.
//!
//! ```
//! # use hugr_sexpr::input::Input;
//! # use hugr_sexpr::output::Output;
//! #[derive(Debug, PartialEq, Input, Output)]
//! pub struct Person {
//! name: String,
//! #[sexpr(required)]
//! company: String,
//! #[sexpr(optional)]
//! birthday: Option<String>,
//! #[sexpr(repeated)]
//! #[sexpr(rename = "email")]
//! email_addresses: Vec<String>,
//! }
//!
//! let person = Person {
//! name: "John Doe".to_string(),
//! company: "ACME".to_string(),
//! birthday: None,
//! email_addresses: vec![
//! "[email protected]".to_string(),
//! "[email protected]".to_string()
//! ],
//! };
//!
//! let sexpr = r#"
//! "John Doe"
//! (company "ACME")
//! (email "[email protected]")
//! (email "[email protected]")
//! "#;
//!
//! let imported = hugr_sexpr::from_str::<Person>(sexpr).unwrap();
//! assert_eq!(imported, person);
//! ```
#[cfg_attr(
feature = "derive",
doc = r##"
```
# use hugr_sexpr::input::Input;
# use hugr_sexpr::output::Output;
#[derive(Debug, PartialEq, Input, Output)]
pub struct Person {
name: String,
#[sexpr(required)]
company: String,
#[sexpr(optional)]
birthday: Option<String>,
#[sexpr(repeated)]
#[sexpr(rename = "email")]
email_addresses: Vec<String>,
}
let person = Person {
name: "John Doe".to_string(),
company: "ACME".to_string(),
birthday: None,
email_addresses: vec![
"[email protected]".to_string(),
"[email protected]".to_string()
],
};
let sexpr = r#"
"John Doe"
(company "ACME")
(email "[email protected]")
(email "[email protected]")
"#;
let imported = hugr_sexpr::from_str::<Person>(sexpr).unwrap();
assert_eq!(imported, person);
```
"##
)]
use ordered_float::OrderedFloat;
use smol_str::SmolStr;
use std::fmt::Display;
Expand Down
15 changes: 13 additions & 2 deletions hugr-sexpr/tests/derive_input.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use hugr_sexpr::{from_str, input::Input, read::ReadError, Symbol, Value};

#[test]
#[cfg(feature = "derive")]
pub fn positional() {
#[derive(Input)]
struct Test {
Expand All @@ -15,6 +16,7 @@ pub fn positional() {
}

#[test]
#[cfg(feature = "derive")]
pub fn optional_given() {
#[derive(Input)]
struct Test {
Expand All @@ -27,6 +29,8 @@ pub fn optional_given() {
assert_eq!(test.field.unwrap(), "string");
}

#[test]
#[cfg(feature = "derive")]
pub fn optional_absent() {
#[derive(Input)]
struct Test {
Expand All @@ -40,6 +44,7 @@ pub fn optional_absent() {
}

#[test]
#[cfg(feature = "derive")]
pub fn optional_duplicate() {
#[derive(Debug, Input)]
struct Test {
Expand All @@ -57,6 +62,7 @@ pub fn optional_duplicate() {
}

#[test]
#[cfg(feature = "derive")]
pub fn required_given() {
#[derive(Input)]
struct Test {
Expand All @@ -69,12 +75,14 @@ pub fn required_given() {
assert_eq!(test.field, "string");
}

#[test]
#[cfg(feature = "derive")]
pub fn required_absent() {
#[derive(Input)]
struct Test {
#[allow(dead_code)]
#[sexpr(optional)]
field: Option<String>,
#[sexpr(required)]
field: String,
}

let result = from_str::<Test>(r#""#);
Expand All @@ -83,6 +91,7 @@ pub fn required_absent() {
}

#[test]
#[cfg(feature = "derive")]
pub fn required_duplicate() {
#[derive(Input)]
struct Test {
Expand All @@ -97,6 +106,7 @@ pub fn required_duplicate() {
}

#[test]
#[cfg(feature = "derive")]
pub fn repeated() {
#[derive(Input)]
struct Test {
Expand All @@ -117,6 +127,7 @@ pub fn repeated() {
}

#[test]
#[cfg(feature = "derive")]
pub fn resursive_field() {
#[derive(Input, PartialEq, Eq, Debug)]
struct Outer {
Expand Down
5 changes: 5 additions & 0 deletions hugr-sexpr/tests/derive_output.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use hugr_sexpr::{from_str, output::Output, to_values, Value};

#[test]
#[cfg(feature = "derive")]
pub fn positional() {
#[derive(Output)]
pub struct Test {
Expand All @@ -19,6 +20,7 @@ pub fn positional() {
}

#[test]
#[cfg(feature = "derive")]
pub fn required() {
#[derive(Output)]
pub struct Test {
Expand All @@ -38,6 +40,7 @@ pub fn required() {
}

#[test]
#[cfg(feature = "derive")]
pub fn optional_given() {
#[derive(Output)]
pub struct Test {
Expand All @@ -57,6 +60,7 @@ pub fn optional_given() {
}

#[test]
#[cfg(feature = "derive")]
pub fn optional_absent() {
#[derive(Output)]
pub struct Test {
Expand All @@ -76,6 +80,7 @@ pub fn optional_absent() {
}

#[test]
#[cfg(feature = "derive")]
pub fn repeated() {
#[derive(Output)]
struct Test {
Expand Down

0 comments on commit eba1c65

Please sign in to comment.