From e075fb80f87dbd25cadc11bf1b28529d334c32a2 Mon Sep 17 00:00:00 2001 From: sharks Date: Sun, 15 Apr 2018 20:58:52 -0500 Subject: [PATCH] Add IPv4 test case --- Cargo.toml | 3 +++ tests/test_json.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/test_json.rs diff --git a/Cargo.toml b/Cargo.toml index 6c01ab2..08244d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,9 @@ clippy = {version = "0.0.104", optional = true} serde = { version = ">=0.8.0, <2.0", optional = true } serde_derive = { version = ">=0.8.0, <2.0", optional = true } +[dev-dependencies] +serde_json = "*" + [badges] travis-ci = { repository = "achanda/ipnetwork" } diff --git a/tests/test_json.rs b/tests/test_json.rs new file mode 100644 index 0000000..9d1aab4 --- /dev/null +++ b/tests/test_json.rs @@ -0,0 +1,35 @@ +#[cfg(feature = "with-serde")] +extern crate serde; +#[cfg(feature = "with-serde")] +extern crate serde_json; +#[cfg(feature = "with-serde")] +#[macro_use] +extern crate serde_derive; + +extern crate ipnetwork; + + +#[cfg(test)] +mod tests { + + use ipnetwork::Ipv4Network; + use std::net::Ipv4Addr; + + #[cfg(feature = "with-serde")] + #[test] + fn test_ipv4_json() { + let json_string = "{\"ipnetwork\":{\"addr\":\"127.1.0.0\",\"prefix\":24}}"; + + #[derive(Serialize, Deserialize)] + struct MyStruct { + ipnetwork: Ipv4Network, + } + + let mystruct: MyStruct = ::serde_json::from_str(json_string).unwrap(); + + assert_eq!(mystruct.ipnetwork.ip(), Ipv4Addr::new(127, 1, 0, 0)); + assert_eq!(mystruct.ipnetwork.prefix(), 24); + + assert_eq!(::serde_json::to_string(&mystruct).unwrap(), json_string); + } +} \ No newline at end of file