/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/tests/test_export_pot.py

  • Committer: Jelmer Vernooij
  • Date: 2019-03-04 00:16:27 UTC
  • mfrom: (7293 work)
  • mto: This revision was merged to the branch mainline in revision 7318.
  • Revision ID: jelmer@jelmer.uk-20190304001627-v6u7o6pf97tukhek
Merge trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
 
33
33
    def test_simple_escape(self):
34
34
        self.assertEqual(
35
 
                export_pot._escape('foobar'),
36
 
                'foobar')
 
35
            export_pot._escape('foobar'),
 
36
            'foobar')
37
37
 
38
38
        s = '''foo\nbar\r\tbaz\\"spam"'''
39
39
        e = '''foo\\nbar\\r\\tbaz\\\\\\"spam\\"'''
82
82
'''
83
83
        cls_lines, _ = export_pot._parse_source(src)
84
84
        self.assertEqual(cls_lines,
85
 
            {"Ancient": 2, "Modern": 5})
 
85
                         {"Ancient": 2, "Modern": 5})
86
86
 
87
87
    def test_classes_nested(self):
88
88
        src = '''
93
93
'''
94
94
        cls_lines, _ = export_pot._parse_source(src)
95
95
        self.assertEqual(cls_lines,
96
 
            {"Matroska": 2, "Smaller": 3, "Smallest":4})
 
96
                         {"Matroska": 2, "Smaller": 3, "Smallest": 4})
97
97
 
98
98
    def test_strings_docstrings(self):
99
99
        src = '''\
110
110
'''
111
111
        _, str_lines = export_pot._parse_source(src)
112
112
        self.assertEqual(str_lines,
113
 
            {"Module": 1, "Function": 4, "Class": 7, "Method": 10})
 
113
                         {"Module": 1, "Function": 4, "Class": 7, "Method": 10})
114
114
 
115
115
    def test_strings_literals(self):
116
116
        src = '''\
120
120
'''
121
121
        _, str_lines = export_pot._parse_source(src)
122
122
        self.assertEqual(str_lines,
123
 
            {"One": 1, "Two": 2, "Three": 3})
 
123
                         {"One": 1, "Two": 2, "Three": 3})
124
124
 
125
125
    def test_strings_multiline(self):
126
126
        src = '''\
136
136
'''
137
137
        _, str_lines = export_pot._parse_source(src)
138
138
        self.assertEqual(str_lines,
139
 
            {"Start\n\nEnd\n": 1, "ABC": 6})
 
139
                         {"Start\n\nEnd\n": 1, "ABC": 6})
140
140
 
141
141
    def test_strings_multiline_escapes(self):
142
142
        src = '''\
