Skip to content

Commit

Permalink
Fix error message when required composite is missing (GH-194)
Browse files Browse the repository at this point in the history
  • Loading branch information
kputnam committed May 28, 2019
1 parent 8fece4a commit 8a63cb0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
5 changes: 4 additions & 1 deletion lib/stupidedi/parser/builder_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,11 @@ def critique(zipper, recursive = false, position = false)
elsif zipper.node.composite?
if zipper.node.blank?
if zipper.node.usage.required?
# GH-194: Normally the position of a composit element is the
# position of it's first child; but an empty composit element
# doesn't have children. So the closest position is of the parent
raise Exceptions::ParseError,
"required element #{zipper.node.descriptor} is blank at #{zipper.node.position.inspect}"
"required #{zipper.node.descriptor} is blank at #{zipper.parent.node.position.inspect}"
end
elsif zipper.node.usage.forbidden?
raise Exceptions::ParseError,
Expand Down
5 changes: 5 additions & 0 deletions lib/stupidedi/schema/composite_element_use.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ def copy(changes = {})
changes.fetch(:parent, @parent)
end

def descriptor
n = parent.element_uses.index{|u| u.eql?(self) } + 1
"element #{parent.id}#{"%02d" % n} #{definition.name}".strip
end

def repeatable?
@repeat_count.try{|r| r.include?(2) }
end
Expand Down
11 changes: 9 additions & 2 deletions lib/stupidedi/values/composite_element_val.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ class CompositeElementVal < AbstractElementVal

def_delegators :@usage, :definition, :descriptor

def_delegators "@children.head", :position

def initialize(children, usage)
@children, @usage =
children, usage
Expand All @@ -31,6 +29,15 @@ def copy(changes = {})
changes.fetch(:usage, @usage)
end

def position
if @children.present?
@children.head.position
else
# GH-194
"<position unknown>"
end
end

# @return false
def leaf?
false
Expand Down
13 changes: 12 additions & 1 deletion spec/lib/stupidedi/parser/builder_dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1416,7 +1416,18 @@ def config(details, version = "005010")
end
end

context "composite" do
fcontext "composite" do
context "when element is missing" do
let(:b) do
strict(Detail("2", Segment(10, COS(), s_mandatory, bounded(1))))
end

it "raises an exception" do
expect { b.COS() }.to raise_error(/required element COS01 .+? is blank/)
expect(b.machine).to be_segment(:ST)
end
end

context "when element should be simple" do
let(:b) do
strict(Detail("2", Segment(10, IDA(), s_mandatory, bounded(1))))
Expand Down
5 changes: 5 additions & 0 deletions spec/support/definitions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ module SegmentDefs
Stupidedi::Schema::SegmentDef.build(:COR, "Example Segment", "",
ElementDefs::DE_CON.simple_use(ElementReqs::Optional, RepeatCount.bounded(3)))

COS =
Stupidedi::Schema::SegmentDef.build(:COS, "Example Segment", "",
ElementDefs::DE_CON.simple_use(ElementReqs::Mandatory, RepeatCount.bounded(1)))

REP =
Stupidedi::Schema::SegmentDef.build(:REP, "Example Segment", "",
ElementDefs::DE_N0.simple_use(ElementReqs::Optional, RepeatCount.bounded(3)))
Expand All @@ -261,6 +265,7 @@ def NNA; SegmentDefs::NNA end
def NNB; SegmentDefs::NNB end
def COM; SegmentDefs::COM end
def COR; SegmentDefs::COR end
def COS; SegmentDefs::COS end
def REP; SegmentDefs::REP end

def ANA(*args) AN_(SegmentDefs::ANA, *args) end
Expand Down

0 comments on commit 8a63cb0

Please sign in to comment.