Skip to content

Commit 482852b

Browse files
authored
Merge pull request #161 from glennsarti/test-hash-roundtrip
(maint) Add tests for roundtripping hash serialisation
2 parents 04eca1f + 134cbd1 commit 482852b

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

lib/puppet-languageserver/sidecar_protocol.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ def from_h!(value)
111111
self.length = value['length']
112112
self
113113
end
114+
115+
private
116+
117+
def value_from_hash(hash, key)
118+
# The key could be a Symbol or String in the hash
119+
hash[key].nil? ? hash[key.to_s] : hash[key]
120+
end
114121
end
115122

116123
class BasePuppetObjectList < Array
@@ -173,9 +180,10 @@ def from_h!(value)
173180
self.parameters = {}
174181
unless value['parameters'].nil?
175182
value['parameters'].each do |attr_name, obj_attr|
183+
# TODO: This should be a class, not a hash
176184
parameters[attr_name] = {
177-
:type => obj_attr['type'],
178-
:doc => obj_attr['doc']
185+
:type => value_from_hash(obj_attr, :type),
186+
:doc => value_from_hash(obj_attr, :doc)
179187
}
180188
end
181189
end
@@ -204,7 +212,7 @@ def to_h
204212
super.to_h.merge(
205213
'doc' => doc,
206214
'function_version' => function_version,
207-
'signatures' => signatures
215+
'signatures' => signatures.map(&:to_h)
208216
)
209217
end
210218

@@ -240,7 +248,7 @@ def to_h
240248
'key' => key,
241249
'doc' => doc,
242250
'return_types' => return_types,
243-
'parameters' => parameters
251+
'parameters' => parameters.map(&:to_h)
244252
}
245253
end
246254

@@ -311,10 +319,11 @@ def from_h!(value)
311319
unless value['attributes'].nil?
312320
value['attributes'].each do |attr_name, obj_attr|
313321
attributes[attr_name.intern] = {
314-
:type => obj_attr['type'].intern,
315-
:doc => obj_attr['doc'],
316-
:required? => obj_attr['required?'],
317-
:isnamevar? => obj_attr['isnamevar?']
322+
# TODO: This should be a class, not a hash
323+
:type => value_from_hash(obj_attr, :type).intern,
324+
:doc => value_from_hash(obj_attr, :doc),
325+
:required? => value_from_hash(obj_attr, :required?),
326+
:isnamevar? => value_from_hash(obj_attr, :isnamevar?)
318327
}
319328
end
320329
end

spec/languageserver/unit/puppet-languageserver/sidecar_protocol_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
expect(subject).to respond_to(testcase)
2323
end
2424
end
25+
26+
it 'should roundtrip to_h to from_h!' do
27+
subject_as_hash = subject.to_h
28+
copy = subject_klass.new.from_h!(subject_as_hash)
29+
expect(subject_as_hash).to eq(copy.to_h)
30+
end
2531
end
2632

2733
shared_examples_for 'a base Sidecar Protocol Puppet object list' do

0 commit comments

Comments
 (0)