Skip to content

Commit

Permalink
Test Variant#initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
mvidner committed May 25, 2022
1 parent 2351007 commit b370efc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
7 changes: 6 additions & 1 deletion lib/dbus/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -759,11 +759,16 @@ def self.guess_type(value)
# @param member_type [Type,nil]
def initialize(value, member_type:)
# TODO: validate that the given *member_type* matches *value*
if value.is_a?(self.class)
case value
when Data::Variant
# Copy the contained value instead of boxing it more
# TODO: except perhaps for round-tripping in exact mode?
@member_type = value.member_type
value = value.exact_value
when Data::Base
@member_type = member_type || value.type
raise ArgumentError, "Variant type #{@member_type} does not match value type #{value.type}" \
unless @member_type == value.type
else
@member_type = member_type || self.class.guess_type(value)
value = Data.make_typed(@member_type, value)
Expand Down
7 changes: 4 additions & 3 deletions spec/data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -625,12 +625,13 @@
input = DBus::Data::UInt16.new(42)

type = DBus.type(T::INT16)
expect { described_class.new(input, member_type: type) }.to raise_error
expect { described_class.new(input, member_type: type) }
.to raise_error(ArgumentError, /Variant type n does not match value type q/)
end
end
end

describe DBus::Data::DictEntry do
include_examples "#== and #eql? work for container types (1 value)",
"/foo", { member_type: DBus.type(T::STRING) }
end
end
end

0 comments on commit b370efc

Please sign in to comment.