Skip to content

demonstrate bug: service provides array_of_hashes but misdeclares as a{sv} #148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/dbus/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def make_typed(type, value)
# not nil because DBus.type validates

data_class.from_typed(value, type: type)
rescue StandardError => e
msg = "When making #{type.inspect} from an instance of #{value.class}: " + e.message
raise e.exception(msg)
end
module_function :make_typed

Expand Down
2 changes: 1 addition & 1 deletion lib/dbus/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def self.[](key_type, value_type)
end

# @example
# t = Type::Hash[Type::INT16]
# t = Type::Hash[Type::STRING, Type::VARIANT]
Hash = HashFactory

# Syntactic helper for constructing a struct Type.
Expand Down
6 changes: 6 additions & 0 deletions spec/mock-service/spaghetti-monster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def initialize(path)
"two" => "dva",
"three" => [3, 3, 3]
}
# reproduce bug:
# type mismatch (user means to declare array_of_hashes as aa{sv})
# should produce informative error
@array_of_hashes = [@my_dict.dup]
@my_variant = @my_array.dup
# 201 is a RET instruction for ZX Spectrum which has turned 40 recently
@my_byte = 201
Expand Down Expand Up @@ -140,6 +144,8 @@ def explosive
dbus_attr_accessor :my_array, "aq"
dbus_attr_accessor :my_dict, "a{sv}"
dbus_attr_accessor :my_variant, "v"
# intentional bug to test reporting it: declaring an array of dict-entries instead
dbus_attr_reader :array_of_hashes, "a{sv}"

dbus_attr_accessor :my_byte, "y"

Expand Down
14 changes: 14 additions & 0 deletions spec/property_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@
val = @iface["MyArray"]
expect(val).to eq([42, 43])
end

it "a wrongly typed property provides a helpful message" do
# Now we get:
# When getting 'org.ruby.SampleInterface.ArrayOfHashes':
# When making ARRAY: [DICT_ENTRY: [STRING, VARIANT]] from an instance of Array:
# When making DICT_ENTRY: [STRING, VARIANT] from an instance of Hash:
# Specified type has 2 members but value has 3 members;
# caused by 1 sender=:1.70...
# But we should really say
# When making HASH: [STRING, VARIANT] from an instance of Array
# and document better this pitfall.
# Do we want to quote the entire data?
expect { @iface["ArrayOfHashes"] }.to raise_error(/DWIM/)
end
end

context "a dict-typed property" do
Expand Down
Loading