Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ex): starts with and ends with evaluates to false with nil #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- uses: actions/checkout@v1
- uses: actions/setup-ruby@v1
with:
ruby-version: '2.7'
ruby-version: '3.0'
- uses: MeilCli/danger-action@v2
with:
plugins_file: 'Gemfile'
Expand Down
11 changes: 11 additions & 0 deletions impl/ex/lib/predicator/machine.ex
Original file line number Diff line number Diff line change
Expand Up @@ -289,12 +289,23 @@ defmodule Predicator.Machine do
put_instruction(machine, val in min..max)
end

def accept_instruction(machine = %__MODULE__{stack: [_match | [nil | _rest_of_stack]]}, [
"compare" | ["STARTSWITH" | _]
]), do: put_instruction(machine, false)

def accept_instruction(machine = %__MODULE__{stack: [match | [stack_val | _rest_of_stack]]}, [
"compare" | ["STARTSWITH" | _]
]) do
put_instruction(machine, String.starts_with?(stack_val, match))
end

def accept_instruction(
machine = %__MODULE__{stack: [_end_match | [nil | _rest_of_stack]]},
["compare" | ["ENDSWITH" | _]]
) do
put_instruction(machine, false)
end

def accept_instruction(
machine = %__MODULE__{stack: [end_match | [stack_val | _rest_of_stack]]},
["compare" | ["ENDSWITH" | _]]
Expand Down
9 changes: 9 additions & 0 deletions impl/ex/test/predicator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ defmodule PredicatorTest do
assert Predicator.matches?("!7 between 5 and 10") == false
assert Predicator.matches?("age between 5 and 10", age: 14) == false
assert Predicator.matches?("!age between 5 and 10", age: 7) == false
assert Predicator.matches?("age between 5 and 10", age: nil) == false
end
end

Expand Down Expand Up @@ -113,6 +114,7 @@ defmodule PredicatorTest do
assert Predicator.matches?("!'joaquin' starts with 'joa'") == false
assert Predicator.matches?("name starts with 'stuff'", name: "joaquin") == false
assert Predicator.matches?("!name starts with 'joa'", name: "joaquin") == false
assert Predicator.matches?("name starts with 'joa'", name: nil) == false
end
end

Expand Down Expand Up @@ -157,6 +159,7 @@ defmodule PredicatorTest do
assert Predicator.matches?("!'foobar' ends with 'bar'") == false
assert Predicator.matches?("foobar ends with 'bar'", foobar: "world") == false
assert Predicator.matches?("!foobar ends with 'bar'", foobar: "foobar") == false
assert Predicator.matches?("foobar ends with 'bar'", foobar: nil) == false
end
end

Expand Down Expand Up @@ -189,6 +192,7 @@ defmodule PredicatorTest do
assert Predicator.matches?("foo = 1", foo: 2) == false
assert Predicator.matches?("!1 = 1") == false
assert Predicator.matches?("!foo = 1", foo: 1) == false
assert Predicator.matches?("foo = 1", foo: nil) == false
end
end

Expand Down Expand Up @@ -254,6 +258,7 @@ defmodule PredicatorTest do
assert Predicator.matches?("12 < 1") == false
assert Predicator.matches?("!foo < 1", foo: 0) == false
assert Predicator.matches?("foo < 1", foo: 1) == false
assert Predicator.matches?("foo < 1", foo: nil) == false
end
end

Expand Down Expand Up @@ -301,6 +306,7 @@ defmodule PredicatorTest do
assert Predicator.matches?("!foo in [0, 1, 2, 3]", foo: 0) == false
assert Predicator.matches?("foo in ['foo', 'bar']", foo: "foobar") == false
assert Predicator.matches?("!foo in ['foo', 'bar']", foo: "foo") == false
assert Predicator.matches?("foo in ['foo', 'bar']", foo: nil) == false
end
end

Expand Down Expand Up @@ -338,6 +344,7 @@ defmodule PredicatorTest do
assert Predicator.matches?("foo not in [1, 2, 3]", foo: 0) == true
assert Predicator.matches?("foo not in ['foo', 'bar']", foo: "foobar") == true
assert Predicator.matches?("!foo not in ['foo', 'bar']", foo: "foo") == true
assert Predicator.matches?("foo not in ['foo', 'bar']", foo: nil) == true
end

test "evaluates to false" do
Expand Down Expand Up @@ -487,6 +494,7 @@ defmodule PredicatorTest do
assert Predicator.matches?("!'foo' is present", [], eval_opts) == false
assert Predicator.matches?("foo is present", [foo: ""], eval_opts) == false
assert Predicator.matches?("!foo is present", [foo: "bar"], eval_opts) == false
assert Predicator.matches?("foo is present", [foo: nil], eval_opts) == false
end
end

Expand All @@ -512,6 +520,7 @@ defmodule PredicatorTest do
assert Predicator.matches?("!'foo' is blank", [], eval_opts) == true
assert Predicator.matches?("foo is blank", [foo: ""], eval_opts) == true
assert Predicator.matches?("!foo is blank", [foo: "bar"], eval_opts) == true
assert Predicator.matches?("foo is blank", [foo: nil], eval_opts) == true
end

test "evaluates to false", %{eval_opts: eval_opts} do
Expand Down