def test_save(self): source = filter(None, SECTIONS.split('\n')) parsed = dosca.parse(source) sink = StringIO() dosca.save(parsed, sink) sink.seek(0) saved = sink.read() self.assertEqual(SECTIONS, saved)
def test_comments_and_spaces(self): source = COMMENTS.split('\n') result = { 'kwd1': 'value1', 'section': { 'kwd2': 'value2', 'kwd3': 'value3' } } self.assertEqual(dosca.parse(source), result)
def test_array_keys(self): source = ARRAY_KEYS.split('\n') result = { 'kwd[]': [1,2,3], 'section': { 'k[]': [1, 2] } } self.assertEqual(dosca.parse(source, key_hooks=[dosca.ext.PHP_ARRAYS]), result)
def test_simple_types(self): source = SIMPLE_TYPES.split('\n') result = { 'kwd1': 'and', 'kwd2': [1, 2, 4], 'kwd3': 'now something', 'kwd4': 'completely different', 'kwd5': 42, 'kwd6': True, 'kwd7': None, 'kwd8': [], 'kwd9': ['123,45', '67,8', '68'] } self.assertEqual(dosca.parse(source), result)
def test_custom_parsers(self): source = StringIO(CUSTOM_PARSERS) result = { 'kwd1': True, 'kwd2': False, 'kwd3': True, 'kwd4': False, 'kwd5': 'simplestring' } custom_parsers = [ (lambda x: x.lower() == 'yes', lambda _: True), (lambda x: x.lower() == 'on', lambda _: True), (lambda x: x.lower() == 'no' or x.lower() == 'off', lambda _: False) ] self.assertEqual(dosca.parse(source, custom_parsers=custom_parsers), result)
def test_sections(self): source = SECTIONS.split('\n') result = { 'kwd1': 'value1', 'section1': { 'kwd2': 'value2', }, 'section2': { 'kwd3': 'value3', 'section3': { 'kwd4': 'value4' }, 'section4': { 'kwd5': 'value5' } }, 'section5': { 'kwd6': 'value6' } } self.assertEqual(dosca.parse(source), result)