File: test_xgettext.rb

package info (click to toggle)
ruby-gettext 3.0.2-2~bpo70+1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 4,728 kB
  • sloc: ruby: 7,977; makefile: 8
file content (607 lines) | stat: -rw-r--r-- 14,062 bytes parent folder | download
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
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012-2013  Kouhei Sutou <kou@clear-code.com>
# Copyright (C) 2012  Haruka Yoshihara <yoshihara@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 "locale"
require "gettext/tools/xgettext"

class TestToolsXGetText < Test::Unit::TestCase
  include GetTextTestUtils

  def setup
    @xgettext = GetText::Tools::XGetText.new
    @now = Time.parse("2012-08-19 18:10+0900")
    stub(@xgettext).now {@now}
  end

  setup :setup_tmpdir
  teardown :teardown_tmpdir

  setup
  def setup_paths
    @rb_file_path = File.join(@tmpdir, "lib", "xgettext.rb")
    @pot_file_path = File.join(@tmpdir, "po", "xgettext.pot")
    @rhtml_file_path = File.join(@tmpdir, "templates", "xgettext.rhtml")
    FileUtils.mkdir_p(File.dirname(@rb_file_path))
    FileUtils.mkdir_p(File.dirname(@pot_file_path))
    FileUtils.mkdir_p(File.dirname(@rhtml_file_path))
  end

  private
  def generate(ruby_source, *command_line_options)
    File.open(@rb_file_path, "w") do |rb_file|
      rb_file.puts(ruby_source)
    end

    command_line = ["--output", @pot_file_path]
    command_line += command_line_options
    command_line += [@rb_file_path]
    @xgettext.run(*command_line)

    File.read(@pot_file_path)
  end

  def header(options=nil)
    options ||= {}
    package_name = options[:package_name] || "PACKAGE"
    package_version = options[:package_version] || "VERSION"
    msgid_bugs_address = options[:msgid_bugs_address] || ""
    copyright_holder = options[:copyright_holder] ||
                         "THE PACKAGE'S COPYRIGHT HOLDER"
    output_encoding = options[:to_code] || "UTF-8"

    time = @now.strftime("%Y-%m-%d %H:%M%z")
    <<-"EOH"
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR #{copyright_holder}
# This file is distributed under the same license as the #{package_name} package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: #{package_name} #{package_version}\\n"
"Report-Msgid-Bugs-To: #{msgid_bugs_address}\\n"
"POT-Creation-Date: #{time}\\n"
"PO-Revision-Date: #{time}\\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"
"Language-Team: LANGUAGE <LL@li.org>\\n"
"Language: \\n"
"MIME-Version: 1.0\\n"
"Content-Type: text/plain; charset=#{output_encoding}\\n"
"Content-Transfer-Encoding: 8bit\\n"
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\\n"
EOH
  end

  class TestReference < self
    def test_relative
      pot_content = generate(<<-EOR)
_("Hello")
EOR
      assert_equal(<<-EOP, pot_content)
#{header}
#: ../lib/xgettext.rb:1
msgid "Hello"
msgstr ""
EOP
    end

    def test_same_message
      pot_content = generate(<<-EOR)
_("Hello")
_("Hello")
EOR
      assert_equal(<<-EOP, pot_content)
#{header}
#: ../lib/xgettext.rb:1 ../lib/xgettext.rb:2
msgid "Hello"
msgstr ""
EOP
    end
  end

  class TestEncoding < self
    def test_different_encoding_from_current_locale
      need_encoding

      rhtml = <<-EOR
<%#-*- coding: sjis -*-%>
<html>
<head>
<title></title>
</head>
<body>
<h1><%= _("わたし") %></h1>
</body>
</html>
EOR
      File.open(@rhtml_file_path, "w") do |rhtml_file|
        rhtml_file.puts(encode(rhtml, "sjis"))
      end

      @xgettext.run("--output", @pot_file_path, @rhtml_file_path)

      encoding = "UTF-8"
      pot_content = File.read(@pot_file_path)
      set_encoding(pot_content, encoding)
      expected_content = <<-EOP
#{header}
#: ../templates/xgettext.rhtml:7
msgid "わたし"
msgstr ""
EOP
      expected_content = encode(expected_content, encoding)
      assert_equal(expected_content, pot_content)
    end

    def test_multiple_encodings
      need_encoding

      File.open(@rb_file_path, "w") do |rb_file|
        rb_file.puts(encode(<<-EOR, "euc-jp"))
# -*- coding: euc-jp -*-
_("こんにちは")
EOR
      end

      File.open(@rhtml_file_path, "w") do |rhtml_file|
        rhtml_file.puts(encode(<<-EOR, "cp932"))
<%# -*- coding: cp932 -*-%>
<h1><%= _("わたし") %></h1>
EOR
      end

      @xgettext.run("--output", @pot_file_path, @rb_file_path, @rhtml_file_path)

      encoding = "UTF-8"
      pot_content = File.read(@pot_file_path)
      set_encoding(pot_content, encoding)
      expected_content = <<-EOP
#{header}
#: ../lib/xgettext.rb:2
msgid "こんにちは"
msgstr ""

