mirror of
https://github.com/kemko/liquid.git
synced 2026-01-02 00:05:42 +03:00
Compare commits
1 Commits
format_fil
...
master-fix
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
604d899496 |
@@ -391,17 +391,6 @@ module Liquid
|
||||
raise Liquid::FloatDomainError, e.message
|
||||
end
|
||||
|
||||
# Defaults are passed as nil so systems can easily override
|
||||
def format_number(input, options = {})
|
||||
options = {} unless options.is_a?(Hash)
|
||||
precision = options['precision'] || 2
|
||||
delimiter = options['delimiter'] || " ".freeze
|
||||
separator = options['separator'] || ".".freeze
|
||||
return input if (prec = Utils.to_number(precision).to_i) < 0
|
||||
whole_part, decimal_part = Kernel.format("%.#{prec}f", Utils.to_number(input)).split('.')
|
||||
[whole_part.gsub(/(\d)(?=\d{3}+$)/, "\\1#{delimiter}"), decimal_part].compact.join(separator.to_s)
|
||||
end
|
||||
|
||||
def ceil(input)
|
||||
Utils.to_number(input).ceil.to_i
|
||||
rescue ::FloatDomainError => e
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
module Liquid
|
||||
class Render < Tag
|
||||
Syntax = /(#{QuotedString})#{QuotedFragment}*/o
|
||||
SYNTAX = /(#{QuotedString})#{QuotedFragment}*/o
|
||||
|
||||
attr_reader :template_name_expr, :attributes
|
||||
|
||||
def initialize(tag_name, markup, options)
|
||||
super
|
||||
|
||||
raise SyntaxError.new(options[:locale].t("errors.syntax.render".freeze)) unless markup =~ Syntax
|
||||
raise SyntaxError.new(options[:locale].t("errors.syntax.render".freeze)) unless markup =~ SYNTAX
|
||||
|
||||
template_name = $1
|
||||
|
||||
|
||||
@@ -610,25 +610,6 @@ class StandardFiltersTest < Minitest::Test
|
||||
assert_template_result "4", "{{ price | round }}", 'price' => NumberLikeThing.new(4.3)
|
||||
end
|
||||
|
||||
def test_format_number
|
||||
assert_template_result "4.60", "{{ input | format_number }}", 'input' => 4.6
|
||||
assert_template_result "4.30", "{{ '4.3' | format_number }}"
|
||||
assert_template_result "4.56", "{{ input | format_number: precision: 2 }}", 'input' => 4.5612
|
||||
assert_template_result "5", "{{ price | format_number: precision: 0 }}", 'price' => NumberLikeThing.new(4.6)
|
||||
assert_template_result "4", "{{ price | format_number: precision: 0 }}", 'price' => NumberLikeThing.new(4.3)
|
||||
assert_template_result "4.30", "{{ price | format_number: precision: 2 }}", 'price' => NumberLikeThing.new(4.3)
|
||||
assert_template_result "5.0000000", "{{ price | format_number: precision: 7 }}", 'price' => 5
|
||||
assert_template_result "50", "{{ price | format_number: precision: -1 }}", 'price' => 50
|
||||
assert_template_result "50.00", "{{ price | format_number: precision: A }}", 'price' => 50
|
||||
assert_template_result "50.00", "{{ price | format_number: precision: '2e' }}", 'price' => 50
|
||||
assert_template_result "50 000 000", "{{ price | format_number: precision: 0 }}", 'price' => 50000000
|
||||
assert_template_result "50 000 000.00", "{{ price | format_number }}", 'price' => 50000000
|
||||
assert_template_result "50000000.00", "{{ price | format_number: precision: 2, delimiter: '', separator: '.'}}", 'price' => 50000000
|
||||
assert_template_result "50$000$000#00", "{{ price | format_number: precision: 2, delimiter: '$', separator:'#'}}", 'price' => 50000000
|
||||
assert_template_result "-50$000$000#00", "{{ price | format_number: precision: 2, delimiter: '$', separator: '#'}}", 'price' => -50000000
|
||||
assert_template_result "-50 000 000.00", "{{ price | format_number: precision: A, delimiter: A, separator: A}}", 'price' => -50000000
|
||||
end
|
||||
|
||||
def test_ceil
|
||||
assert_template_result "5", "{{ input | ceil }}", 'input' => 4.6
|
||||
assert_template_result "5", "{{ '4.3' | ceil }}"
|
||||
|
||||
@@ -110,7 +110,7 @@ class RenderTagTest < Minitest::Test
|
||||
file_system = StubFileSystem.new('snippet' => 'echo')
|
||||
assert_equal 'echoecho',
|
||||
Template.parse('{% render "snippet" %}{% render "snippet" %}')
|
||||
.render!({}, registers: { file_system: file_system })
|
||||
.render!({}, registers: { file_system: file_system })
|
||||
assert_equal 1, file_system.file_read_count
|
||||
end
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ class PartialCacheUnitTest < Minitest::Test
|
||||
def test_uses_the_file_system_register_if_present
|
||||
context = Liquid::Context.build(
|
||||
registers: {
|
||||
file_system: StubFileSystem.new('my_partial' => 'my partial body')
|
||||
file_system: StubFileSystem.new('my_partial' => 'my partial body'),
|
||||
}
|
||||
)
|
||||
|
||||
@@ -41,12 +41,12 @@ class PartialCacheUnitTest < Minitest::Test
|
||||
)
|
||||
context_one = Liquid::Context.build(
|
||||
registers: {
|
||||
file_system: shared_file_system
|
||||
file_system: shared_file_system,
|
||||
}
|
||||
)
|
||||
context_two = Liquid::Context.build(
|
||||
registers: {
|
||||
file_system: shared_file_system
|
||||
file_system: shared_file_system,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user