Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/tests/string.rs

7.2 KiB, 11 runs

created by r1870400018:1123, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1use oxedyne_fe2o3_text::string::Stringer;
2
3use oxedyne_fe2o3_core::{
4 prelude::*,
5 test::test_it,
6};
7
8
9pub fn test_string(filter: &'static str) -> Outcome<()> {
10
11 res!(test_it(filter, &["To lines 000", "all", "string"], || {
12 let input = Stringer::new(r#"[1, "two", {3: [4, "five", 6]}, [7, 8, 9]]"#);
13 let expected =
14r#"[
15 1,
16 "two",
17 {
18 3: [
19 4,
20 "five",
21 6
22 ]
23 },
24 [
25 7,
26 8,
27 9
28 ]
29]"#;
30 let expected_lines: Vec<_> = expected.split('\n').collect();
31 //for line in &expected_lines {
32 // test!("{}", line);
33 //}
34 let lines = input.to_lines(" ");
35 req!(lines.len(), expected_lines.len());
36 for (i, line) in lines.iter().enumerate() {
37 debug!("{}", line);
38 req!(line, expected_lines[i]);
39 }
40 Ok(())
41 }));
42
43 res!(test_it(filter, &["To lines 010", "all", "string"], || {
44 let input = Stringer::new(r#"(LIST|[(I32|1), (STR|"two"), (OMAP|{(I32|3): (LIST|[(I32|4), (STR|"five"), (I32|6), ]), }), (LIST|[(I32|7), (I32|8), (I32|9), ]), ])"#);
45 let expected =
46r#"(LIST|[
47 (I32|1),
48 (STR|"two"),
49 (OMAP|{
50 (I32|3): (LIST|[
51 (I32|4),
52 (STR|"five"),
53 (I32|6),
54 ]),
55 }),
56 (LIST|[
57 (I32|7),
58 (I32|8),
59 (I32|9),
60 ]),
61])"#;
62 let expected_lines: Vec<_> = expected.split('\n').collect();
63 //for line in &expected_lines {
64 // test!("{}", line);
65 //}
66 let lines = input.to_lines(" ");
67 req!(lines.len(), expected_lines.len());
68 for (i, line) in lines.iter().enumerate() {
69 debug!("{}", line);
70 req!(line, expected_lines[i]);
71 }
72 Ok(())
73 }));
74
75 res!(test_it(filter, &["Wrap text 000", "all", "string", "wrap"], || {
76 let text =
77r#"He who has seen everything, I will make known (?) to the lands.
78I will teach (?) about him who experienced all things,
79... alike,
80Anu granted him the totality of knowledge of all.
81He saw the Secret, discovered the Hidden,
82he brought information of (the time) before the Flood.
83He went on a distant journey, pushing himself to exhaustion,
84but then was brought to peace.
85He carved on a stone stela all of his toils,
86and built the wall of Uruk-Haven,
87the wall of the sacred Eanna Temple, the holy sanctuary.
88Look at its wall which gleams like copper(?),
89inspect its inner wall, the likes of which no one can equal!
90Take hold of the threshold stone--it dates from ancient times!
91Go close to the Eanna Temple, the residence of Ishtar,
92such as no later king or man ever equaled!
93Go up on the wall of Uruk and walk around,
94examine its foundation, inspect its brickwork thoroughly.
95Is not (even the core of) the brick structure made of kiln-fired brick,
96and did not the Seven Sages themselves lay out its plans?
97One league city, one league palm gardens, one league lowlands, the open area(?) of the Ishtar Temple,
98three leagues and the open area(?) of Uruk it (the wall) encloses.
99Find the copper tablet box,
100open the ... of its lock of bronze,
101undo the fastening of its secret opening.
102Take and read out from the lapis lazuli tablet
103how Gilgamesh went through every hardship.
104"#;
105 let expected =
106r#"He who has seen
107 everything, I will
108 make known (?) to
109 the lands.
110I will teach (?)
111 about him who
112 experienced all
113 things,
114... alike,
115Anu granted him the
116 totality of
117 knowledge of all.
118He saw the Secret,
119 discovered the
120 Hidden,
121he brought
122 information of (the
123 time) before the
124 Flood.
125He went on a distant
126 journey, pushing
127 himself to
128 exhaustion,
129but then was brought
130 to peace.
131He carved on a stone
132 stela all of his
133 toils,
134and built the wall
135 of Uruk-Haven,
136the wall of the
137 sacred Eanna
138 Temple, the holy
139 sanctuary.
140Look at its wall
141 which gleams like
142 copper(?),
143inspect its inner
144 wall, the likes of
145 which no one can
146 equal!
147Take hold of the
148 threshold stone--it
149 dates from ancient
150 times!
151Go close to the
152 Eanna Temple, the
153 residence of
154 Ishtar,
155such as no later
156 king or man ever
157 equaled!
158Go up on the wall of
159 Uruk and walk
160 around,
161examine its
162 foundation, inspect
163 its brickwork
164 thoroughly.
165Is not (even the
166 core of) the brick
167 structure made of
168 kiln-fired brick,
169and did not the
170 Seven Sages
171 themselves lay out
172 its plans?
173One league city, one
174 league palm
175 gardens, one league
176 lowlands, the open
177 area(?) of the
178 Ishtar Temple,
179three leagues and
180 the open area(?) of
181 Uruk it (the wall)
182 encloses.
183Find the copper
184 tablet box,
185open the ... of its
186 lock of bronze,
187undo the fastening
188 of its secret
189 opening.
190Take and read out
191 from the lapis
192 lazuli tablet
193how Gilgamesh went
194 through every
195 hardship.
196"#;
197 let expected: Vec<String> = expected.lines().map(String::from).collect();
198 test!("Original text:");
199 for line in text.lines() {
200 test!("{}", line);
201 }
202 let width = 20; // > 1
203 let wrapped = Stringer::new(text).wrap_lines(width, Some(" "));
204 test!("Wrapped to width {}:", width);
205 test!("|{}|", "-".repeat(width - 2));
206 for (i, line) in wrapped.iter().enumerate() {
207 test!("{}", line);
208 let len = line.chars().count();
209 if len > width {
210 return Err(err!(
211 "'{}' is {} characters wide, more than the maximum {}.",
212 line, len, width;
213 Test, String, TooBig));
214 }
215 req!(expected[i], *line, "(L: expected, R: wrapped)");
216 }
217 test!("|{}|", "-".repeat(width - 2));
218 Ok(())
219 }));
220
221 res!(test_it(filter, &["Truncate string 000", "all", "string", "truncate"], || {
222 let tests = [
223 ("abcdefg", 9, "abcdefg"),
224 ("abcdefg", 8, "abcdefg"),
225 ("abcdefg", 7, "abcdefg"),
226 ("abcdefg", 6, "abc..."),
227 ("abcdefg", 5, "ab..."),
228 ("abcdefg", 4, "a..."),
229 ("abcdefg", 3, ""),
230 ("abcdefg", 2, ""),
231 ("abcdefg", 1, ""),
232 ];
233 for (input, new_len, expected) in tests {
234 let mut result = Stringer::new(input);
235 result.fit_into(new_len, "...");
236 req!(result, Stringer::new(expected), "(L: result, R: expected), new_len = {}", new_len);
237 }
238 Ok(())
239 }));
240
241 res!(test_it(filter, &["Tidy label 000", "all", "string", "label"], || {
242 let tests = [
243 (" Ningaloo ", 8, Some("Ningaloo")),
244 ("Two\nlines", 80, Some("Twolines")),
245 ("Rosie\tsmith", 80, Some("Rosiesmith")),
246 (" ", 80, None),
247 ("", 80, None),
248 ("\n\t\u{7}", 80, None),
249 // The bound is in characters, so a name in another script is cut
250 // where a reader would count and not where the bytes happen to fall.
251 ("Ολυμπία", 4, Some("Ολυμ")),
252 ("abcdef", 3, Some("abc")),
253 // A cut that lands on a space leaves no trailing one.
254 ("ab cdef", 3, Some("ab")),
255 ];
256 for (input, max, expected) in tests {
257 let got = Stringer::new(input).tidy_label(max);
258 req!(got, expected.map(|s| s.to_string()),
259 "(L: got, R: expected), input {:?} at {}", input, max);
260 }
261 Ok(())
262 }));
263
264 Ok(())
265}