#: ../templates/xgettext.rhtml:2
msgid "わたし"
msgstr ""
EOP
      expected_content = encode(expected_content, encoding)
      assert_equal(expected_content, pot_content)
    end
  end

  class TestTranslatorComment < self
    def test_n_
      pot_content = generate(<<-RB, "--add-comments=TRANSLATORS:")
n_members = 1
# TRANSLATORS: Use this message as test
n_("I will go!",
   "We will go!",
   n_members)
RB
      expected_content = <<-POT
#{header}
#. TRANSLATORS: Use this message as test
#: ../lib/xgettext.rb:3
msgid "I will go!"
msgid_plural "We will go!"
msgstr[0] ""
msgstr[1] ""
POT
      assert_equal(expected_content, pot_content)
    end

    def test_no_add_comments
      pot_content = generate(<<-RUBY)
# TRNALSTORS: There is no translator's tag
_("Message")
      RUBY

      assert_equal(<<-POT, pot_content)
#{header}
#: ../lib/xgettext.rb:2
msgid "Message"
msgstr ""
      POT
    end

    def test_add_comments_without_tag
      pot_content = generate(<<-RUBY, "--add-comments")
# There is no translator's tag but all
# comments are included.
_("Message")
      RUBY

      assert_equal(<<-POT, pot_content)
#{header}
#. There is no translator's tag but all
#. comments are included.
#: ../lib/xgettext.rb:3
msgid "Message"
msgstr ""
      POT
    end

    def test_not_started_with_tag
      pot_content = generate(<<-RUBY, "--add-comments=TRANSLATORS:")
# This comment isn't started with "TRANSLATORS:" tag
_("Message")
      RUBY

      assert_equal(<<-POT, pot_content)
#{header}
#: ../lib/xgettext.rb:2
msgid "Message"
msgstr ""
      POT
    end

    def test_not_first_line_is_started_with_tag
      pot_content = generate(<<-RUBY, "--add-comments=TRANSLATORS:")
# The first line.
# TRANSLATORS: The second line.
# The third line.
_("Message")
      RUBY

      assert_equal(<<-POT, pot_content)
#{header}
#. TRANSLATORS: The second line.
#. The third line.
#: ../lib/xgettext.rb:4
msgid "Message"
msgstr ""
      POT
    end

    def test_indented
      pot_content = generate(<<-RUBY, "--add-comments")
# The following lines are indented
#   * indented1
#   * indented2
_("Message")
      RUBY

      assert_equal(<<-POT, pot_content)
#{header}
#. The following lines are indented
#. * indented1
#. * indented2
#: ../lib/xgettext.rb:4
msgid "Message"
msgstr ""
      POT
    end

    def test_multiple
      pot_content = generate(<<-RUBY, "--add-comments=TRANSLATORS:")
# TRANSLATORS: The first comment
_("Message")

# TRANSLATORS: The second comment
_("Message")
      RUBY

      assert_equal(<<-POT, pot_content)
#{header}
#. TRANSLATORS: The first comment
#. TRANSLATORS: The second comment
#: ../lib/xgettext.rb:2 ../lib/xgettext.rb:5
msgid "Message"
msgstr ""
      POT
    end
  end

  class TestCommandLineOption < self
    def test_package_name
      package_name = "test-package"
      pot_content = generate(":hello", "--package-name", package_name)

      options = {:package_name => package_name}
      assert_equal(header(options), pot_content)
    end

    def test_package_version
      package_version = "1.2.3"
      pot_content = generate(":hello", "--package-version", package_version)

      options = {:package_version => package_version}
      assert_equal(header(options), pot_content)
    end

    def test_report_msgid_bugs_to
      msgid_bugs_address = "me@example.com"
      pot_content = generate(":hello",
                             "--msgid-bugs-address", msgid_bugs_address)

      options = {:msgid_bugs_address => msgid_bugs_address}
      assert_equal(header(options), pot_content)
    end

    def test_copyright_holder
      copyright_holder = "me"
      pot_content = generate(":hello", "--copyright-holder", copyright_holder)

      options = {:copyright_holder => copyright_holder}
      assert_equal(header(options), pot_content)
    end

    def test_to_code
      need_encoding

      output_encoding = "EUC-JP"
      pot_content = generate(<<-EOR, "--output-encoding", output_encoding)
# -*- coding: utf-8 -*-

_("わたし")
EOR
      set_encoding(pot_content, output_encoding)

      options = {:to_code => output_encoding}
      expected_pot = <<-EOP
#{header(options)}
#: ../lib/xgettext.rb:3
msgid "わたし"
msgstr ""
EOP
      expected_pot = encode(expected_pot, output_encoding)

      assert_equal(expected_pot, pot_content)
    end

    class TestLocation < self
      def test_default
        assert_equal(<<-POT, generate("_('hello')"))
#{header}
#: ../lib/xgettext.rb:1
msgid "hello"
msgstr ""
        POT
      end

      def test_location
        assert_equal(<<-POT, generate("_('hello')", "--location"))
