Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(471)

Side by Side Diff: mojo/public/tools/bindings/pylib/mojom/parse/ast.py

Issue 395243002: Mojom: Add some tests for mojom.parse.ast. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | mojo/public/tools/bindings/pylib/mojom_tests/parse/ast_unittest.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Copyright 2014 The Chromium Authors. All rights reserved. 1 # Copyright 2014 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be 2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file. 3 # found in the LICENSE file.
4 4
5 """Node classes for the AST for a Mojo IDL file.""" 5 """Node classes for the AST for a Mojo IDL file."""
6 6
7 # Note: For convenience of testing, you probably want to define __eq__() methods 7 # Note: For convenience of testing, you probably want to define __eq__() methods
8 # for all node types; it's okay to be slightly lax (e.g., not compare filename 8 # for all node types; it's okay to be slightly lax (e.g., not compare filename
9 # and lineno). You may also define __repr__() to help with analyzing test 9 # and lineno). You may also define __repr__() to help with analyzing test
10 # failures, especially for more complex types. 10 # failures, especially for more complex types.
11 11
12 12
13 class NodeBase(object): 13 class NodeBase(object):
14 """Base class for nodes in the AST.""" 14 """Base class for nodes in the AST."""
15 15
16 def __init__(self, filename=None, lineno=None): 16 def __init__(self, filename=None, lineno=None):
17 self.filename = filename 17 self.filename = filename
18 self.lineno = lineno 18 self.lineno = lineno
19 19
20 def __eq__(self, other): 20 def __eq__(self, other):
21 return type(self) == type(other) 21 return type(self) == type(other)
22 22
23 # Make != the inverse of ==. (Subclasses shouldn't have to override this.)
24 def __ne__(self, other):
25 return not self == other
26
23 27
24 # TODO(vtl): Some of this is complicated enough that it should be tested. 28 # TODO(vtl): Some of this is complicated enough that it should be tested.
25 class NodeListBase(NodeBase): 29 class NodeListBase(NodeBase):
26 """Represents a list of other nodes, all having the same type. (This is meant 30 """Represents a list of other nodes, all having the same type. (This is meant
27 to be subclassed, with subclasses defining _list_item_type to be the class (or 31 to be subclassed, with subclasses defining _list_item_type to be the class (or
28 classes, in a tuple) of the members of the list.)""" 32 classes, in a tuple) of the members of the list.)"""
29 33
30 def __init__(self, item_or_items=None, **kwargs): 34 def __init__(self, item_or_items=None, **kwargs):
31 super(NodeListBase, self).__init__(**kwargs) 35 super(NodeListBase, self).__init__(**kwargs)
32 self.items = [] 36 self.items = []
33 if item_or_items is None: 37 if item_or_items is None:
34 pass 38 pass
35 self.items = [] 39 self.items = []
36 elif isinstance(item_or_items, list): 40 elif isinstance(item_or_items, list):
37 for item in item_or_items: 41 for item in item_or_items:
38 assert isinstance(item, self._list_item_type) 42 assert isinstance(item, self._list_item_type)
39 self.Append(item) 43 self.Append(item)
40 else: 44 else:
41 assert isinstance(item_or_items, self._list_item_type) 45 assert isinstance(item_or_items, self._list_item_type)
42 self.Append(item_or_items) 46 self.Append(item_or_items)
43 47
44 # Support iteration. For everything else, users should just access |items| 48 # Support iteration. For everything else, users should just access |items|
45 # directly. (We intentionally do NOT supply |__len__()| or |__nonzero__()|, so 49 # directly. (We intentionally do NOT supply |__len__()| or |__nonzero__()|, so
46 # |bool(NodeListBase())| is true.) 50 # |bool(NodeListBase())| is true.)
47 def __iter__(self): 51 def __iter__(self):
48 return self.items.__iter__() 52 return self.items.__iter__()
49 53
50 def __eq__(self, other): 54 def __eq__(self, other):
51 return super(NodeListBase, self).__eq__(other) and \ 55 return super(NodeListBase, self).__eq__(other) and \
52 len(self.items) == len(other.items) and \ 56 self.items == other.items
53 all(self.items[i] == other.items[i] for i in xrange(len(self.items)))
54 57
55 # Implement this so that on failure, we get slightly more sensible output. 58 # Implement this so that on failure, we get slightly more sensible output.
56 def __repr__(self): 59 def __repr__(self):
57 return self.__class__.__name__ + "([" + \ 60 return self.__class__.__name__ + "([" + \
58 ", ".join([repr(elem) for elem in self.items]) + "])" 61 ", ".join([repr(elem) for elem in self.items]) + "])"
59 62
60 def Insert(self, item): 63 def Insert(self, item):
61 """Inserts item at the front of the list.""" 64 """Inserts item at the front of the list."""
62 65
63 assert isinstance(item, self._list_item_type) 66 assert isinstance(item, self._list_item_type)
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 self.ordinal == other.ordinal and \ 346 self.ordinal == other.ordinal and \
344 self.typename == other.typename and \ 347 self.typename == other.typename and \
345 self.default_value == other.default_value 348 self.default_value == other.default_value
346 349
347 350
348 # This needs to be declared after |StructField|. 351 # This needs to be declared after |StructField|.
349 class StructBody(NodeListBase): 352 class StructBody(NodeListBase):
350 """Represents the body of (i.e., list of definitions inside) a struct.""" 353 """Represents the body of (i.e., list of definitions inside) a struct."""
351 354
352 _list_item_type = (Const, Enum, StructField) 355 _list_item_type = (Const, Enum, StructField)
OLDNEW
« no previous file with comments | « no previous file | mojo/public/tools/bindings/pylib/mojom_tests/parse/ast_unittest.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698