Skip to content

Commit c5fd8f1

Browse files
committed
Added the commented tests
FIXME: remaining 1. comment the Type ctor as allowing incomplete types for backward comp, recommend DBus.type or DBus.types 2: mark Parser as @api private, after merging with data-unmarshall, remove Parser usage 3. see if we can use this as DBus Signature? Is this a good enough dual string-parsed representation?
1 parent 7d4acdb commit c5fd8f1

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

lib/dbus/type.rb

+25-3
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ class Parser
171171
# @param signature [Signature]
172172
def initialize(signature)
173173
@signature = signature
174+
if signature.size > 255
175+
msg = "Potential signature is longer than 255 characters (#{@signature.size}): #{@signature}"
176+
raise SignatureException, msg
177+
end
178+
174179
@idx = 0
175180
end
176181

@@ -227,6 +232,7 @@ def parse_one(char, for_array: false)
227232
end
228233

229234
# Parse the entire signature, return a DBus::Type object.
235+
# @return [Array<Type>]
230236
def parse
231237
@idx = 0
232238
ret = []
@@ -239,19 +245,35 @@ def parse
239245
# Parse one {SingleCompleteType}
240246
# @return [Type]
241247
def parse1
242-
parse_one(nextchar)
248+
c = nextchar
249+
raise SignatureException, "Empty signature, expecting a Single Complete Type" if c.nil?
250+
251+
t = parse_one(c)
252+
raise SignatureException, "Has more than a Single Complete Type: #{@signature}" unless nextchar.nil?
253+
254+
t
243255
end
244256
end
245257
end
246258

247259
# shortcuts
248260

249-
# Parse a String to a DBus::Type::Type
261+
# Parse a String to a DBus::Type
262+
# @param string_type [SingleCompleteType]
263+
# @return [DBus::Type]
250264
def type(string_type)
251-
Type::Parser.new(string_type).parse[0]
265+
Type::Parser.new(string_type).parse1
252266
end
253267
module_function :type
254268

269+
# Parse a String to a DBus::Type
270+
# @param string_type [Signature]
271+
# @return [Array<DBus::Type>]
272+
def types(string_type)
273+
Type::Parser.new(string_type).parse
274+
end
275+
module_function :types
276+
255277
# Make an explicit [Type, value] pair
256278
def variant(string_type, value)
257279
[type(string_type), value]

spec/type_spec.rb

+26-8
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
require "dbus"
66

77
describe DBus do
8-
# FIXME: what about ""?
9-
108
describe ".type" do
119
good = [
1210
"i",
@@ -15,7 +13,7 @@
1513
"aai"
1614
]
1715

18-
context "valid signatures" do
16+
context "valid single types" do
1917
good.each do |s|
2018
it "#{s.inspect} is parsed" do
2119
expect(DBus.type(s).to_s).to eq(s)
@@ -26,8 +24,6 @@
2624
bad = [
2725
["\x00", "Unknown type code"],
2826
["!", "Unknown type code"],
29-
["hmrp", "Unknown type code"],
30-
# TODO: too long single complete type, too long sig
3127

3228
# ARRAY related
3329
["a", "Empty ARRAY"],
@@ -37,7 +33,7 @@
3733
["r", "Abstract STRUCT"],
3834
["()", "Empty STRUCT"],
3935
["(ii", "STRUCT not closed"],
40-
["ii)", "STRUCT unexpectedly closed"],
36+
["a{i)", "STRUCT unexpectedly closed"],
4137

4238
# TODO: deep nesting arrays, structs, combined
4339

@@ -50,9 +46,16 @@
5046
["{sv}", "DICT_ENTRY not an immediate child of an ARRAY"],
5147
["a({sv})", "DICT_ENTRY not an immediate child of an ARRAY"],
5248
["a{sv", "DICT_ENTRY not closed"],
53-
["}", "DICT_ENTRY unexpectedly closed"]
49+
["}", "DICT_ENTRY unexpectedly closed"],
50+
51+
# Too long
52+
["(#{"y" * 254})", "longer than 255"],
53+
54+
# not Single Complete Types
55+
["", "expecting a Single Complete Type"],
56+
["ii", "more than a Single Complete Type"]
5457
]
55-
context "invalid signatures" do
58+
context "invalid single types" do
5659
bad.each.each do |s, msg|
5760
it "#{s.inspect} raises an exception mentioning: #{msg}" do
5861
rx = Regexp.new(Regexp.quote(msg))
@@ -61,4 +64,19 @@
6164
end
6265
end
6366
end
67+
68+
describe ".types" do
69+
good = [
70+
"",
71+
"ii"
72+
]
73+
74+
context "valid signatures" do
75+
good.each do |s|
76+
it "#{s.inspect} is parsed" do
77+
expect(DBus.types(s).map(&:to_s).join).to eq(s)
78+
end
79+
end
80+
end
81+
end
6482
end

0 commit comments

Comments
 (0)