summaryrefslogtreecommitdiff
path: root/utils/ipc/mojo/public/tools/mojom/mojom/parse/conditional_features_unittest.py
blob: aa609be73837b3a98dd0a09f4df52601cde37703 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# Copyright 2018 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.

import imp
import os
import sys
import unittest


def _GetDirAbove(dirname):
  """Returns the directory "above" this file containing |dirname| (which must
  also be "above" this file)."""
  path = os.path.abspath(__file__)
  while True:
    path, tail = os.path.split(path)
    assert tail
    if tail == dirname:
      return path


try:
  imp.find_module('mojom')
except ImportError:
  sys.path.append(os.path.join(_GetDirAbove('pylib'), 'pylib'))
import mojom.parse.ast as ast
import mojom.parse.conditional_features as conditional_features
import mojom.parse.parser as parser

ENABLED_FEATURES = frozenset({'red', 'green', 'blue'})


class ConditionalFeaturesTest(unittest.TestCase):
  """Tests |mojom.parse.conditional_features|."""

  def parseAndAssertEqual(self, source, expected_source):
    definition = parser.Parse(source, "my_file.mojom")
    conditional_features.RemoveDisabledDefinitions(definition, ENABLED_FEATURES)
    expected = parser.Parse(expected_source, "my_file.mojom")
    self.assertEquals(definition, expected)

  def testFilterConst(self):
    """Test that Consts are correctly filtered."""
    const_source = """
      [EnableIf=blue]
      const int kMyConst1 = 1;
      [EnableIf=orange]
      const double kMyConst2 = 2;
      const int kMyConst3 = 3;
    """
    expected_source = """
      [EnableIf=blue]
      const int kMyConst1 = 1;
      const int kMyConst3 = 3;
    """
    self.parseAndAssertEqual(const_source, expected_source)

  def testFilterEnum(self):
    """Test that EnumValues are correctly filtered from an Enum."""
    enum_source = """
      enum MyEnum {
        [EnableIf=purple]
        VALUE1,
        [EnableIf=blue]
        VALUE2,
        VALUE3,
      };
    """
    expected_source = """
      enum MyEnum {
        [EnableIf=blue]
        VALUE2,
        VALUE3
      };
    """
    self.parseAndAssertEqual(enum_source, expected_source)

  def testFilterImport(self):
    """Test that imports are correctly filtered from a Mojom."""
    import_source = """
      [EnableIf=blue]
      import "foo.mojom";
      import "bar.mojom";
      [EnableIf=purple]
      import "baz.mojom";
    """
    expected_source = """
      [EnableIf=blue]
      import "foo.mojom";
      import "bar.mojom";
    """
    self.parseAndAssertEqual(import_source, expected_source)

  def testFilterInterface(self):
    """Test that definitions are correctly filtered from an Interface."""
    interface_source = """
      interface MyInterface {
        [EnableIf=blue]
        enum MyEnum {
          [EnableIf=purple]
          VALUE1,
          VALUE2,
        };
        [EnableIf=blue]
        const int32 kMyConst = 123;
        [EnableIf=purple]
        MyMethod();
      };
    """
    expected_source = """
      interface MyInterface {
        [EnableIf=blue]
        enum MyEnum {
          VALUE2,
        };
        [EnableIf=blue]
        const int32 kMyConst = 123;
      };
    """
    self.parseAndAssertEqual(interface_source, expected_source)

  def testFilterMethod(self):
    """Test that Parameters are correctly filtered from a Method."""
    method_source = """
      interface MyInterface {
        [EnableIf=blue]
        MyMethod([EnableIf=purple] int32 a) => ([EnableIf=red] int32 b);
      };
    """
    expected_source = """
      interface MyInterface {
        [EnableIf=blue]
        MyMethod() => ([EnableIf=red] int32 b);
      };
    """
    self.parseAndAssertEqual(method_source, expected_source)

  def testFilterStruct(self):
    """Test that definitions are correctly filtered from a Struct."""
    struct_source = """
      struct MyStruct {
        [EnableIf=blue]
        enum MyEnum {
          VALUE1,
          [EnableIf=purple]
          VALUE2,
        };
        [EnableIf=yellow]
        const double kMyConst = 1.23;
        [EnableIf=green]
        int32 a;
        double b;
        [EnableIf=purple]
        int32 c;
        [EnableIf=blue]
        double d;
        int32 e;
        [EnableIf=orange]
        double f;
      };
    """
    expected_source = """
      struct MyStruct {
        [EnableIf=blue]
        enum MyEnum {
          VALUE1,
        };
        [EnableIf=green]
        int32 a;
        double b;
        [EnableIf=blue]
        double d;
        int32 e;
      };
    """
    self.parseAndAssertEqual(struct_source, expected_source)

  def testFilterUnion(self):
    """Test that UnionFields are correctly filtered from a Union."""
    union_source = """
      union MyUnion {
        [EnableIf=yellow]
        int32 a;
        [EnableIf=red]
        bool b;
      };
    """
    expected_source = """
      union MyUnion {
        [EnableIf=red]
        bool b;
      };
    """
    self.parseAndAssertEqual(union_source, expected_source)

  def testSameNameFields(self):
    mojom_source = """
      enum Foo {
        [EnableIf=red]
        VALUE1 = 5,
        [EnableIf=yellow]
        VALUE1 = 6,
      };
      [EnableIf=red]
      const double kMyConst = 1.23;
      [EnableIf=yellow]
      const double kMyConst = 4.56;
    """
    expected_source = """
      enum Foo {
        [EnableIf=red]
        VALUE1 = 5,
      };
      [EnableIf=red]
      const double kMyConst = 1.23;
    """
    self.parseAndAssertEqual(mojom_source, expected_source)

  def testMultipleEnableIfs(self):
    source = """
      enum Foo {
        [EnableIf=red,EnableIf=yellow]
        kBarValue = 5,
      };
    """
    definition = parser.Parse(source, "my_file.mojom")
    self.assertRaises(conditional_features.EnableIfError,
                      conditional_features.RemoveDisabledDefinitions,
                      definition, ENABLED_FEATURES)


if __name__ == '__main__':
  unittest.main()