forked from cs327e-spring-2014/xml-tests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adastley-TestXML.py
74 lines (60 loc) · 3.5 KB
/
adastley-TestXML.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
#!/usr/bin/env python
from xml.etree.ElementTree import fromstring
from XML import ReferenceType, FindMatchesInElement, CheckChildren, SolveXMLSearch
import StringIO
import unittest
# Class defining various unit tests
class XMLUnitTests(unittest.TestCase):
# Testing of FindMatchesInElement
def test_findMatches1(self):
sourceElement = fromstring("<red><green><blue></blue><yellow></yellow></green></red>")
searchPatternElement = fromstring("<green><yellow></yellow></green>")
matches = []
FindMatchesInElement(sourceElement, searchPatternElement, ReferenceType(1), matches)
answer = [2]
self.assert_(len(answer) == len(matches) and all(answer[i] == matches[i] for i in range(len(answer)-1)))
def test_findMatches2(self):
sourceElement = fromstring("<red><green><blue></blue><yellow></yellow></green></red>")
searchPatternElement = fromstring("<green><blue></blue></green>")
matches = []
FindMatchesInElement(sourceElement, searchPatternElement, ReferenceType(1), matches)
answer = [2]
self.assert_(len(answer) == len(matches) and all(answer[i] == matches[i] for i in range(len(answer)-1)))
def test_findMatches3(self):
sourceElement = fromstring("<THU><Team><ACRush></ACRush><Jelly></Jelly><Cooly></Cooly></Team><JiaJia><Team><Ahyangyi></Ahyangyi><Dragon></Dragon><Cooly><Amber></Amber></Cooly></Team></JiaJia></THU>")
searchPatternElement = fromstring("<Team><Cooly></Cooly></Team>")
matches = []
FindMatchesInElement(sourceElement, searchPatternElement, ReferenceType(1), matches)
answer = [2, 7]
self.assert_(len(answer) == len(matches) and all(answer[i] == matches[i] for i in range(len(answer)-1)))
# Testing of CheckChildren
def test_checkChildren1(self):
sourceElement = fromstring("<green><blue></blue><yellow></yellow></green>")
searchPatternElement = fromstring("<green><yellow></yellow></green>")
self.assert_(CheckChildren(sourceElement, searchPatternElement))
def test_checkChildren2(self):
sourceElement = fromstring("<Team><ACRush></ACRush><Jelly></Jelly><Cooly></Cooly></Team>")
searchPatternElement = fromstring("<Team><Dragon></Dragon></Team>")
self.assert_(not CheckChildren(sourceElement, searchPatternElement))
def test_checkChildren3(self):
sourceElement = fromstring("<Team><ACRush></ACRush><Jelly></Jelly><Cooly></Cooly></Team>")
searchPatternElement = fromstring("<Team><Cooly></Cooly></Team>")
self.assert_(CheckChildren(sourceElement, searchPatternElement))
# Testing of the entire process
def test_solve1(self):
r = StringIO.StringIO("<red><green><blue></blue><yellow></yellow></green></red><red><green></green></red>")
w = StringIO.StringIO()
SolveXMLSearch(r, w)
self.assert_(w.getvalue() == "1\n1\n")
def test_solve2(self):
r = StringIO.StringIO("<red><green><blue></blue><yellow></yellow></green></red><green><blue></blue></green>")
w = StringIO.StringIO()
SolveXMLSearch(r, w)
self.assert_(w.getvalue() == "1\n2\n")
def test_solve3(self):
r = StringIO.StringIO("<THU><Team><ACRush></ACRush><Jelly></Jelly><Cooly></Cooly></Team><JiaJia><Team><Ahyangyi></Ahyangyi><Dragon></Dragon><Cooly><Amber></Amber></Cooly></Team></JiaJia></THU><Team><Cooly></Cooly></Team>")
w = StringIO.StringIO()
SolveXMLSearch(r, w)
self.assert_(w.getvalue() == "2\n2\n7\n")
# Call the test
unittest.main()