150
150
'''
151
151
        _, str_lines = export_pot._parse_source(src)
152
152
        self.expectFailure("Escaped newlines confuses the multiline handling",
153
 
            self.assertNotEqual, str_lines,
154
 
            {"Escaped\n": 0, "Raw\\n": 2, "A\n\nB\n\nC\n\n": -2})
 
153
                           self.assertNotEqual, str_lines,
 
154
                           {"Escaped\n": 0, "Raw\\n": 2, "A\n\nB\n\nC\n\n": -2})
155
155
        self.assertEqual(str_lines,
156
 
            {"Escaped\n": 1, "Raw\\n": 2, "A\n\nB\n\nC\n\n": 4})
 
156
                         {"Escaped\n": 1, "Raw\\n": 2, "A\n\nB\n\nC\n\n": 4})
157
157
 
158
158
 
159
159
class TestModuleContext(tests.TestCase):
171
171
    def test_from_class(self):
172
172
        """New context returned with lineno updated from class"""
173
173
        path = "cls.py"
174
 
        class A(object): pass
175
 
        class B(object): pass
 
174
 
 
175
        class A(object):
 
176
            pass
 
177
 
 
178
        class B(object):
 
179
            pass
176
180
        cls_lines = {"A": 5, "B": 7}
177
181
        context = export_pot._ModuleContext(path, _source_info=(cls_lines, {}))
178
182
        contextA = context.from_class(A)
187
191
    def test_from_class_missing(self):
188
192
        """When class has no lineno the old context details are returned"""
189
193
        path = "cls_missing.py"
190
 
        class A(object): pass
191
 
        class M(object): pass
 
194
 
 
195
        class A(object):
 
196
            pass
 
197
 
 
198
        class M(object):
 
199
            pass
192
200
        context = export_pot._ModuleContext(path, 3, ({"A": 15}, {}))
193
201
        contextA = context.from_class(A)
194
202
        contextM1 = context.from_class(M)
241
249
    def test_option_with_help(self):
242
250
        opt = option.Option("helpful", help="Info.")
243
251
        self.assertContainsString(self.pot_from_option(opt), "\n"
244
 
            "# help of 'helpful' test\n"
245
 
            "msgid \"Info.\"\n")
 
252
                                  "# help of 'helpful' test\n"
 
253
                                  "msgid \"Info.\"\n")
246
254
 
247
255
    def test_option_hidden(self):
248
256
        opt = option.Option("hidden", help="Unseen.", hidden=True)
252
260
        context = export_pot._ModuleContext("remote.py", 3)
253
261
        opt = option.Option("metaphor", help="Not a literal in the source.")
254
262
        self.assertContainsString(self.pot_from_option(opt, context),
255
 
            "#: remote.py:3\n"
256
 
            "# help of 'metaphor' test\n")
 
263
                                  "#: remote.py:3\n"
 
264
                                  "# help of 'metaphor' test\n")
257
265
 
258
266
    def test_option_context_string(self):
259
267
        s = "Literally."
260
268
        context = export_pot._ModuleContext("local.py", 3, ({}, {s: 17}))
261
269
        opt = option.Option("example", help=s)
262
270
        self.assertContainsString(self.pot_from_option(opt, context),
263
 
            "#: local.py:17\n"
264
 
            "# help of 'example' test\n")
 
271
                                  "#: local.py:17\n"
 
272
                                  "# help of 'example' test\n")
265
273
 
266
274
    def test_registry_option_title(self):
267
275
        opt = option.RegistryOption.from_kwargs("group", help="Pick one.",
268
 
            title="Choose!")
 
276
                                                title="Choose!")
269
277
        pot = self.pot_from_option(opt)
270
278
        self.assertContainsString(pot, "\n"
271
 
            "# title of 'group' test\n"
272
 
            "msgid \"Choose!\"\n")
 
279
                                  "# title of 'group' test\n"
 
280
                                  "msgid \"Choose!\"\n")
273
281
        self.assertContainsString(pot, "\n"
274
 
            "# help of 'group' test\n"
275
 
            "msgid \"Pick one.\"\n")
 
282
                                  "# help of 'group' test\n"
 
283
                                  "msgid \"Pick one.\"\n")
276
284
 
277
285
    def test_registry_option_title_context_missing(self):
278
286
        context = export_pot._ModuleContext("theory.py", 3)
279
287
        opt = option.RegistryOption.from_kwargs("abstract", title="Unfounded!")
280
288
        self.assertContainsString(self.pot_from_option(opt, context),
281
 
            "#: theory.py:3\n"
282
 
            "# title of 'abstract' test\n")
 
289
                                  "#: theory.py:3\n"
 
290
                                  "# title of 'abstract' test\n")
283
291
 
284
292
    def test_registry_option_title_context_string(self):
285
293
        s = "Grounded!"
286
294
        context = export_pot._ModuleContext("practice.py", 3, ({}, {s: 144}))
287
295
        opt = option.RegistryOption.from_kwargs("concrete", title=s)
288
296
        self.assertContainsString(self.pot_from_option(opt, context),
289
 
            "#: practice.py:144\n"
290
 
            "# title of 'concrete' test\n")
 
297
                                  "#: practice.py:144\n"
 
298
                                  "# title of 'concrete' test\n")
291
299
 
292
300
    def test_registry_option_value_switches(self):
293
301
        opt = option.RegistryOption.from_kwargs("switch", help="Flip one.",
294
 
            value_switches=True, enum_switch=False,
295
 
            red="Big.", green="Small.")
 
302
                                                value_switches=True, enum_switch=False,
 
303
                                                red="Big.", green="Small.")
296
304
        pot = self.pot_from_option(opt)
297
305
        self.assertContainsString(pot, "\n"
298
 
            "# help of 'switch' test\n"
299
 
            "msgid \"Flip one.\"\n")
300
 
        self.assertContainsString(pot, "\n"
301
 
            "# help of 'switch=red' test\n"
302
 
            "msgid \"Big.\"\n")
303
 
        self.assertContainsString(pot, "\n"
304
 
            "# help of 'switch=green' test\n"
305
 
            "msgid \"Small.\"\n")
 
306
                                  "# help of 'switch' test\n"
 
307
                                  "msgid \"Flip one.\"\n")
 
308
        self.assertContainsString(pot, "\n"
 
309
                                  "# help of 'switch=red' test\n"
 
310
                                  "msgid \"Big.\"\n")
 
311
        self.assertContainsString(pot, "\n"
 
312
                                  "# help of 'switch=green' test\n"
 
313
                                  "msgid \"Small.\"\n")
306
314
 
307
315
    def test_registry_option_value_switches_hidden(self):
308
316
        reg = registry.Registry()
 
317
 
309
318
        class Hider(object):
310
319
            hidden = True
311
320
        reg.register("new", 1, "Current.")
312
321
        reg.register("old", 0, "Legacy.", info=Hider())
313
322
        opt = option.RegistryOption("protocol", "Talking.", reg,
314
 
            value_switches=True, enum_switch=False)
 
323
                                    value_switches=True, enum_switch=False)
315
324
        pot = self.pot_from_option(opt)
316
325
        self.assertContainsString(pot, "\n"
317
 
            "# help of 'protocol' test\n"
318
 
            "msgid \"Talking.\"\n")
 
326
                                  "# help of 'protocol' test\n"
 
327
                                  "msgid \"Talking.\"\n")
319
328
        self.assertContainsString(pot, "\n"
320
 
            "# help of 'protocol=new' test\n"
321
 
            "msgid \"Current.\"\n")
 
329
                                  "# help of 'protocol=new' test\n"
 
330
                                  "msgid \"Current.\"\n")
322
331
        self.assertNotContainsString(pot, "'protocol=old'")
323
332
 
324
333
 
351
360
 
352
361
    def check_output(self, expected):
353
362
        self.assertEqual(
354
 
                self.exporter.outf.getvalue(),
355
 
                textwrap.dedent(expected)
356
 
                )
 
363
            self.exporter.outf.getvalue(),
 
364
            textwrap.dedent(expected)
 
365
            )
357
366
 
358
367
 
359
368
class TestPoEntry(PoEntryTestCase):
389
398
 
390
399
    def test_single(self):
391
400
        self.exporter.poentry_per_paragraph(
392
 
                'dummy',
393
 
                10,
394
 
                '''foo\nbar\nbaz\n'''
395
 
                )
 
401
            'dummy',
 
402
            10,
 
403
            '''foo\nbar\nbaz\n'''
 
404
            )
396
405
        self.check_output('''\
