コード例 #1
0
def test_qdict():
    node = autotest.add('qdict')

    d = qdict(a=1, b=2, c=3)
    class Test(qdict):
        pass

    def pickle():
        import pickle
        en = pickle.dumps(d)
        de = pickle.loads(en)
        return de == d

    def inherit():
        t = Test(x=1, y=2)
        ct = copy.deepcopy(t)
        return type(ct) == type(t)

    def merge():
        t = Test(x=1, y=2)
        empty = Test()
        empty.merge(t)
        return empty == t


    node.add('pickle', exec=pickle)
    node.add('inherit', exec=inherit)
    node.add('merge', exec=merge)
    return node
コード例 #2
0
ファイル: codegen.py プロジェクト: tornadoyi/pyplus
def test_codegen():
    node = autotest.add('codegen')

    codes = cg.Code([
        cg.pyimport('os'),
        cg.import_as('copy', 'cp'),
        cg.from_import('collections', 'defaultdict'),
        cg.from_import_as('pyplus', 'codegen', 'cg'),
        '',
        '',
        cg.pyclass('Add', [
            cg.member('__init__', ['a', 'b'], [
                'self.__a = a',
                'self.__b = b',
            ]),
            '',
            cg.member('__call__', ['*args', '**kwargs'],
                      ['return self.__a + self.__b']),
            '',
            cg.getter('a', ['return self.__a']),
            '',
            cg.setter('a', ['self.__a = v']),
            '',
        ]),
        '',
        'f = Add({x}, {y})',
        '{result} = f()',
    ])

    def format():
        nonlocal codes
        lines = codes.format(x=1, y=2, result='result').compile()
        return execute(lines2str(lines))['result'] == 3

    def replace():
        nonlocal codes
        lines = codes.replace('{x}',
                              '1').replace('{y}',
                                           '2').replace('{result}',
                                                        'result').compile()
        return execute(lines2str(lines))['result'] == 3

    node.add('format', exec=format)
    node.add('replace', exec=replace)
    return node
コード例 #3
0
def test_shell():
    node = autotest.add('shell')

    async def single():
        await shell.run('touch test.txt')
        await shell.run('echo 123 >> test.txt')
        await shell.run('echo abc >> test.txt')
        lines = await shell.run('cat test.txt')
        await shell.run('rm test.txt')
        return lines[0] == '123' and lines[1] == 'abc'

    async def multiple():
        await shell.run('mkdir test')
        cmds = ['touch test/{}.txt'.format(i) for i in range(3)]
        await shell.run_all(cmds)
        result = [os.path.isfile('test/{}.txt'.format(i)) for i in range(3)]
        await shell.run('rm -rf test')
        return all(result)

    node.add('single', exec=single())
    node.add('multiple', exec=multiple())
    return node
コード例 #4
0
def test_messager():
    node = autotest.add('messager')

    def test_notify():
        @messager
        class Sender():
            def test(self, x):
                pass

        class Receiver(object):
            def test(self, x):
                self.v = x

        v = 100
        s = Sender()
        r = Receiver()
        s.add_listener(r)
        s.test(v)
        return v == r.v

    def test_pass_messager():
        @messager(pass_messager=True)
        class Sender():
            def test(self, x):
                self.v = x

        class Receiver(object):
            def test(self, x, messager):
                self.v = messager.v

        v = 100
        s = Sender()
        r = Receiver()
        s.add_listener(r)
        s.test(v)
        return v == r.v

    def call_before_and_after():
        @messager(func=[('add', NOTIFY_AFTER_CALL),
                        ('sub', NOTIFY_BEFORE_CALL)],
                  pass_messager=True)
        class Sender():
            def __init__(self):
                self.v = 0

            def add(self, x):
                self.v += x

            def sub(self, x):
                self.v -= x

        class Receiver(object):
            def __init__(self):
                self.v = 0

            def add(self, x, messager):
                self.v += messager.v

            def sub(self, x, messager):
                self.v -= messager.v

        v = 100
        s = Sender()
        r = Receiver()
        s.add_listener(r)
        s.add(v)
        s.sub(v)
        return r.v == 0

    node.add('notify', exec=test_notify)
    node.add('pass_messager', exec=test_pass_messager)
    node.add('call_before_and_after', exec=call_before_and_after)
    return node