Skip to content

Commit 13dabf8

Browse files
mportesdevsebastinas
authored andcommitted
Add test case for accessing the history file
1 parent 22db98c commit 13dabf8

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

bpython/test/test_history.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# encoding: utf-8
22

3+
import io
4+
import os
5+
36
from six.moves import range
47

8+
from bpython.config import getpreferredencoding
59
from bpython.history import History
610
from bpython.test import unittest
711

@@ -81,3 +85,46 @@ def test_reset(self):
8185

8286
self.assertEqual(self.history.back(), '#999')
8387
self.assertEqual(self.history.forward(), '')
88+
89+
90+
class TestHistoryFileAccess(unittest.TestCase):
91+
def setUp(self):
92+
self.filename = 'history_temp_file'
93+
self.encoding = getpreferredencoding()
94+
95+
with io.open(self.filename, 'w', encoding=self.encoding,
96+
errors='ignore') as f:
97+
f.write('#1\n#2\n')
98+
99+
def test_load(self):
100+
history = History()
101+
102+
history.load(self.filename, self.encoding)
103+
self.assertEqual(history.entries, ['#1', '#2'])
104+
105+
def test_append_reload_and_write(self):
106+
history = History()
107+
108+
history.append_reload_and_write('#3', self.filename, self.encoding)
109+
self.assertEqual(history.entries, ['#1', '#2', '#3'])
110+
111+
history.append_reload_and_write('#4', self.filename, self.encoding)
112+
self.assertEqual(history.entries, ['#1', '#2', '#3', '#4'])
113+
114+
def test_save(self):
115+
history = History(['#1', '#2', '#3', '#4'])
116+
117+
# save only last 2 lines
118+
history.save(self.filename, self.encoding, lines=2)
119+
120+
# empty the list of entries and load again from the file
121+
history.entries = ['']
122+
history.load(self.filename, self.encoding)
123+
124+
self.assertEqual(history.entries, ['#3', '#4'])
125+
126+
def tearDown(self):
127+
try:
128+
os.remove(self.filename)
129+
except OSError:
130+
pass

0 commit comments

Comments
 (0)