[gobject-introspection] [tests] Add test infrastructure for warnings



commit 64fe43160220f2145e8ce903772251f335e8ca2a
Author: Johan Dahlin <johan gnome org>
Date:   Thu Sep 2 16:42:15 2010 -0300

    [tests] Add test infrastructure for warnings
    
    Add an infrastructure for testing warnings, add two
    examples how to verify warnings

 configure.ac                 |    1 +
 tests/Makefile.am            |    2 +-
 tests/warn/Makefile.am       |   10 ++++
 tests/warn/common.h          |    1 +
 tests/warn/return-gobject.h  |    5 ++
 tests/warn/unresolved-type.h |    7 +++
 tests/warn/warningtester.py  |  104 ++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 129 insertions(+), 1 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 6998119..c2d5047 100644
--- a/configure.ac
+++ b/configure.ac
@@ -235,6 +235,7 @@ tests/Makefile
 tests/offsets/Makefile
 tests/scanner/Makefile
 tests/repository/Makefile
+tests/warn/Makefile
 examples/Makefile
 docs/Makefile
 docs/reference/Makefile
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:]))



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]