# Copyright (C) 2004 Python Software Foundation
# Author: barry@python.org (Barry Warsaw)
# License: http://www.opensource.org/licenses/PythonSoftFoundation.php

import unittest
from string.safedict import safedict, nsdict

global_string = 'global'

class _Bag:
    def __init__(self, **kws):
        self.__dict__.update(kws)

global_obj = _Bag(eno='eno', owt='owt')



class TestDictSubclasses(unittest.TestCase):
    def test_safedict_present_keys(self):
        eq = self.assertEqual
        d = safedict(one='one', two='two')
        eq(d['one'], 'one')
        eq(d['two'], 'two')
        eq(d['{one}'], 'one')
        eq(d['{two}'], 'two')

    def test_safedict_missing_keys(self):
        eq = self.assertEqual
        d = safedict(one='one', two='two')
        eq(d['three'], '$three')
        eq(d['{four}'], '${four}')

    def test_nsdict_present_keys(self):
        eq = self.assertEqual
        local_string = 'local'
        local_obj = _Bag(eerht='eerht', ruof='ruof')
        d = nsdict()
        # Simple globals and locals
        eq(d['local_string'], 'local')
        eq(d['global_string'], 'global')
        # Attribute paths on locals and globals
        eq(d['global_obj.eno'], 'eno')
        eq(d['global_obj.owt'], 'owt')
        eq(d['local_obj.eerht'], 'eerht')
        eq(d['local_obj.ruof'], 'ruof')
        # Missing attributes on present locals and globals
        eq(d['global_obj.one'], '$global_obj.one')
        eq(d['local_obj.one'], '$local_obj.one')
        eq(d['{global_obj.one}'], '${global_obj.one}')
        eq(d['{local_obj.one}'], '${local_obj.one}')
        # Missing locals and globals
        eq(d['missing'], '$missing')
        eq(d['{missing}'], '${missing}')



def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestDictSubclasses))
    return suite


if __name__ == '__main__':
    unittest.main()
