pylit 0.7.10
[pylit.git] / contribs / test_filters.py
blob5aa68baf2a13a525a22da4a10865dcc24416f500
1 # Tests
2 # =====
5 from filters import *
8 # comment string for Elisp
9 COMMENT_STRING = ';; '
11 # simulate calling from pylit main
12 keyw = { 'comment_string':COMMENT_STRING }
15 # Auxiliary functions
16 # ===================
19 def test(actual_iter, expected_iter):
20 """Compare all lines in `actual_iter` with lines in `expected_iter`"""
21 actual = []
22 for line in actual_iter:
23 actual.append(line)
24 expected = []
25 for line in expected_iter:
26 expected.append(line)
27 if len(actual) == len(expected):
28 result = True
29 else:
30 result = False
31 for i in range(0, min(len(actual),len(expected))):
32 if actual[i] != expected[i]:
33 result = False
34 if result:
35 return "OK"
36 else:
37 return "Failed (expected: %s, got: %s)" \
38 % (expected, actual)
41 # Test fixtures
42 # =============
45 sc_in = IdentityFilter([
46 ";; This should be stripped.\n",
47 "This should be not.\n"
49 sc_out = IdentityFilter([
50 "This should be stripped.\n",
51 "This should be not.\n"
54 blank_lines = IdentityFilter([
55 "\n",
56 "\n",
57 "\n"
60 c2t_in_1 = IdentityFilter([
61 ";;;Commentary:\n"
63 c2t_out_1 = IdentityFilter([
64 ";; .. |elisp> ;;;Commentary:\n",
66 c2t_in_2 = IdentityFilter([
67 ";;;Commentary:\n",
68 ";; This is\n",
69 ";; a test."
71 c2t_out_2 = IdentityFilter([
72 ";; .. |elisp> ;;;Commentary:\n",
73 ";; This is\n",
74 ";; a test."
78 # Test cases
79 # ==========
82 # test ElispCode2TextFilter with one sample line
83 f = ElispCode2TextFilter(c2t_in_1, **keyw)
84 print "ElispCode2TextFilter with sample line: %s" \
85 % test(f, c2t_out_1)
87 # test ElispText2CodeFilter with one sample line
88 f = ElispText2CodeFilter(c2t_out_1, **keyw)
89 print "ElispText2CodeFilter with sample line: %s" \
90 % test(f, c2t_in_1)
92 # test ElispCode2TextFilter with one sample para
93 f = ElispCode2TextFilter(c2t_in_2, **keyw)
94 print "ElispCode2TextFilter with sample paragraph: %s" \
95 % test(f, c2t_out_2)
97 # test ElispText2CodeFilter with one sample para
98 f = ElispText2CodeFilter(c2t_out_2, **keyw)
99 print "ElispText2CodeFilter with sample paragraph: %s" \
100 % test(f, c2t_in_2)
102 # round-trip test code->txt->code with sample line
103 f = ElispText2CodeFilter(ElispCode2TextFilter(c2t_in_1, **keyw), **keyw)
104 print "round-trip test code->txt->code with sample line: %s" \
105 % test(f, c2t_in_1)
107 # round-trip test code->txt->code with sample line
108 f = ElispCode2TextFilter(ElispText2CodeFilter(c2t_out_1, **keyw), **keyw)
109 print "round-trip test txt->code->txt with sample line: %s" \
110 % test(f, c2t_out_1)
112 # DISABLED 2007-02-21 -- these need the reverse of strip_comments(),
113 # that is a function that prefix comment_string to non-code lines.
114 # But that would be duplicating PyLit's code here, so just forget it.
116 # round-trip test code->txt->code with sample para
117 #f = ElispText2CodeFilter(IdentityFilter(ElispCode2TextFilter(c2t_in_2,
118 # **keyw)), **keyw)
119 #print "round-trip test code->txt->code with sample paragraph: %s" \
120 # % test(f, c2t_in_2)
122 # round-trip test code->txt->code with sample para
123 #f = strip_comments(ElispCode2TextFilter(
124 # strip_indent(ElispText2CodeFilter(c2t_out_2, **keyw)), **keyw))
125 #print "round-trip test txt->code->txt with sample paragraph: %s" \
126 # % test(f, c2t_out_2)
128 # txt->code should preserve blank lines
129 f = ElispText2CodeFilter(blank_lines, **keyw)
130 print "txt->code with blank lines: %s" \
131 % test(f, blank_lines)
133 # code->txt should preserve blank lines
134 f = ElispCode2TextFilter(blank_lines, **keyw)
135 print "code->txt with blank lines: %s" \
136 % test(f, blank_lines)
138 # round-trip test txt->code->txt
139 f = ElispText2CodeFilter(ElispCode2TextFilter(blank_lines, **keyw), **keyw)
140 print "round-trip test txt->code->txt with blank lines: %s" \
141 % test(f, blank_lines)
143 # round-trip test code->txt->code
144 f = ElispCode2TextFilter(ElispText2CodeFilter(blank_lines, **keyw), **keyw)
145 print "round-trip test txt->code->txt with blank lines: %s" \
146 % test(f, blank_lines)