Skip to content

Commit c39032b

Browse files
committed
test fmt::Debug
1 parent d410764 commit c39032b

File tree

3 files changed

+89
-16
lines changed

3 files changed

+89
-16
lines changed

tests/tests/listable.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ macro_rules! test_primitive {
184184
fn visit_slice(&mut self, slice: Slice<'_>) {
185185
assert_eq!(slice.len(), self.0.len());
186186

187+
// fmt::Debug
188+
assert_eq!(
189+
format!("{:?}", slice),
190+
format!("{:?}", self.0),
191+
);
192+
187193
// Test the expected variant has been received
188194
match slice {
189195
Slice::$variant(slice) => {

tests/tests/structable.rs

Lines changed: 77 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,80 @@
11
use valuable::field::*;
22
use valuable::*;
33

4+
#[test]
5+
fn test_manual_static_impl() {
6+
struct MyStruct {
7+
num: u32,
8+
list: Vec<String>,
9+
sub: SubStruct,
10+
}
11+
12+
static MY_STRUCT_FIELDS: &[NamedField<'static>] = &[
13+
NamedField::new("num"),
14+
NamedField::new("list"),
15+
NamedField::new("sub"),
16+
];
17+
18+
struct SubStruct {
19+
message: &'static str,
20+
}
21+
22+
static SUB_STRUCT_FIELDS: &[NamedField<'static>] = &[NamedField::new("message")];
23+
24+
impl Valuable for MyStruct {
25+
fn as_value(&self) -> Value<'_> {
26+
Value::Structable(self)
27+
}
28+
29+
fn visit(&self, visit: &mut dyn Visit) {
30+
visit.visit_named_fields(&NamedValues::new(
31+
MY_STRUCT_FIELDS,
32+
&[
33+
Value::U32(self.num),
34+
Value::Listable(&self.list),
35+
Value::Structable(&self.sub),
36+
],
37+
));
38+
}
39+
}
40+
41+
impl Structable for MyStruct {
42+
fn definition(&self) -> StructDef<'_> {
43+
StructDef::new("MyStruct", Fields::NamedStatic(MY_STRUCT_FIELDS), false)
44+
}
45+
}
46+
47+
impl Valuable for SubStruct {
48+
fn as_value(&self) -> Value<'_> {
49+
Value::Structable(self)
50+
}
51+
52+
fn visit(&self, visit: &mut dyn Visit) {
53+
visit.visit_named_fields(&NamedValues::new(
54+
SUB_STRUCT_FIELDS,
55+
&[Value::String(self.message)],
56+
));
57+
}
58+
}
59+
60+
impl Structable for SubStruct {
61+
fn definition(&self) -> StructDef<'_> {
62+
StructDef::new("SubStruct", Fields::NamedStatic(SUB_STRUCT_FIELDS), false)
63+
}
64+
}
65+
66+
let my_struct = MyStruct {
67+
num: 12,
68+
list: vec!["hello".to_string()],
69+
sub: SubStruct { message: "world" },
70+
};
71+
72+
assert_eq!(
73+
format!("{:?}", my_struct.as_value()),
74+
"MyStruct { num: 12, list: [\"hello\"], sub: SubStruct { message: \"world\" } }"
75+
);
76+
}
77+
478
#[test]
579
fn test_named_field() {
680
let name = "hello".to_string();
@@ -42,11 +116,7 @@ fn test_fields_unnamed() {
42116

43117
#[test]
44118
fn test_struct_def() {
45-
let def = StructDef::new(
46-
"hello",
47-
Fields::Unnamed,
48-
false,
49-
);
119+
let def = StructDef::new("hello", Fields::Unnamed, false);
50120

51121
assert_eq!(def.name(), "hello");
52122
assert!(matches!(def.fields(), Fields::Unnamed));
@@ -55,18 +125,9 @@ fn test_struct_def() {
55125

56126
#[test]
57127
fn test_named_values() {
58-
let fields = [
59-
NamedField::new("foo"),
60-
NamedField::new("bar"),
61-
];
128+
let fields = [NamedField::new("foo"), NamedField::new("bar")];
62129

63-
let vals = NamedValues::new(
64-
&fields[..],
65-
&[
66-
Value::U32(123),
67-
Value::String("hello"),
68-
]
69-
);
130+
let vals = NamedValues::new(&fields[..], &[Value::U32(123), Value::String("hello")]);
70131

71132
let other_field = NamedField::new("foo");
72133

tests/tests/value.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ macro_rules! assert_value {
6060
let mut visit = VisitValue(src, std::marker::PhantomData);
6161
val.visit(&mut visit);
6262

63+
// fmt::Debug
64+
assert_eq!(
65+
format!("{:?}", val),
66+
format!("{:?}", src),
67+
);
68+
6369
// Test conversion
6470
assert!(matches!(val, $variant(v) if $eq(&v, &src)));
6571

0 commit comments

Comments
 (0)