2 from xml
.dom
import minidom
, Node
7 sys
.path
.append('examples')
8 from CalculateDiscount
import *
10 class TestTable(unittest
.TestCase
):
12 def test_new_table(self
):
20 html
= wiki_table_to_html(wiki
)
22 cell
= table
.rows
[2][1]
24 # x = minidom.parseString(html)
29 def test_null_table(self
):
30 html
= '<table><tr><td></td></tr></table>'
32 self
.assertEqual(table
.rows
[0][0], '')
34 def test_can_read_cell(self
):
35 html
= '<table><tr><td>50.0</td></tr></table>'
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>'
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>'
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>'
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>'
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>'
63 for row
in table
.rows
:
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])
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])
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])
81 self
.assertEqual('<td class="fail">50.0 <span class="fit_label">missing</span></td>',
84 def test_cell_can_be_surplus(self
):
85 cell
= Cell(minidom
.parseString('<td>50.0</td>').childNodes
[0])
87 self
.assertEqual('<td class="fail">50.0 <span class="fit_label">surplus</span></td>',
90 def test_cell_can_have_error(self
):
91 cell
= Cell(minidom
.parseString('<td>amount</td>').childNodes
[0])
92 cell
.error('some message')
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>'
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
):
113 row
= iter(table
.rows()).next()
114 it
= RowIter(iter(row
))
118 self
.assertEqual(['add', 'arg1', 'arg2'], s
)
120 it
= RowIter(iter(row
))
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' \
129 '<tr><td><i>amount</i></td>\n' \
130 '<td><i>discount()</i></td>' \
132 '<tr><td>0.00</td>\n' \
140 #it = RowIter(iter(row))
141 name
= table
.rows
[0][0]
143 def CreateFixture(name
):
144 fixture
= globals()[name
]()
145 fixture
.adapters
= DefaultAdapters()
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>'
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' \
163 '<tr><td><i>amount</i></td>\n' \
164 '<td><i>discount()</i></td>' \
166 '<tr><td>0.00</td>\n' \
171 '<table border="1" cellspacing="0">\n' \
172 '<tr><td colspan="2">CalculateDiscount</td>\n' \
174 '<tr><td><i>amount</i></td>\n' \
175 '<td><i>discount()</i></td>' \
177 '<tr><td>0.00</td>\n' \
186 def _test_flow(self
):
189 def first(self
, table
):
192 def second(self
, table
):
200 # instance - marketPicture
201 # class - MarketPicture
205 def _test_setup(self
):
206 xml
= open('xml').read()
213 def on_table(self
, table
):
216 doc
.visit_tables(Fake())