Example #1
0
def test_signature():

    def test(directive):
        node = Node(directive)
        if VERBOSE:
            print directive,':', node.head.view

    from leonode import directive

    @directive('func')
    def func(**kwds):
        def _func(element):
            return element
        return _func

    f1 = directive('f1')(
        lambda *args, **kwds: lambda element: element)

    f2 = directive('f2')(
        lambda **kwds: lambda element: kwds)

    f3 = directive('f3')(
        lambda *args: lambda element: args)

    f4 = directive('f4')(
        lambda x: lambda element: x + id(element))

    f5 = directive('f5')(
        lambda x, y: lambda element: x+y)

    f5 = directive('f5')(
        lambda x, y, a=1: lambda element: x+y+a)

    h1 = directive('h1', 'h-expr')(
        lambda **kwds: lambda element: element.type)

    directives = [
        '@func', 
        '@f1', 
        '@f2(a=1,b=2)',
        '@f3(1,2)', 
        '@f4(1)', 
        '@f5(1,2)',
        '@h1'
    ]
    for d in directives:
        test(d)
Example #2
0
def test_person():
    "tests a class instance as directive"

    class Person(object):
        def __init__(self, name):
            self.name = name
        def __call__(self, **kwds):
            def _get_person(element):
                element.data = "Hello %s" % self.name
                return element.data
            return _get_person

    from leonode import directive
    person = Person('sam')

    # registers the instance as a directive
    # use this method until class decorators
    # become more popular
    p = directive('person', 'person-obj')(person)
    n = node1('@person says hello')
    if VERBOSE: display(n)