From e65f59e7a129e683a1b4fc11162c3228f7617139 Mon Sep 17 00:00:00 2001 From: Chris Larsen Date: Mon, 13 Jan 2025 09:10:24 -0500 Subject: [PATCH] Add Any#as_number_or_zero (#86) * Add Any#as_number_or_zero * Improve error message for Any#as_number --- spec/any_spec.cr | 24 ++++++++++++++++++++++++ src/liquid/any.cr | 14 ++++++++++---- src/liquid/filters/at_least.cr | 4 ++-- src/liquid/filters/at_most.cr | 4 ++-- src/liquid/filters/divided_by.cr | 4 ++-- 5 files changed, 40 insertions(+), 10 deletions(-) create mode 100644 spec/any_spec.cr diff --git a/spec/any_spec.cr b/spec/any_spec.cr new file mode 100644 index 0000000..680c74b --- /dev/null +++ b/spec/any_spec.cr @@ -0,0 +1,24 @@ +require "./spec_helper" + +describe Liquid::Any do + it "determines if underlying value is a number or not" do + any = Any.new(5) + any.as_number?.should eq(5) + + any = Any.new(5.0) + any.as_number?.should eq(5.0) + + any = Any.new("5.0") + any.as_number?.should eq(5.0) + + any = Any.new("Hello, world!") + any.as_number?.should be_nil + end + + it "raises if underlying value is not a number" do + expect_raises(TypeCastError, "Cast to Number+ failed") do + any = Any.new("Hello, world!") + any.as_number + end + end +end diff --git a/src/liquid/any.cr b/src/liquid/any.cr index 627bbcb..f91bbb4 100644 --- a/src/liquid/any.cr +++ b/src/liquid/any.cr @@ -268,13 +268,13 @@ module Liquid Any.new(-raw) end - # Checks that the underlying value is a numerical value, and returns its value. - # Returns 0 otherwise. + # Checks that the underlying value is a `Number`, and returns its value. + # Raises otherwise. def as_number : Number - as_number? || 0 + as_number? || raise TypeCastError.new("Cast to Number+ failed") end - # Checks that the underlying value is a numerical value, and returns its value. + # Checks that the underlying value is a `Number`, and returns its value. # Returns `nil` otherwise. def as_number? raw = @raw @@ -289,6 +289,12 @@ module Liquid end end + # Checks that the underlying value is a `Number`, and returns its value. + # Returns 0 otherwise. + def as_number_or_zero : Number + as_number? || 0 + end + # Returns a string representation of the underlying value. def to_s(io : IO) : Nil @raw.to_s(io) diff --git a/src/liquid/filters/at_least.cr b/src/liquid/filters/at_least.cr index de15ab5..fc9cda8 100644 --- a/src/liquid/filters/at_least.cr +++ b/src/liquid/filters/at_least.cr @@ -14,8 +14,8 @@ module Liquid::Filters def self.filter(data : Any, args : Array(Any), options : Hash(String, Any)) : Any raise FilterArgumentException.new("at_least filter expects one argument.") if args.size != 1 - min_value = args.first.as_number - result = data.as_number + min_value = args.first.as_number_or_zero + result = data.as_number_or_zero result = min_value if min_value > result Any.new(result) diff --git a/src/liquid/filters/at_most.cr b/src/liquid/filters/at_most.cr index c0a923b..cd95830 100644 --- a/src/liquid/filters/at_most.cr +++ b/src/liquid/filters/at_most.cr @@ -14,8 +14,8 @@ module Liquid::Filters def self.filter(data : Any, args : Array(Any), options : Hash(String, Any)) : Any raise FilterArgumentException.new("at_most filter expects one argument.") if args.size != 1 - max_value = args.first.as_number - result = data.as_number + max_value = args.first.as_number_or_zero + result = data.as_number_or_zero result = max_value if result > max_value Any.new(result) diff --git a/src/liquid/filters/divided_by.cr b/src/liquid/filters/divided_by.cr index 7f0bb29..e79daa2 100644 --- a/src/liquid/filters/divided_by.cr +++ b/src/liquid/filters/divided_by.cr @@ -7,8 +7,8 @@ module Liquid::Filters def self.filter(data : Any, args : Array(Any), options : Hash(String, Any)) : Any raise FilterArgumentException.new("divided_by filter expects one argument.") if args.size != 1 - lvalue = data.as_number - rvalue = args.first.as_number + lvalue = data.as_number_or_zero + rvalue = args.first.as_number_or_zero raise FilterArgumentException.new("divided_by filter cannot divide by 0 or 0.0.") if rvalue.zero? return Any.new(lvalue // rvalue) if rvalue.is_a?(Int) && lvalue.is_a?(Int)