Skip to content

Commit

Permalink
Add/Fix tests for boolean values.
Browse files Browse the repository at this point in the history
  • Loading branch information
elDub committed Oct 6, 2012
1 parent 0b354dc commit 095d435
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 20 deletions.
13 changes: 8 additions & 5 deletions app/models/tolk/translation.rb
Expand Up @@ -29,7 +29,7 @@ class Translation < ActiveRecord::Base
before_validation :set_explicit_nil

def boolean?
text.is_a?(TrueClass) || text.is_a?(FalseClass)
text.is_a?(TrueClass) || text.is_a?(FalseClass) || text == 't' || text == 'f'
end

def up_to_date?
Expand All @@ -50,16 +50,17 @@ def primary_translation

def text=(value)
value = value.to_s if value.kind_of?(Fixnum)
if primary_translation.boolean?
if primary_translation && primary_translation.boolean?
value = case value.to_s.downcase.strip
when 'true'
when 'true', 't'
true
when 'false'
when 'false', 'f'
false
else
self.explicit_nil = true
nil
end
super(value) unless value == text
super unless value == text
else
super unless value.to_s == text
end
Expand All @@ -68,6 +69,8 @@ def text=(value)
def value
if text.is_a?(String) && /^\d+$/.match(text)
text.to_i
elsif (primary_translation || self).boolean?
%w[true t].member?(text.to_s.downcase.strip)
else
text
end
Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/tolk_phrases.yml
Expand Up @@ -16,4 +16,8 @@ cozy:

human_format_precision:
id: 5
key: number.human.format.precision
key: number.human.format.precision

number_currency_format_significant:
id: 6
key: number.currency.format.significant
24 changes: 18 additions & 6 deletions test/fixtures/tolk_translations.yml
@@ -1,24 +1,24 @@
hello_world_en:
id: 1
phrase_id: 1
phrase_id: 1
locale_id: 1
text: Hello World

hello_world_da:
id: 2
phrase_id: 1
phrase_id: 1
locale_id: 2
text: Hej Verden

hello_world_se:
id: 3
phrase_id: 1
phrase_id: 1
locale_id: 3
text: Hejsan Verdon

nested_hello_world_en:
id: 4
phrase_id: 2
phrase_id: 2
locale_id: 1
text: Nested Hello World

Expand All @@ -30,7 +30,7 @@ nested_hello_country_en:

nested_hello_world_da:
id: 6
phrase_id: 2
phrase_id: 2
locale_id: 2
text: Nedarvet Hej Verden

Expand All @@ -39,9 +39,21 @@ cozy_da:
phrase_id: 4
locale_id: 2
text: Hyggeligt

human_format_precision_en:
id: 8
phrase_id: 5
locale_id: 1
text: "1"

number_currency_format_significant_en:
id: 9
phrase_id: 6
locale_id: 1
text: false

number_currency_format_significant_da:
id: 10
phrase_id: 6
locale_id: 2
text: true
4 changes: 4 additions & 0 deletions test/locales/basic/da.yml
Expand Up @@ -4,3 +4,7 @@ da:
hello_world: "Hej Verden"
nested:
hello_world: "Nedarvet Hej Verden"
number:
currency:
format:
significant: true
19 changes: 11 additions & 8 deletions test/unit/locale_test.rb
Expand Up @@ -9,12 +9,12 @@ class LocaleTest < ActiveSupport::TestCase

test "turning locale with nested phrases into a hash" do
assert_equal({ "en" => {
"number"=>{"human"=>{"format"=>{"precision"=>1}}},
"hello_world" => "Hello World",
"nested" => {
"hello_world" => "Nested Hello World",
"hello_country" => "Nested Hello Country"
}
},
"number"=>{"human"=>{"format"=>{"precision"=>1}},"currency"=>{"format"=>{"significant"=>false}}}
}}, tolk_locales(:en).to_hash)
end

Expand All @@ -34,26 +34,29 @@ class LocaleTest < ActiveSupport::TestCase
assert_equal [4, 3], page1.map(&:id)

page2 = locale.phrases_without_translation(2)
assert_equal [2, 5], page2.map(&:id)
assert_equal [2, 6], page2.map(&:id)

page3 = locale.phrases_without_translation(3)
assert page3.blank?
assert_equal [5], page3.map(&:id)

page4 = locale.phrases_without_translation(4)
assert page4.blank?
end

test "paginating phrases with translations" do
Tolk::Phrase.per_page = 4
Tolk::Phrase.per_page = 5
locale = tolk_locales(:en)

page1 = locale.phrases_with_translation
assert_equal [1, 3, 2, 5], page1.map(&:id)
assert_equal [1, 3, 2, 6, 5], page1.map(&:id)

page2 = locale.phrases_with_translation(2)
assert page2.blank?
end

test "counting missing translations" do
assert_equal 2, tolk_locales(:da).count_phrases_without_translation
assert_equal 4, tolk_locales(:se).count_phrases_without_translation
assert_equal 5, tolk_locales(:se).count_phrases_without_translation
end

test "dumping all locales to yml" do
Expand Down Expand Up @@ -81,5 +84,5 @@ class LocaleTest < ActiveSupport::TestCase
assert_equal 'English', tolk_locales(:en).language_name
assert_equal 'pirate', Tolk::Locale.new(:name => 'pirate').language_name
end

end
8 changes: 8 additions & 0 deletions test/unit/translation_test.rb
Expand Up @@ -37,6 +37,14 @@ def setup
assert_equal(1, tolk_translations(:human_format_precision_en).value)
end

test "translation with true value" do
assert_equal(true, tolk_translations(:number_currency_format_significant_da).value)
end

test "translation with false value" do
assert_equal(false, tolk_translations(:number_currency_format_significant_en).value)
end

test "translation with hash value" do
hash = {:foo => "bar"}
assert_equal(hash, Tolk::Translation.new(:text => hash).value)
Expand Down

0 comments on commit 095d435

Please sign in to comment.