File: Test.py

package info (click to toggle)
libinstpatch 1.1.7-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,404 kB
  • sloc: ansic: 50,922; python: 429; xml: 130; makefile: 10
file content (145 lines) | stat: -rw-r--r-- 4,526 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python
#
# Project: lib Instrument Patch Python testing suite
# File:    Testing.py
# Descr:   Testing support functions used by other tests
# Author:  Element Green <element@elementsofsound.org>
# Date:    2016-07-01
# License: Public Domain
#
import sys
import os
import argparse
import gi
from gi.repository import GLib
from gi.repository import GObject

gi.require_version ('Ipatch', '1.1')
from gi.repository import Ipatch

ErrorCount = 0
LogMask = 0
Verbose = 1
TEST_DATA_PATH = "test-data/"

def createArgParser (description):
  parser = argparse.ArgumentParser (description=description)
  parser.add_argument ('-v', '--verbose', action='count', help='Increase verbosity')
  parser.add_argument ('--quiet', action='store_true', help='Only display errors')
  return parser

def parseArgs (parser):
  global Verbose

  args = parser.parse_args ()

  if args.verbose > 0:
    Verbose = args.verbose + 1

  if args.quiet:
    Verbose = 0

  return args

def LogTrap (log_domain, log_level, message, user_data):
  global LogMask

  LogMask |= log_level
  GLib.log_default_handler ("Testing", log_level, message, 0)

def exit ():
  if LogMask: print ("GLib warning/error messages occurred!")
  if ErrorCount > 0: print ("%d Errors occurred!" % ErrorCount)
  sys.exit (0 if ErrorCount == 0 and LogMask == 0 else 1)

def info (s):
  if Verbose >= 2:
    print (s)

def msg (s):
  if Verbose >= 1:
    print (s)

def error (s):
  global ErrorCount

  print (s)
  ErrorCount += 1

def compareItems (item1, item2, recurse=True, blackList=()):
  """Compare two Ipatch.Item objects"""

  if type (item1) != type (item2):
    raise RuntimeError ('Item type %s != %s' % (str (type (item1)), str (type (item2))))

  typename = str (type (item1))
  props = sorted ([prop for prop in item1.list_properties ()], key=lambda prop: prop.name)

  blprops = [propname for typ, propname in blackList if isinstance (item1, typ)]

  for p in props:
    if p.name in blprops: continue

    val1 = item1.get_property (p.name)
    val2 = item2.get_property (p.name)

    valtype = p.value_type

    if val1 is None != val2 is None:
      raise RuntimeError ('Property value None mismatch for property %s:%s'
                          % (typename, p.name))

    if valtype.is_a (GObject.Object):
      if val1 is not None and recurse:
        compareItems (val1, val2, recurse=False, blackList=blackList)   # Don't recurse into object fields (causes recursion loops)
    elif valtype.is_a (Ipatch.Range):
      if val1.low != val2.low or val1.high != val2.high:
        RuntimeError ('Range value mismatch for property %s:%s, %d-%d != %d-%d'
                      % (typename, p.name, val1.low, val1.high, val2.low, val2.high))
    elif valtype.is_a (GObject.GBoxed):
      if valtype.name == 'IpatchSF2ModList':
        if len (val1) != len (val2):
          RuntimeError ('Modulator list length mismatch for property %s:%s, %d != %d'
                        % (typename, p.name, len (val1), len (val2)))

        for i in xrange (0, len (val1)):
          v1 = val1[i]
          v2 = val2[i]

          v1 = (v1.src, v1.amtsrc, v1.dest, v1.amount, v1.trans)
          v2 = (v2.src, v2.amtsrc, v2.dest, v2.amount, v2.trans)

          if v1 != v2:
            RuntimeError ('Modulator value mismatch for property %s:%s, %s != %s'
                          % (typename, p.name, str (v1), str (v2)))
      else:
        print ('Ignoring property %s:%s of type %s'
               % (typename, p.name, str (valtype)))
    elif val1 != val2:
      raise RuntimeError ('Value mismatch for property %s:%s, %s != %s'
                          % (typename, p.name, str (val1), str (val2)))

  if recurse and isinstance (item1, Ipatch.Container):
    children1 = item1.get_children ()
    children2 = item2.get_children ()

    if len (children1) != len (children2):
      raise RuntimeError ('Container of type %s has unequal children count, %d != %d'
                          % (typename, len (children1), len (children2)))

    for i in xrange (0, len (children1)):
      compareItems (children1[i], children2[i], blackList=blackList)

# Log flags we want to trap on
flags = GLib.LogLevelFlags.LEVEL_WARNING | GLib.LogLevelFlags.LEVEL_ERROR \
 | GLib.LogLevelFlags.LEVEL_CRITICAL | GLib.LogLevelFlags.FLAG_FATAL | GLib.LogLevelFlags.FLAG_RECURSION

GLib.log_set_handler (None, flags, LogTrap, None)

# Create test data directory if needed
try: 
  os.makedirs (TEST_DATA_PATH)
except OSError:
  if not os.path.isdir (TEST_DATA_PATH):
    raise