2017-07-09 20:06:36 +08:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 21:39:13 +08:00
|
|
|
|
2018-09-30 08:50:43 +08:00
|
|
|
require_relative "abstract_unit"
|
2016-08-07 00:03:25 +08:00
|
|
|
require "active_support/number_helper"
|
2017-01-29 07:40:34 +08:00
|
|
|
require "active_support/core_ext/hash/keys"
|
2012-06-24 06:36:00 +08:00
|
|
|
|
|
|
|
module ActiveSupport
|
|
|
|
class NumberHelperI18nTest < ActiveSupport::TestCase
|
|
|
|
include ActiveSupport::NumberHelper
|
|
|
|
|
|
|
|
def setup
|
2016-08-07 00:03:25 +08:00
|
|
|
I18n.backend.store_translations "ts",
|
2016-08-07 01:38:33 +08:00
|
|
|
number: {
|
2020-01-03 20:44:14 +08:00
|
|
|
format: { precision: 3, round_mode: :half_even, delimiter: ",", separator: ".", significant: false, strip_insignificant_zeros: false },
|
2016-08-07 01:38:33 +08:00
|
|
|
currency: { format: { unit: "&$", format: "%u - %n", negative_format: "(%u - %n)", precision: 2 } },
|
|
|
|
human: {
|
|
|
|
format: {
|
|
|
|
precision: 2,
|
|
|
|
significant: true,
|
|
|
|
strip_insignificant_zeros: true
|
2012-06-24 06:36:00 +08:00
|
|
|
},
|
2016-08-07 01:38:33 +08:00
|
|
|
storage_units: {
|
|
|
|
format: "%n %u",
|
|
|
|
units: {
|
|
|
|
byte: "b",
|
|
|
|
kb: "k"
|
2012-06-24 06:36:00 +08:00
|
|
|
}
|
|
|
|
},
|
2016-08-07 01:38:33 +08:00
|
|
|
decimal_units: {
|
|
|
|
format: "%n %u",
|
|
|
|
units: {
|
2016-08-16 15:30:11 +08:00
|
|
|
deci: { one: "Tenth", other: "Tenths" },
|
2016-08-07 01:38:33 +08:00
|
|
|
unit: "u",
|
2016-08-16 15:30:11 +08:00
|
|
|
ten: { one: "Ten", other: "Tens" },
|
2016-08-07 01:38:33 +08:00
|
|
|
thousand: "t",
|
|
|
|
million: "m",
|
|
|
|
billion: "b",
|
2017-12-12 19:00:50 +08:00
|
|
|
trillion: "t",
|
2016-08-07 01:38:33 +08:00
|
|
|
quadrillion: "q"
|
2012-06-24 06:36:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2016-08-16 15:30:11 +08:00
|
|
|
percentage: { format: { delimiter: "", precision: 2, strip_insignificant_zeros: true } },
|
|
|
|
precision: { format: { delimiter: "", significant: true } }
|
2012-06-24 06:36:00 +08:00
|
|
|
},
|
2016-08-16 15:30:11 +08:00
|
|
|
custom_units_for_number_to_human: { mili: "mm", centi: "cm", deci: "dm", unit: "m", ten: "dam", hundred: "hm", thousand: "km" }
|
2012-06-24 06:36:00 +08:00
|
|
|
end
|
|
|
|
|
2014-06-15 04:13:15 +08:00
|
|
|
def teardown
|
|
|
|
I18n.backend.reload!
|
|
|
|
end
|
|
|
|
|
2012-06-24 06:36:00 +08:00
|
|
|
def test_number_to_i18n_currency
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal("&$ - 10.00", number_to_currency(10, locale: "ts"))
|
|
|
|
assert_equal("(&$ - 10.00)", number_to_currency(-10, locale: "ts"))
|
|
|
|
assert_equal("-10.00 - &$", number_to_currency(-10, locale: "ts", format: "%n - %u"))
|
2012-06-24 06:36:00 +08:00
|
|
|
end
|
|
|
|
|
2012-06-24 06:39:59 +08:00
|
|
|
def test_number_to_currency_with_empty_i18n_store
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal("$10.00", number_to_currency(10, locale: "empty"))
|
|
|
|
assert_equal("-$10.00", number_to_currency(-10, locale: "empty"))
|
2012-06-24 06:36:00 +08:00
|
|
|
end
|
|
|
|
|
2012-06-25 07:02:52 +08:00
|
|
|
def test_locale_default_format_has_precedence_over_helper_defaults
|
2016-08-07 00:03:25 +08:00
|
|
|
I18n.backend.store_translations "ts",
|
2016-08-07 01:44:11 +08:00
|
|
|
number: { format: { separator: ";" } }
|
2012-06-25 07:02:52 +08:00
|
|
|
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal("&$ - 10;00", number_to_currency(10, locale: "ts"))
|
2012-06-25 07:02:52 +08:00
|
|
|
end
|
|
|
|
|
2012-06-24 06:36:00 +08:00
|
|
|
def test_number_to_currency_without_currency_negative_format
|
2016-08-07 01:38:33 +08:00
|
|
|
I18n.backend.store_translations "no_negative_format", number: {
|
|
|
|
currency: { format: { unit: "@", format: "%n %u" } }
|
2012-06-24 06:39:59 +08:00
|
|
|
}
|
|
|
|
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal("-10.00 @", number_to_currency(-10, locale: "no_negative_format"))
|
2012-06-24 06:36:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_number_with_i18n_precision
|
2017-12-14 16:30:54 +08:00
|
|
|
# Delimiter was set to ""
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal("10000", number_to_rounded(10000, locale: "ts"))
|
2012-06-24 06:36:00 +08:00
|
|
|
|
2017-12-14 16:30:54 +08:00
|
|
|
# Precision inherited and significant was set
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal("1.00", number_to_rounded(1.0, locale: "ts"))
|
2012-06-24 06:36:00 +08:00
|
|
|
end
|
|
|
|
|
2020-01-03 20:44:14 +08:00
|
|
|
def test_number_with_i18n_round_mode
|
|
|
|
# round_mode set as :half_even instead of :default
|
|
|
|
assert_equal("12344", number_to_rounded(12344.5, locale: "ts", precision: 0))
|
|
|
|
end
|
|
|
|
|
2012-06-24 07:05:42 +08:00
|
|
|
def test_number_with_i18n_precision_and_empty_i18n_store
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal("123456789.123", number_to_rounded(123456789.123456789, locale: "empty"))
|
|
|
|
assert_equal("1.000", number_to_rounded(1.0000, locale: "empty"))
|
2012-06-24 07:05:42 +08:00
|
|
|
end
|
|
|
|
|
2012-06-24 06:36:00 +08:00
|
|
|
def test_number_with_i18n_delimiter
|
2017-12-14 16:30:54 +08:00
|
|
|
# Delimiter "," and separator "."
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal("1,000,000.234", number_to_delimited(1000000.234, locale: "ts"))
|
2012-06-24 06:36:00 +08:00
|
|
|
end
|
|
|
|
|
2012-06-24 07:05:42 +08:00
|
|
|
def test_number_with_i18n_delimiter_and_empty_i18n_store
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal("1,000,000.234", number_to_delimited(1000000.234, locale: "empty"))
|
2012-06-24 07:05:42 +08:00
|
|
|
end
|
|
|
|
|
2012-06-24 06:36:00 +08:00
|
|
|
def test_number_to_i18n_percentage
|
|
|
|
# to see if strip_insignificant_zeros is true
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal("1%", number_to_percentage(1, locale: "ts"))
|
2012-06-24 06:36:00 +08:00
|
|
|
# precision is 2, significant should be inherited
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal("1.24%", number_to_percentage(1.2434, locale: "ts"))
|
2012-06-24 06:36:00 +08:00
|
|
|
# no delimiter
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal("12434%", number_to_percentage(12434, locale: "ts"))
|
2012-06-24 06:36:00 +08:00
|
|
|
end
|
|
|
|
|
2012-06-24 07:05:42 +08:00
|
|
|
def test_number_to_i18n_percentage_and_empty_i18n_store
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal("1.000%", number_to_percentage(1, locale: "empty"))
|
|
|
|
assert_equal("1.243%", number_to_percentage(1.2434, locale: "empty"))
|
|
|
|
assert_equal("12434.000%", number_to_percentage(12434, locale: "empty"))
|
2012-06-24 07:05:42 +08:00
|
|
|
end
|
|
|
|
|
2012-06-24 06:36:00 +08:00
|
|
|
def test_number_to_i18n_human_size
|
2017-12-14 16:30:54 +08:00
|
|
|
# b for bytes and k for kbytes
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal("2 k", number_to_human_size(2048, locale: "ts"))
|
|
|
|
assert_equal("42 b", number_to_human_size(42, locale: "ts"))
|
2012-06-24 06:36:00 +08:00
|
|
|
end
|
|
|
|
|
2012-06-24 07:05:42 +08:00
|
|
|
def test_number_to_i18n_human_size_with_empty_i18n_store
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal("2 KB", number_to_human_size(2048, locale: "empty"))
|
|
|
|
assert_equal("42 Bytes", number_to_human_size(42, locale: "empty"))
|
2012-06-24 07:05:42 +08:00
|
|
|
end
|
|
|
|
|
2012-06-24 06:36:00 +08:00
|
|
|
def test_number_to_human_with_default_translation_scope
|
2017-12-14 16:30:54 +08:00
|
|
|
# Using t for thousand
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal "2 t", number_to_human(2000, locale: "ts")
|
2017-12-14 16:30:54 +08:00
|
|
|
# Significant was set to true with precision 2, using b for billion
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal "1.2 b", number_to_human(1234567890, locale: "ts")
|
2017-12-14 16:30:54 +08:00
|
|
|
# Using pluralization (Ten/Tens and Tenth/Tenths)
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal "1 Tenth", number_to_human(0.1, locale: "ts")
|
|
|
|
assert_equal "1.3 Tenth", number_to_human(0.134, locale: "ts")
|
|
|
|
assert_equal "2 Tenths", number_to_human(0.2, locale: "ts")
|
|
|
|
assert_equal "1 Ten", number_to_human(10, locale: "ts")
|
|
|
|
assert_equal "1.2 Ten", number_to_human(12, locale: "ts")
|
|
|
|
assert_equal "2 Tens", number_to_human(20, locale: "ts")
|
2012-06-24 06:36:00 +08:00
|
|
|
end
|
|
|
|
|
2012-06-24 07:05:42 +08:00
|
|
|
def test_number_to_human_with_empty_i18n_store
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal "2 Thousand", number_to_human(2000, locale: "empty")
|
|
|
|
assert_equal "1.23 Billion", number_to_human(1234567890, locale: "empty")
|
2012-06-24 07:05:42 +08:00
|
|
|
end
|
|
|
|
|
2012-06-24 06:36:00 +08:00
|
|
|
def test_number_to_human_with_custom_translation_scope
|
2017-12-14 16:30:54 +08:00
|
|
|
# Significant was set to true with precision 2, with custom translated units
|
2016-08-07 01:38:33 +08:00
|
|
|
assert_equal "4.3 cm", number_to_human(0.0432, locale: "ts", units: :custom_units_for_number_to_human)
|
2012-06-24 06:36:00 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|