def test_erase_in_line(): screen = update(Screen(5, 5), ["sam i", "s foo", "but a", "re yo", "u? "], colored=[0]) screen.cursor_position(1, 3) # a) erase from cursor to the end of line screen.erase_in_line(0) assert (screen.cursor.y, screen.cursor.x) == (0, 2) assert screen.display == ["sa ", "s foo", "but a", "re yo", "u? "] assert screen[0] == [ Char("s", fg="red"), Char("a", fg="red"), screen.default_char, screen.default_char, screen.default_char ] # b) erase from the beginning of the line to the cursor screen = update(screen, ["sam i", "s foo", "but a", "re yo", "u? "], colored=[0]) screen.erase_in_line(1) assert (screen.cursor.y, screen.cursor.x) == (0, 2) assert screen.display == [" i", "s foo", "but a", "re yo", "u? "] assert screen[0] == [ screen.default_char, screen.default_char, screen.default_char, Char(" ", fg="red"), Char("i", fg="red") ] # c) erase the entire line screen = update(screen, ["sam i", "s foo", "but a", "re yo", "u? "], colored=[0]) screen.erase_in_line(2) assert (screen.cursor.y, screen.cursor.x) == (0, 2) assert screen.display == [" ", "s foo", "but a", "re yo", "u? "] assert screen[0] == [screen.default_char] * 5
def test_erase_in_display(): screen = update(Screen(5, 5), ["sam i", "s foo", "but a", "re yo", "u? "], colored=[2, 3]) screen.cursor_position(3, 3) # a) erase from cursor to the end of the display, including # the cursor screen.erase_in_display(0) assert (screen.cursor.y, screen.cursor.x) == (2, 2) assert screen.display == ["sam i", "s foo", "bu ", " ", " "] assert screen[2:] == [[ Char("b", fg="red"), Char("u", fg="red"), screen.default_char, screen.default_char, screen.default_char ], [screen.default_char] * 5, [screen.default_char] * 5] # b) erase from the beginning of the display to the cursor, # including it screen = update(screen, ["sam i", "s foo", "but a", "re yo", "u? "], colored=[2, 3]) screen.erase_in_display(1) assert (screen.cursor.y, screen.cursor.x) == (2, 2) assert screen.display == [" ", " ", " a", "re yo", "u? "] assert screen[:3] == [ [screen.default_char] * 5, [screen.default_char] * 5, [ screen.default_char, screen.default_char, screen.default_char, Char(" ", fg="red"), Char("a", fg="red") ], ] # c) erase the while display screen.erase_in_display(2) assert (screen.cursor.y, screen.cursor.x) == (2, 2) assert screen.display == [" ", " ", " ", " ", " "] assert screen == [[screen.default_char] * 5] * 5
def test_attributes(): screen = Screen(2, 2) assert screen == [[screen.default_char, screen.default_char]] * 2 screen.select_graphic_rendition(1) # Bold # Still default, since we haven't written anything. assert screen == [[screen.default_char, screen.default_char]] * 2 assert screen.cursor.attrs.bold screen.draw("f") assert screen == [[ Char("f", "default", "default", bold=True), screen.default_char ], [screen.default_char, screen.default_char]]
def update(screen, lines, colored=[]): """Updates a given screen object with given lines, colors each line from ``colored`` in "red" and returns the modified screen. """ for y, line in enumerate(lines): for x, char in enumerate(line): if y in colored: attrs = {"fg": "red"} else: attrs = {} screen[y][x] = Char(data=char, **attrs) return screen
def test_erase_character(): screen = update(Screen(3, 3), ["sam", "is ", "foo"], colored=[0]) screen.erase_characters(2) assert (screen.cursor.y, screen.cursor.x) == (0, 0) assert screen.display == [" m", "is ", "foo"] assert screen[0] == [ screen.default_char, screen.default_char, Char("m", fg="red") ] screen.cursor.y, screen.cursor.x = 2, 2 screen.erase_characters() assert (screen.cursor.y, screen.cursor.x) == (2, 2) assert screen.display == [" m", "is ", "fo "] screen.cursor.y, screen.cursor.x = 1, 1 screen.erase_characters(0) assert (screen.cursor.y, screen.cursor.x) == (1, 1) assert screen.display == [" m", "i ", "fo "] # ! extreme cases. screen = update(Screen(5, 1), ["12345"], colored=[0]) screen.cursor.x = 1 screen.erase_characters(3) assert (screen.cursor.y, screen.cursor.x) == (0, 1) assert screen.display == ["1 5"] assert screen[0] == [ Char("1", fg="red"), screen.default_char, screen.default_char, screen.default_char, Char("5", "red") ] screen = update(Screen(5, 1), ["12345"], colored=[0]) screen.cursor.x = 2 screen.erase_characters(10) assert (screen.cursor.y, screen.cursor.x) == (0, 2) assert screen.display == ["12 "] assert screen[0] == [ Char("1", fg="red"), Char("2", fg="red"), screen.default_char, screen.default_char, screen.default_char ] screen = update(Screen(5, 1), ["12345"], colored=[0]) screen.erase_characters(4) assert (screen.cursor.y, screen.cursor.x) == (0, 0) assert screen.display == [" 5"] assert screen[0] == [ screen.default_char, screen.default_char, screen.default_char, screen.default_char, Char("5", fg="red") ]
def test_insert_characters(): screen = update(Screen(3, 3), ["sam", "is ", "foo"], colored=[0]) # a) normal case cursor = copy.copy(screen.cursor) screen.insert_characters(2) assert (screen.cursor.y, screen.cursor.x) == (cursor.y, cursor.x) assert screen[0] == [ screen.default_char, screen.default_char, Char("s", fg="red") ] # b) now inserting from the middle of the line screen.cursor.y, screen.cursor.x = 2, 1 screen.insert_characters(1) assert screen[2] == [Char("f"), screen.default_char, Char("o")] # c) inserting more than we have screen.insert_characters(10) assert screen[2] == [Char("f"), screen.default_char, screen.default_char] # d) 0 is 1 screen = update(Screen(3, 3), ["sam", "is ", "foo"], colored=[0]) screen.cursor_position() screen.insert_characters() assert screen[0] == [ screen.default_char, Char("s", fg="red"), Char("a", fg="red") ] screen = update(Screen(3, 3), ["sam", "is ", "foo"], colored=[0]) screen.cursor_position() screen.insert_characters(1) assert screen[0] == [ screen.default_char, Char("s", fg="red"), Char("a", fg="red") ]
def test_save_cursor(): # a) cursor position screen = Screen(10, 10) screen.save_cursor() screen.cursor.x, screen.cursor.y = 3, 5 screen.save_cursor() screen.cursor.x, screen.cursor.y = 4, 4 screen.restore_cursor() assert screen.cursor.x == 3 assert screen.cursor.y == 5 screen.restore_cursor() assert screen.cursor.x == 0 assert screen.cursor.y == 0 # b) modes screen = Screen(10, 10) screen.set_mode(mo.DECAWM, mo.DECOM) screen.save_cursor() screen.reset_mode(mo.DECAWM) screen.restore_cursor() assert mo.DECAWM in screen.mode assert mo.DECOM in screen.mode # c) attributes screen = Screen(10, 10) screen.select_graphic_rendition(4) screen.save_cursor() screen.select_graphic_rendition(24) assert screen.cursor.attrs == screen.default_char screen.restore_cursor() assert screen.cursor.attrs != screen.default_char assert screen.cursor.attrs == Char(" ", underscore=True)
def test_attributes_reset(): screen = Screen(2, 2) assert screen == [[screen.default_char, screen.default_char]] * 2 screen.select_graphic_rendition(1) screen.draw("f") screen.draw("o") screen.draw("o") assert screen == [ [Char("f", bold=True), Char("o", bold=True)], [Char("o", bold=True), screen.default_char], ] screen.cursor_position() screen.select_graphic_rendition(0) # Reset screen.draw("f") assert screen == [ [Char("f"), Char("o", bold=True)], [Char("o", bold=True), screen.default_char], ]
def test_delete_lines(): # a) without margins screen = update(Screen(3, 3), ["sam", "is ", "foo"], colored=[1]) screen.delete_lines() assert (screen.cursor.y, screen.cursor.x) == (0, 0) assert screen.display == ["is ", "foo", " "] assert screen == [ [Char("i", fg="red"), Char("s", fg="red"), Char(" ", fg="red")], [Char("f"), Char("o"), Char("o")], [screen.default_char] * 3, ] screen.delete_lines(0) assert (screen.cursor.y, screen.cursor.x) == (0, 0) assert screen.display == ["foo", " ", " "] assert screen == [ [Char("f"), Char("o"), Char("o")], [screen.default_char] * 3, [screen.default_char] * 3, ] # b) with margins screen = update(Screen(3, 5), ["sam", "is ", "foo", "bar", "baz"], colored=[2, 3]) screen.set_margins(1, 4) screen.cursor.y = 1 screen.delete_lines(1) assert (screen.cursor.y, screen.cursor.x) == (1, 0) assert screen.display == ["sam", "foo", "bar", " ", "baz"] assert screen == [ [Char("s"), Char("a"), Char("m")], [Char("f", fg="red"), Char("o", fg="red"), Char("o", fg="red")], [Char("b", fg="red"), Char("a", fg="red"), Char("r", fg="red")], [screen.default_char] * 3, [Char("b"), Char("a"), Char("z")], ] screen = update(Screen(3, 5), ["sam", "is ", "foo", "bar", "baz"], colored=[2, 3]) screen.set_margins(1, 4) screen.cursor.y = 1 screen.delete_lines(2) assert (screen.cursor.y, screen.cursor.x) == (1, 0) assert screen.display == ["sam", "bar", " ", " ", "baz"] assert screen == [ [Char("s"), Char("a"), Char("m")], [Char("b", fg="red"), Char("a", fg="red"), Char("r", fg="red")], [screen.default_char] * 3, [screen.default_char] * 3, [Char("b"), Char("a"), Char("z")], ] # c) with margins -- trying to delete more than we have available screen = update( Screen(3, 5), ["sam", "is ", "foo", "bar", "baz"], [None, None, [("red", "default")] * 3, [("red", "default")] * 3, None]) screen.set_margins(1, 4) screen.cursor.y = 1 screen.delete_lines(5) assert (screen.cursor.y, screen.cursor.x) == (1, 0) assert screen.display == ["sam", " ", " ", " ", "baz"] assert screen == [ [Char("s"), Char("a"), Char("m")], [screen.default_char] * 3, [screen.default_char] * 3, [screen.default_char] * 3, [Char("b"), Char("a"), Char("z")], ] # d) with margins -- trying to delete outside scroll boundaries; # expecting nothing to change screen = update(Screen(3, 5), ["sam", "is ", "foo", "bar", "baz"], colored=[2, 3]) screen.set_margins(2, 4) screen.cursor.y = 0 screen.delete_lines(5) assert (screen.cursor.y, screen.cursor.x) == (0, 0) assert screen.display == ["sam", "is ", "foo", "bar", "baz"] assert screen == [ [Char("s"), Char("a"), Char("m")], [Char("i"), Char("s"), Char(" ")], [Char("f", fg="red"), Char("o", fg="red"), Char("o", fg="red")], [Char("b", fg="red"), Char("a", fg="red"), Char("r", fg="red")], [Char("b"), Char("a"), Char("z")], ]
def test_reverse_index(): screen = update(Screen(2, 2), ["wo", "ot"], colored=[0]) # a) reverse indexing on the first row should push rows down # and create a new row at the top. screen.reverse_index() assert (screen.cursor.y, screen.cursor.x) == (0, 0) assert screen == [[screen.default_char, screen.default_char], [Char("w", fg="red"), Char("o", fg="red")]] # b) once again ... screen.reverse_index() assert (screen.cursor.y, screen.cursor.x) == (0, 0) assert screen == [ [screen.default_char, screen.default_char], [screen.default_char, screen.default_char], ] # c) same with margins screen = update(Screen(2, 5), ["bo", "sh", "th", "er", "oh"], colored=[2, 3]) screen.set_margins(2, 4) screen.cursor.y = 1 # ... go! screen.reverse_index() assert (screen.cursor.y, screen.cursor.x) == (1, 0) assert screen.display == ["bo", " ", "sh", "th", "oh"] assert screen == [ [Char("b"), Char("o")], [screen.default_char, screen.default_char], [Char("s"), Char("h")], [Char("t", fg="red"), Char("h", fg="red")], [Char("o"), Char("h")], ] # ... and again ... screen.reverse_index() assert (screen.cursor.y, screen.cursor.x) == (1, 0) assert screen.display == ["bo", " ", " ", "sh", "oh"] assert screen == [ [Char("b"), Char("o")], [screen.default_char, screen.default_char], [screen.default_char, screen.default_char], [Char("s"), Char("h")], [Char("o"), Char("h")], ] # ... and again ... screen.reverse_index() assert (screen.cursor.y, screen.cursor.x) == (1, 0) assert screen.display == ["bo", " ", " ", " ", "oh"] assert screen == [ [Char("b"), Char("o")], [screen.default_char, screen.default_char], [screen.default_char, screen.default_char], [screen.default_char, screen.default_char], [Char("o"), Char("h")], ] # look, nothing changes! screen.reverse_index() assert (screen.cursor.y, screen.cursor.x) == (1, 0) assert screen.display == ["bo", " ", " ", " ", "oh"] assert screen == [ [Char("b"), Char("o")], [screen.default_char, screen.default_char], [screen.default_char, screen.default_char], [screen.default_char, screen.default_char], [Char("o"), Char("h")], ]
def test_index(): screen = update(Screen(2, 2), ["wo", "ot"], colored=[1]) # a) indexing on a row that isn't the last should just move # the cursor down. screen.index() assert (screen.cursor.y, screen.cursor.x) == (1, 0) assert screen == [[Char("w"), Char("o")], [Char("o", fg="red"), Char("t", fg="red")]] # b) indexing on the last row should push everything up and # create a new row at the bottom. screen.index() assert screen.cursor.y == 1 assert screen == [[Char("o", fg="red"), Char("t", fg="red")], [screen.default_char, screen.default_char]] # c) same with margins screen = update(Screen(2, 5), ["bo", "sh", "th", "er", "oh"], colored=[1, 2]) screen.set_margins(2, 4) screen.cursor.y = 3 # ... go! screen.index() assert (screen.cursor.y, screen.cursor.x) == (3, 0) assert screen.display == ["bo", "th", "er", " ", "oh"] assert screen == [ [Char("b"), Char("o", "default")], [Char("t", "red"), Char("h", "red")], [Char("e"), Char("r")], [screen.default_char, screen.default_char], [Char("o"), Char("h")], ] # ... and again ... screen.index() assert (screen.cursor.y, screen.cursor.x) == (3, 0) assert screen.display == ["bo", "er", " ", " ", "oh"] assert screen == [ [Char("b"), Char("o")], [Char("e"), Char("r")], [screen.default_char, screen.default_char], [screen.default_char, screen.default_char], [Char("o"), Char("h")], ] # ... and again ... screen.index() assert (screen.cursor.y, screen.cursor.x) == (3, 0) assert screen.display == ["bo", " ", " ", " ", "oh"] assert screen == [ [Char("b"), Char("o")], [screen.default_char, screen.default_char], [screen.default_char, screen.default_char], [screen.default_char, screen.default_char], [Char("o"), Char("h")], ] # look, nothing changes! screen.index() assert (screen.cursor.y, screen.cursor.x) == (3, 0) assert screen.display == ["bo", " ", " ", " ", "oh"] assert screen == [ [Char("b"), Char("o")], [screen.default_char, screen.default_char], [screen.default_char, screen.default_char], [screen.default_char, screen.default_char], [Char("o"), Char("h")], ]