Skip to content

Commit

Permalink
Semantics.add should raise an error if parent is invalid (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimtng authored Oct 1, 2023
1 parent 0f0a1f7 commit b6a4022
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 7 additions & 2 deletions lib/openhab/core/items/semantics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,14 @@ def add(label: nil, synonyms: "", description: "", **tags)
synonyms = Array.wrap(synonyms).map { |s| s.to_s.strip }

tags.map do |name, parent|
parent = lookup(parent) unless parent.is_a?(SemanticTag)
unless parent.is_a?(SemanticTag)
parent_tag = lookup(parent)
raise ArgumentError, "Unknown parent: #{parent}" unless parent_tag

parent = parent_tag
end

next if lookup(name)
next unless parent

new_tag = org.openhab.core.semantics.SemanticTagImpl.new("#{parent.uid}_#{name}",
label,
Expand Down
4 changes: 2 additions & 2 deletions spec/openhab/core/items/semantics_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ def points(*args)
expect(Semantics.constants).to include(*to_create)
end

it "doesn't create a tag with an invalid parent" do
expect(Semantics.add(InvalidParentTag: :Blah)).to be_empty
it "raises an error when trying to create a tag with an invalid parent" do
expect { Semantics.add(InvalidParentTag: :Blah) }.to raise_error(ArgumentError)
expect(Semantics.constants).not_to include(:InvalidParentTag)
end

Expand Down

0 comments on commit b6a4022

Please sign in to comment.