summaryrefslogtreecommitdiff
path: root/utils/codegen/ipc/mojo/public/tools/mojom/enum_unittest.py
blob: 9269cde526b49ec9995a609a45e6aa950f5448dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# 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


class EnumTest(MojomParserTestCase):
  """Tests enum parsing behavior."""

  def testExplicitValues(self):
    """Verifies basic parsing of assigned integral values."""
    types = self.ExtractTypes('enum E { kFoo=0, kBar=2, kBaz };')
    self.assertEqual('kFoo', types['E'].fields[0].mojom_name)
    self.assertEqual(0, types['E'].fields[0].numeric_value)
    self.assertEqual('kBar', types['E'].fields[1].mojom_name)
    self.assertEqual(2, types['E'].fields[1].numeric_value)
    self.assertEqual('kBaz', types['E'].fields[2].mojom_name)
    self.assertEqual(3, types['E'].fields[2].numeric_value)

  def testImplicitValues(self):
    """Verifies basic automatic assignment of integral values at parse time."""
    types = self.ExtractTypes('enum E { kFoo, kBar, kBaz };')
    self.assertEqual('kFoo', types['E'].fields[0].mojom_name)
    self.assertEqual(0, types['E'].fields[0].numeric_value)
    self.assertEqual('kBar', types['E'].fields[1].mojom_name)
    self.assertEqual(1, types['E'].fields[1].numeric_value)
    self.assertEqual('kBaz', types['E'].fields[2].mojom_name)
    self.assertEqual(2, types['E'].fields[2].numeric_value)

  def testSameEnumReference(self):
    """Verifies that an enum value can be assigned from the value of another
    field within the same enum."""
    types = self.ExtractTypes('enum E { kA, kB, kFirst=kA };')
    self.assertEqual('kA', types['E'].fields[0].mojom_name)
    self.assertEqual(0, types['E'].fields[0].numeric_value)
    self.assertEqual('kB', types['E'].fields[1].mojom_name)
    self.assertEqual(1, types['E'].fields[1].numeric_value)
    self.assertEqual('kFirst', types['E'].fields[2].mojom_name)
    self.assertEqual(0, types['E'].fields[2].numeric_value)

  def testSameModuleOtherEnumReference(self):
    """Verifies that an enum value can be assigned from the value of a field
    in another enum within the same module."""
    types = self.ExtractTypes('enum E { kA, kB }; enum F { kA = E.kB };')
    self.assertEqual(1, types['F'].fields[0].numeric_value)

  def testImportedEnumReference(self):
    """Verifies that an enum value can be assigned from the value of a field
    in another enum within a different module."""
    a_mojom = 'a.mojom'
    self.WriteFile(a_mojom, 'module a; enum E { kFoo=42, kBar };')
    b_mojom = 'b.mojom'
    self.WriteFile(b_mojom,
                   'module b; import "a.mojom"; enum F { kFoo = a.E.kBar };')
    self.ParseMojoms([a_mojom, b_mojom])
    b = self.LoadModule(b_mojom)

    self.assertEqual('F', b.enums[0].mojom_name)
    self.assertEqual('kFoo', b.enums[0].fields[0].mojom_name)
    self.assertEqual(43, b.enums[0].fields[0].numeric_value)

  def testConstantReference(self):
    """Verifies that an enum value can be assigned from the value of an
    integral constant within the same module."""
    types = self.ExtractTypes('const int32 kFoo = 42; enum E { kA = kFoo };')
    self.assertEqual(42, types['E'].fields[0].numeric_value)

  def testInvalidConstantReference(self):
    """Verifies that enum values cannot be assigned from the value of
    non-integral constants."""
    with self.assertRaisesRegexp(ValueError, 'not an integer'):
      self.ExtractTypes('const float kFoo = 1.0; enum E { kA = kFoo };')
    with self.assertRaisesRegexp(ValueError, 'not an integer'):
      self.ExtractTypes('const double kFoo = 1.0; enum E { kA = kFoo };')
    with self.assertRaisesRegexp(ValueError, 'not an integer'):
      self.ExtractTypes('const string kFoo = "lol"; enum E { kA = kFoo };')

  def testImportedConstantReference(self):
    """Verifies that an enum value can be assigned from the value of an integral
    constant within an imported module."""
    a_mojom = 'a.mojom'
    self.WriteFile(a_mojom, 'module a; const int32 kFoo = 37;')
    b_mojom = 'b.mojom'
    self.WriteFile(b_mojom,
                   'module b; import "a.mojom"; enum F { kFoo = a.kFoo };')
    self.ParseMojoms([a_mojom, b_mojom])
    b = self.LoadModule(b_mojom)

    self.assertEqual('F', b.enums[0].mojom_name)
    self.assertEqual('kFoo', b.enums[0].fields[0].mojom_name)
    self.assertEqual(37, b.enums[0].fields[0].numeric_value)

  def testEnumAttributesAreEnums(self):
    """Verifies that enum values in attributes are really enum types."""
    a_mojom = 'a.mojom'
    self.WriteFile(a_mojom, 'module a; enum E { kFoo, kBar };')
    b_mojom = 'b.mojom'
    self.WriteFile(
        b_mojom, 'module b;'
        'import "a.mojom";'
        '[MooCow=a.E.kFoo]'
        'interface Foo { Foo(); };')
    self.ParseMojoms([a_mojom, b_mojom])
    b = self.LoadModule(b_mojom)
    self.assertEqual(b.interfaces[0].attributes['MooCow'].mojom_name, 'kFoo')

  def testConstantAttributes(self):
    """Verifies that constants as attributes are translated to the constant."""
    a_mojom = 'a.mojom'
    self.WriteFile(
        a_mojom, 'module a;'
        'enum E { kFoo, kBar };'
        'const E kB = E.kFoo;'
        '[Attr=kB] interface Hello { Foo(); };')
    self.ParseMojoms([a_mojom])
    a = self.LoadModule(a_mojom)
    self.assertEqual(a.interfaces[0].attributes['Attr'].mojom_name, 'kB')
    self.assertEquals(a.interfaces[0].attributes['Attr'].value.mojom_name,
                      'kFoo')