2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-27 07:40:24 -05:00
|
|
|
require "abstract_unit"
|
2005-04-12 04:04:38 -04:00
|
|
|
|
2008-04-19 14:06:57 -04:00
|
|
|
class NumberHelperTest < ActionView::TestCase
|
|
|
|
tests ActionView::Helpers::NumberHelper
|
2005-04-13 00:49:01 -04:00
|
|
|
|
|
|
|
def test_number_to_phone
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil number_to_phone(nil)
|
2013-02-27 07:40:24 -05:00
|
|
|
assert_equal "555-1234", number_to_phone(5551234)
|
|
|
|
assert_equal "(800) 555-1212 x 123", number_to_phone(8005551212, area_code: true, extension: 123)
|
|
|
|
assert_equal "+18005551212", number_to_phone(8005551212, country_code: 1, delimiter: "")
|
2014-02-11 20:36:10 -05:00
|
|
|
assert_equal "+<script></script>8005551212", number_to_phone(8005551212, country_code: "<script></script>", delimiter: "")
|
|
|
|
assert_equal "8005551212 x <script></script>", number_to_phone(8005551212, extension: "<script></script>", delimiter: "")
|
2005-04-13 00:49:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_number_to_currency
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil number_to_currency(nil)
|
2013-02-27 07:40:24 -05:00
|
|
|
assert_equal "$1,234,567,890.50", number_to_currency(1234567890.50)
|
|
|
|
assert_equal "$1,234,567,892", number_to_currency(1234567891.50, precision: 0)
|
2013-12-01 16:12:47 -05:00
|
|
|
assert_equal "1,234,567,890.50 - Kč", number_to_currency("-1234567890.50", unit: raw("Kč"), format: "%n %u", negative_format: "%n - %u")
|
|
|
|
assert_equal "&pound;1,234,567,890.50", number_to_currency("1234567890.50", unit: "£")
|
2014-02-11 20:36:10 -05:00
|
|
|
assert_equal "<b>1,234,567,890.50</b> $", number_to_currency("1234567890.50", format: "<b>%n</b> %u")
|
|
|
|
assert_equal "<b>1,234,567,890.50</b> $", number_to_currency("-1234567890.50", negative_format: "<b>%n</b> %u")
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "<b>1,234,567,890.50</b> $", number_to_currency("-1234567890.50", "negative_format" => "<b>%n</b> %u")
|
|
|
|
assert_equal "₹ 12,30,000.00", number_to_currency(1230000, delimiter_pattern: /(\d+?)(?=(\d\d)+(\d)(?!\d))/, unit: "₹", format: "%u %n")
|
2005-04-13 00:49:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_number_to_percentage
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil number_to_percentage(nil)
|
2013-02-27 07:40:24 -05:00
|
|
|
assert_equal "100.000%", number_to_percentage(100)
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "100.000 %", number_to_percentage(100, format: "%n %")
|
|
|
|
assert_equal "<b>100.000</b> %", number_to_percentage(100, format: "<b>%n</b> %")
|
|
|
|
assert_equal "<b>100.000</b> %", number_to_percentage(100, format: raw("<b>%n</b> %"))
|
2013-02-27 07:40:24 -05:00
|
|
|
assert_equal "100%", number_to_percentage(100, precision: 0)
|
|
|
|
assert_equal "123.4%", number_to_percentage(123.400, precision: 3, strip_insignificant_zeros: true)
|
|
|
|
assert_equal "1.000,000%", number_to_percentage(1000, delimiter: ".", separator: ",")
|
2014-03-17 05:55:21 -04:00
|
|
|
assert_equal "98a%", number_to_percentage("98a")
|
|
|
|
assert_equal "NaN%", number_to_percentage(Float::NAN)
|
|
|
|
assert_equal "Inf%", number_to_percentage(Float::INFINITY)
|
2015-03-06 03:04:51 -05:00
|
|
|
assert_equal "NaN%", number_to_percentage(Float::NAN, precision: 0)
|
|
|
|
assert_equal "Inf%", number_to_percentage(Float::INFINITY, precision: 0)
|
|
|
|
assert_equal "NaN%", number_to_percentage(Float::NAN, precision: 1)
|
|
|
|
assert_equal "Inf%", number_to_percentage(Float::INFINITY, precision: 1)
|
2005-04-13 00:49:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_number_with_delimiter
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil number_with_delimiter(nil)
|
2013-02-27 07:40:24 -05:00
|
|
|
assert_equal "12,345,678", number_with_delimiter(12345678)
|
|
|
|
assert_equal "0", number_with_delimiter(0)
|
2008-07-21 14:05:27 -04:00
|
|
|
end
|
|
|
|
|
2005-04-13 00:49:01 -04:00
|
|
|
def test_number_with_precision
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil number_with_precision(nil)
|
2013-02-27 07:40:24 -05:00
|
|
|
assert_equal "-111.235", number_with_precision(-111.2346)
|
|
|
|
assert_equal "111.00", number_with_precision(111, precision: 2)
|
|
|
|
assert_equal "0.00100", number_with_precision(0.001, precision: 5)
|
2014-05-28 06:40:42 -04:00
|
|
|
assert_equal "3.33", number_with_precision(Rational(10, 3), precision: 2)
|
2010-03-20 16:37:38 -04:00
|
|
|
end
|
|
|
|
|
2006-10-22 19:47:18 -04:00
|
|
|
def test_number_to_human_size
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil number_to_human_size(nil)
|
2013-02-27 07:40:24 -05:00
|
|
|
assert_equal "3 Bytes", number_to_human_size(3.14159265)
|
|
|
|
assert_equal "1.2 MB", number_to_human_size(1234567, precision: 2)
|
2010-03-20 16:37:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_number_to_human
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil number_to_human(nil)
|
2013-02-27 07:40:24 -05:00
|
|
|
assert_equal "0", number_to_human(0)
|
|
|
|
assert_equal "1.23 Thousand", number_to_human(1234)
|
|
|
|
assert_equal "489.0 Thousand", number_to_human(489000, precision: 4, strip_insignificant_zeros: false)
|
2010-03-22 15:19:30 -04:00
|
|
|
end
|
|
|
|
|
2014-02-11 20:36:10 -05:00
|
|
|
def test_number_to_human_escape_units
|
|
|
|
volume = { unit: "<b>ml</b>", thousand: "<b>lt</b>", million: "<b>m3</b>", trillion: "<b>km3</b>", quadrillion: "<b>Pl</b>" }
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "123 <b>lt</b>", number_to_human(123456, units: volume)
|
|
|
|
assert_equal "12 <b>ml</b>", number_to_human(12, units: volume)
|
|
|
|
assert_equal "1.23 <b>m3</b>", number_to_human(1234567, units: volume)
|
|
|
|
assert_equal "1.23 <b>km3</b>", number_to_human(1_234_567_000_000, units: volume)
|
|
|
|
assert_equal "1.23 <b>Pl</b>", number_to_human(1_234_567_000_000_000, units: volume)
|
2014-02-11 20:36:10 -05:00
|
|
|
|
2017-12-14 03:30:54 -05:00
|
|
|
# Including fractionals
|
2014-02-11 20:36:10 -05:00
|
|
|
distance = { mili: "<b>mm</b>", centi: "<b>cm</b>", deci: "<b>dm</b>", unit: "<b>m</b>",
|
|
|
|
ten: "<b>dam</b>", hundred: "<b>hm</b>", thousand: "<b>km</b>",
|
2016-08-16 03:30:11 -04:00
|
|
|
micro: "<b>um</b>", nano: "<b>nm</b>", pico: "<b>pm</b>", femto: "<b>fm</b>" }
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "1.23 <b>mm</b>", number_to_human(0.00123, units: distance)
|
|
|
|
assert_equal "1.23 <b>cm</b>", number_to_human(0.0123, units: distance)
|
|
|
|
assert_equal "1.23 <b>dm</b>", number_to_human(0.123, units: distance)
|
|
|
|
assert_equal "1.23 <b>m</b>", number_to_human(1.23, units: distance)
|
|
|
|
assert_equal "1.23 <b>dam</b>", number_to_human(12.3, units: distance)
|
|
|
|
assert_equal "1.23 <b>hm</b>", number_to_human(123, units: distance)
|
|
|
|
assert_equal "1.23 <b>km</b>", number_to_human(1230, units: distance)
|
|
|
|
assert_equal "1.23 <b>um</b>", number_to_human(0.00000123, units: distance)
|
|
|
|
assert_equal "1.23 <b>nm</b>", number_to_human(0.00000000123, units: distance)
|
|
|
|
assert_equal "1.23 <b>pm</b>", number_to_human(0.00000000000123, units: distance)
|
|
|
|
assert_equal "1.23 <b>fm</b>", number_to_human(0.00000000000000123, units: distance)
|
2014-02-11 20:36:10 -05:00
|
|
|
end
|
|
|
|
|
2013-02-27 04:19:17 -05:00
|
|
|
def test_number_helpers_escape_delimiter_and_separator
|
2013-02-27 07:40:24 -05:00
|
|
|
assert_equal "111<script></script>111<script></script>1111", number_to_phone(1111111111, delimiter: "<script></script>")
|
2012-01-25 07:30:32 -05:00
|
|
|
|
2013-02-27 07:40:24 -05:00
|
|
|
assert_equal "$1<script></script>01", number_to_currency(1.01, separator: "<script></script>")
|
|
|
|
assert_equal "$1<script></script>000.00", number_to_currency(1000, delimiter: "<script></script>")
|
2012-01-25 07:30:32 -05:00
|
|
|
|
2013-02-27 07:40:24 -05:00
|
|
|
assert_equal "1<script></script>010%", number_to_percentage(1.01, separator: "<script></script>")
|
|
|
|
assert_equal "1<script></script>000.000%", number_to_percentage(1000, delimiter: "<script></script>")
|
2012-01-25 07:30:32 -05:00
|
|
|
|
2013-02-27 07:40:24 -05:00
|
|
|
assert_equal "1<script></script>01", number_with_delimiter(1.01, separator: "<script></script>")
|
|
|
|
assert_equal "1<script></script>000", number_with_delimiter(1000, delimiter: "<script></script>")
|
2012-01-25 07:30:32 -05:00
|
|
|
|
2013-02-27 07:40:24 -05:00
|
|
|
assert_equal "1<script></script>010", number_with_precision(1.01, separator: "<script></script>")
|
|
|
|
assert_equal "1<script></script>000.000", number_with_precision(1000, delimiter: "<script></script>")
|
2012-01-25 07:30:32 -05:00
|
|
|
|
2013-02-27 07:40:24 -05:00
|
|
|
assert_equal "9<script></script>86 KB", number_to_human_size(10100, separator: "<script></script>")
|
2012-01-25 07:30:32 -05:00
|
|
|
|
2013-02-27 07:40:24 -05:00
|
|
|
assert_equal "1<script></script>01", number_to_human(1.01, separator: "<script></script>")
|
|
|
|
assert_equal "100<script></script>000 Quadrillion", number_to_human(10**20, delimiter: "<script></script>")
|
2010-03-22 15:19:30 -04:00
|
|
|
end
|
|
|
|
|
2014-02-11 20:36:10 -05:00
|
|
|
def test_number_to_human_with_custom_translation_scope
|
2016-08-06 12:50:17 -04:00
|
|
|
I18n.backend.store_translations "ts",
|
2016-08-16 03:30:11 -04:00
|
|
|
custom_units_for_number_to_human: { mili: "mm", centi: "cm", deci: "dm", unit: "m", ten: "dam", hundred: "hm", thousand: "km" }
|
2016-08-06 13:36:34 -04:00
|
|
|
assert_equal "1.01 cm", number_to_human(0.0101, locale: "ts", units: :custom_units_for_number_to_human)
|
2014-06-14 15:41:13 -04:00
|
|
|
ensure
|
|
|
|
I18n.reload!
|
2014-02-11 20:36:10 -05:00
|
|
|
end
|
|
|
|
|
2010-03-22 15:19:30 -04:00
|
|
|
def test_number_helpers_outputs_are_html_safe
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate number_to_human(1), :html_safe?
|
|
|
|
assert_not_predicate number_to_human("<script></script>"), :html_safe?
|
|
|
|
assert_predicate number_to_human("asdf".html_safe), :html_safe?
|
|
|
|
assert_predicate number_to_human("1".html_safe), :html_safe?
|
|
|
|
|
|
|
|
assert_predicate number_to_human_size(1), :html_safe?
|
|
|
|
assert_predicate number_to_human_size(1000000), :html_safe?
|
|
|
|
assert_not_predicate number_to_human_size("<script></script>"), :html_safe?
|
|
|
|
assert_predicate number_to_human_size("asdf".html_safe), :html_safe?
|
|
|
|
assert_predicate number_to_human_size("1".html_safe), :html_safe?
|
|
|
|
|
|
|
|
assert_predicate number_with_precision(1, strip_insignificant_zeros: false), :html_safe?
|
|
|
|
assert_predicate number_with_precision(1, strip_insignificant_zeros: true), :html_safe?
|
|
|
|
assert_not_predicate number_with_precision("<script></script>"), :html_safe?
|
|
|
|
assert_predicate number_with_precision("asdf".html_safe), :html_safe?
|
|
|
|
assert_predicate number_with_precision("1".html_safe), :html_safe?
|
|
|
|
|
|
|
|
assert_predicate number_to_currency(1), :html_safe?
|
|
|
|
assert_not_predicate number_to_currency("<script></script>"), :html_safe?
|
|
|
|
assert_predicate number_to_currency("asdf".html_safe), :html_safe?
|
|
|
|
assert_predicate number_to_currency("1".html_safe), :html_safe?
|
|
|
|
|
|
|
|
assert_predicate number_to_percentage(1), :html_safe?
|
|
|
|
assert_not_predicate number_to_percentage("<script></script>"), :html_safe?
|
|
|
|
assert_predicate number_to_percentage("asdf".html_safe), :html_safe?
|
|
|
|
assert_predicate number_to_percentage("1".html_safe), :html_safe?
|
|
|
|
|
|
|
|
assert_predicate number_to_phone(1), :html_safe?
|
2010-10-17 18:42:13 -04:00
|
|
|
assert_equal "<script></script>", number_to_phone("<script></script>")
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate number_to_phone("<script></script>"), :html_safe?
|
|
|
|
assert_predicate number_to_phone("asdf".html_safe), :html_safe?
|
|
|
|
assert_predicate number_to_phone("1".html_safe), :html_safe?
|
|
|
|
|
|
|
|
assert_predicate number_with_delimiter(1), :html_safe?
|
|
|
|
assert_not_predicate number_with_delimiter("<script></script>"), :html_safe?
|
|
|
|
assert_predicate number_with_delimiter("asdf".html_safe), :html_safe?
|
|
|
|
assert_predicate number_with_delimiter("1".html_safe), :html_safe?
|
2010-03-22 15:19:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_number_helpers_should_raise_error_if_invalid_when_specified
|
2012-06-23 17:55:46 -04:00
|
|
|
exception = assert_raise InvalidNumberError do
|
2013-02-27 07:40:24 -05:00
|
|
|
number_to_human("x", raise: true)
|
2010-03-22 15:19:30 -04:00
|
|
|
end
|
2012-06-23 17:55:46 -04:00
|
|
|
assert_equal "x", exception.number
|
2010-03-22 15:19:30 -04:00
|
|
|
|
2012-06-23 17:55:46 -04:00
|
|
|
exception = assert_raise InvalidNumberError do
|
2013-02-27 07:40:24 -05:00
|
|
|
number_to_human_size("x", raise: true)
|
2010-03-22 15:19:30 -04:00
|
|
|
end
|
2012-06-23 17:55:46 -04:00
|
|
|
assert_equal "x", exception.number
|
2010-03-22 15:19:30 -04:00
|
|
|
|
2012-06-23 17:55:46 -04:00
|
|
|
exception = assert_raise InvalidNumberError do
|
2013-02-27 07:40:24 -05:00
|
|
|
number_with_precision("x", raise: true)
|
2010-03-22 15:19:30 -04:00
|
|
|
end
|
2012-06-23 17:55:46 -04:00
|
|
|
assert_equal "x", exception.number
|
2010-03-22 15:19:30 -04:00
|
|
|
|
2012-06-23 17:55:46 -04:00
|
|
|
exception = assert_raise InvalidNumberError do
|
2013-02-27 07:40:24 -05:00
|
|
|
number_to_currency("x", raise: true)
|
2010-03-22 15:19:30 -04:00
|
|
|
end
|
2012-06-23 17:55:46 -04:00
|
|
|
assert_equal "x", exception.number
|
2010-03-22 15:19:30 -04:00
|
|
|
|
2012-06-23 17:55:46 -04:00
|
|
|
exception = assert_raise InvalidNumberError do
|
2013-02-27 07:40:24 -05:00
|
|
|
number_to_percentage("x", raise: true)
|
2010-03-22 15:19:30 -04:00
|
|
|
end
|
2012-06-23 17:55:46 -04:00
|
|
|
assert_equal "x", exception.number
|
2010-03-22 15:19:30 -04:00
|
|
|
|
2012-06-23 17:55:46 -04:00
|
|
|
exception = assert_raise InvalidNumberError do
|
2013-02-27 07:40:24 -05:00
|
|
|
number_with_delimiter("x", raise: true)
|
2010-03-22 15:19:30 -04:00
|
|
|
end
|
2012-06-23 17:55:46 -04:00
|
|
|
assert_equal "x", exception.number
|
2010-03-22 15:19:30 -04:00
|
|
|
|
2012-06-23 17:55:46 -04:00
|
|
|
exception = assert_raise InvalidNumberError do
|
2013-02-27 07:40:24 -05:00
|
|
|
number_to_phone("x", raise: true)
|
2010-03-22 15:19:30 -04:00
|
|
|
end
|
2012-06-23 17:55:46 -04:00
|
|
|
assert_equal "x", exception.number
|
2010-03-22 15:19:30 -04:00
|
|
|
end
|
2005-04-12 04:04:38 -04:00
|
|
|
end
|