Update test style for test_repro_deb822.py
Improve continuation lines and clarity of asserts
Decruft some old mypy and unittest helpers
Stuart Prescott
1 year, 6 months ago
232 | 232 | # _print_ast(deb822_file) |
233 | 233 | # logging.info(f" --- END CASE {i} --- ") |
234 | 234 | assert parse_case.error_element_count == error_element_count, \ |
235 | "Correct number of error tokens for case " + c | |
235 | "Correct number of error tokens for case " + c | |
236 | 236 | assert parse_case.paragraph_count == paragraph_count, \ |
237 | "Correct number of paragraphs parsed for case " + c | |
237 | "Correct number of paragraphs parsed for case " + c | |
238 | 238 | assert parse_case.is_valid_file == deb822_file.is_valid_file, \ |
239 | "Verify deb822_file correctly determines whether the field is invalid" \ | |
240 | " for case " + c | |
239 | "Verify deb822_file correctly determines whether the field is invalid" \ | |
240 | " for case " + c | |
241 | 241 | assert case_input == deb822_file.convert_to_text(), \ |
242 | 242 | "Input of case " + c + " is round trip safe" |
243 | 243 | |
272 | 272 | |
273 | 273 | for repro_paragraph, deb822_paragraph in zip(deb822_file, deb822_paragraphs): |
274 | 274 | assert list(repro_paragraph) == list(deb822_paragraph), \ |
275 | "Ensure keys are the same and in the correct order, case " + c | |
275 | "Ensure keys are the same and in the correct order, case " + c | |
276 | 276 | # Use the key from Deb822 as it is compatible with the round safe version |
277 | 277 | # (the reverse is not true typing wise) |
278 | 278 | for k, ev in deb822_paragraph.items(): |
309 | 309 | ''') |
310 | 310 | |
311 | 311 | assert expected == deb822_file.convert_to_text(), \ |
312 | "Mutation should have worked while preserving comments" | |
312 | "Mutation should have worked while preserving comments" | |
313 | 313 | |
314 | 314 | # As an alternative, we can also fix the problem if we discard comments |
315 | 315 | deb822_file = parse_deb822_file(original.splitlines(keepends=True)) |
329 | 329 | ''') |
330 | 330 | |
331 | 331 | assert expected == deb822_file.convert_to_text(), \ |
332 | "Mutation should have worked while but discarded comments" | |
332 | "Mutation should have worked while but discarded comments" | |
333 | 333 | |
334 | 334 | source_paragraph['Multi-Line-Field-Space'] = textwrap.dedent('''\ |
335 | 335 | foo |
349 | 349 | \tbar |
350 | 350 | ''') |
351 | 351 | assert expected == deb822_file.convert_to_text(), \ |
352 | "Mutation should have worked while preserving space + tab" | |
352 | "Mutation should have worked while preserving space + tab" | |
353 | 353 | |
354 | 354 | def test_empty_fields(self): |
355 | 355 | # type: () -> None |
380 | 380 | Another-Empty-Field: |
381 | 381 | ''') |
382 | 382 | assert expected == deb822_file.convert_to_text(), \ |
383 | "Mutation should have worked and generate a valid file" | |
383 | "Mutation should have worked and generate a valid file" | |
384 | 384 | |
385 | 385 | def test_empty_fields_reorder(self): |
386 | 386 | # type: () -> None |
397 | 397 | Field: foo |
398 | 398 | ''') |
399 | 399 | assert expected == deb822_file.convert_to_text(), \ |
400 | "Mutation should have worked and generate a valid file" | |
400 | "Mutation should have worked and generate a valid file" | |
401 | 401 | # Re-parse |
402 | 402 | deb822_file = parse_deb822_file(original.splitlines(keepends=True)) |
403 | 403 | source_paragraph = next(iter(deb822_file)) |
408 | 408 | Field: foo |
409 | 409 | ''') |
410 | 410 | assert expected == deb822_file.convert_to_text(), \ |
411 | "Mutation should have worked and generate a valid file" | |
411 | "Mutation should have worked and generate a valid file" | |
412 | 412 | |
413 | 413 | def test_case_preservation(self): |
414 | 414 | # type: () -> None |
440 | 440 | ''') |
441 | 441 | |
442 | 442 | assert expected == deb822_file.convert_to_text(), \ |
443 | "Mutation should have worked while preserving case" | |
443 | "Mutation should have worked while preserving case" | |
444 | 444 | |
445 | 445 | # Repeat with duplicated fields |
446 | 446 | original = textwrap.dedent('''\ |
476 | 476 | ''') |
477 | 477 | |
478 | 478 | assert expected == deb822_file.convert_to_text(), \ |
479 | "Mutation should have worked while preserving case" | |
479 | "Mutation should have worked while preserving case" | |
480 | 480 | |
481 | 481 | def test_preserve_field_order_on_mutation(self): |
482 | 482 | # type: () -> None |
508 | 508 | ''') |
509 | 509 | |
510 | 510 | assert expected == deb822_file.convert_to_text(), \ |
511 | "Mutation should have worked while preserving field order" | |
511 | "Mutation should have worked while preserving field order" | |
512 | 512 | |
513 | 513 | # Again - this time with a paragraph containing duplicate fields |
514 | 514 | original = textwrap.dedent('''\ |
544 | 544 | ''') |
545 | 545 | |
546 | 546 | assert expected == deb822_file.convert_to_text(), \ |
547 | "Mutation should have worked while preserving field order" | |
547 | "Mutation should have worked while preserving field order" | |
548 | 548 | |
549 | 549 | def test_preserve_field_case_on_iter(self): |
550 | 550 | # type: () -> None |
570 | 570 | actual_keys = set(source_paragraph.keys()) |
571 | 571 | |
572 | 572 | assert expected_keys == actual_keys, \ |
573 | "Keys returned by iterations should have original case" | |
573 | "Keys returned by iterations should have original case" | |
574 | 574 | |
575 | 575 | def test_append_paragraph(self): |
576 | 576 | # type: () -> None |
598 | 598 | ''') |
599 | 599 | |
600 | 600 | assert expected == deb822_file.convert_to_text(), \ |
601 | "Mutation should have worked while preserving " \ | |
602 | "comments" | |
601 | "Mutation should have worked while preserving comments" | |
603 | 602 | |
604 | 603 | def test_append_paragraph_existing_trailing_newline(self): |
605 | 604 | # type: () -> None |
628 | 627 | ''') |
629 | 628 | |
630 | 629 | assert expected == deb822_file.convert_to_text(), \ |
631 | "Mutation should have worked while preserving " \ | |
632 | "comments" | |
630 | "Mutation should have worked while preserving comments" | |
633 | 631 | |
634 | 632 | def test_append_empty_paragraph(self): |
635 | 633 | # type: () -> None |
653 | 651 | ''') |
654 | 652 | |
655 | 653 | assert expected == deb822_file.convert_to_text(), \ |
656 | "Mutation should have worked while preserving " \ | |
657 | "comments" | |
654 | "Mutation should have worked while preserving comments" | |
658 | 655 | |
659 | 656 | def test_append_tailing_comment(self): |
660 | 657 | # type: () -> None |
684 | 681 | ''') |
685 | 682 | |
686 | 683 | assert expected == deb822_file.convert_to_text(), \ |
687 | "Mutation should have worked while preserving " \ | |
688 | "comments" | |
684 | "Mutation should have worked while preserving comments" | |
689 | 685 | |
690 | 686 | def test_insert_paragraph(self): |
691 | 687 | # type: () -> None |
713 | 709 | ''') |
714 | 710 | |
715 | 711 | assert expected == deb822_file.convert_to_text(), \ |
716 | "Mutation should have worked while preserving " \ | |
717 | "comments" | |
712 | "Mutation should have worked while preserving comments" | |
718 | 713 | |
719 | 714 | # Insert after the existing paragraphs |
720 | 715 | |
737 | 732 | ''') |
738 | 733 | |
739 | 734 | assert expected == deb822_file.convert_to_text(), \ |
740 | "Mutation should have worked while preserving " \ | |
741 | "comments" | |
735 | "Mutation should have worked while preserving comments" | |
742 | 736 | |
743 | 737 | def test_insert_paragraph_with_comments(self): |
744 | 738 | # type: () -> None |
777 | 771 | ''') |
778 | 772 | |
779 | 773 | assert expected == deb822_file.convert_to_text(), \ |
780 | "Mutation should have worked while preserving " \ | |
781 | "comments" | |
774 | "Mutation should have worked while preserving comments" | |
782 | 775 | |
783 | 776 | # Insert after the existing paragraphs |
784 | 777 | |
804 | 797 | ''') |
805 | 798 | |
806 | 799 | assert expected == deb822_file.convert_to_text(), \ |
807 | "Mutation should have worked while preserving " \ | |
808 | "comments" | |
800 | "Mutation should have worked while preserving comments" | |
809 | 801 | |
810 | 802 | def test_insert_paragraph_in_empty_file(self): |
811 | 803 | # type: () -> None |
824 | 816 | ''') |
825 | 817 | |
826 | 818 | assert expected == deb822_file.convert_to_text(), \ |
827 | "Mutation should have worked while preserving " \ | |
828 | "comments" | |
819 | "Mutation should have worked while preserving comments" | |
829 | 820 | |
830 | 821 | def test_remove_paragraph(self): |
831 | 822 | # type: () -> None |
851 | 842 | ''') |
852 | 843 | |
853 | 844 | assert expected == deb822_file.convert_to_text(), \ |
854 | "Mutation should have worked while preserving " \ | |
855 | "comments" | |
845 | "Mutation should have worked while preserving comments" | |
856 | 846 | |
857 | 847 | # Verify that we can add another paragraph. |
858 | 848 | deb822_file.append(Deb822ParagraphElement.from_dict({'Package': 'bloe'})) |
866 | 856 | ''') |
867 | 857 | |
868 | 858 | assert expected == deb822_file.convert_to_text(), \ |
869 | "Adding new paragraph should have worked" | |
859 | "Adding new paragraph should have worked" | |
870 | 860 | |
871 | 861 | deb822_file.remove(list(deb822_file)[1]) |
872 | 862 | |
879 | 869 | ''') |
880 | 870 | |
881 | 871 | assert expected == deb822_file.convert_to_text(), \ |
882 | "Mutation should have worked while preserving " \ | |
883 | "comments" | |
872 | "Mutation should have worked while preserving comments" | |
884 | 873 | |
885 | 874 | original = textwrap.dedent('''\ |
886 | 875 | Source: foo |
912 | 901 | ''') |
913 | 902 | |
914 | 903 | assert expected == deb822_file.convert_to_text(), \ |
915 | "Mutation should have worked while preserving " \ | |
916 | "comments" | |
904 | "Mutation should have worked while preserving comments" | |
917 | 905 | |
918 | 906 | def test_duplicate_fields(self): |
919 | 907 | # type: () -> None |
957 | 945 | |
958 | 946 | expected_fixed = original.replace('Rules-Requires-Root: binary-targets\n', '') |
959 | 947 | assert expected_fixed == deb822_file.convert_to_text(), \ |
960 | "Fixed version should only have one Rules-Requires-Root field" | |
948 | "Fixed version should only have one Rules-Requires-Root field" | |
961 | 949 | |
962 | 950 | # As an alternative, we can also fix the problem if we discard comments |
963 | 951 | deb822_file = parse_deb822_file(original.splitlines(keepends=True), |
982 | 970 | New-Field: value |
983 | 971 | ''') |
984 | 972 | assert expected == deb822_file.convert_to_text(), \ |
985 | "Fixed version should only have one Rules-Requires-Root field" | |
973 | "Fixed version should only have one Rules-Requires-Root field" | |
986 | 974 | |
987 | 975 | def test_sorting(self): |
988 | 976 | # type: () -> None |
1060 | 1048 | paragraph.sort_fields(key=key_func) |
1061 | 1049 | |
1062 | 1050 | assert sorted_nodups == deb822_file_nodups.convert_to_text(), \ |
1063 | "Sorting without duplicated fields work" | |
1051 | "Sorting without duplicated fields work" | |
1064 | 1052 | deb822_file_with_dups = parse_deb822_file(original_with_dups.splitlines(keepends=True), |
1065 | 1053 | accept_files_with_duplicated_fields=True, |
1066 | 1054 | ) |
1069 | 1057 | paragraph.sort_fields(key=key_func) |
1070 | 1058 | |
1071 | 1059 | assert sorted_with_dups == deb822_file_with_dups.convert_to_text(), \ |
1072 | "Sorting with duplicated fields work" | |
1060 | "Sorting with duplicated fields work" | |
1073 | 1061 | |
1074 | 1062 | def test_reorder_nodups(self): |
1075 | 1063 | # type: () -> None |
1364 | 1352 | discard_comments_on_read=False |
1365 | 1353 | ) |
1366 | 1354 | |
1367 | assert ['a', 'b', 'c d', 'e'] == list(comma_list_correctly_read) | |
1368 | ||
1369 | assert ["some", | |
1370 | "fun", | |
1371 | "with\n multi-line\n values", | |
1372 | "separated by", | |
1373 | "commas\n >:)"] == \ | |
1374 | list(ml_comma_list) | |
1375 | assert ["some", | |
1376 | "fun", | |
1377 | "with\n multi-line\n# With a comment inside it for added fun\n values", | |
1378 | "separated by", | |
1379 | "commas\n# Comments in final value\n >:)"] == \ | |
1380 | list(ml_comma_list_w_comments) | |
1381 | ||
1382 | assert [ | |
1383 | "Someone <nobody@example.org>", | |
1384 | "Margrete, I, Ruler <1@margrete.dk>", | |
1385 | "Margrete, II, Queen\n <2@margrete.dk>", | |
1386 | ] == \ | |
1387 | list(uploaders_list) | |
1388 | assert [ | |
1389 | "Someone <nobody@example.org>", | |
1390 | "Margrete, I, Ruler <1@margrete.dk>", | |
1391 | "Margrete, II, Queen\n# We could list additional names here\n <2@margrete.dk>", | |
1392 | ] == \ | |
1393 | list(uploaders_list_with_comments) | |
1355 | assert list(comma_list_correctly_read) == ['a', 'b', 'c d', 'e'] | |
1356 | ||
1357 | assert list(ml_comma_list) == [ | |
1358 | "some", | |
1359 | "fun", | |
1360 | "with\n multi-line\n values", | |
1361 | "separated by", | |
1362 | "commas\n >:)" | |
1363 | ] | |
1364 | ||
1365 | assert list(ml_comma_list_w_comments) == [ | |
1366 | "some", | |
1367 | "fun", | |
1368 | "with\n multi-line\n# With a comment inside it for added fun\n values", | |
1369 | "separated by", | |
1370 | "commas\n# Comments in final value\n >:)" | |
1371 | ] | |
1372 | ||
1373 | assert list(uploaders_list) == [ | |
1374 | "Someone <nobody@example.org>", | |
1375 | "Margrete, I, Ruler <1@margrete.dk>", | |
1376 | "Margrete, II, Queen\n <2@margrete.dk>", | |
1377 | ] | |
1378 | ||
1379 | assert list(uploaders_list_with_comments) == [ | |
1380 | "Someone <nobody@example.org>", | |
1381 | "Margrete, I, Ruler <1@margrete.dk>", | |
1382 | "Margrete, II, Queen\n# We could list additional names here\n <2@margrete.dk>", | |
1383 | ] | |
1384 | ||
1394 | 1385 | |
1395 | 1386 | # Interpretation must not change the content |
1396 | 1387 | assert original == deb822_file.convert_to_text() |