Ejemplo n.º 1
0
    def test1(self):
        config = {
            'PathVars': {
                'foo': 'yep [bar]',
                'bar': 'result2',
                'nest1': 'asdf [foo]',
            }
        }
        Container.bind('Config').toSingle(Config, [config])

        Container.bind('VarManager').toSingle(VarManager)

        pathMgr = Container.resolve('VarManager')

        assertThat(pathMgr.hasKey('foo'))
        assertThat(not pathMgr.hasKey('asdf'))
        assertThat(pathMgr.tryGet('bobsdf') == None)
        assertThat(
            pathMgr.expand('before [bar] after') == 'before result2 after')
        assertThat(
            pathMgr.expand('before [foo] after') == 'before yep result2 after')

        assertThat(not pathMgr.hasKey('qux'))
        pathMgr.add('qux', 'sadf')
        assertThat(pathMgr.hasKey('qux'))
        assertThat(pathMgr.expand('[qux]') == 'sadf')

        assertThat(pathMgr.expand('[nest1]') == 'asdf yep result2')

        print('Done')
Ejemplo n.º 2
0
    def _request(self):
        if not Container.hasBinding(self._identifier):
            return self._default

        obj = Container.resolve(self._identifier)
        self._assertion(obj)
        return obj
Ejemplo n.º 3
0
    def testMultiple(self):

        Container.bind('Config').toSingle(Config, loadYamlFilesThatExist(ScriptDir + '/ExampleConfig.yaml', ScriptDir + '/ExampleConfig2.yaml'))

        config = Container.resolve('Config')

        # From 1
        assertIsEqual(config.getString('receipt'), 'Oz-Ware Purchase Invoice')

        # From 2
        assertIsEqual(config.getString('thing1'), 'Foo')

        # Second one should override
        assertIsEqual(config.getString('foo2'), 'ipsum')

        assertIsEqual(config.getString('nest1', 'firstName'), 'Dorothy')

        # Test concatenating lists together
        assertIsEqual(config.getList('list1'), ['lorem', 'ipsum', 'asdf', 'joe', 'frank'])

        # Test concatenating dictionaries together
        assertIsEqual(config.getDictionary('dict1'), {'joe': 5, 'mary': 15, 'kate': 5, 'jim': 10})

        assertIsEqual(config.tryGetDictionary(None, 'asdfasdfasdf'), None)
        assertIsEqual(config.tryGetDictionary({ 5: 1 }, 'asdfasdfasdf'), { 5: 1 })

        assertIsEqual(config.tryGetList(None, 'asdfasdfasdf'), None)
        assertIsEqual(config.tryGetList([1, 2], 'asdfasdfasdf'), [1, 2])

        assertIsEqual(config.tryGetBool(False, 'zxvzasdfasdfasdf'), False)
        assertIsEqual(config.tryGetBool(True, 'zxvzasdfasdfasdf'), True)

        assertIsEqual(config.tryGetString('asdf', 'zxvzasdfasdfasdf'), 'asdf')

        assertIsEqual(config.tryGetInt(5, 'zxvzasdfasdfasdf'), 5)
Ejemplo n.º 4
0
    def _request(self):
        if not Container.hasBinding(self._identifier):
            return self._default

        obj = Container.resolve(self._identifier)
        self._assertion(obj)
        return obj
Ejemplo n.º 5
0
    def test1(self):
        config = {
            'PathVars': {
                'foo': 'yep [bar]',
                'bar': 'result2',
                'nest1': 'asdf [foo]',
            }
        }
        Container.bind('Config').toSingle(Config, [config])

        Container.bind('VarManager').toSingle(VarManager)

        pathMgr = Container.resolve('VarManager')

        assertThat(pathMgr.hasKey('foo'))
        assertThat(not pathMgr.hasKey('asdf'))
        assertThat(pathMgr.tryGet('bobsdf') == None)
        assertThat(pathMgr.expand('before [bar] after') == 'before result2 after')
        assertThat(pathMgr.expand('before [foo] after') == 'before yep result2 after')

        assertThat(not pathMgr.hasKey('qux'))
        pathMgr.add('qux', 'sadf')
        assertThat(pathMgr.hasKey('qux'))
        assertThat(pathMgr.expand('[qux]') == 'sadf')

        assertThat(pathMgr.expand('[nest1]') == 'asdf yep result2')

        print('Done')
Ejemplo n.º 6
0
    def testUseDefault(self):
        Container.bind('Foo').to(1)
        Container.bind('Test1').toSingle(Test1)

        test1 = Container.resolve('Test1')

        self.assertEqual(test1.foo, 1)
        self.assertEqual(test1.bar, 5)
Ejemplo n.º 7
0
    def testUseDefault(self):
        Container.bind('Foo').to(1)
        Container.bind('Test1').toSingle(Test1)

        test1 = Container.resolve('Test1')

        self.assertEqual(test1.foo, 1)
        self.assertEqual(test1.bar, 5)