#{header}
#: ../lib/xgettext.rb:1
msgid "hello"
msgstr ""
        POT
      end

      def test_no_location
        assert_equal(<<-POT, generate("_('hello')", "--no-location"))
#{header}
msgid "hello"
msgstr ""
        POT
      end
    end

    class TestSort < self
      def setup
        super
        @code = <<-RUBY
        RUBY
      end

      def test_default
        assert_equal(<<-POT, generate)
#{header}
#: ../lib/xgettext.rb:1
msgid "World"
msgstr ""

#: ../lib/xgettext.rb:2
msgid "Hello"
msgstr ""

#: ../templates/xgettext.rhtml:1
msgid "ABC"
msgstr ""

#: ../templates/xgettext.rhtml:2
msgid "123"
msgstr ""
        POT
      end

      def test_no_sort_output
        assert_equal(<<-POT, generate("--no-sort-output"))
#{header}
#: ../templates/xgettext.rhtml:1
msgid "ABC"
msgstr ""

#: ../templates/xgettext.rhtml:2
msgid "123"
msgstr ""

#: ../lib/xgettext.rb:1
msgid "World"
msgstr ""

#: ../lib/xgettext.rb:2
msgid "Hello"
msgstr ""
        POT
      end

      def test_sort_by_file
        assert_equal(<<-POT, generate("--sort-by-file"))
#{header}
#: ../lib/xgettext.rb:1
msgid "World"
msgstr ""

#: ../lib/xgettext.rb:2
msgid "Hello"
msgstr ""

#: ../templates/xgettext.rhtml:1
msgid "ABC"
msgstr ""

#: ../templates/xgettext.rhtml:2
msgid "123"
msgstr ""
        POT
      end

      def test_sort_by_msgid
        assert_equal(<<-POT, generate("--sort-by-msgid"))
#{header}
#: ../templates/xgettext.rhtml:2
msgid "123"
msgstr ""

#: ../templates/xgettext.rhtml:1
msgid "ABC"
msgstr ""

#: ../lib/xgettext.rb:2
msgid "Hello"
msgstr ""

#: ../lib/xgettext.rb:1
msgid "World"
msgstr ""
        POT
      end

      private
      def generate(*command_line_options)
        File.open(@rhtml_file_path, "w") do |rhtml_file|
          rhtml_file.puts(<<-RHTML)
<%= _("ABC") %>
<%= _("123") %>
          RHTML
        end

        File.open(@rb_file_path, "w") do |rb_file|
          rb_file.puts(<<-RUBY)
_('World')
_('Hello')
          RUBY
        end

        command_line = ["--output", @pot_file_path]
        command_line += command_line_options
        command_line += [@rhtml_file_path, @rb_file_path]
        @xgettext.run(*command_line)

        File.read(@pot_file_path)
      end
    end

    class TestWidth < self
      def msgid
        <<-MSGID.chomp
Hello very long line! This line is very long. Yes! This line is very long! Very very long line!
        MSGID
      end

      def test_default
        assert_equal(<<-POT, generate("_('#{msgid}')"))
#{header}
#: ../lib/xgettext.rb:1
msgid ""
"Hello very long line! This line is very long. Yes! This line is very long! Ver"
"y very long line!"
msgstr ""
        POT
      end

      def test_width
        assert_equal(<<-POT, generate("_('#{msgid}')", "--width", "70"))
#{header}
#: ../lib/xgettext.rb:1
msgid ""
"Hello very long line! This line is very long. Yes! This line is very l"
"ong! Very very long line!"
msgstr ""
        POT
      end

      def test_no_wrap
        assert_equal(<<-POT, generate("_('#{msgid}')", "--no-wrap"))
#{header}
#: ../lib/xgettext.rb:1
msgid "Hello very long line! This line is very long. Yes! This line is very long! Very very long line!"
msgstr ""
        POT
      end
    end
  end

  class TestAddParser < self
    setup
    def setup_default_parsers
      @default_parsers = default_parsers.dup
    end

    teardown
    def teardown_default_parsers
      default_parsers.replace(@default_parsers)
    end

    def test_class_method
      GetText::Tools::XGetText.add_parser(mock_html_parser)
      xgettext = GetText::Tools::XGetText.new
      xgettext.parse(["index.html"])
    end

    def test_old_style_parser
      parser = Object.new
      def parser.target?(path)
        true
      end
      def parser.parse(path)
        # It doesn't receive options argument as the second argument
        [["Message"]]
      end

      GetText::Tools::XGetText.add_parser(parser)
      xgettext = GetText::Tools::XGetText.new
      po_entry = GetText::POEntry.new(:normal)
      po_entry.msgid = "Message"
      assert_equal([po_entry],
                   xgettext.parse(["index.html"]).to_a)
    end

    def test_instance_method
      @xgettext.add_parser(mock_html_parser)
      @xgettext.parse(["index.html"])
    end

    private
    def default_parsers
      GetText::Tools::XGetText.module_eval("@@default_parsers")
    end

    def mock_html_parser
      html_parser = Object.new
      mock(html_parser).target?("index.html") {true}
      mock(html_parser).parse("index.html") {[]}
      html_parser
    end
  end
end