File: test_msgcat.rb

package info (click to toggle)
ruby-gettext 3.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 6,424 kB
  • sloc: ruby: 9,643; makefile: 8
file content (627 lines) | stat: -rw-r--r-- 12,394 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# Copyright (C) 2014-2018  Kouhei Sutou <kou@clear-code.com>
#
# License: Ruby's or LGPL
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

require "stringio"
require "tempfile"

require "gettext/tools/msgcat"

class TestToolsMsgCat < Test::Unit::TestCase
  private
  def run_msgcat(input_pos, *options)
    inputs = input_pos.collect do |po|
      input = Tempfile.new("msgcat-input")
      input.write(po)
      input.close
      input
    end
    output = Tempfile.new("msgcat-output")
    command_line = ["--output", output.path]
    command_line.concat(options)
    command_line.concat(inputs.collect(&:path))
    @stdout, @stderr = capture_output do
      GetText::Tools::MsgCat.run(*command_line)
    end
    output.read
  end

  def capture_output
    original_stdout = $stdout
    original_stderr = $stderr
    begin
      stdout = StringIO.new
      stderr = StringIO.new
      $stdout = stdout
      $stderr = stderr
      yield
      [stdout.string, stderr.string]
    ensure
      $stdout = original_stdout
      $stderr = original_stderr
    end
  end

  class TestHeader < self
    def setup
      @po1 = <<-PO
msgid ""
msgstr ""
"Project-Id-Version: gettext 3.0.0\\n"
      PO
      @po2 = <<-PO
msgid ""
msgstr ""
"Language: ja\\n"
      PO
    end

    def test_default
      assert_equal(@po1, run_msgcat([@po1, @po2]))
    end
  end

  class TestNoDuplicated < self
    class TestTranslated < self
      def setup
        @po1 = <<-PO
msgid "Hello"
msgstr "Bonjour"
        PO

        @po2 = <<-PO
msgid "World"
msgstr "Monde"
        PO
      end

      def test_default
        assert_equal(<<-PO.chomp, run_msgcat([@po1, @po2]))
#{@po1}
#{@po2}
        PO
      end
    end
  end

  class TestDuplicated < self
    class TestTranslated < self
      def test_same
        po = <<-PO
msgid "Hello"
msgstr "Bonjour"
        PO

        assert_equal(po, run_msgcat([po, po]))
      end

      def test_different
        po1 = <<-PO
msgid "Hello"
msgstr "Bonjour"
        PO

        po2 = <<-PO
msgid "Hello"
msgstr "Salut"
        PO

        assert_equal(po1, run_msgcat([po1, po2]))
      end

      def test_one_not_translated
        po_not_translated = <<-PO
msgid "Hello"
msgstr ""
        PO

        po_translated = <<-PO
msgid "Hello"
msgstr "Bonjour"
        PO

        assert_equal(po_translated,
                     run_msgcat([po_not_translated, po_translated]))
      end
    end
  end

  class TestSort < self
    class TestByMsgid < self
      def setup
        @po_alice = <<-PO
msgid "Alice"
msgstr ""
        PO

        @po_bob = <<-PO
msgid "Bob"
msgstr ""
        PO

        @po_charlie = <<-PO
msgid "Charlie"
msgstr ""
        PO
      end

      def test_sort_by_msgid
        sorted_po = <<-PO.chomp
#{@po_alice}
#{@po_bob}
#{@po_charlie}
        PO
        assert_equal(sorted_po,
                     run_msgcat([@po_charlie, @po_bob, @po_alice],
                                "--sort-by-msgid"))
      end

      def test_sort_output
        sorted_po = <<-PO.chomp
#{@po_alice}
#{@po_bob}
#{@po_charlie}
        PO
        assert_equal(sorted_po,
                     run_msgcat([@po_charlie, @po_bob, @po_alice],
                                "--sort-output"))
      end

      def test_no_sort_output
        not_sorted_po = <<-PO.chomp
#{@po_charlie}
#{@po_bob}
#{@po_alice}
        PO
        assert_equal(not_sorted_po,
                     run_msgcat([@po_charlie, @po_bob, @po_alice],
                                "--no-sort-output"))
      end
    end

    class TestByReference < self
      def setup
        @po_a1 = <<-PO
#: a.rb:1
msgid "Hello 3"
msgstr ""
        PO

        @po_a2 = <<-PO
#: a.rb:2
msgid "Hello 2"
msgstr ""
        PO

        @po_b1 = <<-PO
#: b.rb:1
msgid "Hello 1"
msgstr ""
        PO
      end

      def test_sort_by_location
        sorted_po = <<-PO.chomp
#{@po_a1}
#{@po_a2}
#{@po_b1}
        PO
        assert_equal(sorted_po,
                     run_msgcat([@po_b1, @po_a2, @po_a1],
                                "--sort-by-location"))
      end

      def test_sort_by_file
        sorted_po = <<-PO.chomp
