Module: Localite::NodeFilter::Etest

Defined in:
lib/localite/node_filter.rb

Instance Method Summary collapse

Instance Method Details

#assert_filtered(locale, src, expected) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/localite/node_filter.rb', line 82

def assert_filtered(locale, src, expected)
  filtered = Localite::NodeFilter.filter_body(src, locale)
  filtered = normalize_xml(filtered)

  expected = normalize_xml(expected)

  if expected == filtered
    assert true
    return
  end

  puts "expected:\n\n" + expected + "\n\n"
  puts "filtered:\n\n" + filtered + "\n\n"
  
  assert_equal expected, filtered
end

#normalize_xml(str) ⇒ Object



75
76
77
78
79
80
# File 'lib/localite/node_filter.rb', line 75

def normalize_xml(str)
  str.
    gsub(/>(.*?)</m) do |s| ">#{$1.gsub(/\s+/m, " ")}<" end.  # normalize spaces between tags
    gsub(/>\s*</m, ">\n<").                                   # normalize spaces in empty text nodes
    gsub(/"/, "'")                                            # normalize ' and "
end

#test_fbml_tagsObject



121
122
123
124
125
126
127
128
129
# File 'lib/localite/node_filter.rb', line 121

def test_fbml_tags
  assert_filtered :de, "<fb:p lang='de'>ppp</fb:p>", "<fb:p lang='de'>ppp</fb:p>"
  assert_filtered :en, "<fb:p lang='de'>ppp</fb:p>", ""
  assert_filtered :fr, "<fb:p lang='de'>ppp</fb:p>", ""

  assert_filtered :de, "<fb:p lang='en'>ppp</fb:p>", ""
  assert_filtered :en, "<fb:p lang='en'>ppp</fb:p>", "<fb:p lang='en'>ppp</fb:p>"
  assert_filtered :fr, "<fb:p lang='en'>ppp</fb:p>", ""
end

#test_full_html_matchObject



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
# File 'lib/localite/node_filter.rb', line 139

def test_full_html_match
  src = <<-HTML
<!--Force IE6 into quirks mode with this comment tag-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>socially.io</title>
</head>
<body x="y">
  <p lang='de'>&Auml; &Ouml;</p>", "<p lang='de'>&Auml; &Ouml;</p>
</body>
</html>
HTML

  expected = <<-HTML
<!--Force IE6 into quirks mode with this comment tag-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>socially.io</title>
</head>
<body x="y">
  <p lang='de'>&Auml; &Ouml;</p>", "<p lang='de'>&Auml; &Ouml;</p>
</body>
</html>
HTML

  assert_filtered :de, src, expected
end

#test_full_html_missObject



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
# File 'lib/localite/node_filter.rb', line 169

def test_full_html_miss
  src = <<-HTML
<!--Force IE6 into quirks mode with this comment tag-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>socially.io</title>
</head>
<body x="y">
  <p lang='de'>&Auml; &Ouml;</p>
</body>
</html>
HTML

  expected = <<-HTML
<!--Force IE6 into quirks mode with this comment tag-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>socially.io</title>
</head>
<body x="y">
</body>
</html>
HTML

  assert_filtered :en, src, expected
end

#test_normalize_xmlObject



100
101
102
103
# File 'lib/localite/node_filter.rb', line 100

def test_normalize_xml
  assert_equal "<p>ppp</p>", normalize_xml("<p>ppp</p>")
  assert_equal "<p>a b c </p>\n", normalize_xml("<p>a   b c </p>\n")
end

#test_simple_htmlObject



105
106
107
108
109
# File 'lib/localite/node_filter.rb', line 105

def test_simple_html
  assert_filtered :de, "<p>ppp</p>", "<p>ppp</p>"
  assert_filtered :en, "<p>ppp</p>", "<p>ppp</p>"
  assert_filtered :fr, "<p>ppp</p>", "<p>ppp</p>"
end

#test_simple_html_w_langObject



111
112
113
114
115
116
117
118
119
# File 'lib/localite/node_filter.rb', line 111

def test_simple_html_w_lang
  assert_filtered :de, "<p lang='de'>ppp</p>", "<p lang='de'>ppp</p>"
  assert_filtered :en, "<p lang='de'>ppp</p>", ""
  assert_filtered :fr, "<p lang='de'>ppp</p>", ""

  assert_filtered :de, "<p lang='en'>ppp</p>", ""
  assert_filtered :en, "<p lang='en'>ppp</p>", "<p lang='en'>ppp</p>"
  assert_filtered :fr, "<p lang='en'>ppp</p>", ""
end

#test_umlautsObject



131
132
133
134
135
136
137
# File 'lib/localite/node_filter.rb', line 131

def test_umlauts
  assert_filtered :de, "<p lang='de'>&Auml; &Ouml;</p>", "<p lang='de'>&Auml; &Ouml;</p>"

  #
  # Nokogiri translates UTF-8 umlauts into entities. That is ok (for now)
  assert_filtered :de, "<p lang='de'>Ä Ö</p>", "<p lang='de'>&Auml; &Ouml;</p>"
end