5 from StringIO import StringIO
8 path=os.getenv('UNINSTALLED_INTROSPECTION_SRCDIR', None)
9 assert path is not None
10 sys.path.insert(0, path)
12 # Not correct, but enough to get the tests going uninstalled
13 __builtin__.__dict__['DATADIR'] = path
15 from giscanner.annotationparser import AnnotationParser
16 from giscanner.ast import Include, Namespace
17 from giscanner.introspectablepass import IntrospectablePass
18 from giscanner.maintransformer import MainTransformer
19 from giscanner.message import MessageLogger
20 from giscanner.sourcescanner import SourceScanner
21 from giscanner.transformer import Transformer
22 from giscanner.scannermain import process_packages
24 currentdir = os.path.dirname(os.path.abspath(sys.argv[0]))
25 current_name = os.path.basename(currentdir)
26 path = os.path.abspath(os.path.join(currentdir, '..', ''))
27 top_srcdir = os.environ['UNINSTALLED_INTROSPECTION_SRCDIR']
28 top_builddir = os.environ['TOP_BUILDDIR']
32 self.cpp_includes = []
34 self.cpp_undefines = []
35 self.library_paths = []
37 def _diff(orig, new, short):
39 return [s + '\n' for line in s.split('\n')]
40 lines = difflib.unified_diff(_tolines(orig),
52 print 'ERROR: while comparing %s:' % (short, )
53 for line in list(lines)[2:]:
54 print '%s: %r' % (short, line[:-1])
58 def _extract_expected(filename):
63 for line in data.split('\n'):
64 if line.startswith('// EXPECT:'):
68 sort_key = int(line.split(":")[1])
71 retval.append((sort_key, line[10:]))
78 namespace = Namespace("Test", "1.0")
79 logger = MessageLogger.get(namespace=namespace,
81 logger.enable_warnings(True)
82 transformer = Transformer(namespace)
83 transformer.set_include_paths([os.path.join(top_srcdir, 'gir'), top_builddir])
84 transformer.register_include(Include.from_string("GObject-2.0"))
89 exit_code = process_packages(options, ['gobject-2.0'])
92 ss.set_cpp_options(options.cpp_includes,
94 options.cpp_undefines)
95 ss.parse_files([filename])
96 ss.parse_macros([filename])
97 transformer.parse(ss.get_symbols())
99 ap = AnnotationParser()
100 blocks = ap.parse(ss.get_comments())
102 main = MainTransformer(transformer, blocks)
105 final = IntrospectablePass(transformer, blocks)
108 raw = output.getvalue()
109 if raw.endswith('\n'):
111 warnings = raw.split('\n')
114 expected_warnings = _extract_expected(filename)
117 if len(expected_warnings) != len(warnings):
119 "ERROR in %r: expected %d warnings, but got %d:\n"
120 "----\nexpected:\n%s\n----\ngot:\n%s\n----" % (
121 os.path.basename(filename),
122 len(expected_warnings), len(warnings),
123 '\n'.join([w[1] for w in expected_warnings]),
124 '\n'.join([w.split(':', 2)[2][1:] for w in warnings])))
125 for warning, (sort_key, expected) in zip(warnings, expected_warnings):
126 actual = warning.split(":", 1)[1]
127 if _diff(expected, actual, filename):
128 raise SystemExit("ERROR: tests %r failed" % (filename, ))
130 sys.exit(check(sys.argv[1:]))