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_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)

    image1.set_pixel((5, 11), blue)

    with pytest.raises(AssertionError, match=r'Images differ by 2 pixels.'):
        differ.assert_equal(image1, image2)
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_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_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_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