|
| 1 | +# coding: utf-8 |
| 2 | +from static_precompiler.compilers.es6 import ES6Script |
| 3 | +from static_precompiler.exceptions import StaticCompilationError |
| 4 | +import unittest |
| 5 | + |
| 6 | + |
| 7 | +class ES6ScriptTestCase(unittest.TestCase): |
| 8 | + |
| 9 | + @staticmethod |
| 10 | + def clean_javascript(js): |
| 11 | + """ Remove comments and all blank lines. """ |
| 12 | + return "\n".join( |
| 13 | + line for line in js.split("\n") if line.strip() and not line.startswith("//") |
| 14 | + ) |
| 15 | + |
| 16 | + def test_compile_file(self): |
| 17 | + compiler = ES6Script() |
| 18 | + |
| 19 | + self.assertEqual( |
| 20 | + self.clean_javascript(compiler.compile_file("scripts/test.es6")), |
| 21 | + """"use strict";\nconsole.log("Hello, World!");""" |
| 22 | + ) |
| 23 | + |
| 24 | + def test_compile_source(self): |
| 25 | + compiler = ES6Script() |
| 26 | + |
| 27 | + self.assertEqual( |
| 28 | + self.clean_javascript(compiler.compile_source('console.log("Hello, World!");')), |
| 29 | + """"use strict";\nconsole.log("Hello, World!");""" |
| 30 | + ) |
| 31 | + |
| 32 | + self.assertRaises( |
| 33 | + StaticCompilationError, |
| 34 | + lambda: compiler.compile_source('console.log "Hello, World!') |
| 35 | + ) |
| 36 | + |
| 37 | + # Test non-ascii |
| 38 | + self.assertEqual( |
| 39 | + self.clean_javascript(compiler.compile_source('console.log("Привет, Мир!");')), |
| 40 | + """"use strict";\nconsole.log("Привет, Мир!");""" |
| 41 | + ) |
| 42 | + |
| 43 | + |
| 44 | +def suite(): |
| 45 | + loader = unittest.TestLoader() |
| 46 | + test_suite = unittest.TestSuite() |
| 47 | + test_suite.addTest(loader.loadTestsFromTestCase(ES6ScriptTestCase)) |
| 48 | + return test_suite |
| 49 | + |
| 50 | + |
| 51 | +if __name__ == '__main__': |
| 52 | + unittest.TextTestRunner(verbosity=2).run(suite()) |
0 commit comments