forked from ron-rs/ron
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
169 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use bitflags::*; | ||
use option_set::option_set; | ||
|
||
#[macro_use] | ||
extern crate bitflags_serial; | ||
|
||
bitflags! { | ||
#[derive(serde::Serialize, serde::Deserialize)] | ||
struct TestGood: u8 { | ||
const ONE = 1; | ||
const TWO = 1 << 1; | ||
const THREE = 1 << 2; | ||
} | ||
} | ||
|
||
option_set! { | ||
struct TestBad: UpperCamel + u8 { | ||
const ONE = 1; | ||
const TWO = 1 << 1; | ||
const THREE = 1 << 2; | ||
} | ||
} | ||
|
||
bitflags_serial! { | ||
struct TestBadTWO: u8 { | ||
const ONE = 1; | ||
const TWO = 1 << 1; | ||
const THREE = 1 << 2; | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_bitflags() { | ||
// Test case provided by jaynus in | ||
// https://github.com/ron-rs/ron/issues/152#issue-421298302 | ||
let flag_good = TestGood::ONE | TestGood::TWO; | ||
|
||
let json_ser_good = serde_json::ser::to_string(&flag_good).unwrap(); | ||
let ron_ser_good = ron::ser::to_string(&flag_good).unwrap(); | ||
|
||
assert_eq!(json_ser_good, "{\"bits\":3}"); | ||
assert_eq!(ron_ser_good, "(bits:3)"); | ||
|
||
let json_de_good: TestGood = serde_json::de::from_str(json_ser_good.as_str()).unwrap(); | ||
let ron_de_good: TestGood = ron::de::from_str(ron_ser_good.as_str()).unwrap(); | ||
|
||
assert_eq!(json_de_good, flag_good); | ||
assert_eq!(ron_de_good, flag_good); | ||
|
||
// option_set | ||
let flag_bad = TestBad::ONE | TestBad::TWO; | ||
|
||
let json_ser_bad = serde_json::ser::to_string(&flag_bad).unwrap(); | ||
let ron_ser_bad = ron::ser::to_string(&flag_bad).unwrap(); | ||
|
||
assert_eq!(json_ser_bad, "[\"One\",\"Two\"]"); | ||
assert_eq!(ron_ser_bad, "[\"One\",\"Two\"]"); | ||
|
||
let json_de_bad: TestBad = serde_json::de::from_str(json_ser_bad.as_str()).unwrap(); | ||
let ron_de_bad: TestBad = ron::de::from_str(ron_ser_bad.as_str()).unwrap(); | ||
|
||
assert_eq!(json_de_bad, flag_bad); | ||
assert_eq!(ron_de_bad, flag_bad); | ||
|
||
// bitflags_serial | ||
let flag_bad_two = TestBadTWO::ONE | TestBadTWO::TWO; | ||
|
||
let json_ser_bad_two = serde_json::ser::to_string(&flag_bad_two).unwrap(); | ||
let ron_ser_bad_two = ron::ser::to_string(&flag_bad_two).unwrap(); | ||
|
||
assert_eq!(json_ser_bad_two, "[\"ONE\",\"TWO\"]"); | ||
assert_eq!(ron_ser_bad_two, "[ONE,TWO]"); | ||
|
||
let json_de_bad_two: TestBadTWO = serde_json::de::from_str(json_ser_bad_two.as_str()).unwrap(); | ||
let ron_de_bad_two: TestBadTWO = ron::de::from_str(ron_ser_bad_two.as_str()).unwrap(); | ||
|
||
assert_eq!(json_de_bad_two, flag_bad_two); | ||
assert_eq!(ron_de_bad_two, flag_bad_two); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#[test] | ||
fn test_deserialize_seed() { | ||
// Test adapted from David Tolnay's serde-yaml: | ||
// https://github.com/dtolnay/serde-yaml/blob/8a806e316302fd2e6541dccee6d166dd51b689d6/tests/test_de.rs#L357-L392 | ||
|
||
struct Seed(i64); | ||
|
||
impl<'de> serde::de::DeserializeSeed<'de> for Seed { | ||
type Value = i64; | ||
|
||
fn deserialize<D>(self, deserializer: D) -> Result<i64, D::Error> | ||
where | ||
D: serde::de::Deserializer<'de>, | ||
{ | ||
struct Visitor(i64); | ||
|
||
impl<'de> serde::de::Visitor<'de> for Visitor { | ||
type Value = i64; | ||
|
||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
write!(formatter, "an integer") | ||
} | ||
|
||
fn visit_i64<E: serde::de::Error>(self, v: i64) -> Result<i64, E> { | ||
Ok(v * self.0) | ||
} | ||
|
||
fn visit_u64<E: serde::de::Error>(self, v: u64) -> Result<i64, E> { | ||
Ok(v as i64 * self.0) | ||
} | ||
} | ||
|
||
deserializer.deserialize_any(Visitor(self.0)) | ||
} | ||
} | ||
|
||
let cases = [("3", 5, 15), ("6", 7, 42), ("-5", 9, -45)]; | ||
|
||
for &(ron, seed, expected) in &cases { | ||
let deserialized = ron::Options::default() | ||
.from_str_seed(ron, Seed(seed)) | ||
.unwrap(); | ||
|
||
assert_eq!(expected, deserialized); | ||
} | ||
} |