-
Notifications
You must be signed in to change notification settings - Fork 2
/
LispFormatStream.py
39 lines (37 loc) · 1.43 KB
/
LispFormatStream.py
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
from StringIO import StringIO
class LispFormatStream (StringIO):
def __init__(self): self.buffer, self.indent = StringIO(), [0]
def getvalue(self): return self.buffer.getvalue()
def prin(self, str):
col = self.indent[-1]
#self.buffer.write(" "*col)
for char in str:
self.buffer.write(char)
if char == "(":
self.indent += [col]
elif char == ")":
self.indent = self.indent
col += 1
self.buffer.write("\n")
if __name__ == "__main__":
import unittest
class LispFormatStreamTest(unittest.TestCase):
def setUp(self):
self.lfs = LispFormatStream()
def testEasy(self):
self.lfs.prin("(plain)")
self.lfs.prin("(plain)")
self.assertEqual("(plain)\n(plain)\n", self.lfs.getvalue())
def testBreakOpen(self):
self.lfs.prin("(plain")
self.lfs.prin("(plain))")
self.assertEqual("(plain\n (plain)\n", self.lfs.getvalue())
def testBreakOpenSecond(self):
self.lfs.prin("(plain second")
self.lfs.prin("foo)")
self.assertEqual("(plain second\n foo)\n", self.lfs.getvalue())
def testBreakDoubleOpen(self):
self.lfs.prin("'((foo bar)")
self.lfs.prin("(foo bar))")
self.assertEqual("'((foo bar)\n (foo bar))\n", self.lfs.getvalue())
unittest.main()