| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/python | |
| 2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 3 # for details. All rights reserved. Use of this source code is governed by a | |
| 4 # BSD-style license that can be found in the LICENSE file. | |
| 5 | |
| 6 """Tests for dartgenerator.""" | |
| 7 | |
| 8 import logging.config | |
| 9 import os.path | |
| 10 import re | |
| 11 import shutil | |
| 12 import tempfile | |
| 13 import unittest | |
| 14 import dartgenerator | |
| 15 import database | |
| 16 import idlnode | |
| 17 import idlparser | |
| 18 | |
| 19 | |
| 20 class DartGeneratorTestCase(unittest.TestCase): | |
| 21 | |
| 22 def _InDatabase(self, interface_name): | |
| 23 return os.path.exists(os.path.join(self._database_dir, | |
| 24 '%s.idl' % interface_name)) | |
| 25 | |
| 26 def _FilePathForDartInterface(self, interface_name): | |
| 27 return os.path.join(self._generator._output_dir, 'src', 'interface', | |
| 28 '%s.dart' % interface_name) | |
| 29 | |
| 30 def _InOutput(self, interface_name): | |
| 31 return os.path.exists( | |
| 32 self._FilePathForDartInterface(interface_name)) | |
| 33 | |
| 34 def _ReadOutputFile(self, interface_name): | |
| 35 self.assertTrue(self._InOutput(interface_name)) | |
| 36 file_path = self._FilePathForDartInterface(interface_name) | |
| 37 f = open(file_path, 'r') | |
| 38 content = f.read() | |
| 39 f.close() | |
| 40 return content, file_path | |
| 41 | |
| 42 def _AssertOutputSansHeaderEquals(self, interface_name, expected_content): | |
| 43 full_actual_content, file_path = self._ReadOutputFile(interface_name) | |
| 44 # Remove file header comments in // or multiline /* ... */ syntax. | |
| 45 header_re = re.compile(r'^(\s*(//.*|/\*([^*]|\*[^/])*\*/)\s*)*') | |
| 46 actual_content = header_re.sub('', full_actual_content) | |
| 47 if expected_content != actual_content: | |
| 48 msg = """ | |
| 49 FILE: %s | |
| 50 EXPECTED: | |
| 51 %s | |
| 52 ACTUAL: | |
| 53 %s | |
| 54 """ % (file_path, expected_content, actual_content) | |
| 55 self.fail(msg) | |
| 56 | |
| 57 def _AssertOutputContains(self, interface_name, expected_content): | |
| 58 actual_content, file_path = self._ReadOutputFile(interface_name) | |
| 59 if expected_content not in actual_content: | |
| 60 msg = """ | |
| 61 STRING: %s | |
| 62 Was found not in output file: %s | |
| 63 FILE CONTENT: | |
| 64 %s | |
| 65 """ % (expected_content, file_path, actual_content) | |
| 66 self.fail(msg) | |
| 67 | |
| 68 def _AssertOutputDoesNotContain(self, interface_name, expected_content): | |
| 69 actual_content, file_path = self._ReadOutputFile(interface_name) | |
| 70 if expected_content in actual_content: | |
| 71 msg = """ | |
| 72 STRING: %s | |
| 73 Was found in output file: %s | |
| 74 FILE CONTENT: | |
| 75 %s | |
| 76 """ % (expected_content, file_path, actual_content) | |
| 77 self.fail(msg) | |
| 78 | |
| 79 def setUp(self): | |
| 80 self._working_dir = tempfile.mkdtemp() | |
| 81 self._output_dir = os.path.join(self._working_dir, 'output') | |
| 82 self._database_dir = os.path.join(self._working_dir, 'database') | |
| 83 self._auxiliary_dir = os.path.join(self._working_dir, 'auxiliary') | |
| 84 self.assertFalse(os.path.exists(self._database_dir)) | |
| 85 | |
| 86 # Create database and add one interface. | |
| 87 db = database.Database(self._database_dir) | |
| 88 os.mkdir(self._auxiliary_dir) | |
| 89 self.assertTrue(os.path.exists(self._database_dir)) | |
| 90 | |
| 91 content = """ | |
| 92 module shapes { | |
| 93 @A1 @A2 | |
| 94 interface Shape { | |
| 95 @A1 @A2 getter attribute int attr; | |
| 96 @A1 setter attribute int attr; | |
| 97 @A3 boolean op(); | |
| 98 const long CONSTANT = 1; | |
| 99 getter attribute DOMString strAttr; | |
| 100 Shape create(); | |
| 101 boolean compare(Shape s); | |
| 102 Rectangle createRectangle(); | |
| 103 void addLine(lines::Line line); | |
| 104 void someDartType(File file); | |
| 105 void someUnidentifiedType(UnidentifiableType t); | |
| 106 }; | |
| 107 }; | |
| 108 | |
| 109 module rectangles { | |
| 110 @A3 | |
| 111 interface Rectangle : @A3 shapes::Shape { | |
| 112 void someTemplatedType(List<Shape> list); | |
| 113 }; | |
| 114 }; | |
| 115 | |
| 116 module lines { | |
| 117 @A1 | |
| 118 interface Line : shapes::Shape { | |
| 119 }; | |
| 120 }; | |
| 121 """ | |
| 122 | |
| 123 parser = idlparser.IDLParser(idlparser.FREMONTCUT_SYNTAX) | |
| 124 ast = parser.parse(content) | |
| 125 idl_file = idlnode.IDLFile(ast) | |
| 126 for interface in idl_file.interfaces: | |
| 127 db.AddInterface(interface) | |
| 128 db.Save() | |
| 129 | |
| 130 self.assertTrue(self._InDatabase('Shape')) | |
| 131 self.assertTrue(self._InDatabase('Rectangle')) | |
| 132 self.assertTrue(self._InDatabase('Line')) | |
| 133 | |
| 134 self._database = database.Database(self._database_dir) | |
| 135 self._generator = dartgenerator.DartGenerator(self._auxiliary_dir, | |
| 136 '../templates', | |
| 137 'test') | |
| 138 | |
| 139 def tearDown(self): | |
| 140 shutil.rmtree(self._database_dir) | |
| 141 shutil.rmtree(self._auxiliary_dir) | |
| 142 | |
| 143 def testBasicGeneration(self): | |
| 144 # Generate all interfaces: | |
| 145 self._database.Load() | |
| 146 self._generator.Generate(self._database, self._output_dir) | |
| 147 self._generator.Flush() | |
| 148 | |
| 149 self.assertTrue(self._InOutput('Shape')) | |
| 150 self.assertTrue(self._InOutput('Rectangle')) | |
| 151 self.assertTrue(self._InOutput('Line')) | |
| 152 | |
| 153 def testFilterByAnnotations(self): | |
| 154 self._database.Load() | |
| 155 self._generator.FilterInterfaces(self._database, ['A1', 'A2'], ['A3']) | |
| 156 self._generator.Generate(self._database, self._output_dir) | |
| 157 self._generator.Flush() | |
| 158 | |
| 159 # Only interfaces with (@A1 and @A2) or @A3 should be generated: | |
| 160 self.assertTrue(self._InOutput('Shape')) | |
| 161 self.assertTrue(self._InOutput('Rectangle')) | |
| 162 self.assertFalse(self._InOutput('Line')) | |
| 163 | |
| 164 # Only members with (@A1 and @A2) or @A3 should be generated: | |
| 165 # TODO(sra): make th | |
| 166 self._AssertOutputSansHeaderEquals('Shape', """interface Shape { | |
| 167 | |
| 168 final int attr; | |
| 169 | |
| 170 bool op(); | |
| 171 } | |
| 172 """) | |
| 173 | |
| 174 self._AssertOutputContains('Rectangle', | |
| 175 'interface Rectangle extends shapes::Shape') | |
| 176 | |
| 177 def testTypeRenames(self): | |
| 178 self._database.Load() | |
| 179 # Translate 'Shape' to spanish: | |
| 180 self._generator.RenameTypes(self._database, {'Shape': 'Forma'}, False) | |
| 181 self._generator.Generate(self._database, self._output_dir) | |
| 182 self._generator.Flush() | |
| 183 | |
| 184 # Validate that all references to Shape have been converted: | |
| 185 self._AssertOutputContains('Forma', | |
| 186 'interface Forma') | |
| 187 self._AssertOutputContains('Forma', 'Forma create();') | |
| 188 self._AssertOutputContains('Forma', | |
| 189 'bool compare(Forma s);') | |
| 190 self._AssertOutputContains('Rectangle', | |
| 191 'interface Rectangle extends Forma') | |
| 192 | |
| 193 def testQualifiedDartTypes(self): | |
| 194 self._database.Load() | |
| 195 self._generator.FilterMembersWithUnidentifiedTypes(self._database) | |
| 196 self._generator.Generate(self._database, self._output_dir) | |
| 197 self._generator.Flush() | |
| 198 | |
| 199 # Verify primitive conversions are working: | |
| 200 self._AssertOutputContains('Shape', | |
| 201 'static const int CONSTANT = 1') | |
| 202 self._AssertOutputContains('Shape', | |
| 203 'final String strAttr;') | |
| 204 | |
| 205 # Verify interface names are converted: | |
| 206 self._AssertOutputContains('Shape', | |
| 207 'interface Shape {') | |
| 208 self._AssertOutputContains('Shape', | |
| 209 ' Shape create();') | |
| 210 # TODO(sra): Why is this broken? Output contains qualified type. | |
| 211 #self._AssertOutputContains('Shape', | |
| 212 # 'void addLine(Line line);') | |
| 213 self._AssertOutputContains('Shape', | |
| 214 'Rectangle createRectangle();') | |
| 215 # TODO(sra): Why is this broken? Output contains qualified type. | |
| 216 #self._AssertOutputContains('Rectangle', | |
| 217 # 'interface Rectangle extends Shape') | |
| 218 # Verify dart names are preserved: | |
| 219 # TODO(vsm): Re-enable when package / namespaces are enabled. | |
| 220 # self._AssertOutputContains('shapes', 'Shape', | |
| 221 # 'void someDartType(File file);') | |
| 222 | |
| 223 # Verify that unidentified types are not removed: | |
| 224 self._AssertOutputDoesNotContain('Shape', | |
| 225 'someUnidentifiedType') | |
| 226 | |
| 227 # Verify template conversion: | |
| 228 # TODO(vsm): Re-enable when core collections are supported. | |
| 229 # self._AssertOutputContains('rectangles', 'Rectangle', | |
| 230 # 'void someTemplatedType(List<Shape> list)') | |
| 231 | |
| 232 | |
| 233 if __name__ == '__main__': | |
| 234 logging.config.fileConfig('logging.conf') | |
| 235 if __name__ == '__main__': | |
| 236 unittest.main() | |
| OLD | NEW |