[gobject-introspection] tests: Add nested struct tests for transformer



commit a36a21890fc8ef33d6e40d7ad1eda4feb9ac1230
Author: Simon Feltman <sfeltman src gnome org>
Date:   Thu Dec 26 22:41:05 2013 -0800

    tests: Add nested struct tests for transformer
    
    Add tests for nested structs as they pass through the transformer.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=581525

 tests/scanner/test_transformer.py |  107 +++++++++++++++++++++++++++++++++++++
 1 files changed, 107 insertions(+), 0 deletions(-)
---
diff --git a/tests/scanner/test_transformer.py b/tests/scanner/test_transformer.py
index 76618a7..5e74ab5 100644
--- a/tests/scanner/test_transformer.py
+++ b/tests/scanner/test_transformer.py
@@ -236,5 +236,112 @@ class TestStructTypedefs(unittest.TestCase):
         self.assertEqual(ptr.ctype, 'TestStructPtr')
 
 
+class TestNestedStructs(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_nested_named(self):
+        load_namespace_from_source_string(self.namespace, """
+            typedef struct {
+                int value;
+            } TestSimpleStruct;
+
+            typedef struct {
+                TestSimpleStruct nested_struct;
+            } TestStructWithNested;
+            """)
+        self.assertEqual(len(self.namespace.names), 2)
+        node = self.namespace.get('StructWithNested')
+        self.assertEqual(len(node.fields), 1)
+
+        simple = self.namespace.get('SimpleStruct')
+        self.assertTrue(node is not None)
+
+        field = node.fields[0]
+        self.assertTrue(field is not None)
+        self.assertTrue(isinstance(field, ast.Field))
+        self.assertEqual(field.type, simple)
+        self.assertEqual(field.name, 'nested_struct')
+
+    def test_nested_anonymous(self):
+        load_namespace_from_source_string(self.namespace, """
+            typedef struct {
+                struct {
+                    int value;
+                };
+            } TestStructWithNestedAnon;
+            """)
+        self.assertEqual(len(self.namespace.names), 1)
+        node = self.namespace.get('StructWithNestedAnon')
+        self.assertEqual(len(node.fields), 1)
+
+        field = node.fields[0]
+        self.assertTrue(field is not None)
+        self.assertTrue(isinstance(field, ast.Field))
+        self.assertEqual(field.name, None)
+
+        anon = field.anonymous_node
+        self.assertTrue(isinstance(anon, ast.Record))
+        self.assertEqual(len(anon.fields), 1)
+
+        anon_field = anon.fields[0]
+        self.assertTrue(anon_field is not None)
+        self.assertTrue(isinstance(anon_field, ast.Field))
+        self.assertEqual(anon_field.name, 'value')
+
+    def test_nested(self):
+        load_namespace_from_source_string(self.namespace, """
+            typedef struct {
+                struct {
+                    int value;
+                } nested;
+            } TestStructWithNested;
+            """)
+        self.assertEqual(len(self.namespace.names), 1)
+        node = self.namespace.get('StructWithNested')
+        self.assertEqual(len(node.fields), 1)
+
+        field = node.fields[0]
+        self.assertTrue(field is not None)
+        self.assertTrue(isinstance(field, ast.Field))
+        self.assertEqual(field.name, 'nested')
+
+        nested = field.anonymous_node
+        self.assertTrue(isinstance(nested, ast.Record))
+        self.assertEqual(len(nested.fields), 1)
+        self.assertEqual(nested.name, 'nested')
+
+        nested_field = nested.fields[0]
+        self.assertTrue(nested_field is not None)
+        self.assertTrue(isinstance(nested_field, ast.Field))
+        self.assertEqual(nested_field.name, 'value')
+
+    def test_struct_ptr(self):
+        load_namespace_from_source_string(self.namespace, """
+            typedef struct {
+                int value;
+            } TestSimpleStruct;
+
+            typedef struct {
+                TestSimpleStruct *struct_ptr;
+            } TestStructWithNestedPtr;
+            """)
+        self.assertEqual(len(self.namespace.names), 2)
+        node = self.namespace.get('StructWithNestedPtr')
+        self.assertEqual(len(node.fields), 1)
+
+        simple = self.namespace.get('SimpleStruct')
+        self.assertTrue(node is not None)
+
+        field = node.fields[0]
+        self.assertTrue(field is not None)
+        self.assertTrue(isinstance(field, ast.Field))
+        self.assertEqual(field.type.ctype, 'TestSimpleStruct*')
+        self.assertEqual(field.name, 'struct_ptr')
+
+
 if __name__ == '__main__':
     unittest.main()


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