diff --git a/src/main/java/liqp/filters/Divided_By.java b/src/main/java/liqp/filters/Divided_By.java index 6c87e4bc..60dfb612 100644 --- a/src/main/java/liqp/filters/Divided_By.java +++ b/src/main/java/liqp/filters/Divided_By.java @@ -18,7 +18,7 @@ public Object apply(Object value, Object... params) { Object rhsObj = params[0]; - if (super.isInteger(value) && super.isInteger(rhsObj)) { + if (super.canBeInteger(value) && super.canBeInteger(rhsObj)) { return super.asNumber(value).longValue() / super.asNumber(rhsObj).longValue(); } diff --git a/src/main/java/liqp/filters/Minus.java b/src/main/java/liqp/filters/Minus.java index 8fc460be..3c54f510 100644 --- a/src/main/java/liqp/filters/Minus.java +++ b/src/main/java/liqp/filters/Minus.java @@ -3,7 +3,7 @@ public class Minus extends Filter { /* - * plus(input, operand) + * minus(input, operand) * * subtraction */ @@ -18,7 +18,7 @@ public Object apply(Object value, Object... params) { Object rhsObj = params[0]; - if (super.isInteger(value) && super.isInteger(rhsObj)) { + if (super.canBeInteger(value) && super.canBeInteger(rhsObj)) { return super.asNumber(value).longValue() - super.asNumber(rhsObj).longValue(); } diff --git a/src/main/java/liqp/filters/Modulo.java b/src/main/java/liqp/filters/Modulo.java index f7d914fe..b31de6e1 100644 --- a/src/main/java/liqp/filters/Modulo.java +++ b/src/main/java/liqp/filters/Modulo.java @@ -3,7 +3,7 @@ public class Modulo extends Filter { /* - * plus(input, operand) + * modulo(input, operand) * * modulus */ @@ -18,7 +18,7 @@ public Object apply(Object value, Object... params) { Object rhsObj = params[0]; - if (super.isInteger(value) && super.isInteger(rhsObj)) { + if (super.canBeInteger(value) && super.canBeInteger(rhsObj)) { return super.asNumber(value).longValue() % super.asNumber(rhsObj).longValue(); } diff --git a/src/main/java/liqp/filters/Plus.java b/src/main/java/liqp/filters/Plus.java index 1f096178..13ebef43 100644 --- a/src/main/java/liqp/filters/Plus.java +++ b/src/main/java/liqp/filters/Plus.java @@ -18,7 +18,7 @@ public Object apply(Object value, Object... params) { Object rhsObj = params[0]; - if (super.isInteger(value) && super.isInteger(rhsObj)) { + if (super.canBeInteger(value) && super.canBeInteger(rhsObj)) { return super.asNumber(value).longValue() + super.asNumber(rhsObj).longValue(); } diff --git a/src/main/java/liqp/filters/Times.java b/src/main/java/liqp/filters/Times.java index 3acf2c7c..efc3f365 100644 --- a/src/main/java/liqp/filters/Times.java +++ b/src/main/java/liqp/filters/Times.java @@ -18,7 +18,7 @@ public Object apply(Object value, Object... params) { Object rhsObj = params[0]; - if (super.isInteger(value) && super.isInteger(rhsObj)) { + if (super.canBeInteger(value) && super.canBeInteger(rhsObj)) { return super.asNumber(value).longValue() * super.asNumber(rhsObj).longValue(); } diff --git a/src/test/java/liqp/filters/MinusTest.java b/src/test/java/liqp/filters/MinusTest.java index e210b75b..5187a350 100644 --- a/src/test/java/liqp/filters/MinusTest.java +++ b/src/test/java/liqp/filters/MinusTest.java @@ -51,4 +51,47 @@ public void applyOriginalTest() { assertThat(Template.parse("{{ input | minus:operand }}").render("{\"input\":5, \"operand\":1}"), is((Object)"4")); assertThat(Template.parse("{{ '4.3' | minus:'2' }}").render(), is((Object)"2.3")); } + + /* + Ruby source: + + # gem 'liquid', '~> 4.0.0' + + require 'liquid' + + sources = [ + '{{ 5 | minus: 2 }}', # int - int + '{{ 5.0 | minus: 2 }}', # double - int + '{{ "5" | minus: 2 }}', # string_int - int + '{{ "5" | minus: 2.0 }}', # string_int - double + '{{ "5" | minus: "2" }}', # string_int - string_int + '{{ "5" | minus: "2.0" }}' # string_int - string_double + ] + + sources.each { |source| + @template = Liquid::Template.parse(source) + result = @template.render({}) + printf("result: '%s'\n", result) + } + + Yields: + + result: '3' + result: '3.0' + result: '3' + result: '3.0' + result: '3' + result: '3.0' + + https://github.com/bkiers/Liqp/issues/110 + */ + @Test + public void bug110() { + assertThat(Template.parse("{{ 5 | minus: 2 }}").render(), is((Object)"3")); + assertThat(Template.parse("{{ 5.0 | minus: 2 }}").render(), is((Object)"3.0")); + assertThat(Template.parse("{{ \"5\" | minus: 2 }}").render(), is((Object)"3")); + assertThat(Template.parse("{{ \"5\" | minus: 2.0 }}").render(), is((Object)"3.0")); + assertThat(Template.parse("{{ \"5\" | minus: \"2\" }}").render(), is((Object)"3")); + assertThat(Template.parse("{{ \"5\" | minus: \"2.0\" }}").render(), is((Object)"3.0")); + } }