#{@po_a1}
#{@po_a2}
#{@po_b1}
        PO
        assert_equal(sorted_po,
                     run_msgcat([@po_b1, @po_a2, @po_a1],
                                "--sort-by-file"))
      end
    end
  end

  class TestComment < self
    class TestReference < self
      def setup
        @po = <<-PO
# translator comment
#: a.rb:1
msgid "Hello"
msgstr ""
        PO
      end

      def test_no_location
        assert_equal(<<-PO, run_msgcat([@po], "--no-location"))
# translator comment
msgid "Hello"
msgstr ""
        PO
      end
    end

    class TestTranslator < self
      def setup
        @po = <<-PO
# translator comment
#: a.rb:1
msgid "Hello"
msgstr ""
        PO
      end

      def test_no_translator_comment
        assert_equal(<<-PO, run_msgcat([@po], "--no-translator-comment"))
#: a.rb:1
msgid "Hello"
msgstr ""
        PO
      end
    end

    class TestExtracted < self
      def setup
        @po = <<-PO
#. extracted comment
#: a.rb:1
msgid "Hello"
msgstr ""
        PO
      end

      def test_no_extracted_comment
        assert_equal(<<-PO, run_msgcat([@po], "--no-extracted-comment"))
#: a.rb:1
msgid "Hello"
msgstr ""
        PO
      end
    end

    class TestFlag < self
      def setup
        @po = <<-PO
#, c-format
#: a.rb:1
msgid "Hello"
msgstr ""
        PO
      end

      def test_no_flag_comment
        assert_equal(<<-PO, run_msgcat([@po], "--no-flag-comment"))
#: a.rb:1
msgid "Hello"
msgstr ""
        PO
      end
    end

    class TestPrevious < self
      def setup
        @po = <<-PO
#| msgid "hello"
#: a.rb:1
msgid "Hello"
msgstr ""
        PO
      end

      def test_no_previous_comment
        assert_equal(<<-PO, run_msgcat([@po], "--no-previous-comment"))
#: a.rb:1
msgid "Hello"
msgstr ""
        PO
      end
    end

    class TestAll < self
      def setup
        @po = <<-PO
# translator comment
#. extracted comment
#: hello.rb:1
#, c-format
#| msgid "Hello"
msgid "Hello"
msgstr ""
        PO
      end

      def test_no_all_comments
        assert_equal(<<-PO, run_msgcat([@po], "--no-all-comments"))
msgid "Hello"
msgstr ""
        PO
      end
    end
  end

  class TestWidth < self
    def setup
      @po = <<-PO
msgid "long long long long long long long long long long long long long long long line"
msgstr ""
      PO
    end

    def test_default
      assert_equal(<<-PO, run_msgcat([@po]))
msgid ""
"long long long long long long long long long long long long long long long "
"line"
msgstr ""
      PO
    end

    def test_width
      assert_equal(<<-PO, run_msgcat([@po], "--width=40"))
msgid ""
"long long long long long long long long "
"long long long long long long long line"
msgstr ""
      PO
    end


    def test_wrap
      assert_equal(<<-PO, run_msgcat([@po], "--wrap"))
msgid ""
"long long long long long long long long long long long long long long long "
"line"
msgstr ""
      PO
    end

    def test_no_wrap
      assert_equal(<<-PO, run_msgcat([@po], "--no-wrap"))
msgid "long long long long long long long long long long long long long long long line"
msgstr ""
      PO
    end
  end

  class TestFuzzy < self
    class TestAllFuzzy < self
      def setup
        @po_fuzzy1 = <<-PO
#, fuzzy
msgid "Hello"
msgstr "Bonjour1"
        PO

        @po_fuzzy2 = <<-PO
#, fuzzy
msgid "Hello"
msgstr "Bonjour2"
        PO
      end

      def test_default
        assert_equal(<<-PO, run_msgcat([@po_fuzzy1, @po_fuzzy2]))
#, fuzzy
msgid "Hello"
msgstr "Bonjour1"
        PO
      end

      def test_no_fuzzy
        assert_equal("", run_msgcat([@po_fuzzy1, @po_fuzzy2], "--no-fuzzy"))
      end
    end

    class TestHaveNoFuzzy < self
      def setup
        @po_fuzzy = <<-PO
#, fuzzy
msgid "Hello"
msgstr "Bonjour1"
        PO

        @po_not_fuzzy = <<-PO
msgid "Hello"
msgstr "Bonjour2"
        PO
      end

      def test_default
        assert_equal(<<-PO, run_msgcat([@po_fuzzy, @po_not_fuzzy]))
