add equal operator

This commit is contained in:
simon lehericey 2022-06-09 13:48:45 +02:00
parent ebe95b83fa
commit 698eff0a50
2 changed files with 35 additions and 0 deletions

18
app/models/logic/eq.rb Normal file
View file

@ -0,0 +1,18 @@
class Logic::Eq < Logic::BinaryOperator
def operation = :==
def errors(stable_ids = [])
errors = []
if !Logic.compatible_type?(@left, @right)
errors += ["les types sont incompatibles : #{self}"]
end
errors + @left.errors(stable_ids) + @right.errors(stable_ids)
end
def ==(other)
self.class == other.class &&
[@left, @right].permutation.any? { |p| p == [other.left, other.right] }
end
end

View file

@ -0,0 +1,17 @@
describe Logic::Eq do
include Logic
describe '#compute' do
it { expect(ds_eq(constant(1), constant(1)).compute).to be(true) }
it { expect(ds_eq(constant(1), constant(2)).compute).to be(false) }
end
describe '#errors' do
it { expect(ds_eq(constant(true), constant(true)).errors).to be_empty }
it { expect(ds_eq(constant(true), constant(1)).errors).to eq(["les types sont incompatibles : (true == 1)"]) }
end
describe '#==' do
it { expect(ds_eq(constant(true), constant(false))).to eq(ds_eq(constant(false), constant(true))) }
end
end