Permalink
Browse files

Add/Fix tests for boolean values.

  • Loading branch information...
elDub committed Oct 6, 2012
1 parent 0b354dc commit 095d4354f6f10902d6f8a8989389fbcd382a3c31
@@ -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?
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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,3 +4,7 @@ da:
hello_world: "Hej Verden"
nested:
hello_world: "Nedarvet Hej Verden"
+ number:
+ currency:
+ format:
+ significant: true
View
@@ -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
@@ -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
@@ -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
@@ -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)

0 comments on commit 095d435

Please sign in to comment.