From 35f649e020f32f30fc4ce88c6716f0f3a0e8e2eb Mon Sep 17 00:00:00 2001 From: Matt Juszczak Date: Fri, 26 Apr 2019 12:30:09 -0400 Subject: [PATCH 1/3] adding test for "in" with variables --- test/json_logic_test.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/json_logic_test.rb b/test/json_logic_test.rb index 6d46fb7..48470bd 100644 --- a/test/json_logic_test.rb +++ b/test/json_logic_test.rb @@ -70,4 +70,16 @@ def test_array_with_logic { "x" => true, "y" => 42} ) end + + def test_in_with_variable + assert_equal true, JSONLogic.apply( + { + "in" => [ + {"var" => "x"}, + {"var" => "x"} + ] + }, + { "x" => "foo"} + ) + end end From 3e9eae1df8da12cd1df55f6af10af995f8c576fe Mon Sep 17 00:00:00 2001 From: Ed Ropple Date: Fri, 26 Apr 2019 13:55:41 -0400 Subject: [PATCH 2/3] fix for 'in' operator; test passes --- json_logic.gemspec | 1 + lib/json_logic/operation.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/json_logic.gemspec b/json_logic.gemspec index 7fa8a7b..4bfee64 100644 --- a/json_logic.gemspec +++ b/json_logic.gemspec @@ -23,4 +23,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'bundler', '~> 1.13' spec.add_development_dependency 'rake', '~> 10.0' spec.add_development_dependency 'minitest', '~> 5.0' + spec.add_development_dependency 'pry' end diff --git a/lib/json_logic/operation.rb b/lib/json_logic/operation.rb index c57f84c..f838b31 100644 --- a/lib/json_logic/operation.rb +++ b/lib/json_logic/operation.rb @@ -92,7 +92,7 @@ class Operation '%' => ->(v, d) { v.map(&:to_i).reduce(:%) }, '^' => ->(v, d) { v.map(&:to_f).reduce(:**) }, 'merge' => ->(v, d) { v.flatten }, - 'in' => ->(v, d) { v[1].include? v[0] }, + 'in' => ->(v, d) { interpolated_block(v[1], d).include? v[0] }, 'cat' => ->(v, d) { v.map(&:to_s).join }, 'log' => ->(v, d) { puts v } } From bf825396f56f510bc15c38cf81fd3eb0b558b02a Mon Sep 17 00:00:00 2001 From: Jesse Cotton Date: Sat, 27 Apr 2019 11:01:56 -0700 Subject: [PATCH 3/3] Add negative test for variable in "in" --- test/json_logic_test.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/json_logic_test.rb b/test/json_logic_test.rb index 48470bd..b803a5d 100644 --- a/test/json_logic_test.rb +++ b/test/json_logic_test.rb @@ -81,5 +81,15 @@ def test_in_with_variable }, { "x" => "foo"} ) + + assert_equal false, JSONLogic.apply( + { + "in" => [ + {"var" => "x"}, + {"var" => "y"}, + ] + }, + { "x" => "foo", "y" => "bar" } + ) end end