summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorJohan Dahlin <johan@gnome.org>2010-09-02 16:42:15 -0300
committerJohan Dahlin <johan@gnome.org>2010-09-02 16:43:57 -0300
commit64fe43160220f2145e8ce903772251f335e8ca2a (patch)
treee55d4d72847dd32549d130de0ebac9dd0ff6d211 /tests
parentbbd1aa9450e54ad57c017f408dae5937dcf074ac (diff)
[tests] Add test infrastructure for warnings
Add an infrastructure for testing warnings, add two examples how to verify warnings
Diffstat (limited to 'tests')
-rw-r--r--tests/Makefile.am2
-rw-r--r--tests/warn/Makefile.am10
-rw-r--r--tests/warn/common.h1
-rw-r--r--tests/warn/return-gobject.h5
-rw-r--r--tests/warn/unresolved-type.h7
-rw-r--r--tests/warn/warningtester.py104
6 files changed, 128 insertions, 1 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 617339d..c95a4cc 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1,6 +1,6 @@
include $(top_srcdir)/common.mk
-SUBDIRS = . scanner repository offsets
+SUBDIRS = . scanner repository offsets warn
EXTRA_DIST=
BUILT_SOURCES=
diff --git a/tests/warn/Makefile.am b/tests/warn/Makefile.am
new file mode 100644
index 0000000..a1e274d
--- /dev/null
+++ b/tests/warn/Makefile.am
@@ -0,0 +1,10 @@
+include $(top_srcdir)/common.mk
+
+warning_tests = \
+ return-gobject.h \
+ unresolved-type.h
+
+EXTRA_DIST = warningtester.py common.h $(warning_tests)
+
+check-local:
+ @PYTHONPATH=$(top_builddir):$(top_srcdir) $(PYTHON) $(srcdir)/warningtester.py $(warning_tests)
diff --git a/tests/warn/common.h b/tests/warn/common.h
new file mode 100644
index 0000000..8ea62fa
--- /dev/null
+++ b/tests/warn/common.h
@@ -0,0 +1 @@
+#include <glib-object.h>
diff --git a/tests/warn/return-gobject.h b/tests/warn/return-gobject.h
new file mode 100644
index 0000000..163d50c
--- /dev/null
+++ b/tests/warn/return-gobject.h
@@ -0,0 +1,5 @@
+#include "common.h"
+
+GObject * test_get_object(void);
+
+// EXPECT:3: Warning: Test: test_get_object: return value: Missing (transfer) annotation
diff --git a/tests/warn/unresolved-type.h b/tests/warn/unresolved-type.h
new file mode 100644
index 0000000..3ee92b7
--- /dev/null
+++ b/tests/warn/unresolved-type.h
@@ -0,0 +1,7 @@
+#include "common.h"
+
+typedef struct {
+ int i;
+} MyStruct;
+
+// EXPECT:5: Warning: Test: symbol='MyStruct': Unknown namespace for identifier 'MyStruct'
diff --git a/tests/warn/warningtester.py b/tests/warn/warningtester.py
new file mode 100644
index 0000000..66c11a6
--- /dev/null
+++ b/tests/warn/warningtester.py
@@ -0,0 +1,104 @@
+import difflib
+import os
+import os.path
+import sys
+from StringIO import StringIO
+
+from giscanner.annotationparser import AnnotationParser
+from giscanner.ast import Include, Namespace
+from giscanner.introspectablepass import IntrospectablePass
+from giscanner.maintransformer import MainTransformer
+from giscanner.message import MessageLogger
+from giscanner.sourcescanner import SourceScanner
+from giscanner.transformer import Transformer
+from giscanner.scannermain import process_packages
+
+currentdir = os.path.dirname(os.path.abspath(sys.argv[0]))
+current_name = os.path.basename(currentdir)
+path = os.path.abspath(os.path.join(currentdir, '..', ''))
+
+class Options:
+ def __init__(self):
+ self.cpp_includes = []
+ self.cpp_defines = []
+ self.cpp_undefines = []
+ self.library_paths = []
+
+def _diff(orig, new, short):
+ def _tolines(s):
+ return [s + '\n' for line in s.split('\n')]
+ lines = difflib.unified_diff(_tolines(orig),
+ _tolines(new))
+ if not lines:
+ return
+
+ diff = False
+ try:
+ first = lines.next()
+ diff = True
+ except StopIteration:
+ pass
+ else:
+ print repr(orig), repr(new)
+ print 'ERROR: while comparing %s:' % (short, )
+ for line in list(lines)[2:]:
+ print '%s: %s' % (short, line[:-1])
+
+ return diff
+
+def check(args):
+ filenames = args
+
+ output = StringIO()
+ namespace = Namespace("Test", "1.0")
+ logger = MessageLogger.get(namespace=namespace,
+ output=output)
+ logger.enable_warnings(True)
+ transformer = Transformer(namespace)
+ transformer.register_include(Include.from_string("GObject-2.0"))
+
+ ss = SourceScanner()
+
+ options = Options()
+ exit_code = process_packages(options, ['gobject-2.0'])
+ if exit_code:
+ sys.exit(exit_code)
+ ss.set_cpp_options(options.cpp_includes,
+ options.cpp_defines,
+ options.cpp_undefines)
+ ss.parse_files(filenames)
+ ss.parse_macros(filenames)
+ transformer.parse(ss.get_symbols())
+
+ ap = AnnotationParser()
+ blocks = ap.parse(ss.get_comments())
+
+ main = MainTransformer(transformer, blocks)
+ main.transform()
+
+ final = IntrospectablePass(transformer)
+ final.validate()
+
+ warnings = output.getvalue()[:-1].split('\n')
+
+ failed_tests = 0
+ for warning in warnings:
+ filename, actual = warning.split(":", 1)
+ fd = open(filename)
+ data = fd.read()
+ pos = data.find("EXPECT:")
+ if pos == -1:
+ raise SystemExit("%s: unexpected warning %s" % (filename, warning,))
+ expected = data[pos+7:]
+ while expected.endswith('\n'):
+ expected = expected[:-1]
+ if _diff(actual, expected, filename):
+ failed_tests += 1
+
+ print 'PASS: %d of %d tested passed' % (len(filenames) - failed_tests,
+ len(filenames))
+
+ if failed_tests:
+ raise SystemExit("ERROR: some tests failed")
+
+sys.exit(check(sys.argv[1:]))