translation_helper_test.rb 2.5 KB
Newer Older
S
Sven Fuchs 已提交
1 2
require 'abstract_unit'

J
José Valim 已提交
3
class TranslationHelperTest < ActiveSupport::TestCase
S
Sven Fuchs 已提交
4 5
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::TranslationHelper
J
José Valim 已提交
6

S
Sven Fuchs 已提交
7
  attr_reader :request
8 9
  def setup
  end
10
  
11
  def test_delegates_to_i18n_setting_the_raise_option
12
    I18n.expects(:translate).with(:foo, :locale => 'en', :raise => true).returns("")
13 14
    translate :foo, :locale => 'en'
  end
15
  
16 17 18 19 20
  def test_returns_missing_translation_message_wrapped_into_span
    expected = '<span class="translation_missing">en, foo</span>'
    assert_equal expected, translate(:foo)
  end

21 22
  def test_translation_of_an_array
    I18n.expects(:translate).with(["foo", "bar"], :raise => true).returns(["foo", "bar"])
23
    assert_equal "foobar", translate(["foo", "bar"])
24 25 26
  end

  def test_translation_of_an_array_with_html
27 28
    expected = '<a href="#">foo</a><a href="#">bar</a>'
    I18n.expects(:translate).with(["foo", "bar", "html"], :raise => true).returns(['<a href="#">foo</a>', '<a href="#">bar</a>'])
29 30
    @view = ActionView::Base.new(ActionController::Base.view_paths, {})
    assert_equal expected, @view.render(:file => "test/array_translation")
31 32
  end

33 34 35 36
  def test_delegates_localize_to_i18n
    @time = Time.utc(2008, 7, 8, 12, 18, 38)
    I18n.expects(:localize).with(@time)
    localize @time
L
Luca Guidi 已提交
37
  end
38
  
39
  def test_scoping_by_partial
40
    I18n.expects(:translate).with("test.translation.helper", :raise => true).returns("helper")
J
José Valim 已提交
41 42
    @view = ActionView::Base.new(ActionController::Base.view_paths, {})
    assert_equal "helper", @view.render(:file => "test/translation")
43
  end
44 45

  def test_scoping_by_partial_of_an_array
46
    I18n.expects(:translate).with("test.scoped_array_translation.foo.bar", :raise => true).returns(["foo", "bar"])
47
    @view = ActionView::Base.new(ActionController::Base.view_paths, {})
48
    assert_equal "foobar", @view.render(:file => "test/scoped_array_translation")
49
  end
50
  
51
  def test_translate_does_not_mark_plain_text_as_safe_html
52
    I18n.expects(:translate).with("hello", :raise => true).returns("Hello World")
53 54 55 56
    assert_equal false, translate("hello").html_safe?
  end

  def test_translate_marks_translations_named_html_as_safe_html
57
    I18n.expects(:translate).with("html", :raise => true).returns("<a>Hello World</a>")
58 59 60 61
    assert translate("html").html_safe?
  end

  def test_translate_marks_translations_with_a_html_suffix_as_safe_html
62
    I18n.expects(:translate).with("hello_html", :raise => true).returns("<a>Hello World</a>")
63 64
    assert translate("hello_html").html_safe?
  end
65
end