Skip to content

Commit eabd550

Browse files
committed
demonstrate bug: service provides array_of_hashes but misdeclares as a{sv}
This is an easy to make mistake because the spec unhelpfully exposes the wire format and works with arrays of dictionary entries instead of with hashes
1 parent 4a6c96d commit eabd550

4 files changed

Lines changed: 24 additions & 1 deletion

File tree

lib/dbus/data.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ def make_typed(type, value)
4444
# not nil because DBus.type validates
4545

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

lib/dbus/type.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def self.[](key_type, value_type)
363363
end
364364

365365
# @example
366-
# t = Type::Hash[Type::INT16]
366+
# t = Type::Hash[Type::STRING, Type::VARIANT]
367367
Hash = HashFactory
368368

369369
# Syntactic helper for constructing a struct Type.

spec/mock-service/spaghetti-monster.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ def initialize(path)
4646
"two" => "dva",
4747
"three" => [3, 3, 3]
4848
}
49+
# reproduce bug:
50+
# type mismatch (user means to declare array_of_hashes as aa{sv})
51+
# should produce informative error
52+
@array_of_hashes = [@my_dict.dup]
4953
@my_variant = @my_array.dup
5054
# 201 is a RET instruction for ZX Spectrum which has turned 40 recently
5155
@my_byte = 201
@@ -140,6 +144,8 @@ def explosive
140144
dbus_attr_accessor :my_array, "aq"
141145
dbus_attr_accessor :my_dict, "a{sv}"
142146
dbus_attr_accessor :my_variant, "v"
147+
# intentional bug to test reporting it: declaring an array of dict-entries instead
148+
dbus_attr_reader :array_of_hashes, "a{sv}"
143149

144150
dbus_attr_accessor :my_byte, "y"
145151

spec/property_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,20 @@
179179
val = @iface["MyArray"]
180180
expect(val).to eq([42, 43])
181181
end
182+
183+
it "a wrongly typed property provides a helpful message" do
184+
# Now we get:
185+
# When getting 'org.ruby.SampleInterface.ArrayOfHashes':
186+
# When making ARRAY: [DICT_ENTRY: [STRING, VARIANT]] from an instance of Array:
187+
# When making DICT_ENTRY: [STRING, VARIANT] from an instance of Hash:
188+
# Specified type has 2 members but value has 3 members;
189+
# caused by 1 sender=:1.70...
190+
# But we should really say
191+
# When making HASH: [STRING, VARIANT] from an instance of Array
192+
# and document better this pitfall.
193+
# Do we want to quote the entire data?
194+
expect { @iface["ArrayOfHashes"] }.to raise_error(/DWIM/)
195+
end
182196
end
183197

184198
context "a dict-typed property" do

0 commit comments

Comments
 (0)