Fixed to_json returns empty dict from generator. #38
This commit is contained in:
parent
06c13218cd
commit
497d3782c0
|
@ -10,7 +10,7 @@ class BaseDict(dict):
|
|||
self._dict = args[0]
|
||||
else:
|
||||
self._dict = dict(*args, **kwargs)
|
||||
super(BaseDict, self).__init__(*args, **kwargs)
|
||||
super(BaseDict, self).__init__(self._dict)
|
||||
|
||||
def __bool__(self):
|
||||
return bool(self._dict)
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from benedict import benedict
|
||||
|
||||
import unittest
|
||||
|
||||
|
||||
class github_issue_0038_test_case(unittest.TestCase):
|
||||
|
||||
"""
|
||||
https://github.com/fabiocaccamo/python-benedict/issues/38
|
||||
|
||||
To run this specific test:
|
||||
- Run python -m unittest tests.github.test_issue_0038
|
||||
"""
|
||||
|
||||
@staticmethod
|
||||
def get_dict_generator():
|
||||
for k, v in enumerate('abcd'):
|
||||
yield k, v
|
||||
|
||||
def test_init_with_generator(self):
|
||||
b = benedict(self.get_dict_generator())
|
||||
self.assertEqual(b, {0: 'a', 1: 'b', 2: 'c', 3: 'd'})
|
||||
self.assertEqual(b.to_json(), '{"0": "a", "1": "b", "2": "c", "3": "d"}')
|
||||
# recast benedict to dict and back to benedict
|
||||
b = benedict(self.get_dict_generator())
|
||||
d = dict(b)
|
||||
b = benedict(d)
|
||||
self.assertEqual(b, {0: 'a', 1: 'b', 2: 'c', 3: 'd'})
|
||||
self.assertEqual(b.to_json(), '{"0": "a", "1": "b", "2": "c", "3": "d"}')
|
Loading…
Reference in New Issue