|
3 | 3 |
|
4 | 4 | import unittest
|
5 | 5 |
|
| 6 | +try: |
| 7 | + from compare_locales.parser import PropertiesParser, DTDParser |
| 8 | +except ImportError: |
| 9 | + PropertiesParser = DTDParser = None |
| 10 | + |
6 | 11 | import fluent.syntax.ast as FTL
|
7 | 12 |
|
8 | 13 | from fluent.migrate.errors import NotSupportedError
|
9 | 14 | from fluent.migrate.transforms import Source, COPY, PLURALS, REPLACE
|
| 15 | +from fluent.migrate.util import parse |
10 | 16 | from fluent.migrate.helpers import EXTERNAL_ARGUMENT
|
11 | 17 |
|
12 | 18 |
|
@@ -49,3 +55,87 @@ def test_replace(self):
|
49 | 55 | }
|
50 | 56 | )
|
51 | 57 | )
|
| 58 | + |
| 59 | + |
| 60 | +class MockContext(unittest.TestCase): |
| 61 | + def get_source(self, _path, key): |
| 62 | + # Ignore _path (test.properties) and get translations from self.strings. |
| 63 | + return self.strings[key].val |
| 64 | + |
| 65 | + |
| 66 | +@unittest.skipUnless(PropertiesParser, 'compare-locales required') |
| 67 | +class TestProperties(MockContext): |
| 68 | + def setUp(self): |
| 69 | + self.strings = parse(PropertiesParser, ''' |
| 70 | + foo = Foo |
| 71 | +
|
| 72 | + unicode-start = \\u0020Foo |
| 73 | + unicode-middle = Foo\\u0020Bar |
| 74 | + unicode-end = Foo\\u0020 |
| 75 | +
|
| 76 | + html-entity = <⇧⌘K> |
| 77 | + ''') |
| 78 | + |
| 79 | + def test_simple_text(self): |
| 80 | + source = Source('test.properties', 'foo') |
| 81 | + self.assertEqual(source(self), 'Foo') |
| 82 | + |
| 83 | + def test_escape_unicode_start(self): |
| 84 | + source = Source('test.properties', 'unicode-start') |
| 85 | + self.assertEqual(source(self), ' Foo') |
| 86 | + |
| 87 | + def test_escape_unicode_middle(self): |
| 88 | + source = Source('test.properties', 'unicode-middle') |
| 89 | + self.assertEqual(source(self), 'Foo Bar') |
| 90 | + |
| 91 | + def test_escape_unicode_end(self): |
| 92 | + source = Source('test.properties', 'unicode-end') |
| 93 | + self.assertEqual(source(self), 'Foo ') |
| 94 | + |
| 95 | + def test_html_entity(self): |
| 96 | + source = Source('test.properties', 'html-entity') |
| 97 | + self.assertEqual(source(self), '<⇧⌘K>') |
| 98 | + |
| 99 | + |
| 100 | +@unittest.skipUnless(DTDParser, 'compare-locales required') |
| 101 | +class TestDTD(MockContext): |
| 102 | + def setUp(self): |
| 103 | + self.strings = parse(DTDParser, ''' |
| 104 | + <!ENTITY foo "Foo"> |
| 105 | +
|
| 106 | + <!ENTITY unicodeEscape "Foo\\u0020Bar"> |
| 107 | +
|
| 108 | + <!ENTITY named "&"> |
| 109 | + <!ENTITY decimal "&"> |
| 110 | + <!ENTITY shorthexcode "&"> |
| 111 | + <!ENTITY longhexcode "&"> |
| 112 | + <!ENTITY unknown "&unknownEntity;"> |
| 113 | + ''') |
| 114 | + |
| 115 | + def test_simple_text(self): |
| 116 | + source = Source('test.dtd', 'foo') |
| 117 | + self.assertEqual(source(self), 'Foo') |
| 118 | + |
| 119 | + def test_backslash_unicode_escape(self): |
| 120 | + source = Source('test.dtd', 'unicodeEscape') |
| 121 | + self.assertEqual(source(self), 'Foo\\u0020Bar') |
| 122 | + |
| 123 | + def test_named_entity(self): |
| 124 | + source = Source('test.dtd', 'named') |
| 125 | + self.assertEqual(source(self), '&') |
| 126 | + |
| 127 | + def test_decimal_entity(self): |
| 128 | + source = Source('test.dtd', 'decimal') |
| 129 | + self.assertEqual(source(self), '&') |
| 130 | + |
| 131 | + def test_shorthex_entity(self): |
| 132 | + source = Source('test.dtd', 'shorthexcode') |
| 133 | + self.assertEqual(source(self), '&') |
| 134 | + |
| 135 | + def test_longhex_entity(self): |
| 136 | + source = Source('test.dtd', 'longhexcode') |
| 137 | + self.assertEqual(source(self), '&') |
| 138 | + |
| 139 | + def test_unknown_entity(self): |
| 140 | + source = Source('test.dtd', 'unknown') |
| 141 | + self.assertEqual(source(self), '&unknownEntity;') |
0 commit comments