Debian Codegraph @

tests/Test.py

raw
1 #!/usr/bin/python
2 #
3 # Project: lib Instrument Patch Python testing suite
4 # File: Testing.py
5 # Descr: Testing support functions used by other tests
6 # Author: Element Green <[email protected]>
7 # Date: 2016-07-01
8 # License: Public Domain
9 #
10 import sys
11 import os
12 import argparse
13 import gi
14 from gi.repository import GLib
15 from gi.repository import GObject
16
17 gi.require_version ('Ipatch', '1.1')
18 from gi.repository import Ipatch
19
20 ErrorCount = 0
21 LogMask = 0
22 Verbose = 1
23 TEST_DATA_PATH = "test-data/"
24
25 def createArgParser (description):
26 parser = argparse.ArgumentParser (description=description)
27 parser.add_argument ('-v', '--verbose', action='count', help='Increase verbosity')
28 parser.add_argument ('--quiet', action='store_true', help='Only display errors')
29 return parser
30
31 def parseArgs (parser):
32 global Verbose
33
34 args = parser.parse_args ()
35
36 if args.verbose > 0:
37 Verbose = args.verbose + 1
38
39 if args.quiet:
40 Verbose = 0
41
42 return args
43
44 def LogTrap (log_domain, log_level, message, user_data):
45 global LogMask
46
47 LogMask |= log_level
48 GLib.log_default_handler ("Testing", log_level, message, 0)
49
50 def exit ():
51 if LogMask: print ("GLib warning/error messages occurred!")
52 if ErrorCount > 0: print ("%d Errors occurred!" % ErrorCount)
53 sys.exit (0 if ErrorCount == 0 and LogMask == 0 else 1)
54
55 def info (s):
56 if Verbose >= 2:
57 print (s)
58
59 def msg (s):
60 if Verbose >= 1:
61 print (s)
62
63 def error (s):
64 global ErrorCount
65
66 print (s)
67 ErrorCount += 1
68
69 def compareItems (item1, item2, recurse=True, blackList=()):
70 """Compare two Ipatch.Item objects"""
71
72 if type (item1) != type (item2):
73 raise RuntimeError ('Item type %s != %s' % (str (type (item1)), str (type (item2))))
74
75 typename = str (type (item1))
76 props = sorted ([prop for prop in item1.list_properties ()], key=lambda prop: prop.name)
77
78 blprops = [propname for typ, propname in blackList if isinstance (item1, typ)]
79
80 for p in props:
81 if p.name in blprops: continue
82
83 val1 = item1.get_property (p.name)
84 val2 = item2.get_property (p.name)
85
86 valtype = p.value_type
87
88 if val1 is None != val2 is None:
89 raise RuntimeError ('Property value None mismatch for property %s:%s'
90 % (typename, p.name))
91
92 if valtype.is_a (GObject.Object):
93 if val1 is not None and recurse:
94 compareItems (val1, val2, recurse=False, blackList=blackList) # Don't recurse into object fields (causes recursion loops)
95 elif valtype.is_a (Ipatch.Range):
96 if val1.low != val2.low or val1.high != val2.high:
97 RuntimeError ('Range value mismatch for property %s:%s, %d-%d != %d-%d'
98 % (typename, p.name, val1.low, val1.high, val2.low, val2.high))
99 elif valtype.is_a (GObject.GBoxed):
100 if valtype.name == 'IpatchSF2ModList':
101 if len (val1) != len (val2):
102 RuntimeError ('Modulator list length mismatch for property %s:%s, %d != %d'
103 % (typename, p.name, len (val1), len (val2)))
104
105 for i in xrange (0, len (val1)):
106 v1 = val1[i]
107 v2 = val2[i]
108
109 v1 = (v1.src, v1.amtsrc, v1.dest, v1.amount, v1.trans)
110 v2 = (v2.src, v2.amtsrc, v2.dest, v2.amount, v2.trans)
111
112 if v1 != v2:
113 RuntimeError ('Modulator value mismatch for property %s:%s, %s != %s'
114 % (typename, p.name, str (v1), str (v2)))
115 else:
116 print ('Ignoring property %s:%s of type %s'
117 % (typename, p.name, str (valtype)))
118 elif val1 != val2:
119 raise RuntimeError ('Value mismatch for property %s:%s, %s != %s'
120 % (typename, p.name, str (val1), str (val2)))
121
122 if recurse and isinstance (item1, Ipatch.Container):
123 children1 = item1.get_children ()
124 children2 = item2.get_children ()
125
126 if len (children1) != len (children2):
127 raise RuntimeError ('Container of type %s has unequal children count, %d != %d'
128 % (typename, len (children1), len (children2)))
129
130 for i in xrange (0, len (children1)):
131 compareItems (children1[i], children2[i], blackList=blackList)
132
133 # Log flags we want to trap on
134 flags = GLib.LogLevelFlags.LEVEL_WARNING | GLib.LogLevelFlags.LEVEL_ERROR \
135 | GLib.LogLevelFlags.LEVEL_CRITICAL | GLib.LogLevelFlags.FLAG_FATAL | GLib.LogLevelFlags.FLAG_RECURSION
136
137 GLib.log_set_handler (None, flags, LogTrap, None)
138
139 # Create test data directory if needed
140 try:
141 os.makedirs (TEST_DATA_PATH)
142 except OSError:
143 if not os.path.isdir (TEST_DATA_PATH):
144 raise
145