Skip to content

Commit

Permalink
Add a test showing how cfg_eval can be used to gate serde_with attrib…
Browse files Browse the repository at this point in the history
…utes

So far the story for cfg-gating serde_with attributes has not been
great, as shown in #331 or #355.
This adds a test showing how cfg_eval can be used to gate serde_with.

Part of #607
  • Loading branch information
jonasbb committed Jul 3, 2023
1 parent 167f25c commit 91630e7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions serde_with_test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ rust-version = "1.60"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cfg_eval = "0.1.2"
s = {package = "serde", version = "1.0.75", features = ["derive"]}
s_json = {package = "serde_json", version = "1.0.59"}
s_with = {package = "serde_with", path = "../serde_with"}
Expand Down
24 changes: 24 additions & 0 deletions serde_with_test/tests/serde_as_cfg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//! Test that cfg_eval helps in cfg-gating serde_with attributes

// Ensure no prelude is available
#![no_implicit_prelude]
#![allow(dead_code, unused_imports)]

#[cfg_attr(test, ::cfg_eval::cfg_eval, ::s_with::serde_as(crate = "::s_with"))]
#[cfg_attr(test, derive(::s::Serialize, ::s::Deserialize))]
#[derive(Debug)]
#[serde(crate = "::s")]
struct S {
#[cfg_attr(test, serde_as(as = "::s_with::DisplayFromStr"))]
int: i32,
}

#[test]
fn serde_as_cfg_gated() {
let s = S { int: 42 };
// Check serialization
let s = ::s_json::to_string(&s).unwrap();
::std::assert_eq!(s, r#"{"int":"42"}"#);
let s: S = ::s_json::from_str(&s).unwrap();
::std::assert_eq!(s.int, 42);
}

0 comments on commit 91630e7

Please sign in to comment.