1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
RSpec.describe Magick::Image, "#chop" do
def build_gray_image
image = Magick::Image.new(3, 3)
pixels = [
[gray(1), gray(2), gray(3)],
[gray(4), gray(5), gray(6)],
[gray(7), gray(8), gray(9)]
]
image.import_pixels(0, 0, 3, 3, "RGB", pixels.flatten)
end
it "removes a cross from the middle of the image" do
image = build_gray_image
new_image = image.chop(1, 1, 1, 1)
expected_pixels = [
[gray(1), gray(3)],
[gray(7), gray(9)]
]
expect(new_image).to match_pixels(expected_pixels)
end
it "removes an L-shape from the bottom left of the image" do
image = build_gray_image
new_image = image.chop(0, 2, 1, 1)
expected_pixels = [
[gray(2), gray(3)],
[gray(5), gray(6)]
]
expect(new_image).to match_pixels(expected_pixels)
end
it "removes 1 column from the middle of the image" do
image = build_gray_image
new_image = image.chop(1, 1, 1, 0)
expected_pixels = [
[gray(1), gray(3)],
[gray(4), gray(6)],
[gray(7), gray(9)]
]
expect(new_image).to match_pixels(expected_pixels)
end
it "removes 1 row from the middle of the image" do
image = build_gray_image
new_image = image.chop(1, 1, 0, 1)
expected_pixels = [
[gray(1), gray(2), gray(3)],
[gray(7), gray(8), gray(9)]
]
expect(new_image).to match_pixels(expected_pixels)
end
it "removes 2 rows and 2 columns from the image" do
image = build_gray_image
new_image = image.chop(0, 0, 2, 2)
expect(new_image).to match_pixels([gray(9)])
end
it "raises an error when x is out of bounds" do
image = build_gray_image
expect { image.chop(5, 1, 1, 1) }.to raise_error(RuntimeError)
end
it "raises an error when y is out of bounds" do
image = build_gray_image
expect { image.chop(1, 5, 1, 1) }.to raise_error(RuntimeError)
end
it "does not raise an error when width or height are out of bounds" do
image = build_gray_image
expect { image.chop(1, 1, 5, 5) }.not_to raise_error
end
it "raises an error when the argument is the wrong type" do
image = build_gray_image
expect { image.chop("hello", 1, 1, 1) }.to raise_error(TypeError)
end
end
|