def test_merge_none_over_dict_negative(self): settings = Settings({'allow_none_override': False}) p1 = Parameters(dict(key=SIMPLE), settings, '') p2 = Parameters(dict(key=None), settings, '') with self.assertRaises(TypeError): p1.merge(p2) p1.initialise_interpolation()
def test_merge_none_over_dict(self): settings = Settings({'allow_none_override': True}) p1 = Parameters(dict(key=SIMPLE), settings, '') p2 = Parameters(dict(key=None), settings, '') p1.merge(p2) p1.initialise_interpolation() self.assertEqual(p1.as_dict()['key'], None)
def test_interpolate_escaping(self): v = 'bar'.join(REFERENCE_SENTINELS) d = {'foo': ESCAPE_CHARACTER + 'bar'.join(REFERENCE_SENTINELS), 'bar': 'unused'} p = Parameters(d) p.initialise_interpolation() self.assertEqual(p.as_dict()['foo'], v)
def test_merge_scalar_over_dict(self): options = MergeOptions() options.allow_scalar_over_dict = True p = Parameters(dict(base=SIMPLE), options=options) mergee = {'base':'foo'} p.merge(Parameters(mergee)) p.initialise_interpolation() self.assertDictEqual(p.as_dict(), mergee)
def test_merge_dicts_overwrite(self): mergee = {'two':5,'four':4,'three':None,'one':(1,2,3)} p = Parameters(dict(dict=SIMPLE)) p.merge(Parameters(dict(dict=mergee))) p.initialise_interpolation() goal = SIMPLE.copy() goal.update(mergee) self.assertDictEqual(p.as_dict(), dict(dict=goal))
def test_merge_scalar_over_list(self): l = ['foo', 1, 2] options = MergeOptions() options.allow_scalar_over_list = True p1 = Parameters(dict(key=l[:2]), options=options) p1.merge(Parameters(dict(key=l[2]))) p1.initialise_interpolation() self.assertEqual(p1.as_dict()['key'], l[2])
def test_merge_list_into_scalar(self): l = ['foo', 1, 2] options = MergeOptions() options.allow_list_over_scalar = True p1 = Parameters(dict(key=l[0]), options=options) p1.merge(Parameters(dict(key=l[1:]))) p1.initialise_interpolation() self.assertListEqual(p1.as_dict()['key'], l)
def test_merge_lists(self): l1 = [1,2,3] l2 = [2,3,4] p1 = Parameters(dict(list=l1[:])) p2 = Parameters(dict(list=l2)) p1.merge(p2) p1.initialise_interpolation() self.assertListEqual(p1.as_dict()['list'], l1+l2)
def test_merge_scalars(self): p = Parameters(SIMPLE) mergee = {'five':5,'four':4,'None':None,'tuple':(1,2,3)} p.merge(mergee) p.initialise_interpolation() goal = SIMPLE.copy() goal.update(mergee) self.assertDictEqual(p.as_dict(), goal)
def test_merge_scalar_over_list(self): l = ['foo', 1, 2] settings = Settings({'allow_scalar_over_list': True}) p1 = Parameters(dict(key=l[:2]), settings, '') p2 = Parameters(dict(key=l[2]), settings, '') p1.merge(p2) p1.initialise_interpolation() self.assertEqual(p1.as_dict()['key'], l[2])
def test_merge_scalars_overwrite(self): p = Parameters(SIMPLE, SETTINGS, '') mergee = {'two': 5, 'four': 4, 'three': None, 'one': (1, 2, 3)} p.merge(mergee) p.initialise_interpolation() goal = SIMPLE.copy() goal.update(mergee) self.assertDictEqual(p.as_dict(), goal)
def test_merge_scalar_over_dict(self): settings = Settings({'allow_scalar_over_dict': True}) p = Parameters(dict(base=SIMPLE), settings, '') mergee = {'base': 'foo'} p2 = Parameters(mergee, settings, '') p.merge(p2) p.initialise_interpolation() self.assertDictEqual(p.as_dict(), mergee)
def test_merge_list_into_scalar(self): settings = Settings({'allow_list_over_scalar': True}) l = ['foo', 1, 2] p1 = Parameters(dict(key=l[0]), settings, '') p2 = Parameters(dict(key=l[1:]), settings, '') p1.merge(p2) p1.initialise_interpolation() self.assertListEqual(p1.as_dict()['key'], l)
def test_merge_dicts(self): mergee = {'five': 5, 'four': 4, 'None': None, 'tuple': (1, 2, 3)} p = Parameters(dict(dict=SIMPLE), SETTINGS, '') p2 = Parameters(dict(dict=mergee), SETTINGS, '') p.merge(p2) p.initialise_interpolation() goal = SIMPLE.copy() goal.update(mergee) self.assertDictEqual(p.as_dict(), dict(dict=goal))
def test_merge_dicts_override(self): """Validate that tilde merge overrides function properly.""" mergee = {'~one': {'a': 'alpha'}, '~two': ['gamma']} base = {'one': {'b': 'beta'}, 'two': ['delta']} goal = {'one': {'a': 'alpha'}, 'two': ['gamma']} p = Parameters(dict(dict=base), SETTINGS, '') p2 = Parameters(dict(dict=mergee), SETTINGS, '') p.merge(p2) p.initialise_interpolation() self.assertDictEqual(p.as_dict(), dict(dict=goal))
def test_interpolate_escaping(self): v = 'bar'.join(SETTINGS.reference_sentinels) d = { 'foo': SETTINGS.escape_character + 'bar'.join(SETTINGS.reference_sentinels), 'bar': 'unused' } p = Parameters(d, SETTINGS, '') p.initialise_interpolation() self.assertEqual(p.as_dict()['foo'], v)
def test_interpolate_escaping_backwards_compatibility(self): """In all following cases, escaping should not happen and the escape character needs to be printed as-is, to ensure backwards compatibility to older versions.""" v = ' '.join([ # Escape character followed by unescapable character '1', ESCAPE_CHARACTER, # Escape character followed by escape character '2', ESCAPE_CHARACTER + ESCAPE_CHARACTER, # Escape character followed by interpolation end sentinel '3', ESCAPE_CHARACTER + REFERENCE_SENTINELS[1], # Escape character at the end of the string '4', ESCAPE_CHARACTER ]) d = {'foo': v} p = Parameters(d) p.initialise_interpolation() self.assertEqual(p.as_dict()['foo'], v)
def test_interpolate_escaping_backwards_compatibility(self): """In all following cases, escaping should not happen and the escape character needs to be printed as-is, to ensure backwards compatibility to older versions.""" v = ' '.join([ # Escape character followed by unescapable character '1', SETTINGS.escape_character, # Escape character followed by escape character '2', SETTINGS.escape_character + SETTINGS.escape_character, # Escape character followed by interpolation end sentinel '3', SETTINGS.escape_character + SETTINGS.reference_sentinels[1], # Escape character at the end of the string '4', SETTINGS.escape_character ]) d = {'foo': v} p = Parameters(d, SETTINGS, '') p.initialise_interpolation() self.assertEqual(p.as_dict()['foo'], v)