Skip to content

Commit

Permalink
Added Less than equal to filter
Browse files Browse the repository at this point in the history
- shortened alias names for filters
  • Loading branch information
viveksoundrapandy committed Feb 17, 2020
1 parent c819f4b commit 0416f4e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/wongi-engine/dsl.rb
Expand Up @@ -81,10 +81,13 @@ module Wongi::Engine::DSL
clause :less
accept Wongi::Engine::LessThanTest

clause :lte
accept Wongi::Engine::LessThanOrEqualTest

clause :greater
accept Wongi::Engine::GreaterThanTest

clause :greaterthanorequal
clause :gte
accept Wongi::Engine::GreaterThanOrEqualTest

clause :assert, :dynamic
Expand Down
3 changes: 2 additions & 1 deletion lib/wongi-engine/filter.rb
Expand Up @@ -4,4 +4,5 @@
require 'wongi-engine/filter/asserting_test'
require 'wongi-engine/filter/less_than_test'
require 'wongi-engine/filter/greater_than_test'
require 'wongi-engine/filter/greater_than_or_equal_test'
require 'wongi-engine/filter/less_than_or_equal_test'
require 'wongi-engine/filter/greater_than_or_equal_test'
32 changes: 32 additions & 0 deletions lib/wongi-engine/filter/less_than_or_equal_test.rb
@@ -0,0 +1,32 @@
# frozen_string_literal: true
module Wongi::Engine
class LessThanOrEqualTest < FilterTest
attr_reader :x, :y

def initialize(x, y)
@x = x
@y = y
end

def passes?(token)
x = if Template.variable? @x
token[@x]
else
@x
end

y = if Template.variable? @y
token[@y]
else
@y
end

return false if x == :_ || y == :_
x <= y
end

def ==(other)
super && x == other.x && y == other.y
end
end
end
2 changes: 1 addition & 1 deletion spec/filter_specs/greater_than_equality_test_spec.rb
Expand Up @@ -21,7 +21,7 @@ def test_rule(&block)
test_rule do
forall do
has :Number, :assign_check, :_
greaterthanorequal :Number, 6
gte :Number, 6
end
end

Expand Down
33 changes: 33 additions & 0 deletions spec/filter_specs/less_than_equality_test_spec.rb
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require 'spec_helper'

describe 'Less Than Or Equal test' do
before :each do
@engine = Wongi::Engine.create
end

attr_reader :engine

attr_reader :production

def test_rule(&block)
@production = (engine << rule('test-rule', &block))
end

it 'should interact with optional node correctly' do
# before the fix, filters would try to piggy-back on optional templates

test_rule do
forall do
has :Number, :assign_check, :_
lte :Number, 6
end
end

engine << [5, :assign_check, nil]
engine << [6, :assign_check, nil]
engine << [7, :assign_check, nil] #should not pass
expect(@production.size).to eq(2)
end
end

0 comments on commit 0416f4e

Please sign in to comment.