tawasuyu

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 501HTML 254C 19JavaScript 81414 Sutichaykuna

⎇ 494b1b4fdc7337e4c08960ac95bce1c366e02aeb tests/lib.rs Rust · 636 siq'ikuna · 21.4 KB Ñawpa willay Pi imata llamkarqan ch'uya

Add more tests bc2a1a531 · Emil Sjölander ·

1#[cfg(test)]
2mod tests {
3    #[test]
4    fn test_1() {
5        let layout = stretch::compute(&stretch::style::Node {
6            width: stretch::style::Dimension::Points(100.0),
7            height: stretch::style::Dimension::Points(100.0),
8            ..Default::default()
9        });
10
11        assert_eq!(layout.width, 100.0);
12        assert_eq!(layout.height, 100.0);
13    }
14
15    #[test]
16    fn test_2() {
17        let layout = stretch::compute(&stretch::style::Node {
18            width: stretch::style::Dimension::Points(100.0),
19            height: stretch::style::Dimension::Points(100.0),
20
21            children: vec![
22                stretch::style::Node {
23                    width: stretch::style::Dimension::Percent(0.5),
24
25                    ..Default::default()
26                },
27            ],
28          
29            ..Default::default()
30        });
31
32        assert_eq!(layout.children[0].width, 50.0);
33        assert_eq!(layout.children[0].height, 100.0);
34    }
35
36    #[test]
37    fn test_3() {
38        let layout = stretch::compute(&stretch::style::Node {
39            width: stretch::style::Dimension::Points(100.0),
40            height: stretch::style::Dimension::Points(100.0),
41
42            children: vec![
43                stretch::style::Node {
44                    width: stretch::style::Dimension::Percent(0.5),
45
46                    ..Default::default()
47                },
48                stretch::style::Node {
49                    width: stretch::style::Dimension::Percent(0.5),
50
51                    ..Default::default()
52                },
53            ],
54          
55            ..Default::default()
56        });
57
58        assert_eq!(layout.children[0].width, 50.0);
59        assert_eq!(layout.children[0].height, 100.0);
60        assert_eq!(layout.children[0].x, 0.0);
61        assert_eq!(layout.children[0].y, 0.0);
62
63        assert_eq!(layout.children[1].width, 50.0);
64        assert_eq!(layout.children[1].height, 100.0);
65        assert_eq!(layout.children[1].x, 50.0);
66        assert_eq!(layout.children[1].y, 0.0);
67    }
68
69    #[test]
70    fn test_4() {
71        let layout = stretch::compute(&stretch::style::Node {
72            width: stretch::style::Dimension::Points(100.0),
73            height: stretch::style::Dimension::Points(100.0),
74
75            children: vec![
76                stretch::style::Node {
77                    width: stretch::style::Dimension::Points(20.0),
78                    flex_grow: 1.0,
79
80                    ..Default::default()
81                },
82                stretch::style::Node {
83                    width: stretch::style::Dimension::Points(20.0),
84
85                    ..Default::default()
86                },
87            ],
88          
89            ..Default::default()
90        });
91
92        assert_eq!(layout.children[0].width, 80.0);
93        assert_eq!(layout.children[0].height, 100.0);
94        assert_eq!(layout.children[0].x, 0.0);
95        assert_eq!(layout.children[0].y, 0.0);
96
97        assert_eq!(layout.children[1].width, 20.0);
98        assert_eq!(layout.children[1].height, 100.0);
99        assert_eq!(layout.children[1].x, 80.0);
100        assert_eq!(layout.children[1].y, 0.0);
101    }
102
103    #[test]
104    fn test_5() {
105        let layout = stretch::compute(&stretch::style::Node {
106            width: stretch::style::Dimension::Points(100.0),
107            height: stretch::style::Dimension::Points(100.0),
108
109            children: vec![
110                stretch::style::Node {
111                    width: stretch::style::Dimension::Points(100.0),
112                    flex_shrink: 1.0,
113
114                    ..Default::default()
115                },
116                stretch::style::Node {
117                    width: stretch::style::Dimension::Points(100.0),
118                    flex_shrink: 1.0,
119
120                    ..Default::default()
121                },
122            ],
123          
124            ..Default::default()
125        });
126
127        assert_eq!(layout.children[0].width, 50.0);
128        assert_eq!(layout.children[0].height, 100.0);
129        assert_eq!(layout.children[0].x, 0.0);
130        assert_eq!(layout.children[0].y, 0.0);
131
132        assert_eq!(layout.children[1].width, 50.0);
133        assert_eq!(layout.children[1].height, 100.0);
134        assert_eq!(layout.children[1].x, 50.0);
135        assert_eq!(layout.children[1].y, 0.0);
136    }
137
138    #[test]
139    fn test_6() {
140        let layout = stretch::compute(&stretch::style::Node {
141            width: stretch::style::Dimension::Points(100.0),
142            height: stretch::style::Dimension::Points(100.0),
143
144            children: vec![
145                stretch::style::Node {
146                    width: stretch::style::Dimension::Points(50.0),
147
148                    ..Default::default()
149                },
150                stretch::style::Node {
151                    width: stretch::style::Dimension::Points(20.0),
152                    margin: stretch::style::Edges {
153                        start: stretch::style::Dimension::Points(30.0),
154                        ..Default::default()
155                    },
156
157                    ..Default::default()
158                },
159            ],
160          
161            ..Default::default()
162        });
163
164        assert_eq!(layout.children[0].width, 50.0);
165        assert_eq!(layout.children[0].height, 100.0);
166        assert_eq!(layout.children[0].x, 0.0);
167        assert_eq!(layout.children[0].y, 0.0);
168
169        assert_eq!(layout.children[1].width, 20.0);
170        assert_eq!(layout.children[1].height, 100.0);
171        assert_eq!(layout.children[1].x, 80.0);
172        assert_eq!(layout.children[1].y, 0.0);
173    }
174
175    #[test]
176    fn test_7() {
177        let layout = stretch::compute(&stretch::style::Node {
178            width: stretch::style::Dimension::Points(100.0),
179            height: stretch::style::Dimension::Points(100.0),
180            padding: stretch::style::Edges {
181                start: stretch::style::Dimension::Points(10.0),
182                end: stretch::style::Dimension::Points(10.0),
183                top: stretch::style::Dimension::Points(10.0),
184                bottom: stretch::style::Dimension::Points(10.0),
185            },
186
187            children: vec![
188                stretch::style::Node {
189                    width: stretch::style::Dimension::Points(40.0),
190
191                    ..Default::default()
192                },
193                stretch::style::Node {
194                    width: stretch::style::Dimension::Points(40.0),
195
196                    ..Default::default()
197                },
198            ],
199          
200            ..Default::default()
201        });
202
203        assert_eq!(layout.children[0].width, 40.0);
204        assert_eq!(layout.children[0].height, 80.0);
205        assert_eq!(layout.children[0].x, 10.0);
206        assert_eq!(layout.children[0].y, 10.0);
207
208        assert_eq!(layout.children[1].width, 40.0);
209        assert_eq!(layout.children[1].height, 80.0);
210        assert_eq!(layout.children[1].x, 50.0);
211        assert_eq!(layout.children[1].y, 10.0);
212    }
213
214    #[test]
215    fn test_8() {
216        let layout = stretch::compute(&stretch::style::Node {
217            width: stretch::style::Dimension::Points(100.0),
218            height: stretch::style::Dimension::Points(100.0),
219            border: stretch::style::Edges {
220                start: stretch::style::Dimension::Points(10.0),
221                end: stretch::style::Dimension::Points(10.0),
222                top: stretch::style::Dimension::Points(10.0),
223                bottom: stretch::style::Dimension::Points(10.0),
224            },
225
226            children: vec![
227                stretch::style::Node {
228                    width: stretch::style::Dimension::Points(40.0),
229
230                    ..Default::default()
231                },
232                stretch::style::Node {
233                    width: stretch::style::Dimension::Points(40.0),
234
235                    ..Default::default()
236                },
237            ],
238          
239            ..Default::default()
240        });
241
242        assert_eq!(layout.children[0].width, 40.0);
243        assert_eq!(layout.children[0].height, 80.0);
244        assert_eq!(layout.children[0].x, 10.0);
245        assert_eq!(layout.children[0].y, 10.0);
246
247        assert_eq!(layout.children[1].width, 40.0);
248        assert_eq!(layout.children[1].height, 80.0);
249        assert_eq!(layout.children[1].x, 50.0);
250        assert_eq!(layout.children[1].y, 10.0);
251    }
252
253    #[test]
254    fn test_9() {
255        let layout = stretch::compute(&stretch::style::Node {
256            width: stretch::style::Dimension::Points(100.0),
257            height: stretch::style::Dimension::Points(100.0),
258            justify_content: stretch::style::JustifyContent::Center,
259
260            children: vec![
261                stretch::style::Node {
262                    width: stretch::style::Dimension::Points(20.0),
263                    height: stretch::style::Dimension::Points(20.0),
264
265                    ..Default::default()
266                },
267                stretch::style::Node {
268                    width: stretch::style::Dimension::Points(20.0),
269                    height: stretch::style::Dimension::Points(20.0),
270
271                    ..Default::default()
272                },
273            ],
274          
275            ..Default::default()
276        });
277
278        assert_eq!(layout.children[0].width, 20.0);
279        assert_eq!(layout.children[0].height, 20.0);
280        assert_eq!(layout.children[0].x, 30.0);
281        assert_eq!(layout.children[0].y, 0.0);
282
283        assert_eq!(layout.children[1].width, 20.0);
284        assert_eq!(layout.children[1].height, 20.0);
285        assert_eq!(layout.children[1].x, 50.0);
286        assert_eq!(layout.children[1].y, 0.0);
287    }
288
289    #[test]
290    fn test_10() {
291        let layout = stretch::compute(&stretch::style::Node {
292            width: stretch::style::Dimension::Points(100.0),
293            height: stretch::style::Dimension::Points(100.0),
294            justify_content: stretch::style::JustifyContent::FlexEnd,
295
296            children: vec![
297                stretch::style::Node {
298                    width: stretch::style::Dimension::Points(20.0),
299                    height: stretch::style::Dimension::Points(20.0),
300
301                    ..Default::default()
302                },
303                stretch::style::Node {
304                    width: stretch::style::Dimension::Points(20.0),
305                    height: stretch::style::Dimension::Points(20.0),
306
307                    ..Default::default()
308                },
309            ],
310          
311            ..Default::default()
312        });
313
314        assert_eq!(layout.children[0].width, 20.0);
315        assert_eq!(layout.children[0].height, 20.0);
316        assert_eq!(layout.children[0].x, 60.0);
317        assert_eq!(layout.children[0].y, 0.0);
318
319        assert_eq!(layout.children[1].width, 20.0);
320        assert_eq!(layout.children[1].height, 20.0);
321        assert_eq!(layout.children[1].x, 80.0);
322        assert_eq!(layout.children[1].y, 0.0);
323    }
324
325    #[test]
326    fn test_11() {
327        let layout = stretch::compute(&stretch::style::Node {
328            width: stretch::style::Dimension::Points(100.0),
329            height: stretch::style::Dimension::Points(100.0),
330            justify_content: stretch::style::JustifyContent::SpaceBetween,
331
332            children: vec![
333                stretch::style::Node {
334                    width: stretch::style::Dimension::Points(20.0),
335                    height: stretch::style::Dimension::Points(20.0),
336
337                    ..Default::default()
338                },
339                stretch::style::Node {
340                    width: stretch::style::Dimension::Points(20.0),
341                    height: stretch::style::Dimension::Points(20.0),
342
343                    ..Default::default()
344                },
345            ],
346          
347            ..Default::default()
348        });
349
350        assert_eq!(layout.children[0].width, 20.0);
351        assert_eq!(layout.children[0].height, 20.0);
352        assert_eq!(layout.children[0].x, 0.0);
353        assert_eq!(layout.children[0].y, 0.0);
354
355        assert_eq!(layout.children[1].width, 20.0);
356        assert_eq!(layout.children[1].height, 20.0);
357        assert_eq!(layout.children[1].x, 80.0);
358        assert_eq!(layout.children[1].y, 0.0);
359    }
360
361    #[test]
362    fn test_12() {
363        let layout = stretch::compute(&stretch::style::Node {
364            width: stretch::style::Dimension::Points(100.0),
365            height: stretch::style::Dimension::Points(100.0),
366            align_items: stretch::style::AlignItems::FlexEnd,
367
368            children: vec![
369                stretch::style::Node {
370                    width: stretch::style::Dimension::Points(20.0),
371                    height: stretch::style::Dimension::Points(20.0),
372
373                    ..Default::default()
374                },
375                stretch::style::Node {
376                    width: stretch::style::Dimension::Points(20.0),
377                    height: stretch::style::Dimension::Points(20.0),
378
379                    ..Default::default()
380                },
381            ],
382          
383            ..Default::default()
384        });
385
386        assert_eq!(layout.children[0].width, 20.0);
387        assert_eq!(layout.children[0].height, 20.0);
388        assert_eq!(layout.children[0].x, 0.0);
389        assert_eq!(layout.children[0].y, 80.0);
390
391        assert_eq!(layout.children[1].width, 20.0);
392        assert_eq!(layout.children[1].height, 20.0);
393        assert_eq!(layout.children[1].x, 20.0);
394        assert_eq!(layout.children[1].y, 80.0);
395    }
396
397    #[test]
398    fn test_13() {
399        let layout = stretch::compute(&stretch::style::Node {
400            width: stretch::style::Dimension::Points(100.0),
401            height: stretch::style::Dimension::Points(100.0),
402            align_items: stretch::style::AlignItems::Center,
403
404            children: vec![
405                stretch::style::Node {
406                    width: stretch::style::Dimension::Points(20.0),
407                    height: stretch::style::Dimension::Points(20.0),
408
409                    ..Default::default()
410                },
411                stretch::style::Node {
412                    width: stretch::style::Dimension::Points(20.0),
413                    height: stretch::style::Dimension::Points(20.0),
414
415                    ..Default::default()
416                },
417            ],
418          
419            ..Default::default()
420        });
421
422        assert_eq!(layout.children[0].width, 20.0);
423        assert_eq!(layout.children[0].height, 20.0);
424        assert_eq!(layout.children[0].x, 0.0);
425        assert_eq!(layout.children[0].y, 40.0);
426
427        assert_eq!(layout.children[1].width, 20.0);
428        assert_eq!(layout.children[1].height, 20.0);
429        assert_eq!(layout.children[1].x, 20.0);
430        assert_eq!(layout.children[1].y, 40.0);
431    }
432
433    #[test]
434    fn test_14() {
435        let layout = stretch::compute(&stretch::style::Node {
436            width: stretch::style::Dimension::Points(100.0),
437            height: stretch::style::Dimension::Points(100.0),
438            align_items: stretch::style::AlignItems::Center,
439
440            children: vec![
441                stretch::style::Node {
442                    width: stretch::style::Dimension::Points(20.0),
443                    height: stretch::style::Dimension::Points(20.0),
444
445                    ..Default::default()
446                },
447                stretch::style::Node {
448                    width: stretch::style::Dimension::Points(20.0),
449                    height: stretch::style::Dimension::Points(20.0),
450                    align_self: stretch::style::AlignSelf::FlexStart,
451
452                    ..Default::default()
453                },
454            ],
455          
456            ..Default::default()
457        });
458
459        assert_eq!(layout.children[0].width, 20.0);
460        assert_eq!(layout.children[0].height, 20.0);
461        assert_eq!(layout.children[0].x, 0.0);
462        assert_eq!(layout.children[0].y, 40.0);
463
464        assert_eq!(layout.children[1].width, 20.0);
465        assert_eq!(layout.children[1].height, 20.0);
466        assert_eq!(layout.children[1].x, 20.0);
467        assert_eq!(layout.children[1].y, 0.0);
468    }
469
470    #[test]
471    fn test_15() {
472        let layout = stretch::compute(&stretch::style::Node {
473            width: stretch::style::Dimension::Points(100.0),
474
475            children: vec![
476                stretch::style::Node {
477                    width: stretch::style::Dimension::Points(40.0),
478                    height: stretch::style::Dimension::Points(40.0),
479
480                    ..Default::default()
481                },
482                stretch::style::Node {
483                    width: stretch::style::Dimension::Points(40.0),
484                    height: stretch::style::Dimension::Points(40.0),
485
486                    ..Default::default()
487                },
488            ],
489          
490            ..Default::default()
491        });
492
493        assert_eq!(layout.height, 40.0);
494
495        assert_eq!(layout.children[0].width, 40.0);
496        assert_eq!(layout.children[0].height, 40.0);
497        assert_eq!(layout.children[0].x, 0.0);
498        assert_eq!(layout.children[0].y, 0.0);
499
500        assert_eq!(layout.children[1].width, 40.0);
501        assert_eq!(layout.children[1].height, 40.0);
502        assert_eq!(layout.children[1].x, 40.0);
503        assert_eq!(layout.children[1].y, 0.0);
504    }
505
506    #[test]
507    fn test_16() {
508        let layout = stretch::compute(&stretch::style::Node {
509            width: stretch::style::Dimension::Points(100.0),
510            wrap: stretch::style::Wrap::Wrap,
511
512            children: vec![
513                stretch::style::Node {
514                    width: stretch::style::Dimension::Points(40.0),
515                    height: stretch::style::Dimension::Points(40.0),
516
517                    ..Default::default()
518                },
519                stretch::style::Node {
520                    width: stretch::style::Dimension::Points(40.0),
521                    height: stretch::style::Dimension::Points(40.0),
522
523                    ..Default::default()
524                },
525                stretch::style::Node {
526                    width: stretch::style::Dimension::Points(40.0),
527                    height: stretch::style::Dimension::Points(40.0),
528
529                    ..Default::default()
530                },
531                stretch::style::Node {
532                    width: stretch::style::Dimension::Points(40.0),
533                    height: stretch::style::Dimension::Points(40.0),
534
535                    ..Default::default()
536                },
537                stretch::style::Node {
538                    width: stretch::style::Dimension::Points(40.0),
539                    height: stretch::style::Dimension::Points(40.0),
540
541                    ..Default::default()
542                },
543            ],
544          
545            ..Default::default()
546        });
547
548        assert_eq!(layout.height, 120.0);
549
550        assert_eq!(layout.children[0].width, 40.0);
551        assert_eq!(layout.children[0].height, 40.0);
552        assert_eq!(layout.children[0].x, 0.0);
553        assert_eq!(layout.children[0].y, 0.0);
554
555        assert_eq!(layout.children[1].width, 40.0);
556        assert_eq!(layout.children[1].height, 40.0);
557        assert_eq!(layout.children[1].x, 40.0);
558        assert_eq!(layout.children[1].y, 0.0);
559
560        assert_eq!(layout.children[2].width, 40.0);
561        assert_eq!(layout.children[2].height, 40.0);
562        assert_eq!(layout.children[2].x, 0.0);
563        assert_eq!(layout.children[2].y, 40.0);
564
565        assert_eq!(layout.children[3].width, 40.0);
566        assert_eq!(layout.children[3].height, 40.0);
567        assert_eq!(layout.children[3].x, 40.0);
568        assert_eq!(layout.children[3].y, 40.0);
569
570        assert_eq!(layout.children[4].width, 40.0);
571        assert_eq!(layout.children[4].height, 40.0);
572        assert_eq!(layout.children[4].x, 0.0);
573        assert_eq!(layout.children[4].y, 80.0);
574    }
575
576    #[test]
577    fn test_17() {
578        let layout = stretch::compute(&stretch::style::Node {
579            width: stretch::style::Dimension::Points(100.0),
580            height: stretch::style::Dimension::Points(100.0),
581            align_content: stretch::style::AlignContent::Center,
582            wrap: stretch::style::Wrap::Wrap,
583
584            children: vec![
585                stretch::style::Node {
586                    width: stretch::style::Dimension::Points(40.0),
587                    height: stretch::style::Dimension::Points(40.0),
588
589                    ..Default::default()
590                },
591                stretch::style::Node {
592                    width: stretch::style::Dimension::Points(40.0),
593                    height: stretch::style::Dimension::Points(40.0),
594
595                    ..Default::default()
596                },
597                stretch::style::Node {
598                    width: stretch::style::Dimension::Points(40.0),
599                    height: stretch::style::Dimension::Points(40.0),
600
601                    ..Default::default()
602                },
603                stretch::style::Node {
604                    width: stretch::style::Dimension::Points(40.0),
605                    height: stretch::style::Dimension::Points(40.0),
606
607                    ..Default::default()
608                },
609            ],
610          
611            ..Default::default()
612        });
613
614        assert_eq!(layout.height, 100.0);
615
616        assert_eq!(layout.children[0].width, 40.0);
617        assert_eq!(layout.children[0].height, 40.0);
618        assert_eq!(layout.children[0].x, 0.0);
619        assert_eq!(layout.children[0].y, 10.0);
620
621        assert_eq!(layout.children[1].width, 40.0);
622        assert_eq!(layout.children[1].height, 40.0);
623        assert_eq!(layout.children[1].x, 40.0);
624        assert_eq!(layout.children[1].y, 10.0);
625
626        assert_eq!(layout.children[2].width, 40.0);
627        assert_eq!(layout.children[2].height, 40.0);
628        assert_eq!(layout.children[2].x, 0.0);
629        assert_eq!(layout.children[2].y, 50.0);
630
631        assert_eq!(layout.children[3].width, 40.0);
632        assert_eq!(layout.children[3].height, 40.0);
633        assert_eq!(layout.children[3].x, 40.0);
634        assert_eq!(layout.children[3].y, 50.0);
635    }
636}