/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: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2018-07-13 00:34:53 UTC
  • mfrom: (7027.3.8 python3-n)
  • Revision ID: breezy.the.bot@gmail.com-20180713003453-3wofrh7brng5jz3l
Add some more bees; support writing both bytes and unicode strings in build_tree_contents.

Merged from https://code.launchpad.net/~jelmer/brz/python3-n/+merge/349091

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
import re
18
18
import textwrap
19
19
 
 
20
from io import StringIO
 
21
 
20
22
from .. import (
21
23
    commands,
22
24
    export_pot,
24
26
    registry,
25
27
    tests,
26
28
    )
27
 
from ..sixish import (
28
 
    BytesIO,
29
 
    )
30
29
 
31
30
 
32
31
class TestEscape(tests.TestCase):
228
227
    """Tests for writing texts extracted from options in pot format"""
229
228
 
230
229
    def pot_from_option(self, opt, context=None, note="test"):
231
 
        sio = BytesIO()
 
230
        sio = StringIO()
232
231
        exporter = export_pot._PotExporter(sio)
233
232
        if context is None:
234
233
            context = export_pot._ModuleContext("nowhere", 0)
237
236
 
238
237
    def test_option_without_help(self):
239
238
        opt = option.Option("helpless")
240
 
        self.assertEqual(b"", self.pot_from_option(opt))
 
239
        self.assertEqual("", self.pot_from_option(opt))
241
240
 
242
241
    def test_option_with_help(self):
243
242
        opt = option.Option("helpful", help="Info.")
244
 
        self.assertContainsString(self.pot_from_option(opt), b"\n"
245
 
            b"# help of 'helpful' test\n"
246
 
            b"msgid \"Info.\"\n")
 
243
        self.assertContainsString(self.pot_from_option(opt), "\n"
 
244
            "# help of 'helpful' test\n"
 
245
            "msgid \"Info.\"\n")
247
246
 
248
247
    def test_option_hidden(self):
249
248
        opt = option.Option("hidden", help="Unseen.", hidden=True)
250
 
        self.assertEqual(b"", self.pot_from_option(opt))
 
249
        self.assertEqual("", self.pot_from_option(opt))
251
250
 
252
251
    def test_option_context_missing(self):
253
252
        context = export_pot._ModuleContext("remote.py", 3)
254
253
        opt = option.Option("metaphor", help="Not a literal in the source.")
255
254
        self.assertContainsString(self.pot_from_option(opt, context),
256
 
            b"#: remote.py:3\n"
257
 
            b"# help of 'metaphor' test\n")
 
255
            "#: remote.py:3\n"
 
256
            "# help of 'metaphor' test\n")
258
257
 
259
258
    def test_option_context_string(self):
260
259
        s = "Literally."
261
260
        context = export_pot._ModuleContext("local.py", 3, ({}, {s: 17}))
262
261
        opt = option.Option("example", help=s)
263
262
        self.assertContainsString(self.pot_from_option(opt, context),
264
 
            b"#: local.py:17\n"
265
 
            b"# help of 'example' test\n")
 
263
            "#: local.py:17\n"
 
264
            "# help of 'example' test\n")
266
265
 
267
266
    def test_registry_option_title(self):
268
267
        opt = option.RegistryOption.from_kwargs("group", help="Pick one.",
269
268
            title="Choose!")
270
269
        pot = self.pot_from_option(opt)
271
 
        self.assertContainsString(pot, b"\n"
272
 
            b"# title of 'group' test\n"
273
 
            b"msgid \"Choose!\"\n")
274
 
        self.assertContainsString(pot, b"\n"
275
 
            b"# help of 'group' test\n"
276
 
            b"msgid \"Pick one.\"\n")
 
270
        self.assertContainsString(pot, "\n"
 
271
            "# title of 'group' test\n"
 
272
            "msgid \"Choose!\"\n")
 
273
        self.assertContainsString(pot, "\n"
 
274
            "# help of 'group' test\n"
 
275
            "msgid \"Pick one.\"\n")
277
276
 
278
277
    def test_registry_option_title_context_missing(self):
279
278
        context = export_pot._ModuleContext("theory.py", 3)
280
279
        opt = option.RegistryOption.from_kwargs("abstract", title="Unfounded!")
281
280
        self.assertContainsString(self.pot_from_option(opt, context),
282
 
            b"#: theory.py:3\n"
283
 
            b"# title of 'abstract' test\n")
 
281
            "#: theory.py:3\n"
 
282
            "# title of 'abstract' test\n")
284
283
 
285
284
    def test_registry_option_title_context_string(self):
286
285
        s = "Grounded!"
287
286
        context = export_pot._ModuleContext("practice.py", 3, ({}, {s: 144}))
288
287
        opt = option.RegistryOption.from_kwargs("concrete", title=s)
289
288
        self.assertContainsString(self.pot_from_option(opt, context),
290
 
            b"#: practice.py:144\n"
291
 
            b"# title of 'concrete' test\n")
 
289
            "#: practice.py:144\n"
 
290
            "# title of 'concrete' test\n")
292
291
 
293
292
    def test_registry_option_value_switches(self):
294
293
        opt = option.RegistryOption.from_kwargs("switch", help="Flip one.",
295
294
            value_switches=True, enum_switch=False,
296
295
            red="Big.", green="Small.")
297
296
        pot = self.pot_from_option(opt)
298
 
        self.assertContainsString(pot, b"\n"
299
 
            b"# help of 'switch' test\n"
300
 
            b"msgid \"Flip one.\"\n")
301
 
        self.assertContainsString(pot, b"\n"
302
 
            b"# help of 'switch=red' test\n"
303
 
            b"msgid \"Big.\"\n")
