Skip to content

Commit 7640b04

Browse files
authored
Expand Structable/Enumerable tests; fix Structable Debug impl (#7)
1 parent 7a38168 commit 7640b04

File tree

3 files changed

+114
-28
lines changed

3 files changed

+114
-28
lines changed

tests/tests/enumerable.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use valuable::field::*;
22
use valuable::*;
33

44
#[test]
5-
fn test_manual_impl() {
5+
fn test_manual_static_impl() {
66
enum Enum {
77
Struct { x: &'static str },
88
Tuple(u8),
@@ -52,3 +52,48 @@ fn test_manual_impl() {
5252
let v = Enum::Unit;
5353
assert_eq!(format!("{:?}", v.as_value()), r#"Enum::Unit"#);
5454
}
55+
56+
#[test]
57+
#[ignore]
58+
fn test_manual_dyn_impl() {
59+
todo!();
60+
}
61+
62+
#[test]
63+
fn test_variant_named_field() {
64+
let name = "my_field".to_string();
65+
let fields = [NamedField::new(&name[..])];
66+
let variant = VariantDef::new("Hello", Fields::Named(&fields[..]), false);
67+
68+
assert_eq!(variant.name(), "Hello");
69+
assert!(!variant.is_dynamic());
70+
71+
match *variant.fields() {
72+
Fields::Named(f) => {
73+
assert!(std::ptr::eq((&fields[..]).as_ptr(), f.as_ptr(),));
74+
}
75+
_ => panic!(),
76+
}
77+
}
78+
79+
#[test]
80+
fn test_variant_unnamed_field() {
81+
let variant = VariantDef::new("Hello", Fields::Unnamed, false);
82+
83+
assert_eq!(variant.name(), "Hello");
84+
assert!(!variant.is_dynamic());
85+
assert!(matches!(variant.fields(), Fields::Unnamed));
86+
}
87+
88+
#[test]
89+
fn test_enum_def() {
90+
let fields = [NamedField::new("foo")];
91+
let a = VariantDef::new("A", Fields::Named(&fields[..]), false);
92+
let b = VariantDef::new("B", Fields::Unnamed, false);
93+
let variants = [a, b];
94+
let def = EnumDef::new("Foo", &variants, false);
95+
96+
assert_eq!(def.name(), "Foo");
97+
assert!(std::ptr::eq(variants.as_ptr(), def.variants().as_ptr(),));
98+
assert!(!def.is_dynamic());
99+
}

tests/tests/structable.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,41 @@ fn test_manual_static_impl() {
7575
);
7676
}
7777

78+
#[test]
79+
fn test_manual_dyn_impl() {
80+
struct MyStruct;
81+
82+
impl Valuable for MyStruct {
83+
fn as_value(&self) -> Value<'_> {
84+
Value::Structable(self)
85+
}
86+
87+
fn visit(&self, visit: &mut dyn Visit) {
88+
visit.visit_named_fields(&NamedValues::new(
89+
&[NamedField::new("foo")],
90+
&[Value::U32(1)],
91+
));
92+
visit.visit_named_fields(&NamedValues::new(
93+
&[NamedField::new("bar")],
94+
&[Value::String("two")],
95+
));
96+
}
97+
}
98+
99+
impl Structable for MyStruct {
100+
fn definition(&self) -> StructDef<'_> {
101+
StructDef::new("MyStruct", Fields::NamedStatic(&[]), true)
102+
}
103+
}
104+
105+
let my_struct = MyStruct;
106+
107+
assert_eq!(
108+
format!("{:?}", my_struct.as_value()),
109+
"MyStruct { foo: 1, bar: \"two\" }"
110+
);
111+
}
112+
78113
#[test]
79114
fn test_named_field() {
80115
let name = "hello".to_string();

valuable/src/structable.rs

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,49 @@ pub struct StructDef<'a> {
2020

2121
impl fmt::Debug for dyn Structable + '_ {
2222
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
23-
struct DebugStruct<'a, 'b> {
24-
name: &'b str,
25-
fmt: &'b mut fmt::Formatter<'a>,
26-
res: fmt::Result,
27-
}
23+
let def = self.definition();
24+
25+
if def.fields().is_named() {
26+
struct DebugStruct<'a, 'b> {
27+
fmt: fmt::DebugStruct<'a, 'b>,
28+
}
2829

29-
impl Visit for DebugStruct<'_, '_> {
30-
fn visit_named_fields(&mut self, record: &NamedValues<'_>) {
31-
let mut d = self.fmt.debug_struct(self.name);
30+
let mut debug = DebugStruct {
31+
fmt: fmt.debug_struct(def.name()),
32+
};
3233

33-
for (field, value) in record.entries() {
34-
d.field(field.name(), value);
34+
impl Visit for DebugStruct<'_, '_> {
35+
fn visit_named_fields(&mut self, named_values: &NamedValues<'_>) {
36+
for (field, value) in named_values.entries() {
37+
self.fmt.field(field.name(), value);
38+
}
3539
}
40+
}
41+
42+
self.visit(&mut debug);
3643

37-
self.res = d.finish();
44+
debug.fmt.finish()
45+
} else {
46+
struct DebugStruct<'a, 'b> {
47+
fmt: fmt::DebugTuple<'a, 'b>,
3848
}
3949

40-
fn visit_unnamed_fields(&mut self, values: &[Value<'_>]) {
41-
let mut d = self.fmt.debug_tuple(self.name);
50+
let mut debug = DebugStruct {
51+
fmt: fmt.debug_tuple(def.name()),
52+
};
4253

43-
for value in values {
44-
d.field(value);
54+
impl Visit for DebugStruct<'_, '_> {
55+
fn visit_unnamed_fields(&mut self, values: &[Value<'_>]) {
56+
for value in values {
57+
self.fmt.field(value);
58+
}
4559
}
46-
47-
self.res = d.finish();
4860
}
49-
}
5061

51-
let def = self.definition();
52-
let mut visit = DebugStruct {
53-
name: def.name(),
54-
fmt,
55-
res: Ok(()),
56-
};
57-
58-
self.visit(&mut visit);
59-
visit.res
62+
self.visit(&mut debug);
63+
64+
debug.fmt.finish()
65+
}
6066
}
6167
}
6268

0 commit comments

Comments
 (0)