示例#1
0
def test_register_match():
    args = None

    @when('hit TARGET with WEAPON', verb='hit')
    def func(target, weapon, verb):
        nonlocal args
        args = [target, weapon, verb]
    _handle_command('hit dragon with glass sword')
    assert args == ['dragon', 'glass sword', 'hit']
示例#2
0
def test_register_args():
    args = None

    @when('north', dir='north')
    def func(dir):
        nonlocal args
        args = [dir]
    _handle_command('north')
    assert args == ['north']
def test_register():
    called = False

    @when('north')
    def func():
        nonlocal called
        called = True

    print(adventurelib.commands)
    _handle_command('north')
    assert called is True
示例#4
0
def test_register_context(ctx, expected):
    """The result of a command changes in different contexts."""
    cmd = None

    # Register out of order to test tie-breaking by context nesting depth
    @when('north', dir='north')
    @when('north', dir='cauliflower', context='confused.really')
    @when('north', dir='south', context='confused')
    def func(dir):
        nonlocal cmd
        cmd = dir

    with active_context(ctx):
        _handle_command('north')
    assert cmd == expected