[gobject-introspection] tests: Add transformer typedef struct tests



commit 4800d30d01d5640cdbd4489daf39eed53496ff9d
Author: Simon Feltman <sfeltman src gnome org>
Date:   Tue Dec 24 21:02:09 2013 -0800

    tests: Add transformer typedef struct tests
    
    Add tests for various combinations of struct tags and typedefs. This
    includes tests for orderings of a typedef prior to struct definition,
    reversed, all in one, and multiple typdefs. The currently failing tests have
    been marked as "expectedFailure" so the test suite doesn't bail out.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=581525

 tests/scanner/Makefile.am         |    4 +-
 tests/scanner/test_transformer.py |  240 +++++++++++++++++++++++++++++++++++++
 2 files changed, 243 insertions(+), 1 deletions(-)
---
diff --git a/tests/scanner/Makefile.am b/tests/scanner/Makefile.am
index e3fd432..999116d 100644
--- a/tests/scanner/Makefile.am
+++ b/tests/scanner/Makefile.am
@@ -192,7 +192,9 @@ else
 CHECKDOCS =
 endif
 
-PYTESTS = test_sourcescanner.py
+PYTESTS = \
+       test_sourcescanner.py \
+       test_transformer.py
 
 XFAIL_TESTS = Typedefs-1.0.gir
 TESTS = Headeronly-1.0.gir $(CHECKGIRS) $(CHECKDOCS) $(TYPELIBS) $(PYTESTS)
