@@ -52,7 +52,7 @@ def __from_yaml_dict__(cls, # type: Type[Y]
5252
5353
5454def test_yamlable ():
55- """ Tests that YamlAbleMixIn works correctly """
55+ """ Tests that YamlAble works correctly """
5656
5757 @yaml_info (yaml_tag_ns = 'yaml.tests' )
5858 class Foo (YamlAble ):
@@ -250,3 +250,77 @@ class Foo(YamlAble):
250250
251251 assert Foo ().dumps_yaml () == """!yamlable/com.example.Foo {}
252252"""
253+
254+
255+ def test_abstract_parent_error ():
256+ """This tests that we can define an abstract parent class with the YamlAble behaviour and inherit it"""
257+
258+ class AbstractFooE (YamlAble ):
259+ pass
260+
261+ class FooError (AbstractFooE ):
262+ """
263+ This class inherits from the parent without redefining a yaml tag
264+ """
265+ def __init__ (self , a , b ):
266+ self .a = a
267+ self .b = b
268+
269+ def __eq__ (self , other ):
270+ return vars (self ) == vars (other )
271+
272+ # instantiate
273+ e = FooError (1 , 'hello' )
274+
275+ # dump
276+ with pytest .raises (NotImplementedError ):
277+ e .dumps_yaml ()
278+
279+
280+ def test_abstract_parent ():
281+ """This tests that we can define an abstract parent class with the YamlAble behaviour and inherit it"""
282+
283+ class AbstractFooV (YamlAble ):
284+ pass
285+
286+ @yaml_info (yaml_tag_ns = 'yaml.tests' )
287+ class FooValid (AbstractFooV ):
288+
289+ def __init__ (self , a , b ):
290+ self .a = a
291+ self .b = b
292+
293+ def __eq__ (self , other ):
294+ return vars (self ) == vars (other )
295+
296+ # instantiate
297+ f = FooValid (1 , 'hello' ) # note:
298+
299+ # dump
300+ y = f .dumps_yaml ()
301+ assert y == "!yamlable/yaml.tests.FooValid {a: 1, b: hello}\n "
302+
303+ # dump io
304+ class MemorizingStringIO (StringIO ):
305+ """ A StringIO object that memorizes its buffer when it is closed (as opposed to the standard StringIO) """
306+
307+ def close (self ):
308+ self .value = self .getvalue ()
309+ # super(StringIO, self).close() # this does not work with python 2 old-style classes (StringIO is one)
310+ StringIO .close (self )
311+
312+ s = MemorizingStringIO ()
313+ f .dump_yaml (s )
314+ assert s .value == y
315+
316+ # dump pyyaml
317+ assert dump (f ) == y
318+
319+ # load
320+ assert f == FooValid .loads_yaml (y )
321+
322+ # load io
323+ assert f == FooValid .load_yaml (StringIO (y ))
324+
325+ # load pyyaml
326+ assert f == load (y )
0 commit comments