add n_ary operators

This commit is contained in:
simon lehericey 2022-06-09 14:00:18 +02:00
parent 698eff0a50
commit daaa54b6f0
9 changed files with 146 additions and 2 deletions

View file

@ -0,0 +1,18 @@
describe Logic::And do
include Logic
describe '#compute' do
it { expect(and_from([true, true, true]).compute).to be true }
it { expect(and_from([true, true, false]).compute).to be false }
end
describe '#to_s' do
it do
expect(and_from([true, false, true]).to_s).to eq "(true && false && true)"
end
end
def and_from(boolean_to_constants)
ds_and(boolean_to_constants.map { |b| constant(b) })
end
end

View file

@ -1,7 +1,7 @@
include Logic
describe Logic::BinaryOperator do
let(:two_greater_than_one) { greater_than(constant(2), constant(1))}
let(:two_greater_than_one) { greater_than(constant(2), constant(1)) }
describe '#type' do
it { expect(two_greater_than_one.type).to eq(:boolean) }

View file

@ -0,0 +1,29 @@
describe Logic::NAryOperator do
include Logic
describe '#errors' do
it { expect(ds_and([]).errors).to eq(["opérateur 'Et' vide"]) }
it { expect(ds_and([constant(1), constant('toto')]).errors).to eq(["'Et' ne contient pas que des booléens : 1, toto"]) }
it { expect(ds_and([double(type: :boolean, errors: ['from double'])]).errors).to eq(["from double"]) }
end
describe '#==' do
it do
expect(and_from([true, true, false])).to eq(and_from([false, true, true]))
expect(and_from([true, true, false])).not_to eq(and_from([false, false, true]))
# perf test
left = [false, false] + Array.new(10) { true }
right = [false] + Array.new(11) { true }
expect(and_from(left)).not_to eq(and_from(right))
left = (1..10).to_a
right = (1..10).to_a.reverse
expect(and_from(left)).to eq(and_from(right))
end
end
def and_from(boolean_to_constants)
ds_and(boolean_to_constants.map { |b| constant(b) })
end
end

View file

@ -8,10 +8,21 @@ describe Logic do
expect(Logic.from_h(empty.to_h)).to eq(empty)
expect(Logic.from_h(greater_than(constant(1), constant(2)).to_h)).to eq(greater_than(constant(1), constant(2)))
expect(Logic.from_h(ds_and([constant(true), constant(true), constant(false)]).to_h))
.to eq(ds_and([constant(true), constant(true), constant(false)]))
end
describe '.compatible_type?' do
it { expect(Logic.compatible_type?(constant(true), constant(true))).to be true }
it { expect(Logic.compatible_type?(constant(1), constant(true))).to be false }
end
describe 'priority' do
# (false && true) || true = true
it { expect(ds_or([ds_and([constant(false), constant(true)]), constant(true)]).compute).to be true }
# false && (true || true) = false
it { expect(ds_and([constant(false), ds_or([constant(true), constant(true)])]).compute).to be false }
end
end

17
spec/models/or_spec.rb Normal file
View file

@ -0,0 +1,17 @@
describe Logic::Or do
include Logic
describe '#compute' do
it { expect(or_from([true, true, true]).compute).to be true }
it { expect(or_from([true, true, false]).compute).to be true }
it { expect(or_from([false, false, false]).compute).to be false }
end
describe '#to_s' do
it { expect(or_from([true, false, true]).to_s).to eq "(true || false || true)" }
end
def or_from(boolean_to_constants)
ds_or(boolean_to_constants.map { |b| constant(b) })
end
end