diff --git a/tests/scanner/test_transformer.py b/tests/scanner/test_transformer.py
new file mode 100644
index 0000000..76618a7
--- /dev/null
+++ b/tests/scanner/test_transformer.py
@@ -0,0 +1,240 @@
+import unittest
+import tempfile
+import os
+import sys
+import __builtin__
+
+
+os.environ['GI_SCANNER_DISABLE_CACHE'] = '1'
+path = os.getenv('UNINSTALLED_INTROSPECTION_SRCDIR', None)
+assert path is not None
+sys.path.insert(0, path)
+
+# Not correct, but enough to get the tests going uninstalled
+__builtin__.__dict__['DATADIR'] = path
+
+from giscanner import ast
+from giscanner.sourcescanner import SourceScanner
+from giscanner.transformer import Transformer
+from giscanner.message import MessageLogger, WARNING, ERROR, FATAL
+
+
+def create_scanner_from_source_string(source):
+    ss = SourceScanner()
+    tmp_fd, tmp_name = tempfile.mkstemp(suffix='.h', text=True)
+    file = os.fdopen(tmp_fd, 'wt')
+    file.write(source)
+    file.close()
+
+    ss.parse_files([tmp_name])
+    return ss
+
+
+def load_namespace_from_source_string(namespace, source):
+    ss = create_scanner_from_source_string(source)
+    xformer = Transformer(namespace)
+    xformer.parse(ss.get_symbols())
+
+
+class TestStructTypedefs(unittest.TestCase):
+    def setUp(self):
+        # Hack to set logging singleton
+        self.namespace = ast.Namespace('Test', '1.0')
+        logger = MessageLogger.get(namespace=self.namespace)
+        logger.enable_warnings((WARNING, ERROR, FATAL))
+
+    def test_anonymous_typedef(self):
+        load_namespace_from_source_string(self.namespace, """
+            typedef struct {
+                int value;
+            } TestStruct;
+            """)
+        self.assertEqual(len(self.namespace.names), 1)
+        node = self.namespace.get('Struct')
+        self.assertTrue(node is not None)
+        self.assertTrue(isinstance(node, ast.Record))
+        self.assertFalse(node.disguised)
+        self.assertEqual(len(node.fields), 1)
+
+    def test_typedef_before(self):
+        load_namespace_from_source_string(self.namespace, """
+            typedef struct _TestStruct TestStruct;
+            struct _TestStruct {
+                int value;
+            };
+            """)
+        self.assertEqual(len(self.namespace.names), 1)
+        node = self.namespace.get('Struct')
+        self.assertTrue(node is not None)
+        self.assertTrue(isinstance(node, ast.Record))
+        self.assertFalse(node.disguised)
+        self.assertEqual(len(node.fields), 1)
+
+    @unittest.expectedFailure
+    def test_typedef_after(self):
+        load_namespace_from_source_string(self.namespace, """
+            struct _TestStruct {
+                int value;
+            };
+            typedef struct _TestStruct TestStruct;
+            """)
+        self.assertEqual(len(self.namespace.names), 1)
+        node = self.namespace.get('Struct')
+        self.assertTrue(node is not None)
+        self.assertTrue(isinstance(node, ast.Record))
+        self.assertFalse(node.disguised)
+        self.assertEqual(len(node.fields), 1)
+
+    @unittest.expectedFailure
+    def test_tag_and_typedef(self):
+        load_namespace_from_source_string(self.namespace, """
+            typedef struct _TestStruct {
+                int value;
+            } TestStruct;
+            """)
+        self.assertEqual(len(self.namespace.names), 1)
+        node = self.namespace.get('Struct')
+        self.assertTrue(node is not None)
+        self.assertTrue(isinstance(node, ast.Record))
+        self.assertFalse(node.disguised)
+        self.assertEqual(len(node.fields), 1)
+
+    def test_struct_tag_only(self):
+        load_namespace_from_source_string(self.namespace, """
+            struct TestStruct {
+                int value;
+            };
+            """)
+        self.assertEqual(len(self.namespace.names), 1)
+        node = self.namespace.get('Struct')
+        self.assertTrue(node is not None)
+        self.assertTrue(isinstance(node, ast.Record))
+        self.assertFalse(node.disguised)
+        self.assertEqual(len(node.fields), 1)
+
+    def test_struct_aliases(self):
+        load_namespace_from_source_string(self.namespace, """
+            typedef struct {
+                int value;
+            } TestStruct;
+            typedef TestStruct TestStructAlias1;
+            typedef TestStruct TestStructAlias2;
+            """)
+        self.assertEqual(len(self.namespace.names), 3)
+
+        node = self.namespace.get('Struct')
+        self.assertTrue(node is not None)
+        self.assertTrue(isinstance(node, ast.Record))
+        self.assertFalse(node.disguised)
+        self.assertEqual(len(node.fields), 1)
+
+        alias = self.namespace.get('StructAlias1')
+        self.assertTrue(isinstance(alias, ast.Alias))
+        self.assertEqual(alias.target, node)
+        self.assertEqual(alias.ctype, 'TestStructAlias1')
+
+        alias = self.namespace.get('StructAlias2')
+        self.assertTrue(isinstance(alias, ast.Alias))
+        self.assertEqual(alias.target, node)
+        self.assertEqual(alias.ctype, 'TestStructAlias2')
+
+    def test_struct_tag_aliases_before(self):
+        # This test is similar to how GObject and GInitiallyUnowned are setup
+        load_namespace_from_source_string(self.namespace, """
+            typedef struct _TestStruct TestStruct;
+            typedef struct _TestStruct TestStructAlias;
+            struct _TestStruct {
+                int value;
+            };
+            """)
+        self.assertEqual(len(self.namespace.names), 2)
+
+        node = self.namespace.get('Struct')
+        self.assertTrue(node is not None)
+        self.assertTrue(isinstance(node, ast.Record))
+        self.assertFalse(node.disguised)
+        self.assertEqual(len(node.fields), 1)
+        self.assertEqual(node.ctype, 'TestStruct')
+
+        shared = self.namespace.get('StructAlias')
+        self.assertTrue(shared is not None)
+        self.assertTrue(isinstance(shared, ast.Record))
+        self.assertTrue(shared.disguised)
+        self.assertEqual(len(shared.fields), 0)
+        self.assertEqual(shared.ctype, 'TestStructAlias')
+
+    @unittest.expectedFailure
+    def test_struct_tag_aliases_after(self):
+        load_namespace_from_source_string(self.namespace, """
+            struct _TestStruct {
+                int value;
+            };
+            typedef struct _TestStruct TestStruct;
+            typedef struct _TestStruct TestStructAlias;
+            """)
+        self.assertEqual(len(self.namespace.names), 2)
+
+        node = self.namespace.get('Struct')
+        self.assertTrue(node is not None)
+        self.assertTrue(isinstance(node, ast.Record))
+        self.assertFalse(node.disguised)
+        self.assertEqual(len(node.fields), 1)
+        self.assertEqual(node.ctype, 'TestStruct')
+
+        shared = self.namespace.get('StructAlias')
+        self.assertTrue(shared is not None)
+        self.assertTrue(isinstance(shared, ast.Record))
+        self.assertTrue(shared.disguised)
+        self.assertEqual(len(shared.fields), 0)
+        self.assertEqual(shared.ctype, 'TestStructAlias')
+
+    def test_struct_pointer(self):
+        load_namespace_from_source_string(self.namespace, """
+            typedef struct {
+                int value;
+            } TestStruct;
+            typedef TestStruct* TestStructPtr;
+            """)
+        self.assertEqual(len(self.namespace.names), 2)
+
+        node = self.namespace.get('Struct')
+        self.assertTrue(node is not None)
+        self.assertTrue(isinstance(node, ast.Record))
+        self.assertEqual(len(node.fields), 1)
+        self.assertEqual(node.ctype, 'TestStruct')
+
+        ptr = self.namespace.get('StructPtr')
+        self.assertTrue(ptr is not None)
+        self.assertTrue(isinstance(ptr, ast.Alias))
+        self.assertEqual(ptr.ctype, 'TestStructPtr')
+        # This loses type information about the struct which seems broken.
+        self.assertEqual(ptr.target, ast.TYPE_ANY)
+
+    def test_struct_tag_pointer(self):
+        load_namespace_from_source_string(self.namespace, """
+            typedef struct _TestStruct TestStruct;
+            struct _TestStruct{
+                int value;
+            };
+            typedef struct _TestStruct* TestStructPtr;
+            """)
+        self.assertEqual(len(self.namespace.names), 2)
+
+        node = self.namespace.get('Struct')
+        self.assertTrue(node is not None)
+        self.assertTrue(isinstance(node, ast.Record))
+        self.assertFalse(node.disguised)
+        self.assertEqual(node.ctype, 'TestStruct')
+        self.assertEqual(len(node.fields), 1)
+
+        ptr = self.namespace.get('StructPtr')
+        self.assertTrue(ptr is not None)
+        # This currently gives a disguised Record instead of an Alias
+        self.assertTrue(isinstance(ptr, ast.Record))
+        self.assertTrue(ptr.disguised)
+        self.assertEqual(len(ptr.fields), 0)
+        self.assertEqual(ptr.ctype, 'TestStructPtr')
+
+
+if __name__ == '__main__':
+    unittest.main()


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