File: test_parser.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 (217 lines) | stat: -rw-r--r-- 8,872 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
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012  Haruka Yoshihara <yoshihara@clear-code.com>
# Copyright (C) 2012-2013  Kouhei Sutou <kou@clear-code.com>
# Copyright (C) 2010  masone (Christian Felder) <ema@rh-productions.ch>
# Copyright (C) 2009  Vladimir Dobriakov <vladimir@geekq.net>
# Copyright (C) 2009-2010  Masao Mutoh
#
# 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 "tempfile"
require "gettext/tools/parser/ruby"
require "gettext/tools/parser/glade"
require "gettext/tools/parser/erb"

require "gettext/tools/xgettext"

class TestGetTextParser < Test::Unit::TestCase
  def setup
    @xgettext = GetText::Tools::XGetText.new
  end

  class TestRuby < self
    def test__
      @xgettext.parse_options[:comment_tag] = "TRANSLATORS:"
      @ary = @xgettext.parse(['fixtures/_.rb'])

      assert_target 'jjj', ['fixtures/_.rb:71']
      assert_target 'kkk', ['fixtures/_.rb:72']
      assert_target 'lllmmm', ['fixtures/_.rb:76']
      assert_target "nnn\nooo", ['fixtures/_.rb:84']
      assert_target "\#", ['fixtures/_.rb:88', 'fixtures/_.rb:92']
      assert_target "\\taaa", ['fixtures/_.rb:96']
      assert_target "Here document1\nHere document2\n", ['fixtures/_.rb:100']
      assert_target "Francois Pinard", ['fixtures/_.rb:120'] do |t|
        assert_match(/proper name/, t.extracted_comment)
        assert_match(/Pronunciation/, t.extracted_comment)
      end

      assert_target("No TRANSLATORS comment", ["fixtures/_.rb:123"]) do |t|
        assert_nil(t.comment)
      end

      assert_target "self explaining", ['fixtures/_.rb:128'] do |t|
        assert_nil t.comment
      end

      assert_target "This is a # including string.", ["fixtures/_.rb:132"]

      # TODO: assert_target "in_quote", ['fixtures/_.rb:118']
    end

    def test_N_
      @ary = @xgettext.parse(['fixtures/N_.rb'])

      assert_target 'aaa', ['fixtures/N_.rb:10']
      assert_target "aaa\n", ['fixtures/N_.rb:14']
      assert_target "bbb\nccc", ['fixtures/N_.rb:18']
      assert_target "bbb\nccc\nddd\n", ['fixtures/N_.rb:22']
      assert_target 'eee', ['fixtures/N_.rb:29', 'fixtures/N_.rb:33']
      assert_target 'fff', ['fixtures/N_.rb:33']
      assert_target 'ggghhhiii', ['fixtures/N_.rb:37']
      assert_target 'a"b"c"', ['fixtures/N_.rb:43']
      assert_target 'd"e"f"', ['fixtures/N_.rb:47']
      assert_target 'jjj', ['fixtures/N_.rb:51']
      assert_target 'kkk', ['fixtures/N_.rb:52']
      assert_target 'lllmmm', ['fixtures/N_.rb:56']
      assert_target "nnn\nooo", ['fixtures/N_.rb:64']
    end

    def test_n_
      @xgettext.parse_options[:comment_tag] = "TRANSLATORS:"
      @ary = @xgettext.parse(['fixtures/n_.rb'])
      assert_plural_target "aaa", "aaa2", ['fixtures/n_.rb:29']
      assert_plural_target "bbb\n", "ccc2\nccc2", ['fixtures/n_.rb:33']
      assert_plural_target "ddd\nddd", "ddd2\nddd2", ['fixtures/n_.rb:37']
      assert_plural_target "eee\neee\n", "eee2\neee2\n", ['fixtures/n_.rb:42']
      assert_plural_target "ddd\neee\n", "ddd\neee2", ['fixtures/n_.rb:48']
      assert_plural_target "fff", "fff2", ['fixtures/n_.rb:55', 'fixtures/n_.rb:59']
      assert_plural_target "ggg", "ggg2", ['fixtures/n_.rb:59']
      assert_plural_target "ggghhhiii", "jjjkkklll", ['fixtures/n_.rb:63']
      assert_plural_target "a\"b\"c\"", "a\"b\"c\"2", ['fixtures/n_.rb:72']
      assert_plural_target "mmmmmm", "mmm2mmm2", ['fixtures/n_.rb:80']
      assert_plural_target "nnn", "nnn2", ['fixtures/n_.rb:81']
      assert_plural_target "comment", "comments", ['fixtures/n_.rb:97'] do |t|
        assert_equal "TRANSLATORS:please provide translations for all\n the plural forms!",
                       t.extracted_comment
      end
    end

    def test_p_
      @xgettext.parse_options[:comment_tag] = "TRANSLATORS:"
      @ary = @xgettext.parse(['fixtures/p_.rb'])
      assert_target_in_context "AAA", "BBB", ["fixtures/p_.rb:29", "fixtures/p_.rb:33"]
      assert_target_in_context "AAA|BBB", "CCC", ["fixtures/p_.rb:37"]
      assert_target_in_context "AAA", "CCC", ["fixtures/p_.rb:41"]
      assert_target_in_context "CCC", "BBB", ["fixtures/p_.rb:45"]
      assert_target_in_context "program", "name", ['fixtures/p_.rb:55'] do |t|
        assert_equal "TRANSLATORS:please translate 'name' in the context of 'program'.\n Hint: the translation should NOT contain the translation of 'program'.", t.extracted_comment
      end
    end
  end

  class TestGlade < self
    def test_old_style
      # Old style (~2.0.4)
      ary = GetText::GladeParser.parse('fixtures/gladeparser.glade')

      assert_equal(['window1', 'fixtures/gladeparser.glade:8'], ary[0])
      assert_equal(['normal text', 'fixtures/gladeparser.glade:29'], ary[1])
      assert_equal(['1st line\n2nd line\n3rd line', 'fixtures/gladeparser.glade:50'], ary[2])
      assert_equal(['<span color="red" weight="bold" size="large">markup </span>', 'fixtures/gladeparser.glade:73'], ary[3])
      assert_equal(['<span color="red">1st line markup </span>\n<span color="blue">2nd line markup</span>', 'fixtures/gladeparser.glade:94'], ary[4])
      assert_equal(['<span>&quot;markup&quot; with &lt;escaped strings&gt;</span>', 'fixtures/gladeparser.glade:116'], ary[5])
      assert_equal(['duplicated', 'fixtures/gladeparser.glade:137', 'fixtures/gladeparser.glade:158'], ary[6])
    end
  end

  class TestErbParser < self
    include GetTextTestUtils

    def test_detect_encoding
      need_encoding

      euc_file = Tempfile.new("euc-jp.rhtml")
      euc_file.open
      euc_file.puts("<%#-*- coding: euc-jp -*-%>")
      euc_file.close

      erb_source = ERB.new(File.read(euc_file.path)).src
      encoding = GetText::ErbParser.new(euc_file.path).detect_encoding(erb_source)

      assert_equal("EUC-JP", encoding)
    end

    def test_ascii
      @ary = GetText::ErbParser.parse('fixtures/erb/ascii.rhtml')

      assert_target 'aaa', ['fixtures/erb/ascii.rhtml:8']
      assert_target "aaa\n", ['fixtures/erb/ascii.rhtml:11']
      assert_target 'bbb', ['fixtures/erb/ascii.rhtml:12']
      assert_plural_target "ccc1", "ccc2", ['fixtures/erb/ascii.rhtml:13']
    end

    def test_non_ascii
      fixture_path = "fixtures/erb/non_ascii.rhtml"
      @ary = GetText::ErbParser.parse(fixture_path)

      assert_target('わたし', ["#{fixture_path}:11"])
    end
  end

  def test_xgettext_parse
    GetText::ErbParser.init(:extnames => ['.rhtml', '.rxml'])
    @ary = @xgettext.parse(['fixtures/erb/ascii.rhtml'])
    assert_target 'aaa', ['fixtures/erb/ascii.rhtml:8']
    assert_target "aaa\n", ['fixtures/erb/ascii.rhtml:11']
    assert_target 'bbb', ['fixtures/erb/ascii.rhtml:12']
    assert_plural_target "ccc1", "ccc2", ['fixtures/erb/ascii.rhtml:13']

    @ary = @xgettext.parse(['fixtures/erb/ascii.rxml'])
    assert_target 'aaa', ['fixtures/erb/ascii.rxml:9']
    assert_target "aaa\n", ['fixtures/erb/ascii.rxml:12']
    assert_target 'bbb', ['fixtures/erb/ascii.rxml:13']
    assert_plural_target "ccc1", "ccc2", ['fixtures/erb/ascii.rxml:14']

    @ary = @xgettext.parse(['fixtures/n_.rb'])
    assert_plural_target "ooo", "ppp", ['fixtures/n_.rb:85', 'fixtures/n_.rb:86']
    assert_plural_target "qqq", "rrr", ['fixtures/n_.rb:90', 'fixtures/n_.rb:91']
  end

  private

  def assert_target(msgid, references = nil)
    t = @ary.detect {|elem| elem.msgid == msgid}
    if t
      if references
        assert_equal references.sort, t.references.sort, 'Translation target references do not match.'
      end
      yield t if block_given?
    else
      flunk "Expected a translation target with id '#{msgid}'. Not found."
    end
  end

  def assert_plural_target(msgid, plural, references = nil)
    assert_target msgid, references do |t|
      assert_equal plural, t.msgid_plural, 'Expected plural form'
      yield t if block_given?
    end
  end

  def assert_target_in_context(msgctxt, msgid, references = nil)
    t = @ary.detect {|elem| elem.msgid == msgid && elem.msgctxt == msgctxt}
    if t
      if references
        assert_equal references.sort, t.references.sort, 'Translation target references do not match.'
      end
      yield t if block_given?
    else
      flunk "Expected a translation target with id '#{msgid}' and context '#{msgctxt}'. Not found."
    end
  end
end