|
| 1 | +#!/bin/env python3 |
| 2 | +#------------------------------------------------------------------------------# |
| 3 | +# hsd-python: package for manipulating HSD-formatted data in Python # |
| 4 | +# Copyright (C) 2011 - 2021 DFTB+ developers group # |
| 5 | +# Licensed under the BSD 2-clause license. # |
| 6 | +#------------------------------------------------------------------------------# |
| 7 | +# |
| 8 | +"""Tests for the dictbuilder class""" |
| 9 | + |
| 10 | +import io |
| 11 | +import pytest |
| 12 | +import hsd |
| 13 | + |
| 14 | +_HSD_LINE = hsd.HSD_ATTRIB_LINE |
| 15 | +_HSD_EQUAL = hsd.HSD_ATTRIB_EQUAL |
| 16 | +_HSD_NAME = hsd.HSD_ATTRIB_NAME |
| 17 | + |
| 18 | +# General test list format for valid tests |
| 19 | +# [("Test name", ([List of HSD events], expected dictionary outcome))] |
| 20 | + |
| 21 | +# Tests without hsd attribute recording |
| 22 | +_TESTS_NO_HSDATTRIB = [ |
| 23 | + ( |
| 24 | + "Simple", ( |
| 25 | + "Test {}", |
| 26 | + {"Test": {}}, |
| 27 | + ) |
| 28 | + ), |
| 29 | + ( |
| 30 | + "Data with quoted strings", ( |
| 31 | + "O = SelectedShells { \"s\" \"p\" }", |
| 32 | + {"O": {"SelectedShells": ['"s"', '"p"']}}, |
| 33 | + ) |
| 34 | + ), |
| 35 | + ( |
| 36 | + "Attribute containing comma", ( |
| 37 | + "PolarRadiusCharge [AA^3,AA,] = {\n1.030000 3.800000 2.820000\n}", |
| 38 | + {"PolarRadiusCharge": [1.03, 3.8, 2.82], "PolarRadiusCharge.attrib": "AA^3,AA,"}, |
| 39 | + ) |
| 40 | + ), |
| 41 | + ( |
| 42 | + "Duplicate node entry", ( |
| 43 | + "a { b = 1 }\na { b = 2 }\n", |
| 44 | + {"a.attrib": [None, None], "a": [{"b": 1}, {"b": 2}]}, |
| 45 | + ) |
| 46 | + ), |
| 47 | + ( |
| 48 | + "Duplicate value entry", ( |
| 49 | + "a = 1\na = 2", |
| 50 | + {"a.attrib": [None, None], "a": [{None: 1}, {None: 2}]}, |
| 51 | + ) |
| 52 | + ), |
| 53 | +] |
| 54 | +_TESTS_NO_HSDATTRIB_NAMES, _TESTS_NO_HSDATTRIB_CASES = zip(*_TESTS_NO_HSDATTRIB) |
| 55 | + |
| 56 | + |
| 57 | +# Tests with HSD attribute recording |
| 58 | +# The input string should be formatted the same way as it comes out from the formatter since |
| 59 | +# these tests are also used to test backwards direction (dictionary -> string). |
| 60 | +_TESTS_HSDATTRIB = [ |
| 61 | + ( |
| 62 | + "Simple", ( |
| 63 | + "Test {}\n", |
| 64 | + {"Test.hsdattrib": {_HSD_LINE: 0, _HSD_EQUAL: False}, "Test": {}} |
| 65 | + ) |
| 66 | + ), |
| 67 | + ( |
| 68 | + "Data with quoted strings", ( |
| 69 | + "O = SelectedShells {\n \"s\" \"p\"\n}\n", |
| 70 | + { |
| 71 | + "O.hsdattrib": {_HSD_EQUAL: True, _HSD_LINE: 0}, |
| 72 | + "O": { |
| 73 | + "SelectedShells.hsdattrib": {_HSD_LINE: 0, _HSD_EQUAL: False}, |
| 74 | + "SelectedShells": ['"s"', '"p"'] |
| 75 | + } |
| 76 | + } |
| 77 | + ) |
| 78 | + ), |
| 79 | + ( |
| 80 | + "Duplicate node", ( |
| 81 | + "a {\n b = 1\n}\na {\n b = 2\n}\n", |
| 82 | + { |
| 83 | + "a.hsdattrib": [{_HSD_LINE: 0, _HSD_EQUAL: False}, |
| 84 | + {_HSD_LINE: 3, _HSD_EQUAL: False}], |
| 85 | + "a.attrib": [None, None], |
| 86 | + "a": [ |
| 87 | + {"b.hsdattrib": {_HSD_LINE: 1, _HSD_EQUAL: True}, "b": 1}, |
| 88 | + {"b.hsdattrib": {_HSD_LINE: 4, _HSD_EQUAL: True}, "b": 2} |
| 89 | + ] |
| 90 | + }, |
| 91 | + ) |
| 92 | + ), |
| 93 | + ( |
| 94 | + "Duplicate value", ( |
| 95 | + "a = 1\na = 2\n", |
| 96 | + { |
| 97 | + "a.hsdattrib": [{_HSD_LINE: 0, _HSD_EQUAL: True}, {_HSD_LINE: 1, _HSD_EQUAL: True}], |
| 98 | + "a.attrib": [None, None], |
| 99 | + "a": [{None: 1}, {None: 2}] |
| 100 | + }, |
| 101 | + ) |
| 102 | + ), |
| 103 | + ( |
| 104 | + "Triple value with attrib", ( |
| 105 | + "a = 1\na = 2\na [someunit] {\n 3\n}\n", |
| 106 | + { |
| 107 | + "a.hsdattrib": [{_HSD_LINE: 0, _HSD_EQUAL: True}, {_HSD_LINE: 1, _HSD_EQUAL: True}, |
| 108 | + {_HSD_LINE: 2, _HSD_EQUAL: False}], |
| 109 | + "a.attrib": [None, None, "someunit"], |
| 110 | + "a": [{None: 1}, {None: 2}, {None: 3}] |
| 111 | + }, |
| 112 | + ) |
| 113 | + ), |
| 114 | + |
| 115 | +] |
| 116 | +_TESTS_HSDATTRIB_NAMES, _TESTS_HSDATTRIB_CASES = zip(*_TESTS_HSDATTRIB) |
| 117 | + |
| 118 | + |
| 119 | +# Tests with HSD attribute recording and tag name lowering switched on |
| 120 | +# The input string should be formatted the same way as it comes out from the formatter since |
| 121 | +# these tests are also used to test backwards direction (dictionary -> string). |
| 122 | +_TESTS_HSDATTRIB_LOWER = [ |
| 123 | + ( |
| 124 | + "Simple", ( |
| 125 | + "Test {}\n", |
| 126 | + {"test.hsdattrib": {_HSD_NAME: "Test", _HSD_LINE: 0, _HSD_EQUAL: False}, "test": {}} |
| 127 | + ) |
| 128 | + ), |
| 129 | +] |
| 130 | +_TESTS_HSDATTRIB_LOWER_NAMES, _TESTS_HSDATTRIB_LOWER_CASES = zip(*_TESTS_HSDATTRIB_LOWER) |
| 131 | + |
| 132 | + |
| 133 | +@pytest.mark.parametrize( |
| 134 | + "hsdstr,hsddict", |
| 135 | + _TESTS_NO_HSDATTRIB_CASES, |
| 136 | + ids=_TESTS_NO_HSDATTRIB_NAMES |
| 137 | +) |
| 138 | +def test_builder_nohsdattr(hsdstr, hsddict): |
| 139 | + """Test transformation from hsd to dictionary without HSD attributes.""" |
| 140 | + dictbuilder = hsd.HsdDictBuilder(include_hsd_attribs=False) |
| 141 | + parser = hsd.HsdParser(eventhandler=dictbuilder) |
| 142 | + fobj = io.StringIO(hsdstr) |
| 143 | + parser.parse(fobj) |
| 144 | + assert dictbuilder.hsddict == hsddict |
| 145 | + |
| 146 | + |
| 147 | +@pytest.mark.parametrize( |
| 148 | + "hsdstr,hsddict", |
| 149 | + _TESTS_HSDATTRIB_CASES, |
| 150 | + ids=_TESTS_HSDATTRIB_NAMES |
| 151 | +) |
| 152 | +def test_builder_hsdattr(hsdstr, hsddict): |
| 153 | + """Test transformation from hsd to dictionary with HSD attributes.""" |
| 154 | + dictbuilder = hsd.HsdDictBuilder(include_hsd_attribs=True) |
| 155 | + parser = hsd.HsdParser(eventhandler=dictbuilder) |
| 156 | + fobj = io.StringIO(hsdstr) |
| 157 | + parser.parse(fobj) |
| 158 | + assert dictbuilder.hsddict == hsddict |
| 159 | + |
| 160 | + |
| 161 | +@pytest.mark.parametrize( |
| 162 | + "hsdstr,hsddict", |
| 163 | + _TESTS_HSDATTRIB_LOWER_CASES, |
| 164 | + ids=_TESTS_HSDATTRIB_LOWER_NAMES |
| 165 | +) |
| 166 | +def test_builder_hsdattr_lower(hsdstr, hsddict): |
| 167 | + """Test transformation from hsd to dictionary with HSD attributes and case lowering.""" |
| 168 | + dictbuilder = hsd.HsdDictBuilder(include_hsd_attribs=True) |
| 169 | + parser = hsd.HsdParser(eventhandler=dictbuilder, lower_tag_names=True) |
| 170 | + fobj = io.StringIO(hsdstr) |
| 171 | + parser.parse(fobj) |
| 172 | + assert dictbuilder.hsddict == hsddict |
| 173 | + |
| 174 | + |
| 175 | +@pytest.mark.parametrize( |
| 176 | + "hsdstr,hsddict", |
| 177 | + _TESTS_HSDATTRIB_CASES, |
| 178 | + ids=_TESTS_HSDATTRIB_NAMES |
| 179 | +) |
| 180 | +def test_walker_hsdattr(hsdstr, hsddict): |
| 181 | + """Test transformation from dictionary to string using HSD attributes.""" |
| 182 | + output = io.StringIO() |
| 183 | + formatter = hsd.HsdFormatter(output, use_hsd_attribs=True) |
| 184 | + dictwalker = hsd.HsdDictWalker(formatter) |
| 185 | + dictwalker.walk(hsddict) |
| 186 | + assert output.getvalue() == hsdstr |
| 187 | + |
| 188 | + |
| 189 | +@pytest.mark.parametrize( |
| 190 | + "hsdstr,hsddict", |
| 191 | + _TESTS_HSDATTRIB_LOWER_CASES, |
| 192 | + ids=_TESTS_HSDATTRIB_LOWER_NAMES |
| 193 | +) |
| 194 | +def test_walker_hsdattr_lower(hsdstr, hsddict): |
| 195 | + """Test transformation from dictionary to string using HSD attributes.""" |
| 196 | + output = io.StringIO() |
| 197 | + formatter = hsd.HsdFormatter(output, use_hsd_attribs=True) |
| 198 | + dictwalker = hsd.HsdDictWalker(formatter) |
| 199 | + dictwalker.walk(hsddict) |
| 200 | + assert output.getvalue() == hsdstr |
0 commit comments