demarches-normaliennes/spec/models/logic_spec.rb

95 lines
3.6 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
2022-06-09 11:48:30 +02:00
describe Logic do
include Logic
it 'serializes deserializes' do
expect(Logic.from_h(constant(1).to_h)).to eq(constant(1))
expect(Logic.from_json(constant(1).to_json)).to eq(constant(1))
2022-06-09 11:54:29 +02:00
expect(Logic.from_h(empty.to_h)).to eq(empty)
2022-06-09 12:14:08 +02:00
2022-06-16 17:21:47 +02:00
expect(Logic.from_h(champ_value(1).to_h)).to eq(champ_value(1))
2022-06-09 12:14:08 +02:00
expect(Logic.from_h(greater_than(constant(1), constant(2)).to_h)).to eq(greater_than(constant(1), constant(2)))
2022-06-09 14:00:18 +02:00
expect(Logic.from_h(ds_and([constant(true), constant(true), constant(false)]).to_h))
.to eq(ds_and([constant(true), constant(true), constant(false)]))
2022-06-09 11:48:30 +02:00
end
2022-06-09 13:43:35 +02:00
2022-06-09 13:45:56 +02:00
describe '.ensure_compatibility_from_left' do
2022-09-26 21:22:43 +02:00
let(:type_de_champs) { [] }
subject { Logic.ensure_compatibility_from_left(condition, type_de_champs) }
2022-06-09 13:45:56 +02:00
context 'when it s fine' do
let(:condition) { greater_than(constant(1), constant(1)) }
it { is_expected.to eq(condition) }
end
context 'when empty equal true' do
let(:condition) { ds_eq(empty, constant(true)) }
it { is_expected.to eq(empty_operator(empty, empty)) }
end
context 'when true greater_than 1' do
let(:condition) { greater_than(constant(true), constant(0)) }
it { is_expected.to eq(ds_eq(constant(true), constant(true))) }
end
context 'when number empty operator true' do
let(:condition) { empty_operator(constant(1), constant(true)) }
it { is_expected.to eq(ds_eq(constant(1), constant(0))) }
end
context 'when dropdown empty operator true' do
let(:drop_down) { create(:type_de_champ_drop_down_list) }
2022-09-26 21:22:43 +02:00
let(:type_de_champs) { [drop_down] }
let(:first_option) { drop_down.drop_down_options.first }
2022-09-26 21:22:43 +02:00
let(:condition) { empty_operator(champ_value(drop_down.stable_id), constant(true)) }
2022-06-09 13:45:56 +02:00
2022-09-26 21:22:43 +02:00
it { is_expected.to eq(ds_eq(champ_value(drop_down.stable_id), constant(first_option))) }
2022-06-09 13:45:56 +02:00
end
context 'when multiple dropdown empty operator true' do
let(:multiple_drop_down) { create(:type_de_champ_multiple_drop_down_list) }
2022-09-26 21:22:43 +02:00
let(:type_de_champs) { [multiple_drop_down] }
let(:first_option) { multiple_drop_down.drop_down_options.first }
2022-09-26 21:22:43 +02:00
let(:condition) { empty_operator(champ_value(multiple_drop_down.stable_id), constant(true)) }
2022-09-26 21:22:43 +02:00
it { is_expected.to eq(ds_include(champ_value(multiple_drop_down.stable_id), constant(first_option))) }
end
2022-06-09 13:45:56 +02:00
end
2022-06-09 13:43:35 +02:00
describe '.compatible_type?' do
2022-09-26 21:20:32 +02:00
it { expect(Logic.compatible_type?(constant(true), constant(true), [])).to be true }
it { expect(Logic.compatible_type?(constant(1), constant(true), [])).to be false }
2022-06-09 14:20:06 +02:00
context 'with a dropdown' do
let(:drop_down) { create(:type_de_champ_drop_down_list) }
let(:first_option) { drop_down.drop_down_options.first }
2022-06-09 14:20:06 +02:00
it do
2022-09-26 21:20:32 +02:00
expect(Logic.compatible_type?(champ_value(drop_down.stable_id), constant('a'), [drop_down])).to be true
2022-06-09 14:20:06 +02:00
end
end
2022-06-09 13:43:35 +02:00
end
2022-06-09 14:00:18 +02:00
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
2022-07-01 17:42:44 +02:00
describe '.add_empty_condition_to' do
it { expect(Logic.add_empty_condition_to(nil)).to eq(empty_operator(empty, empty)) }
it { expect(Logic.add_empty_condition_to(constant(true))).to eq(ds_and([constant(true), empty_operator(empty, empty)])) }
it { expect(Logic.add_empty_condition_to(ds_or([constant(true)]))).to eq(ds_or([constant(true), empty_operator(empty, empty)])) }
end
2022-06-09 11:48:30 +02:00
end