summaryrefslogtreecommitdiff
path: root/utils/ipc/mojo/public/tools/mojom/stable_attribute_unittest.py
blob: d45ec5862999d5ddb463ac3e06abd050cdd18e9e (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
121
122
123
124
125
126
127
# Copyright 2020 The Chromium Authors. All rights reserved.
# 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


class StableAttributeTest(MojomParserTestCase):
  """Tests covering usage of the [Stable] attribute."""

  def testStableAttributeTagging(self):
    """Verify that we recognize the [Stable] attribute on relevant definitions
    and the resulting parser outputs are tagged accordingly."""
    mojom = 'test.mojom'
    self.WriteFile(
        mojom, """\
        [Stable] enum TestEnum { kFoo };
        enum UnstableEnum { kBar };
        [Stable] struct TestStruct { TestEnum a; };
        struct UnstableStruct { UnstableEnum a; };
        [Stable] union TestUnion { TestEnum a; TestStruct b; };
        union UnstableUnion { UnstableEnum a; UnstableStruct b; };
        [Stable] interface TestInterface { Foo@0(TestUnion x) => (); };
        interface UnstableInterface { Foo(UnstableUnion x) => (); };
        """)
    self.ParseMojoms([mojom])

    m = self.LoadModule(mojom)
    self.assertEqual(2, len(m.enums))
    self.assertTrue(m.enums[0].stable)
    self.assertFalse(m.enums[1].stable)
    self.assertEqual(2, len(m.structs))
    self.assertTrue(m.structs[0].stable)
    self.assertFalse(m.structs[1].stable)
    self.assertEqual(2, len(m.unions))
    self.assertTrue(m.unions[0].stable)
    self.assertFalse(m.unions[1].stable)
    self.assertEqual(2, len(m.interfaces))
    self.assertTrue(m.interfaces[0].stable)
    self.assertFalse(m.interfaces[1].stable)

  def testStableStruct(self):
    """A [Stable] struct is valid if all its fields are also stable."""
    self.ExtractTypes('[Stable] struct S {};')
    self.ExtractTypes('[Stable] struct S { int32 x; bool b; };')
    self.ExtractTypes('[Stable] enum E { A }; [Stable] struct S { E e; };')
    self.ExtractTypes('[Stable] struct S {}; [Stable] struct T { S s; };')
    self.ExtractTypes(
        '[Stable] struct S {}; [Stable] struct T { array<S> ss; };')
    self.ExtractTypes(
        '[Stable] interface F {}; [Stable] struct T { pending_remote<F> f; };')

    with self.assertRaisesRegexp(Exception, 'because it depends on E'):
      self.ExtractTypes('enum E { A }; [Stable] struct S { E e; };')
    with self.assertRaisesRegexp(Exception, 'because it depends on X'):
      self.ExtractTypes('struct X {}; [Stable] struct S { X x; };')
    with self.assertRaisesRegexp(Exception, 'because it depends on T'):
      self.ExtractTypes('struct T {}; [Stable] struct S { array<T> xs; };')
    with self.assertRaisesRegexp(Exception, 'because it depends on T'):
      self.ExtractTypes('struct T {}; [Stable] struct S { map<int32, T> xs; };')
    with self.assertRaisesRegexp(Exception, 'because it depends on T'):
      self.ExtractTypes('struct T {}; [Stable] struct S { map<T, int32> xs; };')
    with self.assertRaisesRegexp(Exception, 'because it depends on F'):
      self.ExtractTypes(
          'interface F {}; [Stable] struct S { pending_remote<F> f; };')
    with self.assertRaisesRegexp(Exception, 'because it depends on F'):
      self.ExtractTypes(
          'interface F {}; [Stable] struct S { pending_receiver<F> f; };')

  def testStableUnion(self):
    """A [Stable] union is valid if all its fields' types are also stable."""
    self.ExtractTypes('[Stable] union U {};')
    self.ExtractTypes('[Stable] union U { int32 x; bool b; };')
    self.ExtractTypes('[Stable] enum E { A }; [Stable] union U { E e; };')
    self.ExtractTypes('[Stable] struct S {}; [Stable] union U { S s; };')
    self.ExtractTypes(
        '[Stable] struct S {}; [Stable] union U { array<S> ss; };')
    self.ExtractTypes(
        '[Stable] interface F {}; [Stable] union U { pending_remote<F> f; };')

    with self.assertRaisesRegexp(Exception, 'because it depends on E'):
      self.ExtractTypes('enum E { A }; [Stable] union U { E e; };')
    with self.assertRaisesRegexp(Exception, 'because it depends on X'):
      self.ExtractTypes('struct X {}; [Stable] union U { X x; };')
    with self.assertRaisesRegexp(Exception, 'because it depends on T'):
      self.ExtractTypes('struct T {}; [Stable] union U { array<T> xs; };')
    with self.assertRaisesRegexp(Exception, 'because it depends on T'):
      self.ExtractTypes('struct T {}; [Stable] union U { map<int32, T> xs; };')
    with self.assertRaisesRegexp(Exception, 'because it depends on T'):
      self.ExtractTypes('struct T {}; [Stable] union U { map<T, int32> xs; };')
    with self.assertRaisesRegexp(Exception, 'because it depends on F'):
      self.ExtractTypes(
          'interface F {}; [Stable] union U { pending_remote<F> f; };')
    with self.assertRaisesRegexp(Exception, 'because it depends on F'):
      self.ExtractTypes(
          'interface F {}; [Stable] union U { pending_receiver<F> f; };')

  def testStableInterface(self):
    """A [Stable] interface is valid if all its methods' parameter types are
    stable, including response parameters where applicable."""
    self.ExtractTypes('[Stable] interface F {};')
    self.ExtractTypes('[Stable] interface F { A@0(int32 x); };')
    self.ExtractTypes('[Stable] interface F { A@0(int32 x) => (bool b); };')
    self.ExtractTypes("""\
        [Stable] enum E { A, B, C };
        [Stable] struct S {};
        [Stable] interface F { A@0(E e, S s) => (bool b, array<S> s); };
        """)

    with self.assertRaisesRegexp(Exception, 'because it depends on E'):
      self.ExtractTypes(
          'enum E { A, B, C }; [Stable] interface F { A@0(E e); };')
    with self.assertRaisesRegexp(Exception, 'because it depends on E'):
      self.ExtractTypes(
          'enum E { A, B, C }; [Stable] interface F { A@0(int32 x) => (E e); };'
      )
    with self.assertRaisesRegexp(Exception, 'because it depends on S'):
      self.ExtractTypes(
          'struct S {}; [Stable] interface F { A@0(int32 x) => (S s); };')
    with self.assertRaisesRegexp(Exception, 'because it depends on S'):
      self.ExtractTypes(
          'struct S {}; [Stable] interface F { A@0(S s) => (bool b); };')

    with self.assertRaisesRegexp(Exception, 'explicit method ordinals'):
      self.ExtractTypes('[Stable] interface F { A() => (); };')