Added fit.ActionFixture and fixed the loader. Added ListAdapter.
[stuff.git] / pyfit / test_table.py
blobd9499a72c67d1de50c552f2f533fa1193358e96f
1 import unittest
2 from xml.dom import minidom, Node
3 import re
4 import sys
5 from table import *
6 from util import *
7 sys.path.append('examples')
8 from CalculateDiscount import *
10 class TestTable(unittest.TestCase):
12 def test_new_table(self):
13 wiki = '''
14 |zero|
15 |one |1|2|
16 |two |3|4|
17 |three|5|6|
18 '''
20 html = wiki_table_to_html(wiki)
21 table = Table(html)
22 cell = table.rows[2][1]
23 cell.passed()
24 # x = minidom.parseString(html)
25 # t = x.childNodes[0]
27 #print rows
29 def test_null_table(self):
30 html = '<table><tr><td></td></tr></table>'
31 table = Table(html)
32 self.assertEqual(table.rows[0][0], '')
34 def test_can_read_cell(self):
35 html = '<table><tr><td>50.0</td></tr></table>'
36 table = Table(html)
37 self.assertEqual(str(table.rows[0][0]), '50.0')
39 def test_can_read_cell_with_newline(self):
40 html = '<table><tr><td>amount</td>\n</tr></table>'
41 table = Table(html)
42 self.assertEqual(str(table.rows[0][0]), 'amount')
44 def test_can_read_formatted_text(self):
45 html = '<table><tr><td><i>amount</i></td>\n</tr></table>'
46 table = Table(html)
47 self.assertEqual(str(table.rows[0][0]), 'amount')
49 def test_can_get_second(self):
50 html = '<table><tr><td>50.0</td><td>52.0</td></tr></table>'
51 table = Table(html)
52 self.assertEqual(str(table.rows[0][1]), '52.0')
53 self.assertEqual(str(table.cell(col=1, row=0)), '52.0')
55 def test_can_get_second_with_extra_text_node(self):
56 html = '<table><tr><td>50.0</td>ignore<td>52.0</td></tr></table>'
57 table = Table(html)
58 self.assertEqual(str(table.cell(col=1, row=0)), '52.0')
60 def test_can_iterate_over_table(self):
61 html = '<table><tr><td>50.0</td></tr></table>'
62 table = Table(html)
63 for row in table.rows:
64 for col in row:
65 self.assertEqual(str(col), "50.0")
67 def test_cell_can_pass(self):
68 cell = Cell(minidom.parseString('<td>50.0</td>').childNodes[0])
69 cell.passed()
70 self.assertEqual('<td class="pass">50.0</td>', cell.data.toxml())
72 def test_cell_can_fail(self):
73 cell = Cell(minidom.parseString('<td>50.0</td>').childNodes[0])
74 cell.failed(49.5)
75 self.assertEqual('<td class="fail">50.0 <span class="fit_label">expected</span><hr>49.5 </hr>'
76 '<span class="fit_label">actual</span></td>', cell.data.toxml())
78 def test_cell_can_be_missing(self):
79 cell = Cell(minidom.parseString('<td>50.0</td>').childNodes[0])
80 cell.missing()
81 self.assertEqual('<td class="fail">50.0 <span class="fit_label">missing</span></td>',
82 cell.data.toxml())
84 def test_cell_can_be_surplus(self):
85 cell = Cell(minidom.parseString('<td>50.0</td>').childNodes[0])
86 cell.surplus()
87 self.assertEqual('<td class="fail">50.0 <span class="fit_label">surplus</span></td>',
88 cell.data.toxml())
90 def test_cell_can_have_error(self):
91 cell = Cell(minidom.parseString('<td>amount</td>').childNodes[0])
92 cell.error('some message')
93 self.assertEqual(
94 '<td class="error">amount<hr>some message</hr></td>',
95 str(cell.data.toxml()))
97 def test_adding_row(self):
98 html = '<table><tr><td>name</td><td>room</td></tr></table>'
99 table = Table(html)
100 table.append_row(['anna', 'lotr'])
101 #print table.data.toxml()
102 #self.assertEqual(table.rows, [ [], ['anna', 'lotr'] ])
104 def _test_iterator_can_get_n_fields(self):
105 html = '<table>' \
106 '<tr><td>add</td>' \
107 '<td>arg1</td>' \
108 '<td>arg2</td>\n' \
109 '</tr>' \
110 '</table>'
112 table = Table(html)
113 row = iter(table.rows()).next()
114 it = RowIter(iter(row))
115 s = []
116 for cell in it:
117 s.append(str(cell))
118 self.assertEqual(['add', 'arg1', 'arg2'], s)
119 #print s
120 it = RowIter(iter(row))
121 for cell in it:
122 self.assertEqual('add', str(cell))
123 self.assertEqual(['arg1', 'arg2'], it.get(2))
125 def test_fixture(self):
126 html = '<table border="1" cellspacing="0">\n' \
127 '<tr><td colspan="2">CalculateDiscount</td>\n' \
128 '</tr>\n' \
129 '<tr><td><i>amount</i></td>\n' \
130 '<td><i>discount()</i></td>' \
131 '</tr>\n' \
132 '<tr><td>0.00</td>\n' \
133 '<td>0.00</td>\n' \
134 '</tr>\n' \
135 '</table>\n'
137 table = Table(html)
138 rows = table.rows
139 #row = rows.next()
140 #it = RowIter(iter(row))
141 name = table.rows[0][0]
143 def CreateFixture(name):
144 fixture = globals()[name]()
145 fixture.adapters = DefaultAdapters()
146 return fixture
148 fixture = CreateFixture(str(name))
149 fixture.process(table)
151 self.assertEqual('CalculateDiscount', str(name))
152 #print table.doc.toxml()
154 def _test_td_with_newline(self):
155 html = '<table><tr><td colspan="2">CalculateDiscount</td>\n</tr></table>'
156 table = Table(html)
157 self.assertEqual('CalculateDiscount', table.name())
159 def _test_action_fixture(self):
160 html = '<table border="1" cellspacing="0">\n' \
161 '<tr><td colspan="2">CalculateDiscount</td>\n' \
162 '</tr>\n' \
163 '<tr><td><i>amount</i></td>\n' \
164 '<td><i>discount()</i></td>' \
165 '</tr>\n' \
166 '<tr><td>0.00</td>\n' \
167 '<td>0.00</td>\n' \
168 '</tr>\n' \
169 '</table>\n' \
170 '\n' \
171 '<table border="1" cellspacing="0">\n' \
172 '<tr><td colspan="2">CalculateDiscount</td>\n' \
173 '</tr>\n' \
174 '<tr><td><i>amount</i></td>\n' \
175 '<td><i>discount()</i></td>' \
176 '</tr>\n' \
177 '<tr><td>0.00</td>\n' \
178 '<td>0.00</td>\n' \
179 '</tr>\n' \
180 '</table>\n'
182 doc = Document(html)
183 #doc.visit_tables()
184 html = doc.html()
186 def _test_flow(self):
187 table = [1, 2, 3]
188 class Flow(object):
189 def first(self, table):
190 return self.second
192 def second(self, table):
193 pass
195 f = Flow()
197 x1 = f.first
198 x2 = x1(table)
200 # instance - marketPicture
201 # class - MarketPicture
203 x2(table)
205 def _test_setup(self):
206 xml = open('xml').read()
207 #print xml
208 doc = Document(xml)
209 #print doc.doc
210 class Fake(object):
211 def report(self):
212 pass
213 def on_table(self, table):
214 print table.name()
215 pass
216 doc.visit_tables(Fake())