-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathdata_tree_reflection.rs
More file actions
168 lines (160 loc) · 5.03 KB
/
data_tree_reflection.rs
File metadata and controls
168 lines (160 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use parallel_disk_usage::{
data_tree::{reflection::ConversionError, DataTree, Reflection},
size::Bytes,
};
use pretty_assertions::assert_eq;
use std::mem::transmute;
type SampleName = &'static str;
type SampleData = Bytes;
type SampleReflection = Reflection<SampleName, SampleData>;
type SampleTree = DataTree<SampleName, SampleData>;
fn valid_reflection() -> SampleReflection {
Reflection {
name: "root",
size: Bytes::new(7853),
children: vec![
Reflection {
name: "a",
size: Bytes::new(78),
children: Vec::new(),
},
Reflection {
name: "b",
size: Bytes::new(321),
children: vec![Reflection {
name: "0",
size: Bytes::new(321),
children: Vec::new(),
}],
},
Reflection {
name: "c",
size: Bytes::new(3456),
children: vec![
Reflection {
name: "0",
size: Bytes::new(732),
children: Vec::new(),
},
Reflection {
name: "1",
size: Bytes::new(352),
children: Vec::new(),
},
],
},
],
}
}
fn invalid_reflection_excessive_children() -> SampleReflection {
Reflection {
name: "root",
size: Bytes::new(2468),
children: vec![
Reflection {
name: "a",
size: Bytes::new(78),
children: Vec::new(),
},
Reflection {
name: "b",
size: Bytes::new(321),
children: vec![Reflection {
name: "0",
size: Bytes::new(321),
children: vec![
Reflection {
name: "abc",
size: Bytes::new(123),
children: vec![Reflection {
name: "xyz",
size: Bytes::new(4321),
children: Vec::new(),
}],
},
Reflection {
name: "def",
size: Bytes::new(456),
children: Vec::new(),
},
],
}],
},
Reflection {
name: "c",
size: Bytes::new(1084),
children: vec![
Reflection {
name: "0",
size: Bytes::new(732),
children: Vec::new(),
},
Reflection {
name: "1",
size: Bytes::new(352),
children: Vec::new(),
},
],
},
],
}
}
#[test]
fn valid_conversion() {
let actual = valid_reflection()
.par_try_into_tree()
.expect("create tree")
.into_reflection();
let expected = valid_reflection();
assert_eq!(actual, expected);
}
#[test]
fn invalid_conversion_excessive_children() {
let actual = invalid_reflection_excessive_children()
.par_try_into_tree()
.expect_err("create error");
let expected = ConversionError::ExcessiveChildren {
path: vec!["root", "b", "0"].into_iter().collect(),
size: Bytes::new(321),
child: Reflection {
name: "def",
size: Bytes::new(456),
children: Vec::new(),
},
};
assert_eq!(actual, expected);
}
#[test]
fn display_excessive_children() {
let actual = invalid_reflection_excessive_children()
.par_try_into_tree()
.expect_err("create error")
.to_string();
let expected = if cfg!(unix) {
r#"ExcessiveChildren: "root/b/0" (Bytes(321)) is less than a child named "def" (Bytes(456))"#
} else if cfg!(windows) {
// TODO: stop using debug format
r#"ExcessiveChildren: "root\\b\\0" (Bytes(321)) is less than a child named "def" (Bytes(456))"#
} else {
eprintln!("ACTUAL: {actual}");
panic!("This platform isn't supported!");
};
assert_eq!(actual, expected);
}
#[test]
fn transmute_tree_into_reflection() {
let valid_tree = valid_reflection()
.par_try_into_tree()
.expect("create valid tree");
let actual: SampleReflection = unsafe { transmute(valid_tree) };
let expected = valid_reflection();
assert_eq!(actual, expected);
}
#[test]
fn transmute_reflection_into_tree() {
let actual: SampleTree = unsafe { transmute(valid_reflection()) };
let expected = valid_reflection()
.par_try_into_tree()
.expect("create expected tree");
assert_eq!(actual, expected);
}