def assert_dicts_equal(self, dic, upd, ref): if not is_dict_like(upd): upd = OrderedDict(upd) self.assertTrue(all(dic[k] == upd[k] for k in upd.keys())) self.assertTrue( all(dic[k] == ref[k] for k in ref.keys() if k not in upd))
class Test_10_update_with_replace(unittest.TestCase): ac_merge = TT.MS_REPLACE dic = OrderedDict((("a", 1), ("b", [1, 3]), ("c", "abc"), ("f", None))) other = OrderedDict( (("a", 2), ("b", [0, 1]), ("c", OrderedDict( (("d", "d"), ("e", 1)))), ("d", "d"))) def assert_dicts_equal(self, dic, upd, ref): if not is_dict_like(upd): upd = OrderedDict(upd) self.assertTrue(all(dic[k] == upd[k] for k in upd.keys())) self.assertTrue( all(dic[k] == ref[k] for k in ref.keys() if k not in upd)) def assert_updated(self, other): dic = copy.deepcopy(self.dic) TT.merge(dic, other, ac_merge=self.ac_merge) self.assert_dicts_equal(dic, other, self.dic) def test_10_update_with_a_odict(self): self.assert_updated(self.other) def test_12_update_with_a_dict(self): self.assert_updated(dict(self.other)) def test_20_update_with_iterable(self): self.assert_updated(self.other.items()) def test_30_update_with_invalid(self): self.assertRaises((ValueError, TypeError), TT.merge, self.dic, 1)
def assert_dicts_equal(self, dic, upd, ref): if not is_dict_like(upd): upd = OrderedDict(upd) self.assertTrue(all(dic[k] == upd[k] for k in upd.keys())) self.assertTrue(all(dic[k] == ref[k] for k in ref.keys() if k not in upd))
def assert_dicts_equal(self, dic, upd, ref): if not is_dict_like(upd): upd = OrderedDict(upd) self.assertTrue(all(dic[k] == upd[k] for k in ["d"])) self.assertEqual(dic["c"], upd["c"]) # Overwritten. self.assertTrue( all(dic[k] == ref[k] for k in ref.keys() if k not in upd))
def test_dicts_equal(self): dic0 = {'a': 1} dic1 = OrderedDict((('a', [1, 2, 3]), ('b', OrderedDict((('c', "CCC"), ))))) dic2 = dic1.copy() dic2["b"] = None dic3 = OrderedDict((('b', OrderedDict((('c', "CCC"), ))), ('a', [1, 2, 3]))) self.assertTrue(dicts_equal({}, {})) self.assertTrue(dicts_equal(dic0, dic0)) self.assertTrue(dicts_equal(dic1, dic1)) self.assertTrue(dicts_equal(dic2, dic2)) self.assertTrue(dicts_equal(dic1, dic3)) self.assertFalse(dicts_equal(dic0, {})) self.assertFalse(dicts_equal(dic0, dic1)) self.assertFalse(dicts_equal(dic1, dic2)) self.assertFalse(dicts_equal(dic1, dic3, ordered=True))
def test_13_dump_and_single_load__namedtuple(self): if not IS_PYTHON_3: # TODO: it does not work with python3. cpath = os.path.join(self.workdir, "a.json") cnf = OrderedDict(sorted(self.cnf.items())) cnf0 = convert_to(cnf, ac_namedtuple=True) TT.dump(cnf0, cpath) self.assertTrue(os.path.exists(cpath)) cnf1 = TT.single_load(cpath, ac_namedtuple=True) self.assertTrue(cnf0 == cnf1, "\n%r ->\n%r" % (cnf0, cnf1))
def test_dicts_equal(self): dic0 = {'a': 1} dic1 = OrderedDict((('a', [1, 2, 3]), ('b', OrderedDict((('c', "CCC"), ))))) dic2 = dic1.copy() dic2["b"] = None dic3 = OrderedDict((('b', OrderedDict( (('c', "CCC"), ))), ('a', [1, 2, 3]))) self.assertTrue(dicts_equal({}, {})) self.assertTrue(dicts_equal(dic0, dic0)) self.assertTrue(dicts_equal(dic1, dic1)) self.assertTrue(dicts_equal(dic2, dic2)) self.assertTrue(dicts_equal(dic1, dic3)) self.assertFalse(dicts_equal(dic0, {})) self.assertFalse(dicts_equal(dic0, dic1)) self.assertFalse(dicts_equal(dic1, dic2)) self.assertFalse(dicts_equal(dic1, dic3, ordered=True))
a: 0 b: bbb c: - 1 - 2 - 3 sect0: §0 d: ["x", "y", "z"] sect1: <<: *sect0 e: true """ CNF = OrderedDict((("a", 0), ("b", "bbb"), ("c", [1, 2, 3]), ("sect0", OrderedDict((("d", "x y z".split()), ))), ("sect1", OrderedDict((("d", "x y z".split()), ("e", True)))))) class HasParserTrait(TBC.HasParserTrait): psr = TT.Parser() cnf = CNF cnf_s = CNF_S class Test_10(TBC.Test_10_dumps_and_loads, HasParserTrait): load_options = dict(ac_safe=True, Loader=TT.yaml.loader.Loader) dump_options = dict(ac_safe=True) empty_patterns = [('', {}), (' ', {}), ('[]', []),
# pylint: disable=ungrouped-imports from __future__ import absolute_import import anyconfig.backend.shellvars as TT import tests.backend.common as TBC from anyconfig.compat import OrderedDict CNF_S = """\ a=0 b='bbb' # a comment c="ccc" # an another comment export d='ddd' ## double comment export e="eee" ### tripple comment """ CNF = OrderedDict( (("a", "0"), ("b", "bbb"), ("c", "ccc"), ("d", "ddd"), ("e", "eee"))) class HasParserTrait(TBC.HasParserTrait): psr = TT.Parser() cnf = CNF cnf_s = CNF_S class Test_10(TBC.Test_10_dumps_and_loads, HasParserTrait): pass class Test_20(TBC.Test_20_dump_and_load, HasParserTrait):
CNF_S = """ a = 0 b = bbb c: sect0.c = x;y;z sect1.d = \\ 1,2,3 d=\\ val1,\\ val2,\\ val3 """ CNF = OrderedDict((("a", "0"), ("b", "bbb"), ("c", ""), ("sect0.c", "x;y;z"), ("sect1.d", "1,2,3"), ("d", "val1,val2,val3"))) class HasParserTrait(TBC.HasParserTrait): psr = TT.Parser() cnf = CNF cnf_s = CNF_S class Test_00(unittest.TestCase): def test_10_unescape(self): exp = "aaa:bbb" res = TT.unescape(r"aaa\:bbb") self.assertEqual(res, exp, res)
from __future__ import absolute_import import copy import os.path import unittest from os import linesep as lsep import tests.common import anyconfig.ioinfo from anyconfig.compat import OrderedDict from tests.common import to_bytes as _bytes CNF_0 = OrderedDict( (("DEFAULT", OrderedDict((("a", "0"), ("b", "bbb"), ("c", "5")))), ("sect0", OrderedDict((("a", "0"), ("b", "bbb"), ("c", "5"), ("d", "x,y,z")))))) CNF_1 = copy.deepcopy(CNF_0) CNF_1["sect0"]["d"] = CNF_1["sect0"]["d"].split() CNF_2 = OrderedDict( (("a", 0.1), ("b", _bytes("bbb")), ("sect0", OrderedDict((("c", [_bytes("x"), _bytes("y"), _bytes("z")]), ))))) class MyDict(dict): pass
# # Copyright (C) 2012 - 2018 Satoru SATOH <ssato @ redhat.com> # License: MIT # # pylint: disable=missing-docstring,invalid-name,too-few-public-methods # pylint: disable=ungrouped-imports from __future__ import absolute_import import anyconfig.backend.json.default as TT import tests.backend.common as TBC from anyconfig.compat import OrderedDict CNF_0_S = TBC.read_from_res("20-00-cnf.json") CNF_0 = OrderedDict((("a", 0), ("b", "bbb"), ("c", 5), ("sect0", OrderedDict((("d", ["x", "y", "z"]), ))))) class HasParserTrait(TBC.HasParserTrait): psr = TT.Parser() cnf_s = CNF_0_S cnf = CNF_0 class Test_10(TBC.Test_10_dumps_and_loads, HasParserTrait): load_options = dump_options = dict(parse_int=None, indent=2) empty_patterns = [('', {}), ('{}', {}), ('[]', []), ('null', None)]