Example #1
0
def test_eval_nested():
    script = "upper [reverse foo]"
    bindings = [
        command_binding("upper", lambda s: s.upper()),
        command_binding("reverse", lambda s: s[::-1]),
    ]
    result = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "OOF"
Example #2
0
def test_eval_nested():
    script = "upper [reverse foo]"
    bindings = [
        command_binding("upper", lambda s: s.upper()),
        command_binding("reverse", lambda s: s[::-1]),
    ]
    result, command = interpret(new_context(bindings), script)
    assert result == "OOF"
    assert command == "upper"
Example #3
0
def test_eval_contextual_propagates_context():
    script = "ctxaware"
    context = {"a": "foo"}
    bindings = [command_binding("ctxaware", lambda context: context["a"], contextual=True)]
    result, command = interpret(bindings, context, script)
    assert result == "foo"
    assert command == "ctxaware"
Example #4
0
def test_eval_contextual_means_extra_arg():
    script = "ctxaware foo"
    bindings = [
        command_binding("ctxaware", lambda _context, arg: arg, contextual=True)
    ]
    result, command = interpret(new_context(bindings), script)
    assert result == "foo"
    assert command == "ctxaware"
Example #5
0
 def register_command(self,
                      name,
                      fn,
                      varargs=False,
                      contextual=False,
                      source=None):
     self.bindings.append(
         command_binding(name, fn, varargs, contextual, source))
Example #6
0
def test_eval_contextual_regex_command():
    script = "grooooovy"
    bindings = [
        command_binding(re.compile("^groo+vy$"),
                        lambda context: context.command_name,
                        contextual=True)
    ]
    result, command = interpret(new_context(bindings), script)
    assert result == "grooooovy"
    assert command == "/groo+vy/"
Example #7
0
def test_eval_contextual_adds_command_name():
    script = "ctxaware"
    bindings = [
        command_binding("ctxaware",
                        lambda context: context.command_name,
                        contextual=True)
    ]
    result, command = interpret(new_context(bindings), script)
    assert result == "ctxaware"
    assert command == "ctxaware"
Example #8
0
def test_eval_contextual_propagates_metadata():
    script = "ctxaware"
    metadata = {"a": "foo"}
    bindings = [
        command_binding("ctxaware",
                        lambda context: context.metadata["a"],
                        contextual=True)
    ]
    result, command = interpret(new_context(bindings, metadata), script)
    assert result == "foo"
    assert command == "ctxaware"
Example #9
0
 def register_command(self,
                      name,
                      fn,
                      varargs=False,
                      regex=False,
                      contextual=False,
                      parseoutput=False):
     if regex:
         name = re.compile("^" + name + "$")
     self.bindings.append(
         command_binding(name, fn, varargs, regex, contextual, parseoutput))
Example #10
0
def test_eval_text_varargs_with_extra_whitespace():
    script = "echo   one   two   three   "
    bindings = [command_binding("echo", lambda text: text, varargs=True)]
    result, command = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "one two three"
    assert command == "echo"
Example #11
0
def test_eval_text_varargs_with_no_args():
    script = "echo"
    bindings = [command_binding("echo", lambda text: text, varargs=True)]
    with pytest.raises(ApplyError):
        interpret(bindings, EMPTY_CONTEXT, script)
Example #12
0
def test_eval_too_many_args():
    script = "cat3 one two three four"
    bindings = [command_binding("cat3", lambda x, y, z: x + y + z)]
    with pytest.raises(ApplyError):
        interpret(bindings, EMPTY_CONTEXT, script)
Example #13
0
def test_eval_optional_arg_unfilled():
    script = "maybe foo"
    bindings = [command_binding("maybe", lambda x="bar": x)]
    result, command = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "foo"
    assert command == "maybe"
Example #14
0
def test_eval_contextual_regex_command():
    script = "grooooovy"
    bindings = [command_binding(re.compile("^groo+vy$"), lambda context: context["command"], regex=True, contextual=True)]
    result, command = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "grooooovy"
    assert command == "/groo+vy/"
Example #15
0
def test_eval_multple_args():
    script = "cat3 one two three"
    bindings = [command_binding("cat3", lambda x, y, z: x + y + z)]
    result, command = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "onetwothree"
    assert command == "cat3"
Example #16
0
def test_eval_too_few_args():
    script = "cat3 one two"
    bindings = [command_binding("cat3", lambda x, y, z: x + y + z)]
    with pytest.raises(ApplyError):
        interpret(new_context(bindings), script)
Example #17
0
def test_eval_regex_command():
    script = "grooooovy"
    bindings = [command_binding(re.compile("^groo+vy$"), lambda: "foo", regex=True)]
    result = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "foo"
Example #18
0
def test_eval_simple():
    script = "upper foo"
    bindings = [command_binding("upper", lambda s: s.upper())]
    result, command = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "FOO"
    assert command == "upper"
Example #19
0
def test_eval_python_varargs_with_no_args():
    script = "list"
    bindings = [command_binding("list", lambda *words: ",".join(words))]
    result, command = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == ""
    assert command == "list"
Example #20
0
def test_eval_optional_arg_filled():
    script = "maybe foo"
    bindings = [command_binding("maybe", lambda x="bar": x)]
    result, command = interpret(new_context(bindings), script)
    assert result == "foo"
    assert command == "maybe"
Example #21
0
def test_eval_python_varargs():
    script = "list one two three"
    bindings = [command_binding("list", lambda *words: ",".join(words))]
    result = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "one,two,three"
Example #22
0
def test_eval_text_varargs():
    script = "echo one two three"
    bindings = [command_binding("echo", lambda text: text, varargs=True)]
    result, command = interpret(new_context(bindings), script)
    assert result == "one two three"
    assert command == "echo"
Example #23
0
def test_eval_text_varargs_with_no_args_and_default():
    script = "echo"
    bindings = [command_binding("echo", lambda text="": text, varargs=True)]
    result, command = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == ""
    assert command == "echo"
Example #24
0
def test_eval_contextual_adds_command_name():
    script = "ctxaware"
    bindings = [command_binding("ctxaware", lambda context: context["command"], contextual=True)]
    result, command = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "ctxaware"
    assert command == "ctxaware"
Example #25
0
def test_eval_python_varargs():
    script = "list one two three"
    bindings = [command_binding("list", lambda *words: ",".join(words))]
    result, command = interpret(new_context(bindings), script)
    assert result == "one,two,three"
    assert command == "list"
Example #26
0
def test_eval_contextual_means_extra_arg():
    script = "ctxaware foo"
    bindings = [command_binding("ctxaware", lambda _context, arg: arg, contextual=True)]
    result = interpret(bindings, EMPTY_CONTEXT, script)
    assert result == "foo"