Skip to content

Commit

Permalink
Fix a regression in Navigation#element
Browse files Browse the repository at this point in the history
  • Loading branch information
kputnam committed Mar 9, 2019
1 parent dbb229f commit 7b887e9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
22 changes: 13 additions & 9 deletions lib/stupidedi/parser/navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def element(m, n = nil, o = nil)
elsif element_use.composite? and not element_use.repeatable?
# m: element of segment
# n: component of composite element
# o: occurence of repeated component
# o: occurence of repeated component (commented-out below)
descriptor = "%s%02d" % [segment_id, m]
components = element_def.component_uses.length
unless n <= components
Expand All @@ -165,8 +165,12 @@ def element(m, n = nil, o = nil)
# component_use = element_def.component_uses.at(n - 1)

if o.nil?
# This is a component of a composite element
return Either.success(element_zip.child(n - 1))
if element_zip.node.blank?
Either.failure("#{descriptor} is empty")
else
# This is a component of a composite element
Either.success(element_zip.child(n - 1))
end

# @todo: There currently doesn't seem to be any instances of this in
# the real world (a composite element that has a component that can
Expand Down Expand Up @@ -209,11 +213,11 @@ def element(m, n = nil, o = nil)
if o.nil?
description = (element_use.composite?) ? "repeatable composite" : "repeatable"
if element_zip.node.blank?
return Either.failure("#{description} element #{descriptor} does not occur")
Either.failure("#{description} element #{descriptor} does not occur")
elsif occurs_count < n
return Either.failure("#{description} element #{descriptor} only occurs #{occurs_count} times")
Either.failure("#{description} element #{descriptor} only occurs #{occurs_count} times")
else
return Either.success(element_zip.child(n - 1))
Either.success(element_zip.child(n - 1))
end

elsif element_use.composite?
Expand All @@ -226,12 +230,12 @@ def element(m, n = nil, o = nil)
descriptor = "%s%02d" % [segment_id, m]

if element_zip.node.blank?
return Either.failure("repeatable composite element #{descriptor} does not occur")
Either.failure("repeatable composite element #{descriptor} does not occur")
elsif occurs_count < n
return Either.failure("repeatable composite element #{descriptor} only occurs #{occurs_count} times")
Either.failure("repeatable composite element #{descriptor} only occurs #{occurs_count} times")
else
component_zip = element_zip.children.at(n - 1)
return Either.success(component_zip.child(o - 1))
Either.success(component_zip.child(o - 1))
end

else
Expand Down
2 changes: 1 addition & 1 deletion lib/stupidedi/parser/tokenization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def mksegment_tok(segment_dict, id, elements, position)
end

element_idx = "00"
element_uses.zip(elements) do |e_use, (e_tag, e_val, e_position)|
elements.zip(element_uses) do |(e_tag, e_val, e_position), e_use|
element_idx = element_idx.succ
designator = "#{id}#{element_idx}"

Expand Down
7 changes: 7 additions & 0 deletions spec/lib/stupidedi/parser/navigation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,13 @@ def config(details)
end
end

context "when mth element does not occur" do
it "returns a failure" do
result = b.COM.machine.tap{|x| pp x }.elementn(1, 2)
expect(result).to be_failure(/COM01 is empty/)
end
end

context "when nth component does not occur" do
it "returns an empty value" do
result = b.COM(b.composite(1, 2)).machine.elementn(1, 3)
Expand Down

0 comments on commit 7b887e9

Please sign in to comment.