397
406
                #: dummy:10
398
407
                msgid ""
404
413
 
405
414
    def test_multi(self):
406
415
        self.exporter.poentry_per_paragraph(
407
 
                'dummy',
408
 
                10,
409
 
                '''spam\nham\negg\n\nSPAM\nHAM\nEGG\n'''
410
 
                )
 
416
            'dummy',
 
417
            10,
 
418
            '''spam\nham\negg\n\nSPAM\nHAM\nEGG\n'''
 
419
            )
411
420
        self.check_output('''\
412
421
                #: dummy:10
413
422
                msgid ""
449
458
        result = re.sub(r'(?m)^#: [^\n]+\n', '', result)
450
459
 
451
460
        self.assertEqualDiff(
452
 
                'msgid "A sample command."\n'
453
 
                'msgstr ""\n'
454
 
                '\n'                # :Usage: should not be translated.
455
 
                'msgid ""\n'
456
 
                '":Examples:\\n"\n'
457
 
                '"    Example 1::"\n'
458
 
                'msgstr ""\n'
459
 
                '\n'
460
 
                'msgid "        cmd arg1"\n'
461
 
                'msgstr ""\n'
462
 
                '\n'
463
 
                'msgid "Blah Blah Blah"\n'
464
 
                'msgstr ""\n'
465
 
                '\n',
466
 
                result
467
 
                )
 
461
            'msgid "A sample command."\n'
 
462
            'msgstr ""\n'
 
463
            '\n'                # :Usage: should not be translated.
 
464
            'msgid ""\n'
 
465
            '":Examples:\\n"\n'
 
466
            '"    Example 1::"\n'
 
467
            'msgstr ""\n'
 
468
            '\n'
 
469
            'msgid "        cmd arg1"\n'
 
470
            'msgstr ""\n'
 
471
            '\n'
 
472
            'msgid "Blah Blah Blah"\n'
 
473
            'msgstr ""\n'
 
474
            '\n',
 
475
            result
 
476
            )