early python port of the FIT test framework (http://fit.zwiki.org/)

root / FrameworkTest.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
Python translation of fit..
which is copyright (c) 2002 Cunningham & Cunningham, Inc.
Released under the terms of the GNU General License version 2 or later.
"""

import string, re, unittest
from Parse import Parse, ParseException
from Fixture import Fixture
from ColumnFixture import ColumnFixture
from TypeAdapter import adapterOnType, adapterOnField, adapterOnMethod

class FrameworkTest(unittest.TestCase):
    def testParsing(self):
        p = Parse("leader<Table foo=2>body</table>trailer", ("table",))
        self.assertEquals("leader", p.leader)
        self.assertEquals("<Table foo=2>", p.tag)
        self.assertEquals("body", p.body)
        self.assertEquals("trailer", p.trailer)
    
    def testRecursing(self):
        p = Parse("leader<table><TR><Td>body</tD></TR></table>trailer")
        self.assertEquals(None, p.body)
        self.assertEquals(None, p.parts.body)
        self.assertEquals("body", p.parts.parts.body)
    
    def testIterating(self):
        p = Parse("leader<table><tr><td>one</td><td>two</td><td>three</td></tr></table>trailer")
        self.assertEquals("one", p.parts.parts.body)
        self.assertEquals("two", p.parts.parts.more.body)
        self.assertEquals("three", p.parts.parts.more.more.body)
    
    def testIndexing(self):
        p = Parse("leader<table><tr><td>one</td><td>two</td><td>three</td></tr><tr><td>four</td></tr></table>trailer")
        self.assertEquals("one", p.at(0,0,0).body)
        self.assertEquals("two", p.at(0,0,1).body)
        self.assertEquals("three", p.at(0,0,2).body)
        self.assertEquals("three", p.at(0,0,3).body)
        self.assertEquals("three", p.at(0,0,4).body)
        self.assertEquals("four", p.at(0,1,0).body)
        self.assertEquals("four", p.at(0,1,1).body)
        self.assertEquals("four", p.at(0,2,0).body)
        self.assertEquals(1, p.size())
        self.assertEquals(2, p.parts.size())
        self.assertEquals(3, p.parts.parts.size())
        self.assertEquals("one", p.leaf().body)
        self.assertEquals("four", p.parts.last().leaf().body)

    def testParseException(self):
        try:
            p = Parse("leader<table><tr><th>one</th><th>two</th><th>three</th></tr><tr><td>four</td></tr></table>trailer")
        except ParseException, e:
            self.assertEquals(17, e.offset)
            self.assertEquals("Can't find tag: td", e.message)
            return
        self.fail("expected exception not thrown")

    def testText(self):
        tags =("td",)
        p = Parse("<td>a&lt;b</td>", tags)
        self.assertEquals("a&lt;b", p.body)
        self.assertEquals("a<b", p.text())
        p = Parse("<td>\ta&gt;b&nbsp;&amp;&nbsp;b>c &&&nbsp;</td>", tags)
        self.assertEquals("a>b & b>c &&", p.text())
        p = Parse("<TD><P><FONT FACE=\"Arial\" SIZE=2>GroupTestFixture</FONT></TD>", tags)
        self.assertEquals("GroupTestFixture",p.text())

    def testUnescape(self):
        self.assertEquals("a<b", Parse().unescape("a&lt;b"))
        self.assertEquals("a>b & b>c &&", Parse().unescape("a&gt;b&nbsp;&amp;&nbsp;b>c &&"))
    
    def testUnformat(self):
        self.assertEquals("ab",Parse().unformat("<font size=+1>a</font>b"))
        self.assertEquals("ab",Parse().unformat("a<font size=+1>b</font>"))
        self.assertEquals("a<b",Parse().unformat("a<b"))

    def testTypeAdapter(self):
        f = self.TestFixture()
        a = adapterOnField(f, 'sampleInt')
        a.set(a.parse("123456"))
        self.assertEquals(123456, f.sampleInt)
        self.assertEquals("-234567", str(a.parse("-234567")))
        a = adapterOnMethod(f, f.__class__.pi)
        self.assert_(abs(3.14159 - a.invoke()) < 0.00001)
        self.assertEquals(3.14159862, a.invoke())
        a = adapterOnField(f, 'sampleString')
        a.set(a.parse("xyzzy"))
        self.assertEquals('xyzzy', f.sampleString)
        a = adapterOnField(f, 'sampleFloat')
        a.set(a.parse("6.02e23"))
        self.assertEquals(6.02e23, f.sampleFloat) #, 1e17
#        a = adapterOnField(f, 'sampleList')
#        a.set(a.parse("1,2,3"))
#        self.assertEquals(1, f.sampleArray[0])
#        self.assertEquals(2, f.sampleArray[1])
#        self.assertEquals(3, f.sampleArray[2])
#        self.assertEquals("1,2,3", a.toString(f.sampleArray))
#        self.assertTrue(a.equals((1,2,3), f.sampleArray))
#         a = TypeAdapter.on(f, f.getClass().getField("sampleDate"))
#         Date date = new Date(49,4,26)
#         a.set(a.parse(DateFormat.getDateInstance().format(date)))
#         self.assertEquals(date, f.sampleDate)
    
    class TestFixture(ColumnFixture): ## used in testTypeAdapter
         sampleInt = 0
         sampleFloat = 0.0
         def pi(self): return 3.14159862
         sampleString = ''
         sampleList = []
         sampleDate = ''

#     def testScientificDouble(self):
#         Double pi = new Double(3.141592865)
#         self.assertEquals(ScientificDouble.valueOf("3.14"), pi)
#         self.assertEquals(ScientificDouble.valueOf("3.141"), pi)
#         self.assertEquals(ScientificDouble.valueOf("3.1415"), pi)
#         self.assertEquals(ScientificDouble.valueOf("3.14159"), pi)
#         self.assertEquals(ScientificDouble.valueOf("3.141592865"), pi)
#         self.assertTrue(!ScientificDouble.valueOf("3.140").equals(pi))
#         self.assertTrue(!ScientificDouble.valueOf("3.144").equals(pi))
#         self.assertTrue(!ScientificDouble.valueOf("3.1414").equals(pi))
#         self.assertTrue(!ScientificDouble.valueOf("3.141592863").equals(pi))
#         Float av = new Float(6.02e23)
#         self.assertEquals(ScientificDouble.valueOf("6.0e23"), av)

    def testEscape(self):
        junk = "!@#$%^*()_-+={|[]\\:\";',./?`"
        self.assertEquals(junk, Fixture().escape(junk))
        self.assertEquals("", Fixture().escape(""))
        self.assertEquals("&lt;", Fixture().escape("<"))
        self.assertEquals("&lt;&lt;", Fixture().escape("<<"))
        self.assertEquals("x&lt;", Fixture().escape("x<"))
        self.assertEquals("&amp;", Fixture().escape("&"))
        self.assertEquals("&lt;&amp;&lt;", Fixture().escape("<&<"))
        self.assertEquals("&amp;&lt;&amp;", Fixture().escape("&<&"))
        self.assertEquals("a &lt; b &amp;&amp; c &lt; d", Fixture().escape("a < b && c < d"))


def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(FrameworkTest))
    return suite

def main():
    unittest.TextTestRunner().run(test_suite())

if __name__ == '__main__':
    main()