from PIL import Image, ImageDraw import cyber from cyber import BG, CYAN, DIM, RED, SCAN, YELLOW from tests.helpers import count_colour, count_near def canvas(w=200, h=100): img = Image.new("RGB", (w, h), BG) return img, ImageDraw.Draw(img) def test_background_has_scanlines_and_is_a_private_copy(): a = cyber.background() assert a.size == (480, 320) assert a.getpixel((5, 0)) == SCAN and a.getpixel((5, 1)) == BG a.putpixel((5, 1), (1, 2, 3)) assert cyber.background().getpixel((5, 1)) == BG # the cached original is untouched def test_chamfer_cuts_the_requested_corner_only(): img, d = canvas() cyber.chamfer(d, (10, 10, 90, 50), 12, fill=YELLOW, cuts="tl") assert img.getpixel((11, 11)) == BG # cut away assert img.getpixel((88, 48)) == YELLOW # the bottom-right corner is square assert img.getpixel((30, 30)) == YELLOW def test_glitch_text_shows_all_three_colours(): img, d = canvas() cyber.glitch_text(d, (20, 10), "88", cyber.font(48)) for colour in (YELLOW, CYAN, RED): # the fringes are anti-aliased, so compare with a tolerance assert count_near(img, colour) > 0 def test_slanted_bar_lights_proportionally(): img, d = canvas() cyber.slanted_bar(d, 10, 10, 50, 10, 10, 8, 2) assert count_colour(img, YELLOW) > 0 and count_colour(img, DIM) > 0 lit, unlit = count_colour(img, YELLOW), count_colour(img, DIM) assert abs(lit - unlit) <= lit * 0.15 # 5 of 10 segments def test_slanted_bar_turns_red_when_hot(): img, d = canvas() cyber.slanted_bar(d, 10, 10, 90, 10, 10, 8, 2) assert count_colour(img, RED) > 0 and count_colour(img, YELLOW) == 0 def test_slanted_bar_none_is_all_dim(): img, d = canvas() cyber.slanted_bar(d, 10, 10, None, 10, 10, 8, 2) assert count_colour(img, DIM) > 0 assert count_colour(img, YELLOW) == 0 and count_colour(img, RED) == 0 def test_tag_is_sized_from_its_text_and_returns_its_box(): img, d = canvas(300, 60) f = cyber.font(22) short = cyber.tag(d, (10, 10), "HI", f) long_ = cyber.tag(d, (10, 10), "HOME // SERVER", f) assert (long_[2] - long_[0]) > (short[2] - short[0]) assert (long_[2] - long_[0]) >= d.textlength("HOME // SERVER", font=f) assert count_colour(img, YELLOW) > 0 def test_tag_colour_can_be_changed(): img, d = canvas() cyber.tag(d, (10, 10), "STARÉ", cyber.font(14), colour=RED) assert count_colour(img, RED) > 0 and count_colour(img, YELLOW) == 0 def test_fit_shortens_long_text_with_an_ellipsis(): img, d = canvas() f = cyber.font(15) text = cyber.fit(d, "A-VERY-LONG-CONTAINER-NAME-INDEED", f, 80) assert text.endswith("…") and d.textlength(text, font=f) <= 80 assert cyber.fit(d, "SHORT", f, 80) == "SHORT"