Debian Codegraph @

tests/test_exporters.py

raw symbols SCIP
1 from __future__ import annotations
2
3 from testfixtures import compare
4
5 import pytermgui
6 from pytermgui import Color, DensePixelMatrix, str_to_color, tim
7 from pytermgui.term import Recorder, Terminal, terminal
8
9 try:
10 from ._exporter_targets import HTML_TARGET, SVG_TARGET
11 except ImportError:
12 from _exporter_targets import HTML_TARGET, SVG_TARGET
13
14 MATRIX = [
15 [
16 "142",
17 "96",
18 "139",
19 "32",
20 "179",
21 "191",
22 "214",
23 "127",
24 "211",
25 "25",
26 "93",
27 "61",
28 "60",
29 "103",
30 "109",
31 "150",
32 "36",
33 "165",
34 "188",
35 "103",
36 "55",
37 "167",
38 "212",
39 "52",
40 "151",
41 "114",
42 "213",
43 "65",
44 "58",
45 "101",
46 ],
47 [
48 "222",
49 "82",
50 "143",
51 "199",
52 "221",
53 "70",
54 "53",
55 "49",
56 "122",
57 "35",
58 "69",
59 "162",
60 "167",
61 "146",
62 "114",
63 "213",
64 "185",
65 "167",
66 "118",
67 "62",
68 "196",
69 "170",
70 "38",
71 "204",
72 "92",
73 "47",
74 "217",
75 "21",
76 "51",
77 "167",
78 ],
79 [
80 "178",
81 "83",
82 "168",
83 "211",
84 "18",
85 "95",
86 "222",
87 "52",
88 "205",
89 "172",
90 "82",
91 "147",
92 "181",
93 "162",
94 "89",
95 "226",
96 "136",
97 "92",
98 "30",
99 "84",
100 "66",
101 "183",
102 "229",
103 "100",
104 "160",
105 "82",
106 "218",
107 "223",
108 "32",
109 "153",
110 ],
111 [
112 "174",
113 "196",
114 "59",
115 "148",
116 "130",
117 "126",
118 "27",
119 "51",
120 "180",
121 "47",
122 "142",
123 "54",
124 "104",
125 "63",
126 "21",
127 "87",
128 "113",
129 "186",
130 "146",
131 "212",
132 "69",
133 "134",
134 "157",
135 "116",
136 "226",
137 "63",
138 "198",
139 "231",
140 "219",
141 "199",
142 ],
143 [
144 "178",
145 "65",
146 "39",
147 "188",
148 "226",
149 "75",
150 "159",
151 "42",
152 "143",
153 "154",
154 "228",
155 "179",
156 "104",
157 "85",
158 "128",
159 "48",
160 "195",
161 "183",
162 "43",
163 "22",
164 "191",
165 "55",
166 "190",
167 "86",
168 "31",
169 "139",
170 "228",
171 "55",
172 "117",
173 "182",
174 ],
175 [
176 "47",
177 "44",
178 "18",
179 "45",
180 "188",
181 "49",
182 "33",
183 "52",
184 "123",
185 "211",
186 "84",
187 "210",
188 "159",
189 "195",
190 "215",
191 "174",
192 "206",
193 "219",
194 "208",
195 "17",
196 "221",
197 "167",
198 "94",
199 "159",
200 "21",
201 "175",
202 "226",
203 "149",
204 "160",
205 "179",
206 ],
207 [
208 "110",
209 "53",
210 "120",
211 "139",
212 "129",
213 "122",
214 "189",
215 "72",
216 "205",
217 "150",
218 "149",
219 "199",
220 "44",
221 "60",
222 "85",
223 "33",
224 "145",
225 "207",
226 "37",
227 "88",
228 "53",
229 "70",
230 "139",
231 "115",
232 "208",
233 "185",
234 "80",
235 "161",
236 "142",
237 "45",
238 ],
239 [
240 "18",
241 "221",
242 "85",
243 "55",
244 "126",
245 "210",
246 "23",
247 "142",
248 "59",
249 "228",
250 "49",
251 "128",
252 "57",
253 "81",
254 "79",
255 "120",
256 "231",
257 "112",
258 "37",
259 "123",
260 "218",
261 "23",
262 "138",
263 "169",
264 "89",
265 "86",
266 "35",
267 "193",
268 "184",
269 "126",
270 ],
271 [
272 "198",
273 "154",
274 "157",
275 "129",
276 "199",
277 "71",
278 "109",
279 "217",
280 "186",
281 "91",
282 "107",
283 "125",
284 "36",
285 "156",
286 "218",
287 "30",
288 "216",
289 "218",
290 "106",
291 "127",
292 "175",
293 "95",
294 "61",
295 "174",
296 "124",
297 "200",
298 "52",
299 "72",
300 "49",
301 "29",
302 ],
303 [
304 "93",
305 "84",
306 "194",
307 "115",
308 "24",
309 "49",
310 "132",
311 "157",
312 "151",
313 "145",
314 "154",
315 "74",
316 "41",
317 "218",
318 "82",
319 "179",
320 "24",
321 "186",
322 "45",
323 "97",
324 "111",
325 "210",
326 "100",
327 "194",
328 "119",
329 "193",
330 "214",
331 "53",
332 "27",
333 "54",
334 ],
335 [
336 "18",
337 "178",
338 "171",
339 "228",
340 "168",
341 "157",
342 "131",
343 "150",
344 "71",
345 "222",
346 "88",
347 "220",
348 "119",
349 "187",
350 "160",
351 "151",
352 "119",
353 "94",
354 "121",
355 "204",
356 "66",
357 "88",
358 "73",
359 "115",
360 "102",
361 "115",
362 "190",
363 "200",
364 "52",
365 "75",
366 ],
367 [
368 "162",
369 "80",
370 "116",
371 "115",
372 "211",
373 "101",
374 "78",
375 "111",
376 "50",
377 "169",
378 "85",
379 "50",
380 "216",
381 "78",
382 "183",
383 "33",
384 "220",
385 "153",
386 "228",
387 "39",
388 "131",
389 "81",
390 "99",
391 "223",
392 "227",
393 "178",
394 "54",
395 "52",
396 "38",
397 "209",
398 ],
399 [
400 "127",
401 "42",
402 "124",
403 "106",
404 "142",
405 "190",
406 "22",
407 "232",
408 "89",
409 "217",
410 "192",
411 "213",
412 "89",
413 "96",
414 "53",
415 "113",
416 "168",
417 "119",
418 "118",
419 "59",
420 "83",
421 "184",
422 "77",
423 "112",
424 "60",
425 "179",
426 "193",
427 "170",
428 "146",
429 "148",
430 ],
431 [
432 "105",
433 "107",
434 "86",
435 "131",
436 "140",
437 "127",
438 "64",
439 "24",
440 "230",
441 "19",
442 "100",
443 "107",
444 "112",
445 "207",
446 "214",
447 "124",
448 "128",
449 "80",
450 "202",
451 "131",
452 "180",
453 "25",
454 "209",
455 "207",
456 "110",
457 "130",
458 "68",
459 "19",
460 "231",
461 "128",
462 ],
463 [
464 "95",
465 "75",
466 "83",
467 "179",
468 "187",
469 "94",
470 "73",
471 "200",
472 "194",
473 "224",
474 "195",
475 "226",
476 "61",
477 "168",
478 "128",
479 "65",
480 "123",
481 "92",
482 "183",
483 "160",
484 "39",
485 "227",
486 "70",
487 "37",
488 "158",
489 "221",
490 "152",
491 "90",
492 "177",
493 "224",
494 ],
495 [
496 "215",
497 "125",
498 "189",
499 "226",
500 "30",
501 "81",
502 "198",
503 "232",
504 "17",
505 "35",
506 "37",
507 "180",
508 "116",
509 "160",
510 "113",
511 "175",
512 "29",
513 "76",
514 "33",
515 "46",
516 "175",
517 "26",
518 "178",
519 "48",
520 "66",
521 "64",
522 "30",
523 "211",
524 "123",
525 "215",
526 ],
527 [
528 "205",
529 "178",
530 "127",
531 "123",
532 "112",
533 "209",
534 "141",
535 "19",
536 "94",
537 "218",
538 "170",
539 "71",
540 "167",
541 "59",
542 "131",
543 "191",
544 "147",
545 "52",
546 "22",
547 "194",
548 "150",
549 "26",
550 "95",
551 "34",
552 "212",
553 "102",
554 "121",
555 "66",
556 "132",
557 "29",
558 ],
559 [
560 "189",
561 "165",
562 "224",
563 "180",
564 "133",
565 "43",
566 "204",
567 "83",
568 "78",
569 "84",
570 "99",
571 "18",
572 "212",
573 "144",
574 "120",
575 "130",
576 "112",
577 "47",
578 "103",
579 "103",
580 "97",
581 "50",
582 "143",
583 "110",
584 "96",
585 "72",
586 "83",
587 "130",
588 "111",
589 "194",
590 ],
591 [
592 "180",
593 "96",
594 "63",
595 "91",
596 "52",
597 "229",
598 "104",
599 "17",
600 "195",
601 "213",
602 "18",
603 "74",
604 "188",
605 "74",
606 "69",
607 "210",
608 "204",
609 "183",
610 "166",
611 "71",
612 "41",
613 "159",
614 "232",
615 "141",
616 "45",
617 "135",
618 "161",
619 "197",
620 "100",
621 "228",
622 ],
623 [
624 "77",
625 "162",
626 "92",
627 "65",
628 "51",
629 "186",
630 "68",
631 "80",
632 "184",
633 "145",
634 "217",
635 "126",
636 "103",
637 "110",
638 "24",
639 "25",
640 "48",
641 "80",
642 "74",
643 "212",
644 "88",
645 "150",
646 "186",
647 "41",
648 "48",
649 "24",
650 "194",
651 "217",
652 "96",
653 "61",
654 ],
655 [
656 "170",
657 "119",
658 "151",
659 "80",
660 "157",
661 "77",
662 "129",
663 "183",
664 "43",
665 "162",
666 "167",
667 "226",
668 "67",
669 "77",
670 "117",
671 "132",
672 "97",
673 "150",
674 "94",
675 "84",
676 "45",
677 "66",
678 "222",
679 "47",
680 "213",
681 "117",
682 "198",
683 "119",
684 "181",
685 "72",
686 ],
687 [
688 "20",
689 "141",
690 "142",
691 "228",
692 "169",
693 "39",
694 "151",
695 "21",
696 "117",
697 "42",
698 "42",
699 "110",
700 "175",
701 "202",
702 "232",
703 "210",
704 "129",
705 "57",
706 "78",
707 "88",
708 "227",
709 "55",
710 "76",
711 "35",
712 "223",
713 "111",
714 "96",
715 "82",
716 "47",
717 "224",
718 ],
719 [
720 "190",
721 "134",
722 "133",
723 "193",
724 "220",
725 "174",
726 "157",
727 "200",
728 "48",
729 "231",
730 "157",
731 "226",
732 "155",
733 "128",
734 "144",
735 "231",
736 "44",
737 "73",
738 "79",
739 "211",
740 "144",
741 "29",
742 "44",
743 "45",
744 "178",
745 "213",
746 "122",
747 "96",
748 "230",
749 "90",
750 ],
751 [
752 "73",
753 "113",
754 "117",
755 "141",
756 "22",
757 "220",
758 "135",
759 "83",
760 "185",
761 "210",
762 "26",
763 "74",
764 "67",
765 "227",
766 "139",
767 "210",
768 "232",
769 "90",
770 "192",
771 "223",
772 "194",
773 "142",
774 "117",
775 "209",
776 "184",
777 "59",
778 "219",
779 "59",
780 "145",
781 "214",
782 ],
783 [
784 "101",
785 "216",
786 "81",
787 "212",
788 "17",
789 "194",
790 "95",
791 "137",
792 "86",
793 "207",
794 "221",
795 "144",
796 "199",
797 "45",
798 "205",
799 "127",
800 "141",
801 "97",
802 "216",
803 "121",
804 "224",
805 "136",
806 "39",
807 "142",
808 "198",
809 "102",
810 "181",
811 "218",
812 "62",
813 "61",
814 ],
815 [
816 "134",
817 "192",
818 "73",
819 "39",
820 "169",
821 "126",
822 "80",
823 "19",
824 "191",
825 "184",
826 "221",
827 "141",
828 "172",
829 "224",
830 "183",
831 "219",
832 "21",
833 "181",
834 "45",
835 "191",
836 "132",
837 "171",
838 "203",
839 "155",
840 "57",
841 "141",
842 "98",
843 "36",
844 "61",
845 "63",
846 ],
847 [
848 "68",
849 "174",
850 "172",
851 "192",
852 "138",
853 "84",
854 "137",
855 "175",
856 "109",
857 "88",
858 "103",
859 "162",
860 "69",
861 "155",
862 "183",
863 "20",
864 "93",
865 "77",
866 "85",
867 "63",
868 "191",
869 "18",
870 "93",
871 "61",
872 "38",
873 "213",
874 "34",
875 "56",
876 "86",
877 "55",
878 ],
879 [
880 "114",
881 "203",
882 "194",
883 "25",
884 "94",
885 "97",
886 "51",
887 "33",
888 "199",
889 "170",
890 "57",
891 "163",
892 "215",
893 "128",
894 "27",
895 "153",
896 "105",
897 "211",
898 "168",
899 "232",
900 "28",
901 "146",
902 "193",
903 "70",
904 "180",
905 "61",
906 "54",
907 "166",
908 "188",
909 "42",
910 ],
911 [
912 "199",
913 "232",
914 "138",
915 "74",
916 "187",
917 "211",
918 "111",
919 "133",
920 "138",
921 "37",
922 "221",
923 "137",
924 "39",
925 "66",
926 "48",
927 "214",
928 "208",
929 "209",
930 "115",
931 "117",
932 "202",
933 "120",
934 "24",
935 "189",
936 "137",
937 "151",
938 "170",
939 "222",
940 "56",
941 "117",
942 ],
943 [
944 "58",
945 "194",
946 "165",
947 "191",
948 "36",
949 "171",
950 "182",
951 "127",
952 "215",
953 "207",
954 "115",
955 "124",
956 "28",
957 "122",
958 "147",
959 "99",
960 "98",
961 "203",
962 "23",
963 "27",
964 "107",
965 "138",
966 "207",
967 "122",
968 "181",
969 "161",
970 "53",
971 "62",
972 "52",
973 "35",
974 ],
975 ]
976
977
978 class SizedTerminal(Terminal):
979 """A terminal class with a pre-determined size."""
980
981 def _get_size(self) -> tuple[int, int]:
982 return (80, 25)
983
984
985 def _generate_stressor(term: Terminal = terminal) -> Recorder:
986 pytermgui.set_global_terminal(term)
987
988 with term.record() as recording:
989 width, height = 30, 30
990 matrix = DensePixelMatrix(width, height)
991
992 for y, row in enumerate(MATRIX):
993 for x, char in enumerate(row):
994 matrix[y, x] = char
995 matrix.build()
996
997 for line in matrix.get_lines():
998 term.print(line)
999
1000 tim.print("[@141 61]Hello[inverse]There")
1001
1002 return recording
1003
1004
1005 def _diff(output: str, expected: str) -> tuple[str, str]:
1006 red = str_to_color("red")
1007
1008 buff = ""
1009 count = 0
1010 for o, e in zip(output, expected):
1011 if o != e:
1012 buff += red(o)
1013 count += 1
1014 else:
1015 buff += o
1016
1017 return buff, red(str(count))
1018
1019
1020 def test_export_html():
1021 recording = _generate_stressor()
1022
1023 Color.default_background = str_to_color("#000000")
1024 Color.default_foreground = str_to_color("#ffffff")
1025
1026 output = recording.export_html()
1027
1028 if output != HTML_TARGET:
1029 print(*_diff(output, HTML_TARGET))
1030
1031 compare(output, HTML_TARGET)
1032
1033
1034 def test_export_svg():
1035 terminal = SizedTerminal()
1036
1037 recording = _generate_stressor(terminal)
1038
1039 Color.default_background = str_to_color("#000000")
1040 Color.default_foreground = str_to_color("#ffffff")
1041
1042 output = recording.export_svg()
1043
1044 if output != SVG_TARGET:
1045 print(*_diff(output, SVG_TARGET))
1046
1047 compare(output, SVG_TARGET)
1048
1049
1050 def regenerate_targets():
1051 Color.default_background = str_to_color("#000000")
1052 Color.default_foreground = str_to_color("#ffffff")
1053
1054 html_target = _generate_stressor().export_html()
1055 svg_target = _generate_stressor(SizedTerminal()).export_svg()
1056
1057 with open("tests/_exporter_targets.py", "w") as f:
1058 f.write(f'SVG_TARGET = """\\\n{svg_target}"""\n\n')
1059 f.write(f'HTML_TARGET = """\\\n{html_target}"""\n')
1060
1061
1062 if __name__ == "__main__":
1063 regenerate_targets()