Ejemplo n.º 8
0
    def test1(self):
        assertThat(False, "TODO")
        #Container.bind('Config').toSingle(ConfigXml)
        Container.bind('VarManager').toSingle(VarManager)
        Container.bind('SystemHelper').toSingle(SystemHelper)
        Container.bind('Logger').toSingle(Logger)

        self._test1(Container.resolve('SystemHelper'))
Ejemplo n.º 9
0
    def test1(self):
        assertThat(False, "TODO")
        #Container.bind('Config').toSingle(ConfigXml)
        Container.bind('VarManager').toSingle(VarManager)
        Container.bind('SystemHelper').toSingle(SystemHelper)
        Container.bind('Logger').toSingle(Logger)

        self._test1(Container.resolve('SystemHelper'))
Ejemplo n.º 10
0
    def testOverrideDefault(self):
        Container.bind('Foo').to(2)
        Container.bind('Test1').to(Test1)
        Container.bind('Bar').to(3)

        test1 = Container.resolve('Test1')

        self.assertEqual(test1.foo, 2)
        self.assertEqual(test1.bar, 3)
Ejemplo n.º 11
0
    def testOverrideDefault(self):
        Container.bind('Foo').to(2)
        Container.bind('Test1').to(Test1)
        Container.bind('Bar').to(3)

        test1 = Container.resolve('Test1')

        self.assertEqual(test1.foo, 2)
        self.assertEqual(test1.bar, 3)
Ejemplo n.º 12
0
    def testSpecialChars(self):
        Container.bind('Config').toSingle(Config, loadYamlFilesThatExist(ScriptDir + '/ExampleConfig.yaml', ScriptDir + '/ExampleConfig2.yaml'))

        config = Container.resolve('Config')

        assertIsEqual(config.tryGetString(None, 'foo4'), 'asdf')

        assertIsEqual(config.tryGetString(None, 'foo5'), 'zxcv')

        assertIsEqual(config.tryGetString(None, 'foo6'), 'asdf')
        assertIsEqual(config.tryGetString(None, 'foo7'), 'zxcv')
Ejemplo n.º 13
0
    def testSimple(self):
        yamlPath = ScriptDir + '/ExampleConfig.yaml'

        Container.bind('Config').toSingle(Config, loadYamlFilesThatExist(yamlPath))

        config = Container.resolve('Config')

        assertIsEqual(config.getString('date'), '2012-08-06')
        assertIsEqual(config.getString('receipt'), 'Oz-Ware Purchase Invoice')

        assertIsEqual(config.getList('places'), ['New Jersey', 'New York'])
        assertRaisesAny(lambda: config.getString('places'))
        assertRaisesAny(lambda: config.getDictionary('places'))

        assertIsEqual(config.getDictionary('customer'),
          {'first_name': 'Dorothy', 'family_name': 'Gale'})

        # Tests YAML references
        assertIsEqual(config.getString('foo1'), config.getString('receipt'))
Ejemplo n.º 14
0
    def _logInternal(self, msg, logType, *args):

        if len(args) > 0:
            msg = msg.format(*args)

        for stream in self._streams:
            stream.log(logType, msg)


if __name__ == "__main__":
    import prj.ioc.Container as Container

    class Log1:
        def log(self, logType, msg):
            print("log 1: " + msg)

    class Log2:
        def log(self, logType, msg):
            print("log 2: " + msg)

    Container.bind("LogStream").toSingle(Log1)
    Container.bind("LogStream").toSingle(Log2)

    Container.bind("Logger").toSingle(Logger)

    logger = Container.resolve("Logger")

    logger.info("test info")
    logger.error("test error")
Ejemplo n.º 15
0
 def _request(self):
     obj = Container.resolve(self._identifier)
     self._assertion(obj)
     return obj
Ejemplo n.º 16
0
    def test1(self):
        Container.bind('Foo1').to(Foo1, 7)
        Container.bind('Foo2').to(Foo2, 6)

        foo2 = Container.resolve('Foo2')
        foo2.Start()
Ejemplo n.º 17
0
 def _request(self):
     obj = Container.resolve(self._identifier)
     self._assertion(obj)
     return obj
Ejemplo n.º 18
0
    def _logInternal(self, msg, logType, *args):

        if len(args) > 0:
            msg = msg.format(*args)

        for stream in self._streams:
            stream.log(logType, msg)


if __name__ == '__main__':
    import prj.ioc.Container as Container

    class Log1:
        def log(self, logType, msg):
            print('log 1: ' + msg)

    class Log2:
        def log(self, logType, msg):
            print('log 2: ' + msg)

    Container.bind('LogStream').toSingle(Log1)
    Container.bind('LogStream').toSingle(Log2)

    Container.bind('Logger').toSingle(Logger)

    logger = Container.resolve('Logger')

    logger.info('test info')
    logger.error('test error')