summaryrefslogtreecommitdiff
path: root/test/rdoc/test_rdoc_i18n_text.rb
blob: 4169b92079b2493a233bcee2e38a5866c9c0b525 (plain)
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
# frozen_string_literal: false
require 'rdoc/test_case'

class TestRDocI18nText < RDoc::TestCase

  def test_multiple_paragraphs
    paragraph1 = <<-PARAGRAPH.strip
RDoc produces HTML and command-line documentation for Ruby projects.  RDoc
includes the +rdoc+ and +ri+ tools for generating and displaying documentation
from the command-line.
    PARAGRAPH

    paragraph2 = <<-PARAGRAPH.strip
This command generates documentation for all the Ruby and C source
files in and below the current directory.  These will be stored in a
documentation tree starting in the subdirectory +doc+.
    PARAGRAPH

    raw = <<-RAW
#{paragraph1}

#{paragraph2}
    RAW

    expected = [
      {
        :type      => :paragraph,
        :paragraph => paragraph1,
        :line_no   => 1,
      },
      {
        :type      => :paragraph,
        :paragraph => paragraph2,
        :line_no   => 5,
      },
    ]
    assert_equal expected, extract_messages(raw)
  end

  def test_translate_multiple_paragraphs
    paragraph1 = <<-PARAGRAPH.strip
Paragraph 1.
    PARAGRAPH
    paragraph2 = <<-PARAGRAPH.strip
Paragraph 2.
    PARAGRAPH

    raw = <<-RAW
#{paragraph1}

#{paragraph2}
    RAW

    expected = <<-TRANSLATED
Paragraphe 1.

Paragraphe 2.
    TRANSLATED
    assert_equal expected, translate(raw)
  end

  def test_translate_not_translated_message
    nonexistent_paragraph = <<-PARAGRAPH.strip
Nonexistent paragraph.
    PARAGRAPH

    raw = <<-RAW
#{nonexistent_paragraph}
    RAW

    expected = <<-TRANSLATED
#{nonexistent_paragraph}
    TRANSLATED
    assert_equal expected, translate(raw)
  end

  def test_translate_keep_empty_lines
    raw = <<-RAW
Paragraph 1.




Paragraph 2.
    RAW

    expected = <<-TRANSLATED
Paragraphe 1.




Paragraphe 2.
    TRANSLATED
    assert_equal expected, translate(raw)
  end

  private

  def extract_messages(raw)
    text = RDoc::I18n::Text.new(raw)
    messages = []
    text.extract_messages do |message|
      messages << message
    end
    messages
  end

  def locale
    locale = RDoc::I18n::Locale.new('fr')
    messages = locale.instance_variable_get(:@messages)
    messages['markdown'] = 'markdown (markdown in fr)'
    messages['Hello'] = 'Bonjour (Hello in fr)'
    messages['Paragraph 1.'] = 'Paragraphe 1.'
    messages['Paragraph 2.'] = 'Paragraphe 2.'
    locale
  end

  def translate(raw)
    text = RDoc::I18n::Text.new(raw)
    text.translate(locale)
  end

end