msgid "Hello"
msgstr "Bonjour2"
        PO
      end

      def test_no_fuzzy
        assert_equal(<<-PO, run_msgcat([@po_fuzzy, @po_not_fuzzy], "--no-fuzzy"))
msgid "Hello"
msgstr "Bonjour2"
        PO
      end
    end

    class TestNoAllComments < self
      def setup
        @po_fuzzy1 = <<-PO
#, fuzzy
msgid "Hello"
msgstr "Bonjour1"
        PO

        @po_fuzzy2 = <<-PO
#, fuzzy
msgid "Hello"
msgstr "Bonjour2"
        PO
      end

      def test_default
        pos = [@po_fuzzy1, @po_fuzzy2]
        assert_equal(<<-PO, run_msgcat(pos, "--no-all-comments"))
msgid "Hello"
msgstr "Bonjour1"
        PO
      end

      def test_no_fuzzy
        pos = [@po_fuzzy1, @po_fuzzy2]
        assert_equal("", run_msgcat(pos, "--no-all-comments", "--no-fuzzy"))
      end
    end
  end

  class TestWarning < self
    def setup
      @po_fuzzy = <<-PO
#, fuzzy
msgid "Hello World"
msgstr "Bonjour"
      PO
    end

    def test_default
      run_msgcat([@po_fuzzy])
      assert_not_empty(@stderr)
    end

    def test_no_report_warning
      run_msgcat([@po_fuzzy], "--no-report-warning")
      assert_empty(@stderr)
    end
  end

  class TestObsoleteEntries < self
    def setup
      @po_obsolete = <<-PO
#~ msgid "Hello World"
#~ msgstr "Bonjour"
      PO
    end

    def test_default
      assert_equal(<<-PO, run_msgcat([@po_obsolete]))
#~ msgid "Hello World"
#~ msgstr "Bonjour"
      PO
    end

    def test_no_obsolete_entries
      assert_equal("", run_msgcat([@po_obsolete], "--no-obsolete-entries"))
    end
  end

  class TestUpdatePORevisionDate < self
    def setup
      @po_revision_date = "2018-03-05 14:55+0900"
      @po_source = <<-PO
msgid ""
msgstr ""
"Language: ja\\n"
"PO-Revision-Date: #{@po_revision_date}\\n"
"MIME-Version: 1.0\\n"
      PO
    end

    def test_default
      po = run_msgcat([@po_source])
      assert_equal(<<-PO, normalize_result(po))
msgid ""
msgstr ""
"Language: ja\\n"
"PO-Revision-Date: ORIGINAL-PO-REVISION-DATE\\n"
"MIME-Version: 1.0\\n"
      PO
    end

    def test_update_po_revision_date
      po = run_msgcat([@po_source], "--update-po-revision-date")
      assert_equal(<<-PO, normalize_result(po))
msgid ""
msgstr ""
"Language: ja\\n"
"PO-Revision-Date: UPDATED-PO-REVISION-DATE\\n"
"MIME-Version: 1.0\\n"
      PO
    end

    def test_no_po_revision_date
      po = run_msgcat([<<-PO], "--update-po-revision-date")
msgid ""
msgstr ""
"Language: ja\\n"
"MIME-Version: 1.0\\n"
      PO
      assert_equal(<<-PO, normalize_result(po))
msgid ""
msgstr ""
"Language: ja\\n"
"MIME-Version: 1.0\\n"
"PO-Revision-Date: UPDATED-PO-REVISION-DATE\\n"
      PO
    end

    private
    def normalize_result(result)
      result.gsub(/"PO-Revision-Date: (.+?)\\n"/) do
        po_revision_date = $1
        if po_revision_date == @po_revision_date
          new_po_revision_date = "ORIGINAL-PO-REVISION-DATE"
        else
          new_po_revision_date = "UPDATED-PO-REVISION-DATE"
        end
        "\"PO-Revision-Date: #{new_po_revision_date}\\n\""
      end
    end
  end

  class TestRemoveHeaderField < self
    def setup
      @po_header = <<-PO
msgid ""
msgstr ""
"Project-Id-Version: gettext 3.0.0\\n"
"POT-Creation-Date: 2014-02-23 19:02+0900\\n"
"Language: ja\\n"
      PO
    end

    def test_one
      options = ["--remove-header-field=POT-Creation-Date"]
      assert_equal(<<-PO, run_msgcat([@po_header], *options))
msgid ""
msgstr ""
"Project-Id-Version: gettext 3.0.0\\n"
"Language: ja\\n"
      PO
    end

    def test_multiple
      options = [
        "--remove-header-field=Project-Id-Version",
        "--remove-header-field=POT-Creation-Date",
      ]
      assert_equal(<<-PO, run_msgcat([@po_header], *options))
msgid ""
msgstr ""
"Language: ja\\n"
      PO
    end
  end
end