| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/python | |
| 2 # Copyright (c) 2011, 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 import logging.config | |
| 7 import unittest | |
| 8 import templateloader | |
| 9 | |
| 10 class TemplateLoaderTestCase(unittest.TestCase): | |
| 11 | |
| 12 def _preprocess(self, input_text, conds): | |
| 13 loader = templateloader.TemplateLoader('.', [], conds) | |
| 14 return loader._Preprocess(input_text, '<file>') | |
| 15 | |
| 16 | |
| 17 def _preprocess_test(self, input_text, conds, expected_text): | |
| 18 output_text = self._preprocess(input_text, conds) | |
| 19 if output_text != expected_text: | |
| 20 msg = ''' | |
| 21 EXPECTED: | |
| 22 %s | |
| 23 --- | |
| 24 ACTUAL : | |
| 25 %s | |
| 26 ---''' % (expected_text, output_text) | |
| 27 self.fail(msg) | |
| 28 | |
| 29 | |
| 30 def _preprocess_error_test(self, input_text, conds, expected_message): | |
| 31 threw = False | |
| 32 try: | |
| 33 output_text = self._preprocess(input_text, conds) | |
| 34 except Exception, e: | |
| 35 threw = True | |
| 36 if str(e).find(expected_message) == -1: | |
| 37 self.fail("'%s' does not contain '%s'" % (e, expected_message)) | |
| 38 if not threw: | |
| 39 self.fail("missing error, expected '%s'" % expected_message) | |
| 40 | |
| 41 | |
| 42 def test_freevar(self): | |
| 43 input_text = '''$A | |
| 44 $B''' | |
| 45 self._preprocess_test(input_text, {}, input_text) | |
| 46 | |
| 47 def test_comments(self): | |
| 48 input_text = '''//$ comment 1 | |
| 49 Hello | |
| 50 //$ comment 2''' | |
| 51 self._preprocess_test(input_text, {}, 'Hello\n') | |
| 52 | |
| 53 | |
| 54 def test_ite1(self): | |
| 55 input_text = ''' | |
| 56 aaa | |
| 57 $if A | |
| 58 bbb | |
| 59 $else | |
| 60 ccc | |
| 61 $endif | |
| 62 ddd | |
| 63 ''' | |
| 64 self._preprocess_test(input_text, {'A':True}, | |
| 65 ''' | |
| 66 aaa | |
| 67 bbb | |
| 68 ddd | |
| 69 ''') | |
| 70 | |
| 71 def test_ite2(self): | |
| 72 input_text = ''' | |
| 73 aaa | |
| 74 $if A | |
| 75 bbb | |
| 76 $else | |
| 77 ccc | |
| 78 $endif | |
| 79 ddd | |
| 80 ''' | |
| 81 self._preprocess_test(input_text, {'A':False}, | |
| 82 ''' | |
| 83 aaa | |
| 84 ccc | |
| 85 ddd | |
| 86 ''') | |
| 87 | |
| 88 def test_if1(self): | |
| 89 input_text = ''' | |
| 90 $if | |
| 91 ''' | |
| 92 self._preprocess_error_test(input_text, {}, | |
| 93 '$if does not have single variable') | |
| 94 | |
| 95 def test_if2(self): | |
| 96 input_text = ''' | |
| 97 $if A | |
| 98 ''' | |
| 99 self._preprocess_error_test(input_text, {}, 'Unknown $if variable') | |
| 100 | |
| 101 def test_else1(self): | |
| 102 input_text = ''' | |
| 103 $else | |
| 104 ''' | |
| 105 self._preprocess_error_test(input_text, {}, '$else without $if') | |
| 106 | |
| 107 def test_else2(self): | |
| 108 input_text = ''' | |
| 109 $if A | |
| 110 $else | |
| 111 $else | |
| 112 ''' | |
| 113 self._preprocess_error_test(input_text, {'A':True}, 'Double $else') | |
| 114 | |
| 115 def test_eof1(self): | |
| 116 input_text = ''' | |
| 117 $if A | |
| 118 ''' | |
| 119 self._preprocess_error_test(input_text, {'A':True}, 'Unterminated') | |
| 120 | |
| 121 def test_eof2(self): | |
| 122 input_text = ''' | |
| 123 $if A | |
| 124 $else | |
| 125 ''' | |
| 126 self._preprocess_error_test(input_text, {'A':True}, 'Unterminated') | |
| 127 | |
| 128 if __name__ == "__main__": | |
| 129 logging.config.fileConfig("logging.conf") | |
| 130 if __name__ == '__main__': | |
| 131 unittest.main() | |
| OLD | NEW |