summaryrefslogtreecommitdiff
path: root/tests/scanner
diff options
context:
space:
mode:
authorSimon Feltman <sfeltman@src.gnome.org>2013-12-27 04:01:05 -0800
committerSimon Feltman <sfeltman@src.gnome.org>2014-01-02 14:43:13 -0800
commit3189b3cfe90ab96e51fb9c1e498f5d2e750d0a49 (patch)
treeffe874164a3a824f8d496a68576c0d3711b4d086 /tests/scanner
parenta36a21890fc8ef33d6e40d7ad1eda4feb9ac1230 (diff)
downloadgobject-introspection-3189b3cfe90ab96e51fb9c1e498f5d2e750d0a49.tar.gz
tests: Add nested union unittests
Add a sub-set of tests found in struct testing as only basic validation is needed due to the shared code paths. https://bugzilla.gnome.org/show_bug.cgi?id=581525
Diffstat (limited to 'tests/scanner')
-rw-r--r--tests/scanner/test_transformer.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/tests/scanner/test_transformer.py b/tests/scanner/test_transformer.py
index 5e74ab5e..5a0e82cd 100644
--- a/tests/scanner/test_transformer.py
+++ b/tests/scanner/test_transformer.py
@@ -343,5 +343,90 @@ class TestNestedStructs(unittest.TestCase):
self.assertEqual(field.name, 'struct_ptr')
+class TestUnions(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_union_with_struct(self):
+ load_namespace_from_source_string(self.namespace, """
+ typedef struct {
+ int value;
+ } TestSimpleStruct;
+
+ typedef union {
+ TestSimpleStruct nested_struct;
+ int value;
+ } TestUnionWithNested;
+ """)
+ self.assertEqual(len(self.namespace.names), 2)
+ node = self.namespace.get('UnionWithNested')
+ self.assertEqual(len(node.fields), 2)
+
+ simple = self.namespace.get('SimpleStruct')
+ self.assertTrue(node is not None)
+
+ field = node.fields[0]
+ self.assertEqual(field.type, simple)
+ self.assertEqual(field.name, 'nested_struct')
+
+ field = node.fields[1]
+ self.assertEqual(field.type.ctype, 'int')
+ self.assertEqual(field.name, 'value')
+
+ def test_nested(self):
+ load_namespace_from_source_string(self.namespace, """
+ typedef struct {
+ union {
+ int ival;
+ float fval;
+ } nested;
+ } TestNestedUnion;
+ """)
+ self.assertEqual(len(self.namespace.names), 1)
+ node = self.namespace.get('NestedUnion')
+ self.assertEqual(len(node.fields), 1)
+
+ field = node.fields[0]
+ self.assertEqual(field.name, 'nested')
+
+ nested = field.anonymous_node
+ self.assertTrue(isinstance(nested, ast.Union))
+ self.assertEqual(nested.name, 'nested')
+ self.assertEqual(len(nested.fields), 2)
+
+ self.assertEqual(nested.fields[0].name, 'ival')
+ self.assertEqual(nested.fields[1].name, 'fval')
+
+ def test_nested_anonymous(self):
+ load_namespace_from_source_string(self.namespace, """
+ typedef struct {
+ union {
+ int ival;
+ float fval;
+ };
+ } TestStructWithNestedUnion;
+ """)
+ self.assertEqual(len(self.namespace.names), 1)
+ node = self.namespace.get('StructWithNestedUnion')
+ self.assertEqual(len(node.fields), 1)
+
+ field = node.fields[0]
+ self.assertEqual(field.name, None)
+
+ nested = field.anonymous_node
+ self.assertTrue(isinstance(nested, ast.Union))
+ self.assertEqual(nested.name, None)
+ self.assertEqual(len(nested.fields), 2)
+
+ self.assertEqual(nested.fields[0].name, 'ival')
+ self.assertEqual(nested.fields[0].type.ctype, 'int')
+
+ self.assertEqual(nested.fields[1].name, 'fval')
+ self.assertEqual(nested.fields[1].type.ctype, 'float')
+
+
if __name__ == '__main__':
unittest.main()