| OLD | NEW |
| (Empty) |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 # pylint: disable=import-error,print-statement,relative-import,protected-access | |
| 6 | |
| 7 """Unit tests for code_generator_web_module.py.""" | |
| 8 | |
| 9 import unittest | |
| 10 | |
| 11 from code_generator_web_module import InterfaceContextBuilder | |
| 12 from code_generator_web_module import TypeResolver | |
| 13 from code_generator_web_module import STRING_INCLUDE_PATH | |
| 14 from idl_definitions import IdlAttribute | |
| 15 from idl_definitions import IdlOperation | |
| 16 from idl_types import IdlType | |
| 17 from idl_types import PRIMITIVE_TYPES | |
| 18 from idl_types import STRING_TYPES | |
| 19 | |
| 20 | |
| 21 # TODO(dglazkov): Convert to use actual objects, not stubs. | |
| 22 # See http://crbug.com/673214 for more details. | |
| 23 class IdlTestingHelper(object): | |
| 24 """A collection of stub makers and helper utils to make testing code | |
| 25 generation easy.""" | |
| 26 | |
| 27 def make_stub_idl_attribute(self, name, return_type): | |
| 28 idl_attribute_stub = IdlAttribute() | |
| 29 idl_attribute_stub.name = name | |
| 30 idl_attribute_stub.idl_type = IdlType(return_type) | |
| 31 return idl_attribute_stub | |
| 32 | |
| 33 def make_stub_idl_operation(self, name, return_type): | |
| 34 idl_operation_stub = IdlOperation() | |
| 35 idl_operation_stub.name = name | |
| 36 idl_operation_stub.idl_type = IdlType(return_type) | |
| 37 return idl_operation_stub | |
| 38 | |
| 39 def make_stub_idl_type(self, base_type): | |
| 40 return IdlType(base_type) | |
| 41 | |
| 42 | |
| 43 class TypeResolverTest(unittest.TestCase): | |
| 44 | |
| 45 def test_includes_from_type_should_filter_primitive_types(self): | |
| 46 type_resolver = TypeResolver() | |
| 47 helper = IdlTestingHelper() | |
| 48 for primitive_type in PRIMITIVE_TYPES: | |
| 49 idl_type = helper.make_stub_idl_type(primitive_type) | |
| 50 self.assertEqual( | |
| 51 type_resolver._includes_from_type(idl_type), set()) | |
| 52 | |
| 53 def test_includes_from_type_should_filter_void(self): | |
| 54 type_resolver = TypeResolver() | |
| 55 helper = IdlTestingHelper() | |
| 56 idl_type = helper.make_stub_idl_type('void') | |
| 57 self.assertEqual( | |
| 58 type_resolver._includes_from_type(idl_type), set()) | |
| 59 | |
| 60 def test_includes_from_type_should_handle_string(self): | |
| 61 type_resolver = TypeResolver() | |
| 62 helper = IdlTestingHelper() | |
| 63 for string_type in STRING_TYPES: | |
| 64 idl_type = helper.make_stub_idl_type(string_type) | |
| 65 self.assertEqual( | |
| 66 type_resolver._includes_from_type(idl_type), | |
| 67 set([STRING_INCLUDE_PATH])) | |
| 68 | |
| 69 | |
| 70 class InterfaceContextBuilderTest(unittest.TestCase): | |
| 71 | |
| 72 def test_empty(self): | |
| 73 builder = InterfaceContextBuilder('test', TypeResolver()) | |
| 74 | |
| 75 self.assertEqual({'code_generator': 'test'}, builder.build()) | |
| 76 | |
| 77 def test_set_name(self): | |
| 78 builder = InterfaceContextBuilder('test', TypeResolver()) | |
| 79 | |
| 80 builder.set_class_name('foo') | |
| 81 self.assertEqual({ | |
| 82 'code_generator': 'test', | |
| 83 'class_name': 'foo', | |
| 84 }, builder.build()) | |
| 85 | |
| 86 def test_set_inheritance(self): | |
| 87 builder = InterfaceContextBuilder('test', TypeResolver()) | |
| 88 builder.set_inheritance('foo') | |
| 89 self.assertEqual({ | |
| 90 'code_generator': 'test', | |
| 91 'inherits_expression': ' : public foo', | |
| 92 'cpp_includes': set(['foo']), | |
| 93 }, builder.build()) | |
| 94 | |
| 95 builder = InterfaceContextBuilder('test', TypeResolver()) | |
| 96 builder.set_inheritance(None) | |
| 97 self.assertEqual({'code_generator': 'test'}, builder.build()) | |
| 98 | |
| 99 | |
| 100 def test_add_attribute(self): | |
| 101 helper = IdlTestingHelper() | |
| 102 builder = InterfaceContextBuilder('test', TypeResolver()) | |
| 103 | |
| 104 attribute = helper.make_stub_idl_attribute('foo', 'bar') | |
| 105 builder.add_attribute(attribute) | |
| 106 self.assertEqual({ | |
| 107 'code_generator': 'test', | |
| 108 'cpp_includes': set(['bar']), | |
| 109 'attributes': [{'name': 'foo', 'return_type': 'bar'}], | |
| 110 }, builder.build()) | |
| 111 | |
| 112 def test_add_method(self): | |
| 113 helper = IdlTestingHelper() | |
| 114 builder = InterfaceContextBuilder('test', TypeResolver()) | |
| 115 | |
| 116 operation = helper.make_stub_idl_operation('foo', 'bar') | |
| 117 builder.add_operation(operation) | |
| 118 self.assertEqual({ | |
| 119 'code_generator': 'test', | |
| 120 'cpp_includes': set(['bar']), | |
| 121 'methods': [{'name': 'foo', 'return_type': 'bar'}], | |
| 122 }, builder.build()) | |
| OLD | NEW |