summaryrefslogtreecommitdiff
path: root/utils/codegen/ipc/mojo/public/tools/mojom/const_unittest.py
diff options
context:
space:
mode:
authorLaurent Pinchart <laurent.pinchart@ideasonboard.com>2024-08-08 18:13:00 +0300
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2024-08-15 23:59:08 +0300
commit50c92cc7e2924009ecab3e4004448b01d687707c (patch)
treec22b49816a3c79dae4727780962aa0928df42b52 /utils/codegen/ipc/mojo/public/tools/mojom/const_unittest.py
parentd3bf27180ef1d91b86b7b87a2378e559eaff5455 (diff)
meson: Move all code generation scripts to utils/codegen/
We have multiple code generation scripts in utils/, mixed with other miscellaneous utilities, as well as a larger code base based on mojom in utils/ipc/. To make code sharing easier between the generator scripts, without creating a mess in the utils/ directory, move all the code generation code to utils/codegen/. Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Daniel Scally <dan.scally@ideasonboard.com> Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>
Diffstat (limited to 'utils/codegen/ipc/mojo/public/tools/mojom/const_unittest.py')
-rw-r--r--utils/codegen/ipc/mojo/public/tools/mojom/const_unittest.py90
1 files changed, 90 insertions, 0 deletions
diff --git a/utils/codegen/ipc/mojo/public/tools/mojom/const_unittest.py b/utils/codegen/ipc/mojo/public/tools/mojom/const_unittest.py
new file mode 100644
index 00000000..e8ed36a7
--- /dev/null
+++ b/utils/codegen/ipc/mojo/public/tools/mojom/const_unittest.py
@@ -0,0 +1,90 @@
+# Copyright 2020 The Chromium Authors
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+from mojom_parser_test_case import MojomParserTestCase
+from mojom.generate import module as mojom
+
+
+class ConstTest(MojomParserTestCase):
+ """Tests constant parsing behavior."""
+
+ def testLiteralInt(self):
+ a_mojom = 'a.mojom'
+ self.WriteFile(a_mojom, 'const int32 k = 42;')
+ self.ParseMojoms([a_mojom])
+ a = self.LoadModule(a_mojom)
+ self.assertEqual(1, len(a.constants))
+ self.assertEqual('k', a.constants[0].mojom_name)
+ self.assertEqual('42', a.constants[0].value)
+
+ def testLiteralFloat(self):
+ a_mojom = 'a.mojom'
+ self.WriteFile(a_mojom, 'const float k = 42.5;')
+ self.ParseMojoms([a_mojom])
+ a = self.LoadModule(a_mojom)
+ self.assertEqual(1, len(a.constants))
+ self.assertEqual('k', a.constants[0].mojom_name)
+ self.assertEqual('42.5', a.constants[0].value)
+
+ def testLiteralString(self):
+ a_mojom = 'a.mojom'
+ self.WriteFile(a_mojom, 'const string k = "woot";')
+ self.ParseMojoms([a_mojom])
+ a = self.LoadModule(a_mojom)
+ self.assertEqual(1, len(a.constants))
+ self.assertEqual('k', a.constants[0].mojom_name)
+ self.assertEqual('"woot"', a.constants[0].value)
+
+ def testEnumConstant(self):
+ a_mojom = 'a.mojom'
+ self.WriteFile(a_mojom, 'module a; enum E { kA = 41, kB };')
+ b_mojom = 'b.mojom'
+ self.WriteFile(
+ b_mojom, """\
+ import "a.mojom";
+ const a.E kE1 = a.E.kB;
+
+ // We also allow value names to be unqualified, implying scope from the
+ // constant's type.
+ const a.E kE2 = kB;
+ """)
+ self.ParseMojoms([a_mojom, b_mojom])
+ a = self.LoadModule(a_mojom)
+ b = self.LoadModule(b_mojom)
+ self.assertEqual(1, len(a.enums))
+ self.assertEqual('E', a.enums[0].mojom_name)
+ self.assertEqual(2, len(b.constants))
+ self.assertEqual('kE1', b.constants[0].mojom_name)
+ self.assertEqual(a.enums[0], b.constants[0].kind)
+ self.assertEqual(a.enums[0].fields[1], b.constants[0].value.field)
+ self.assertEqual(42, b.constants[0].value.field.numeric_value)
+ self.assertEqual('kE2', b.constants[1].mojom_name)
+ self.assertEqual(a.enums[0].fields[1], b.constants[1].value.field)
+ self.assertEqual(42, b.constants[1].value.field.numeric_value)
+
+ def testConstantReference(self):
+ a_mojom = 'a.mojom'
+ self.WriteFile(a_mojom, 'const int32 kA = 42; const int32 kB = kA;')
+ self.ParseMojoms([a_mojom])
+ a = self.LoadModule(a_mojom)
+ self.assertEqual(2, len(a.constants))
+ self.assertEqual('kA', a.constants[0].mojom_name)
+ self.assertEqual('42', a.constants[0].value)
+ self.assertEqual('kB', a.constants[1].mojom_name)
+ self.assertEqual('42', a.constants[1].value)
+
+ def testImportedConstantReference(self):
+ a_mojom = 'a.mojom'
+ self.WriteFile(a_mojom, 'const int32 kA = 42;')
+ b_mojom = 'b.mojom'
+ self.WriteFile(b_mojom, 'import "a.mojom"; const int32 kB = kA;')
+ self.ParseMojoms([a_mojom, b_mojom])
+ a = self.LoadModule(a_mojom)
+ b = self.LoadModule(b_mojom)
+ self.assertEqual(1, len(a.constants))
+ self.assertEqual(1, len(b.constants))
+ self.assertEqual('kA', a.constants[0].mojom_name)
+ self.assertEqual('42', a.constants[0].value)
+ self.assertEqual('kB', b.constants[0].mojom_name)
+ self.assertEqual('42', b.constants[0].value)