add ensure_compatibility_from_left
This commit is contained in:
parent
5ac3049033
commit
9f49e8c2ea
2 changed files with 79 additions and 0 deletions
|
@ -12,6 +12,43 @@ module Logic
|
|||
.find { |c| c.name == name }
|
||||
end
|
||||
|
||||
def self.ensure_compatibility_from_left(condition)
|
||||
left = condition.left
|
||||
right = condition.right
|
||||
operator_class = condition.class
|
||||
|
||||
case [left.type, condition]
|
||||
in [:boolean, _]
|
||||
operator_class = Eq
|
||||
in [:empty, _]
|
||||
operator_class = EmptyOperator
|
||||
in [:enum, _]
|
||||
operator_class = Eq
|
||||
in [:number, EmptyOperator]
|
||||
operator_class = Eq
|
||||
in [:number, _]
|
||||
in [:string, _]
|
||||
operator_class = Eq
|
||||
end
|
||||
|
||||
if !compatible_type?(left, right)
|
||||
right = case left.type
|
||||
when :boolean
|
||||
Constant.new(true)
|
||||
when :empty
|
||||
Empty.new
|
||||
when :enum
|
||||
Constant.new(left.options.first)
|
||||
when :number
|
||||
Constant.new(0)
|
||||
when :string
|
||||
Constant.new('')
|
||||
end
|
||||
end
|
||||
|
||||
operator_class.new(left, right)
|
||||
end
|
||||
|
||||
def self.compatible_type?(left, right)
|
||||
case [left.type, right.type]
|
||||
in [a, ^a] # syntax for same type
|
||||
|
|
|
@ -15,6 +15,48 @@ describe Logic do
|
|||
.to eq(ds_and([constant(true), constant(true), constant(false)]))
|
||||
end
|
||||
|
||||
describe '.ensure_compatibility_from_left' do
|
||||
subject { Logic.ensure_compatibility_from_left(condition) }
|
||||
|
||||
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 string empty operator true' do
|
||||
let(:condition) { empty_operator(constant('a'), constant(true)) }
|
||||
|
||||
it { is_expected.to eq(ds_eq(constant('a'), constant(''))) }
|
||||
end
|
||||
|
||||
context 'when dropdown empty operator true' do
|
||||
let(:drop_down) { create(:type_de_champ_drop_down_list) }
|
||||
let(:first_option) { drop_down.drop_down_list_enabled_non_empty_options.first }
|
||||
let(:condition) { empty_operator(champ_value(drop_down), constant(true)) }
|
||||
|
||||
it { is_expected.to eq(ds_eq(champ_value(drop_down), constant(first_option))) }
|
||||
end
|
||||
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 }
|
||||
|
|
Loading…
Reference in a new issue