File: test_xml.py

package info (click to toggle)
gamera 1:3.4.2+git20160808.1725654-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 22,312 kB
  • ctags: 24,991
  • sloc: xml: 122,324; ansic: 52,869; cpp: 50,664; python: 35,034; makefile: 118; sh: 101
file content (100 lines) | stat: -rw-r--r-- 3,558 bytes parent folder | download | duplicates (3)
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
import py.test

from gamera.core import *
init_gamera()

from gamera import gamera_xml

features = ['aspect_ratio', 'moments', 'volume64regions']

# check for identical training data *except features*
# feature values are subject to rounding errors and
# are thus randomly different => test files not portable
def equal_files(filea, fileb, gz=False):
   if gz:
      import gzip
      a = gzip.open(filea, "r")
      b = gzip.open(fileb, "r")
   else:
      a = open(filea, "r")
      b = open(fileb, "r")
   i = 0
   linea = a.readline()
   lineb = b.readline()
   feature = False
   while linea:
      i += 1
      if not lineb:
         # a longer than b
         return False
      if linea != lineb:
         return False
      if "<feature name=" in linea:
         while linea and "</feature>" not in linea:
            linea = a.readline()
         while lineb and "</feature>" not in lineb:
            lineb = b.readline()
      linea = a.readline()
      lineb = b.readline()
   if lineb:
      # b longer than a
      return False
   return True

# High-level API
def test_glyphs_from_xml():
   glyphs = gamera_xml.glyphs_from_xml("data/testline.xml")
   assert len(glyphs) == 66
      
def test_glyphs_with_features_from_xml():
   glyphs = gamera_xml.glyphs_with_features_from_xml(
      "data/testline.xml", ["area", "aspect_ratio"])
   assert len(glyphs) == 66
   assert len(glyphs[0].features) == 2

def test_glyphs_to_xml():
   glyphs = gamera_xml.glyphs_from_xml("data/testline.xml")
   gamera_xml.glyphs_to_xml("tmp/testline_test1.xml", glyphs, False)
   assert equal_files("tmp/testline_test1.xml", "data/testline_test1.xml")

def test_glyphs_to_xml_with_features():
   glyphs = gamera_xml.glyphs_with_features_from_xml("data/testline.xml", feature_functions=features)
   gamera_xml.glyphs_to_xml("tmp/testline_test2.xml", glyphs, True)
   assert equal_files("tmp/testline_test2.xml", "data/testline_test2.xml")

def test_glyphs_from_xml_gz():
   glyphs = gamera_xml.glyphs_from_xml("data/testline.xml.gz")
   assert len(glyphs) == 66

def test_glyphs_to_xml_gz():
   glyphs = gamera_xml.glyphs_from_xml("data/testline.xml")
   gamera_xml.glyphs_to_xml("tmp/testline_test1.xml.gz", glyphs, False)
   assert equal_files("tmp/testline_test1.xml.gz", "data/testline_test1.xml.gz", gz=True)

# Low-level API
def test_write_xml():
   glyphs = gamera_xml.glyphs_from_xml("data/testline.xml")
   writer = gamera_xml.WriteXML(glyphs)
   result_string = writer.string()
   writer.write_filename("tmp/testline_test3.xml")
   assert equal_files("tmp/testline_test3.xml", "data/testline_test3.xml")

def test_symbol_table():
   symbol_table = gamera_xml.LoadXML(parts=['symbol_table']).parse_filename("data/symbol_table.xml").symbol_table
   gamera_xml.WriteXMLFile([], symbol_table).write_filename("tmp/symbol_table.xml")
   assert equal_files("data/symbol_table.xml", "tmp/symbol_table.xml")

   symbol_table = gamera_xml.LoadXML(parts=['symbol_table']).parse_filename("data/testline.xml").symbol_table
   gamera_xml.WriteXMLFile([], symbol_table).write_filename("tmp/symbol_table2.xml")
   assert equal_files("data/symbol_table.xml", "tmp/symbol_table2.xml")

# Error cases
def test_missing_attributes():
   def _test_missing_attributes():
      glyphs = gamera_xml.glyphs_from_xml("data/missing_attributes.xml")
   py.test.raises(gamera_xml.XMLError, _test_missing_attributes)

def test_malformed():
   def _test_malformed():
      glyphs = gamera_xml.glyphs_from_xml("data/malformed.xml")
   py.test.raises(gamera_xml.XMLError, _test_malformed)