Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tifv committed Jul 10, 2024
1 parent 47fa8da commit 370bd49
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ flate2 = "=1.*"
serde = { version = "=1.*", features = ["derive"]}

[dev-dependencies]
const_format = "=0.2.*"
ron = { version = "=0.8.*" }
serde_json = { version = "=1.*" }

25 changes: 25 additions & 0 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,28 @@ impl<'de> de::EnumAccess<'de> for TableEnumDeserializer {
}
}

#[cfg(test)]
mod test {

use serde::Deserialize;

use crate::common::{
TransparentRef,
serde::OptionSerdeWrap,
};

use super::{Value, ValueDeserializer};

#[test]
fn test_value_de() {
let value1: Option<Value> =
ron::from_str::<OptionSerdeWrap<_>>(crate::test::RON_VALUE_1)
.unwrap().into_inner();
let value2 = Option::<Value>::deserialize(
ValueDeserializer::new(value1.clone())
).unwrap();
assert_eq!(value1, value2);
}

}

23 changes: 23 additions & 0 deletions src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,3 +566,26 @@ impl<F: ValueFinisher> ser::SerializeStructVariant for TableSerializer<F> {
}
}

#[cfg(test)]
mod test {

use serde::Serialize;

use crate::common::{
TransparentRef,
serde::OptionSerdeWrap,
};

use super::{Value, ValueSerializer};

#[test]
fn test_value_ser() {
let value1: Option<Value> =
ron::from_str::<OptionSerdeWrap<_>>(crate::test::RON_VALUE_1)
.unwrap().into_inner();
let value2 = value1.serialize(ValueSerializer::new()).unwrap();
assert_eq!(value1, value2);
}

}

29 changes: 29 additions & 0 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,32 @@ pub(crate) const EXCHANGE_BEHAVIOR_4_SUB: &str = "\
Hxvbh2TXeff0jq\
";

pub(crate) const RON_VALUE_1: &str = r#"{
"bool1" : true ,
"bool2" : false,
"int1" : 42,
"int2" : -42,
"int3" : 0,
"float1" : 42.0,
"float2" : -42.0,
"float3" : 0.0,
"string" : "string",
"array" : [1, 2, 3, 4],
"array_but_different" : {1: 1, 2: 2, 3: 3, 4: {}, 5: {}},
"map" : {"key" : "value"},
"mixed_table" : {1: 42, -1: -42, 0: 0, "key" : "value"},
}"#;

pub(crate) const RON_VALUE_1_COMPACT: &str = const_format::concatcp!(
"{",
r#""array":[1,2,3,4],"#,
r#""array_but_different":[1,2,3,{},{}],"#,
r#""bool1":true,"bool2":false,"#,
r#""float1":42.0,"float2":-42.0,"float3":0.0,"#,
r#""int1":42,"int2":-42,"int3":0,"#,
r#""map":{"key":"value"},"#,
r#""mixed_table":{-1:-42,0:0,1:42,"key":"value"},"#,
r#""string":"string""#,
"}"
);

25 changes: 24 additions & 1 deletion src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn err_key_from_value() -> crate::error::DumpError {
"only integers ans strings can serve as keys")
}

#[derive(Clone)]
#[derive(Clone, PartialEq)]
pub enum Value {
Boolean(bool),
Integer(i32),
Expand Down Expand Up @@ -452,3 +452,26 @@ common_serde::impl_flat_se_option!(Value);

}


#[cfg(test)]
mod test {

use crate::common::{
TransparentRef,
serde::{OptionSerdeWrap, OptionRefSerdeWrap},
};

use super::Value;

#[test]
fn test_value_serde() {
let value: Option<Value> =
ron::from_str::<OptionSerdeWrap<_>>(crate::test::RON_VALUE_1)
.unwrap().into_inner();
let ron_again = ron::to_string(
OptionRefSerdeWrap::from_ref(&value.as_ref()) ).unwrap();
assert_eq!(ron_again.as_str(), crate::test::RON_VALUE_1_COMPACT);
}

}

8 changes: 7 additions & 1 deletion src/value/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ use super::Key;

mod assoc;

#[derive(Clone, )]
#[derive(Clone)]
pub struct Table<V> {
items: Vec<(Key, V)>,
// the range of positive integer keys
indices: Range<usize>,
}

impl<V: PartialEq> PartialEq for Table<V> {
fn eq(&self, other: &Self) -> bool {
self.items == other.items
}
}

impl<V: std::fmt::Debug> std::fmt::Debug for Table<V> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut f = f.debug_map();
Expand Down

0 comments on commit 370bd49

Please sign in to comment.