Skip to content

Commit

Permalink
Add Predicate#assert!, with the help of minitest.
Browse files Browse the repository at this point in the history
  • Loading branch information
blambeau committed Jun 9, 2023
1 parent f4aa467 commit 1c040aa
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
* BREAKING: removed support for ruby < 2.7 and upgraded sexpr
to 1.1.x

* Added Predicate#assert!, with the help of minitest.

Implementation is fuly functional, yet error messages will
be improved in the future by a better usage of minitest itself.

# 2.7.1 - 2022-04-21

* Add shadow support for Exists tree nodes, that are used by
Expand Down
5 changes: 5 additions & 0 deletions lib/predicate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_relative 'predicate/grammar'
require_relative 'predicate/processors'
require_relative 'predicate/dsl'
require_relative 'predicate/asserter'
class Predicate

class Error < StandardError; end
Expand Down Expand Up @@ -125,6 +126,10 @@ def call(tuple)
expr.evaluate(tuple)
end

def assert!(tuple = {})
expr.assert!(tuple)
end

# Splits this predicate, say P, as too predicates P1 & P2
# such that `P <=> P1 & P2` and P2 makes no reference to
# any attribute in `attr_list`.
Expand Down
12 changes: 12 additions & 0 deletions lib/predicate/asserter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'minitest'
class Predicate
class Asserter
include Minitest::Assertions

def initialize
@assertions = 0
end
attr_accessor :assertions

end # class Asserter
end # class Predicate
6 changes: 6 additions & 0 deletions lib/predicate/nodes/eq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ def evaluate(tuple)
left.evaluate(tuple) == right.evaluate(tuple)
end

def assert!(tuple, asserter = Asserter.new)
l, r = left.evaluate(tuple), right.evaluate(tuple)
asserter.assert_equal(l, r)
l
end

def to_hash
if left.identifier? && right.literal? && !right.has_placeholder?
{ left.name => right.value }
Expand Down
4 changes: 4 additions & 0 deletions lib/predicate/nodes/expr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def |(other)
sexpr([:or, self, other])
end

def assert!(tuple, asserter = Asserter.new)
asserter.assert(evaluate(tuple))
end

def and_split(attr_list)
# If we have no reference to attr_list, then we are P2, else we are P1
(free_variables & attr_list).empty? ? [ tautology, self ] : [ self, tautology ]
Expand Down
6 changes: 6 additions & 0 deletions lib/predicate/nodes/neq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ def evaluate(tuple)
left.evaluate(tuple) != right.evaluate(tuple)
end

def assert!(tuple, asserter = Asserter.new)
l, r = left.evaluate(tuple), right.evaluate(tuple)
asserter.refute_equal(l, r)
l
end

end
end
67 changes: 67 additions & 0 deletions spec/predicate/test_assert!.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require 'spec_helper'
class Predicate
describe Predicate, "assert!" do

describe "eq" do
let(:predicate) {
Predicate.eq(:x, 2)
}

subject{
predicate.assert!(input)
}

context "when ok" do
let(:input){
{ x: 2 }
}
it 'works and returns 2' do
expect(subject).to eql(2)
end
end

context "when ko" do
let(:input){
{ x: 3 }
}
it 'works and throws' do
expect{
subject
}.to raise_error(Minitest::Assertion, /Expected: 3\s+Actual: 2/)
end
end
end

describe "neq" do
let(:predicate) {
Predicate.neq(:x, 2)
}

subject{
predicate.assert!(input)
}

context "when ok" do
let(:input){
{ x: 3 }
}
it 'works and returns 3' do
expect(subject).to eql(3)
end
end

context "when ko" do
let(:input){
{ x: 2 }
}
it 'works and throws' do
expect{
subject
}.to raise_error(Minitest::Assertion, /Expected 2 to not be equal to 2./)
end
end
end

end
end

0 comments on commit 1c040aa

Please sign in to comment.