Refactor file internals to separate class
This commit is contained in:
178
tests/buf_test.py
Normal file
178
tests/buf_test.py
Normal file
@@ -0,0 +1,178 @@
|
||||
import pytest
|
||||
|
||||
from babi.buf import Buf
|
||||
|
||||
|
||||
def test_buf_repr():
|
||||
ret = repr(Buf(['a', 'b', 'c']))
|
||||
assert ret == "Buf(['a', 'b', 'c'], x=0, y=0, file_y=0)"
|
||||
|
||||
|
||||
def test_buf_item_retrieval():
|
||||
buf = Buf(['a', 'b', 'c'])
|
||||
assert buf[1] == 'b'
|
||||
assert buf[-1] == 'c'
|
||||
with pytest.raises(IndexError):
|
||||
buf[3]
|
||||
|
||||
|
||||
def test_buf_del():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
buf = Buf(lst)
|
||||
|
||||
with buf.record() as modifications:
|
||||
del buf[1]
|
||||
|
||||
assert lst == ['a', 'c']
|
||||
|
||||
buf.apply(modifications)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
|
||||
|
||||
def test_buf_del_with_negative():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
buf = Buf(lst)
|
||||
|
||||
with buf.record() as modifications:
|
||||
del buf[-1]
|
||||
|
||||
assert lst == ['a', 'b']
|
||||
|
||||
buf.apply(modifications)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
|
||||
|
||||
def test_buf_insert():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
buf = Buf(lst)
|
||||
|
||||
with buf.record() as modifications:
|
||||
buf.insert(1, 'q')
|
||||
|
||||
assert lst == ['a', 'q', 'b', 'c']
|
||||
|
||||
buf.apply(modifications)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
|
||||
|
||||
def test_buf_insert_with_negative():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
buf = Buf(lst)
|
||||
|
||||
with buf.record() as modifications:
|
||||
buf.insert(-1, 'q')
|
||||
|
||||
assert lst == ['a', 'b', 'q', 'c']
|
||||
|
||||
buf.apply(modifications)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
|
||||
|
||||
def test_buf_set_value():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
buf = Buf(lst)
|
||||
|
||||
with buf.record() as modifications:
|
||||
buf[1] = 'hello'
|
||||
|
||||
assert lst == ['a', 'hello', 'c']
|
||||
|
||||
buf.apply(modifications)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
|
||||
|
||||
def test_buf_set_value_idx_negative():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
buf = Buf(lst)
|
||||
|
||||
with buf.record() as modifications:
|
||||
buf[-1] = 'hello'
|
||||
|
||||
assert lst == ['a', 'b', 'hello']
|
||||
|
||||
buf.apply(modifications)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
|
||||
|
||||
def test_buf_multiple_modifications():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
buf = Buf(lst)
|
||||
|
||||
with buf.record() as modifications:
|
||||
buf[1] = 'hello'
|
||||
buf.insert(1, 'ohai')
|
||||
del buf[0]
|
||||
|
||||
assert lst == ['ohai', 'hello', 'c']
|
||||
|
||||
buf.apply(modifications)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
|
||||
|
||||
def test_buf_iter():
|
||||
buf = Buf(['a', 'b', 'c'])
|
||||
buf_iter = iter(buf)
|
||||
assert next(buf_iter) == 'a'
|
||||
assert next(buf_iter) == 'b'
|
||||
assert next(buf_iter) == 'c'
|
||||
with pytest.raises(StopIteration):
|
||||
next(buf_iter)
|
||||
|
||||
|
||||
def test_buf_append():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
buf = Buf(lst)
|
||||
|
||||
with buf.record() as modifications:
|
||||
buf.append('q')
|
||||
|
||||
assert lst == ['a', 'b', 'c', 'q']
|
||||
|
||||
buf.apply(modifications)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
|
||||
|
||||
def test_buf_pop_default():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
buf = Buf(lst)
|
||||
|
||||
with buf.record() as modifications:
|
||||
buf.pop()
|
||||
|
||||
assert lst == ['a', 'b']
|
||||
|
||||
buf.apply(modifications)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
|
||||
|
||||
def test_buf_pop_idx():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
buf = Buf(lst)
|
||||
|
||||
with buf.record() as modifications:
|
||||
buf.pop(1)
|
||||
|
||||
assert lst == ['a', 'c']
|
||||
|
||||
buf.apply(modifications)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
@@ -25,6 +25,18 @@ def test_modify_file_with_windows_newlines(run, tmpdir):
|
||||
assert f.read_binary() == b'\r\nfoo\r\nbar\r\n'
|
||||
|
||||
|
||||
def test_saving_file_with_multiple_lines_at_end_maintains_those(run, tmpdir):
|
||||
f = tmpdir.join('f')
|
||||
f.write('foo\n\n')
|
||||
with run(str(f)) as h, and_exit(h):
|
||||
h.press('a')
|
||||
h.await_text('*')
|
||||
h.press('^S')
|
||||
h.await_text('saved!')
|
||||
|
||||
assert f.read() == 'afoo\n\n'
|
||||
|
||||
|
||||
def test_new_file(run):
|
||||
with run('this_is_a_new_file') as h, and_exit(h):
|
||||
h.await_text('this_is_a_new_file')
|
||||
|
||||
@@ -4,6 +4,7 @@ from unittest import mock
|
||||
|
||||
import pytest
|
||||
|
||||
from babi.buf import Buf
|
||||
from babi.color_manager import ColorManager
|
||||
from babi.hl.interface import HL
|
||||
from babi.hl.syntax import Syntax
|
||||
@@ -161,7 +162,7 @@ def test_syntax_highlight_cache_first_line(stdscr, make_grammars):
|
||||
syntax = Syntax(grammars, THEME, ColorManager.make())
|
||||
syntax._init_screen(stdscr)
|
||||
file_hl = syntax.file_highlighter('foo.demo', '')
|
||||
file_hl.highlight_until(['int', 'int'], 2)
|
||||
file_hl.highlight_until(Buf(['int', 'int']), 2)
|
||||
assert file_hl.regions == [
|
||||
(HL(0, 3, curses.A_BOLD | 2 << 8),),
|
||||
(),
|
||||
|
||||
@@ -1,144 +0,0 @@
|
||||
import pytest
|
||||
|
||||
from babi.list_spy import ListSpy
|
||||
|
||||
|
||||
def test_list_spy_repr():
|
||||
assert repr(ListSpy(['a', 'b', 'c'])) == "ListSpy(['a', 'b', 'c'])"
|
||||
|
||||
|
||||
def test_list_spy_item_retrieval():
|
||||
spy = ListSpy(['a', 'b', 'c'])
|
||||
assert spy[1] == 'b'
|
||||
assert spy[-1] == 'c'
|
||||
with pytest.raises(IndexError):
|
||||
spy[3]
|
||||
|
||||
|
||||
def test_list_spy_del():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
spy = ListSpy(lst)
|
||||
del spy[1]
|
||||
|
||||
assert lst == ['a', 'c']
|
||||
|
||||
spy.undo(lst)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
|
||||
|
||||
def test_list_spy_del_with_negative():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
spy = ListSpy(lst)
|
||||
del spy[-1]
|
||||
|
||||
assert lst == ['a', 'b']
|
||||
|
||||
spy.undo(lst)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
|
||||
|
||||
def test_list_spy_insert():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
spy = ListSpy(lst)
|
||||
spy.insert(1, 'q')
|
||||
|
||||
assert lst == ['a', 'q', 'b', 'c']
|
||||
|
||||
spy.undo(lst)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
|
||||
|
||||
def test_list_spy_insert_with_negative():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
spy = ListSpy(lst)
|
||||
spy.insert(-1, 'q')
|
||||
|
||||
assert lst == ['a', 'b', 'q', 'c']
|
||||
|
||||
spy.undo(lst)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
|
||||
|
||||
def test_list_spy_set_value():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
spy = ListSpy(lst)
|
||||
spy[1] = 'hello'
|
||||
|
||||
assert lst == ['a', 'hello', 'c']
|
||||
|
||||
spy.undo(lst)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
|
||||
|
||||
def test_list_spy_multiple_modifications():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
spy = ListSpy(lst)
|
||||
spy[1] = 'hello'
|
||||
spy.insert(1, 'ohai')
|
||||
del spy[0]
|
||||
|
||||
assert lst == ['ohai', 'hello', 'c']
|
||||
|
||||
spy.undo(lst)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
|
||||
|
||||
def test_list_spy_iter():
|
||||
spy = ListSpy(['a', 'b', 'c'])
|
||||
spy_iter = iter(spy)
|
||||
assert next(spy_iter) == 'a'
|
||||
assert next(spy_iter) == 'b'
|
||||
assert next(spy_iter) == 'c'
|
||||
with pytest.raises(StopIteration):
|
||||
next(spy_iter)
|
||||
|
||||
|
||||
def test_list_spy_append():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
spy = ListSpy(lst)
|
||||
spy.append('q')
|
||||
|
||||
assert lst == ['a', 'b', 'c', 'q']
|
||||
|
||||
spy.undo(lst)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
|
||||
|
||||
def test_list_spy_pop_default():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
spy = ListSpy(lst)
|
||||
spy.pop()
|
||||
|
||||
assert lst == ['a', 'b']
|
||||
|
||||
spy.undo(lst)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
|
||||
|
||||
def test_list_spy_pop_idx():
|
||||
lst = ['a', 'b', 'c']
|
||||
|
||||
spy = ListSpy(lst)
|
||||
spy.pop(1)
|
||||
|
||||
assert lst == ['a', 'c']
|
||||
|
||||
spy.undo(lst)
|
||||
|
||||
assert lst == ['a', 'b', 'c']
|
||||
Reference in New Issue
Block a user