304
 
        self.assertContainsString(pot, b"\n"
305
 
            b"# help of 'switch=green' test\n"
306
 
            b"msgid \"Small.\"\n")
 
297
        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")
307
306
 
308
307
    def test_registry_option_value_switches_hidden(self):
309
308
        reg = registry.Registry()
314
313
        opt = option.RegistryOption("protocol", "Talking.", reg,
315
314
            value_switches=True, enum_switch=False)
316
315
        pot = self.pot_from_option(opt)
317
 
        self.assertContainsString(pot, b"\n"
318
 
            b"# help of 'protocol' test\n"
319
 
            b"msgid \"Talking.\"\n")
320
 
        self.assertContainsString(pot, b"\n"
321
 
            b"# help of 'protocol=new' test\n"
322
 
            b"msgid \"Current.\"\n")
323
 
        self.assertNotContainsString(pot, b"'protocol=old'")
 
316
        self.assertContainsString(pot, "\n"
 
317
            "# help of 'protocol' test\n"
 
318
            "msgid \"Talking.\"\n")
 
319
        self.assertContainsString(pot, "\n"
 
320
            "# help of 'protocol=new' test\n"
 
321
            "msgid \"Current.\"\n")
 
322
        self.assertNotContainsString(pot, "'protocol=old'")
324
323
 
325
324
 
326
325
class TestPotExporter(tests.TestCase):
328
327
 
329
328
    # This test duplicates test_duplicates below
330
329
    def test_duplicates(self):
331
 
        exporter = export_pot._PotExporter(BytesIO())
 
330
        exporter = export_pot._PotExporter(StringIO())
332
331
        context = export_pot._ModuleContext("mod.py", 1)
333
332
        exporter.poentry_in_context(context, "Common line.")
334
333
        context.lineno = 3
335
334
        exporter.poentry_in_context(context, "Common line.")
336
 
        self.assertEqual(1, exporter.outf.getvalue().count(b"Common line."))
 
335
        self.assertEqual(1, exporter.outf.getvalue().count("Common line."))
337
336
 
338
337
    def test_duplicates_included(self):
339
 
        exporter = export_pot._PotExporter(BytesIO(), True)
 
338
        exporter = export_pot._PotExporter(StringIO(), True)
340
339
        context = export_pot._ModuleContext("mod.py", 1)
341
340
        exporter.poentry_in_context(context, "Common line.")
342
341
        context.lineno = 3
343
342
        exporter.poentry_in_context(context, "Common line.")
344
 
        self.assertEqual(2, exporter.outf.getvalue().count(b"Common line."))
 
343
        self.assertEqual(2, exporter.outf.getvalue().count("Common line."))
345
344
 
346
345
 
347
346
class PoEntryTestCase(tests.TestCase):
348
347
 
349
348
    def setUp(self):
350
349
        super(PoEntryTestCase, self).setUp()
351
 
        self.exporter = export_pot._PotExporter(BytesIO())
 
350
        self.exporter = export_pot._PotExporter(StringIO())
352
351
 
353
352
    def check_output(self, expected):
354
353
        self.assertEqual(
355
354
                self.exporter.outf.getvalue(),
356
 
                textwrap.dedent(expected.decode('utf-8')).encode('utf-8')
 
355
                textwrap.dedent(expected)
357
356
                )
358
357
 
359
358
 
362
361
    def test_simple(self):
363
362
        self.exporter.poentry('dummy', 1, "spam")
364
363
        self.exporter.poentry('dummy', 2, "ham", 'EGG')
365
 
        self.check_output(b'''\
 
364
        self.check_output('''\
366
365
                #: dummy:1
367
366
                msgid "spam"
368
367
                msgstr ""
379
378
        # This should be ignored.
380
379
        self.exporter.poentry('dummy', 2, "spam", 'EGG')
381
380
 
382
 
        self.check_output(b'''\
 
381
        self.check_output('''\
383
382
                #: dummy:1
384
383
                msgid "spam"
385
384
                msgstr ""\n
394
393
                10,
395
394
                '''foo\nbar\nbaz\n'''
396
395
                )
397
 
        self.check_output(b'''\
 
396
        self.check_output('''\
398
397
                #: dummy:10
399
398
                msgid ""
400
399
                "foo\\n"
409
408
                10,
410
409
                '''spam\nham\negg\n\nSPAM\nHAM\nEGG\n'''
411
410
                )
412
 
        self.check_output(b'''\
 
411
        self.check_output('''\
413
412
                #: dummy:10
414
413
                msgid ""
415
414
                "spam\\n"
447
446
        export_pot._write_command_help(self.exporter, cmd_Demo())
448
447
        result = self.exporter.outf.getvalue()
449
448
        # We don't care about filename and lineno here.
450
 
        result = re.sub(br'(?m)^#: [^\n]+\n', b'', result)
 
449
        result = re.sub(r'(?m)^#: [^\n]+\n', '', result)
451
450
 
452
451
        self.assertEqualDiff(
453
 
                b'msgid "A sample command."\n'
454
 
                b'msgstr ""\n'
455
 
                b'\n'                # :Usage: should not be translated.
456
 
                b'msgid ""\n'
457
 
                b'":Examples:\\n"\n'
458
 
                b'"    Example 1::"\n'
459
 
                b'msgstr ""\n'
460
 
                b'\n'
461
 
                b'msgid "        cmd arg1"\n'
462
 
                b'msgstr ""\n'
463
 
                b'\n'
464
 
                b'msgid "Blah Blah Blah"\n'
465
 
                b'msgstr ""\n'
466
 
                b'\n',
 
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',
467
466
                result
468
467
                )