Source

pyyaml / tests / lib / test_dictorder.py

Full commit
tlimgoogle 13953ba 










































import yaml

def _name_first(items):
    items.sort()
    for i in range(0, len(items)):
        if items[i][0] == 'name':
            items.insert(0, items.pop(i))

def test_dictorder(dictorder_filename, verbose=False):
    inputs = open(dictorder_filename, 'rb').read().split('++++++\n')[1:]
    for input in inputs:
        normal, reordered = input.split('---\n')
        normal_parsed = yaml.load(normal)
        format1 = yaml.dump(normal_parsed, indent=2)
        format2 = yaml.dump(normal_parsed, indent=2, dictorder=lambda x: x.sort())
        format3 = yaml.dump(normal_parsed, indent=2, dictorder=_name_first)
        reordered_parsed = yaml.load(reordered)

        if verbose:
            print '-' * 40, 'SORTED', '-' * 40
            print repr(normal)
        assert normal_parsed == reordered_parsed, repr(reordered_parsed)
        assert format1 == normal, format1
        assert format2 == normal, format2
        if verbose:
            print '-' * 40, 'NAME FIRST', '-' * 40
            print repr(reordered)
        assert format3 == reordered, format3

        format_da = yaml.dump_all([normal_parsed], indent=2, dictorder=_name_first)
        assert format_da == reordered, format_da
        format_sd = yaml.safe_dump(normal_parsed, indent=2, dictorder=_name_first)
        assert format_sd == reordered, format_sd
        format_sda = yaml.safe_dump_all([normal_parsed], indent=2, dictorder=_name_first)
        assert format_sda == reordered, format_sda

test_dictorder.unittest = ['.dictorder']

if __name__ == '__main__':
    import test_appliance
    test_appliance.run(globals())