def test_differ_assert_passes(): image1 = LivePillowImage(Image.new('RGBA', (10, 20))) image2 = LivePillowImage(Image.new('RGBA', (10, 20))) differ = LiveImageDiffer() differ.assert_equal(image1, image2)
def test_differ_assert_fails(): blue = (0, 0, 255, 255) image1 = LivePillowImage(Image.new('RGBA', (10, 20))) image1.set_pixel((5, 10), blue) image2 = LivePillowImage(Image.new('RGBA', (10, 20))) differ = LiveImageDiffer() with pytest.raises(AssertionError, match=r'Images differ by 1 pixel.'): differ.assert_equal(image1, image2)
def test_live_pillow_pixels(): default = (0, 0, 0, 0) blue = (0, 0, 255, 255) image = LivePillowImage(Image.new('RGBA', (10, 20))) image.set_pixel((5, 10), blue) p1 = image.get_pixel((5, 10)) p2 = image.get_pixel((6, 10)) assert p1 == blue assert p2 == default
def test_differ_compare_writes_no_files_without_file_prefix(tmp_path): image1 = LivePillowImage(Image.new('RGBA', (10, 20))) image1.set_pixel((5, 10), (0, 255, 0, 255)) image2 = LivePillowImage(Image.new('RGBA', (10, 20))) diffs_path = tmp_path / 'image_diffs' differ = LiveImageDiffer(diffs_path) differ.compare(image1, image2) assert list(diffs_path.iterdir()) == []
def test_differ_compare_uses_test_name(tmp_path, request): image1 = LivePillowImage(Image.new('RGBA', (10, 20))) image1.set_pixel((5, 10), (0, 0, 123, 255)) image2 = LivePillowImage(Image.new('RGBA', (10, 20))) diffs_path = tmp_path / 'image_diffs' actual_path = (diffs_path / 'test-PySrc-tests-test_live_image-py--' 'test_differ_compare_uses_test_name-actual.png') differ = LiveImageDiffer(diffs_path, request) differ.compare(image1, image2) assert actual_path.exists()
def test_differ_compare_display(patched_turtle): expected_report = """\ create_text -1 20 anchor='sw' fill='black' font=('Arial', 10, 'normal') text='Actual' create_image 0 20 image='...' create_text -1 60 anchor='sw' fill='black' font=('Arial', 10, 'normal') text='Diff (0 pixels)' create_image 0 60 image='...' create_text -1 100 anchor='sw' fill='black' font=('Arial', 10, 'normal') text='Expected' create_image 0 100 image='...' """ t = MockTurtle() image1 = LivePillowImage(Image.new('RGBA', (10, 20))) image2 = LivePillowImage(Image.new('RGBA', (10, 20))) differ = LiveImageDiffer() differ.compare(image1, image2) report = t.report assert replace_image(report) == expected_report
def test_differ_compare_two_sets(tmp_path): image1 = LivePillowImage(Image.new('RGBA', (10, 20))) image1.set_pixel((5, 10), (0, 0, 100, 255)) image2 = LivePillowImage(Image.new('RGBA', (10, 20))) diffs_path = tmp_path / 'image_diffs' actual_path1 = diffs_path / 'test_name-actual.png' actual_path2 = diffs_path / 'other_name-actual.png' differ = LiveImageDiffer(diffs_path) differ.compare(image1, image2, 'test_name') differ.compare(image1, image2, 'other_name') assert actual_path1.exists() assert actual_path2.exists()
def test_differ_compare_display_disabled(patched_turtle): expected_report = '\n' t = MockTurtle() image1 = LivePillowImage(Image.new('RGBA', (10, 20))) image2 = LivePillowImage(Image.new('RGBA', (10, 20))) differ = LiveImageDiffer(is_displayed=False) differ.compare(image1, image2) report = t.report assert replace_image(report) == expected_report
def test_differ_compare_writes_file(tmp_path): image1 = LivePillowImage(Image.new('RGBA', (10, 20))) image1.set_pixel((5, 10), (0, 0, 255, 255)) image2 = LivePillowImage(Image.new('RGBA', (10, 20))) diffs_path = tmp_path / 'image_diffs' actual_path = diffs_path / 'test_name-actual.png' diff_path = diffs_path / 'test_name-diff.png' expected_path = diffs_path / 'test_name-expected.png' differ = LiveImageDiffer(diffs_path) differ.compare(image1, image2, 'test_name') assert actual_path.exists() assert diff_path.exists() assert expected_path.exists()
def test_differ_remove_no_prefix(tmp_path): image1 = LivePillowImage(Image.new('RGBA', (10, 20))) image1.set_pixel((5, 10), (0, 0, 101, 255)) image2 = LivePillowImage(Image.new('RGBA', (10, 20))) diffs_path = tmp_path / 'image_diffs' actual_path1 = diffs_path / 'apple-actual.png' actual_path2 = diffs_path / 'banana-actual.png' differ = LiveImageDiffer(diffs_path) differ.compare(image1, image2, 'apple') differ.compare(image1, image2, 'banana') differ.remove_common_prefix() assert actual_path1.exists() assert actual_path2.exists()
def test_differ_compare_test_name_without_path(): image1 = LivePillowImage(Image.new('RGBA', (10, 20))) differ = LiveImageDiffer() with pytest.raises(ValueError, match=r'Used file_prefix without diffs_path\.'): differ.compare(image1, image1, 'test_name')
def test_differ_duplicate_file_prefix(tmp_path): image = LivePillowImage(Image.new('RGBA', (10, 20))) differ = LiveImageDiffer(tmp_path) differ.compare(image, image, 'first_test') differ.compare(image, image, 'other_test') with pytest.raises(ValueError, match=r"Duplicate file_prefix: 'first_test'\."): differ.compare(image, image, 'first_test')
def test_differ_compare(): blue = (0, 0, 255, 255) white = (255, 255, 255, 255) expected_match = (0, 0, 255, 255 // 3) expected_diff = (255, 255 // 5, 255 * 2 // 5, 255) image1 = LivePillowImage(Image.new('RGBA', (10, 20))) image1.set_pixel((5, 10), blue) image1.set_pixel((6, 10), blue) image2 = LivePillowImage(Image.new('RGBA', (10, 20))) image2.set_pixel((5, 10), blue) image2.set_pixel((6, 10), white) differ = LiveImageDiffer() diff = differ.compare(image1, image2).convert_to_painter() diff_pixel1 = diff.get_pixel((5, 10)) diff_pixel2 = diff.get_pixel((6, 10)) assert diff_pixel1 == expected_match assert diff_pixel2 == expected_diff assert differ.diff_count == 1
def test_display_pillow_image(patched_turtle): image = Image.new('RGB', (2, 2)) expected_report = """\ create_image 0 0 image='...' """ t = MockTurtle() LivePillowImage(image).display() report = t.report assert replace_image(report) == expected_report
def test_display_image_bottom_left(patched_turtle, align: str, position: typing.Tuple[int, int]): image = Image.new('RGB', (10, 20)) expected_report = """\ create_image 100 -200 image='...' """ t = MockTurtle() LivePillowImage(image).display(position, align) report = t.report assert replace_image(report) == expected_report
def test_differ_compare_two_sizes(): blue = (0, 0, 255, 255) green = (0, 255, 0, 255) missing_blue = (255, 0, 51, 255) missing_green = (255, 51, 0, 255) image1 = LivePillowImage(Image.new('RGBA', (2, 3))) image1.set_pixel((0, 2), blue) image2 = LivePillowImage(Image.new('RGBA', (3, 2))) image2.set_pixel((2, 0), green) differ = LiveImageDiffer() diff = differ.compare(image1, image2).convert_to_painter() diff_pixel1 = diff.get_pixel((0, 2)) diff_pixel2 = diff.get_pixel((2, 0)) assert diff_pixel1 == missing_blue assert diff_pixel2 == missing_green assert differ.diff_count == 5
def test_live_png_as_painter(tmp_path): blue = (0, 0, 255, 255) white = (255, 255, 255, 255) image1 = LivePillowImage(Image.new('RGBA', (10, 20))) image1.set_pixel((5, 10), blue) image1.set_pixel((6, 10), white) image2 = LivePillowImage(Image.new('RGBA', (10, 20))) image2.set_pixel((5, 10), blue) bytes2 = image2.convert_to_png() image3 = LivePng(bytes2) painter3 = image3.convert_to_painter() painter3.set_pixel((6, 10), white) differ = LiveImageDiffer(tmp_path) differ.assert_equal(image1, painter3, 'live_png_as_painter')
def test_display_image_bad_align(patched_turtle): image = Image.new('RGB', (10, 20)) with pytest.raises(ValueError, match="Invalid align: 'topfloop'."): LivePillowImage(image).display(align='topfloop')