add empty

This commit is contained in:
simon lehericey 2022-06-09 11:54:29 +02:00
parent 58da4805fa
commit 6ebfc505c4
4 changed files with 42 additions and 1 deletions

View file

@ -8,10 +8,12 @@ module Logic
end
def self.class_from_name(name)
[Constant]
[Constant, Empty]
.find { |c| c.name == name }
end
def constant(value) = Logic::Constant.new(value)
def empty = Logic::Empty.new
end

21
app/models/logic/empty.rb Normal file
View file

@ -0,0 +1,21 @@
class Logic::Empty < Logic::Term
def to_s = "empty member"
def type = :empty
def errors(_stable_ids = nil) = ['empty']
def to_h
{
"op" => self.class.name
}
end
def self.from_h(_h)
self.new
end
def ==(other)
self.class == other.class
end
end

View file

@ -0,0 +1,16 @@
describe Logic::Constant do
include Logic
describe '#type' do
it { expect(empty.type).to eq(:empty) }
end
describe '#errors' do
it { expect(empty.errors).to eq(['empty']) }
end
describe '#==' do
it { expect(empty).to eq(empty) }
it { expect(empty).not_to eq(constant(true)) }
end
end

View file

@ -4,5 +4,7 @@ describe Logic do
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))
expect(Logic.from_h(empty.to_h)).to eq(empty)
end
end