Beispiel #1
0
def test_CommandController_load_options():
    def test(hist, expect):
        with replace_history() as history:
            ctl = FakeController()
            eq_(ctl.history, history)
            if hist:
                history.append(hist)
            ctl.load_options()
            eq_(ctl.options.__dict__["_target"], expect)

    yield test, None, Options(value=None)
    yield test, "123", Options(value=123)
Beispiel #2
0
def test_save_options():
    def test(options, hist, command=dummy_command):
        with replace_history() as history:
            mod.save_options(options, command, history)
            eq_(next(iter(history), None), hist)

    yield test, Options(value=123), "123"
    yield test, Options(value=None), "abc"

    @mod.command(arg_parser=CommandParser(Int("value", default=42)))
    def xyz(textview, sender, options):
        pass

    yield test, Options(value=345), "xyz 345", xyz
    yield test, Options(value=42), "xyz", xyz
Beispiel #3
0
def test_TextCommandController_lookup_full_command():
    def test(c):
        m = Mocker()
        menu = m.mock(ak.NSMenu)
        ctl = TextCommandController("<history>")
        for command in c.commands:
            ctl.add_command(command, None, menu)
            menu.insertItem_atIndex_(ANY, ANY)
        eq_(ctl.lookup_full_command(c.lookup), c.result)

    @command(name="cm")
    def cmd(*args):
        pass

    @command(arg_parser=CommandParser(Int("value")),
             lookup_with_arg_parser=True)
    def num(*args):
        pass

    c = TestConfig(commands=[], lookup='cmd', result=(None, None))
    yield test, c
    yield test, c(commands=[cmd])
    yield test, c(commands=[num])
    yield test, c(commands=[num],
                  lookup='123',
                  result=(num, Options(value=123)))
Beispiel #4
0
def test_CommandController_save_options():
    with replace_history() as history:
        eq_(next(iter(history), None), None)
        slc = FakeController()
        slc.options = Options(value=42)
        slc.save_options()
        eq_(next(iter(history)), "42")
Beispiel #5
0
 def check(err):
     eq_(
         str(err), "invalid arguments: num x\n"
         "invalid literal for int() with base 10: 'x'")
     eq_(err.options, Options(var=None))
     eq_(err.errors, [
         ParseError("invalid literal for int() with base 10: 'x'",
                    Int("num"), 4, 5)
     ])
Beispiel #6
0
def test_SubParser():
    sub = SubArgs("val", Int("num"), abc="xyz")
    su2 = SubArgs("str", Choice(('yes', True), ('no', False)), abc="mno")
    su3 = SubArgs("stx", VarArgs("args", placeholder="..."), abc="pqr")
    arg = SubParser("var", sub, su2, su3)
    eq_(str(arg), 'var')
    eq_(
        repr(arg), "SubParser('var', SubArgs('val', Int('num'), abc='xyz'), "
        "SubArgs('str', Choice(('yes', True), ('no', False)), abc='mno'), "
        "SubArgs('stx', VarArgs('args', placeholder='...'), abc='pqr'))")

    test = make_completions_checker(arg)
    yield test, "", (["str", "stx", "val"], 0)
    yield test, "v", (["val"], 1)
    yield test, "v ", ([], 2)
    yield test, "val", (["val"], 3)
    yield test, "val ", ([], 4)
    yield test, "val v", (None, 4)
    yield test, "st", (["str", "stx"], 2)
    yield test, "str ", (["yes", "no"], 4)
    yield test, "str y", (["yes"], 5)

    test = make_placeholder_checker(arg)
    yield test, "", 0, ("var ...", 0)
    yield test, "v", 0, ("al num", 1)
    yield test, "v ", 0, ("num", 2)
    yield test, "val", 0, (" num", 3)
    yield test, "val ", 0, ("num", 4)
    yield test, "val 1", 0, ("", 5)
    yield test, "val x", 0, (None, None)
    yield test, "s", 0, ("...", 1)
    yield test, "s ", 0, (None, None)
    yield test, "st", 0, ("...", 2)
    yield test, "str", 0, (" yes", 3)
    yield test, "str ", 0, ("yes", 4)
    yield test, "str y", 0, ("es", 5)
    yield test, "str yes", 0, ("", 7)
    yield test, "str n", 0, ("o", 5)
    yield test, "str x", 0, (None, None)
    yield test, "str x ", 0, (None, None)

    test = make_type_checker(arg)
    yield test, '', 0, (None, 0)
    yield test, 'x', 0, ParseError("'x' does not match any of: str, stx, val",
                                   arg, 0, 1)
    yield test, 'v 1', 0, ((sub, Options(num=1)), 3)
    yield test, 'val 1', 0, ((sub, Options(num=1)), 5)
    yield test, 'val 1 2', 0, ((sub, Options(num=1)), 6)
    yield test, 'val x 2', 0, ArgumentError(
        "invalid arguments: val x 2", Options(num=None), [
            ParseError("invalid literal for int() with base 10: 'x'",
                       Int("num"), 4, 6)
        ], 4)

    test = make_arg_string_checker(arg)
    yield test, (sub, Options(num=1)), "val 1"
    yield test, (su2, Options(yes=True)), "str "
    yield test, (su2, Options(yes=False)), "str no"
Beispiel #7
0
def test_command_decorator_with_args():
    cmd = dummy_command
    assert cmd.is_text_command
    eq_(cmd.title, 'Title')
    eq_(cmd.hotkey, (',', 0))
    eq_(cmd.name, 'abc')
    eq_(cmd.is_enabled(None, None), False)
    with assert_raises(ArgumentError):
        cmd.arg_parser.parse('abc def')
    eq_(cmd.arg_parser.parse('42'), Options(value=42))
    eq_(cmd.lookup_with_arg_parser, True)
Beispiel #8
0
def test_command_decorator_defaults():
    @mod.command
    def cmd(textview, sender, args):
        pass

    assert cmd.is_text_command
    eq_(cmd.title, None)
    eq_(cmd.hotkey, None)
    eq_(cmd.name, 'cmd')
    eq_(cmd.is_enabled(None, None), True)
    eq_(cmd.arg_parser.parse('abc def'), Options(args=['abc', 'def']))
    eq_(cmd.lookup_with_arg_parser, False)
Beispiel #9
0
def test_CommandParser_order():
    def test(text, result):
        if isinstance(result, Options):
            eq_(parser.parse(text), result)
        else:
            assert_raises(result, parser.parse, text)

    parser = CommandParser(
        Choice(('selection', True), ('all', False)),
        Choice(('forward', False), ('reverse', True), name='reverse'),
    )
    tt = Options(selection=True, reverse=True)
    yield test, 'selection reverse', tt
    yield test, 'sel rev', tt
    yield test, 'rev sel', ArgumentError
    yield test, 'r s', ArgumentError
    yield test, 's r', tt
    yield test, 'rev', tt
    yield test, 'sel', Options(selection=True, reverse=False)
    yield test, 'r', tt
    yield test, 's', Options(selection=True, reverse=False)
Beispiel #10
0
class WrapLinesController(SheetController):
    """Window controller for sort lines text command"""

    COMMAND = wrap_lines
    NIB_NAME = "WrapLines"
    OPTIONS_FACTORY = lambda self: Options(
        wrap_column=const.DEFAULT_RIGHT_MARGIN,
        indent=True,
    )

    @objc_delegate
    def wrap_(self, sender):
        wrap_selected_lines(self.textview, self.options)
        self.save_options()
        self.cancel_(sender)
Beispiel #11
0
def test_CommandParser_arg_string():
    parser = CommandParser(yesno, Choice('arg', 'all'))

    def test(options, argstr):
        if isinstance(argstr, Exception):

            def check(err):
                eq_(err, argstr)

            with assert_raises(type(argstr), msg=check):
                parser.arg_string(options)
        else:
            result = parser.arg_string(options)
            eq_(result, argstr)

    yield test, Options(yes=True, arg="arg"), ""
    yield test, Options(yes=False, arg="arg"), "no"
    yield test, Options(yes=True, arg="all"), " all"
    yield test, Options(yes=False, arg="all"), "no all"
    yield test, Options(), Error("missing option: yes")
    yield test, Options(yes=True), Error("missing option: arg")
    yield test, Options(yes=None), Error("invalid value: yes=None")
Beispiel #12
0
def wrap_at_margin(textview, sender, args):
    opts = Options()
    opts.wrap_column = const.DEFAULT_RIGHT_MARGIN
    opts.indent = args.indent if args is not None else True
    wrap_selected_lines(textview, opts)
Beispiel #13
0
def wrap_at_margin(editor, args):
    opts = Options()
    opts.wrap_column = const.DEFAULT_WRAP_COLUMN
    opts.indent = args.indent if args is not None else True
    wrap_selected_lines(editor, opts)
Beispiel #14
0
def wrap_at_margin(editor, args):
    opts = Options()
    opts.wrap_column = const.DEFAULT_RIGHT_MARGIN
    opts.indent = args.indent if args is not None else True
    wrap_selected_lines(editor.text_view, opts)
Beispiel #15
0
 def check(err):
     eq_(err.options, Options(arg="arg"))
     eq_(err.errors, [
         ParseError("'a' is ambiguous: arg, all", Choice('arg', 'all'), 0,
                    1)
     ])
Beispiel #16
0
def test_CommandParser_empty():
    eq_(arg_parser.parse(''), Options(yes=True))
Beispiel #17
0
 def test(argstr=None, value=None):
     with replace_history() as history:
         if argstr:
             history.append(argstr)
         options = mod.load_options(dummy_command, history)
         eq_(options, Options(value=value))
Beispiel #18
0
def test_CommandParser():
    def test_parser(argstr, options, parser):
        if isinstance(options, Exception):

            def check(err):
                eq_(err, options)

            with assert_raises(type(options), msg=check):
                parser.parse(argstr)
        else:
            opts = parser.default_options()
            opts.__dict__.update(options)
            eq_(parser.parse(argstr), opts)

    test = partial(test_parser, parser=CommandParser(yesno))
    yield test, "", Options(yes=True)
    yield test, "no", Options(yes=False)

    manual = SubArgs("manual", Int("bass", default=50),
                     Int("treble", default=50))
    preset = SubArgs("preset", Choice("flat", "rock", "cinema", name="value"))
    level = Choice(("off", 0), ('high', 4), ("medium", 2), ('low', 1),
                   name="level")
    radio_parser = CommandParser(
        SubParser(
            "equalizer",
            manual,
            preset,
        ),
        level,
        Int("volume", default=50),  #, min=0, max=100),
        String("name"),  #, quoted=True),
    )
    test = partial(test_parser, parser=radio_parser)
    yield test, "manual", Options(equalizer=(manual,
                                             Options(bass=50, treble=50)))
    yield test, "", Options()
    yield test, "preset rock low", Options(level=1,
                                           equalizer=(preset,
                                                      Options(value="rock")))
    yield test, "  high", Options(level=0, name="high")
    yield test, " high", Options(level=4)
    yield test, "high", Options(level=4)
    yield test, "hi", Options(level=4)
    yield test, "high '' yes", ArgumentError(
        'unexpected argument(s): yes',
        Options(volume=50, equalizer=None, name='', level=4), [], 8)

    def test_placeholder(argstr, expected, parser=radio_parser):
        eq_(parser.get_placeholder(argstr), expected)

    test = test_placeholder
    yield test, "", "equalizer ... off 50 name"
    yield test, "  ", "50 name"
    yield test, "  5", " name"
    yield test, "  5 ", "name"
    yield test, "  high", ""
    yield test, " hi", "gh 50 name"
    yield test, " high", " 50 name"
    yield test, "hi", "gh 50 name"
    yield test, "high ", "50 name"

    def make_completions_checker(argstr, expected, parser=radio_parser):
        eq_(parser.get_completions(argstr), expected)

    test = make_completions_checker
    yield test, "", ['manual', 'preset']
    yield test, "  ", []
    yield test, "  5", []
    yield test, "  5 ", []
    yield test, "  high", []
    yield test, " ", ["off", "high", "medium", "low"]
    yield test, " hi", ["high"]

    parser = CommandParser(level, Int("value"),
                           Choice("highlander", "tundra", "4runner"))
    test = partial(make_completions_checker, parser=parser)
    yield test, "h", ["high"]
    yield test, "t", ["tundra"]
    yield test, "high", ["high"]
    yield test, "high ", []
    yield test, "high 4", []
    yield test, "high x", None  # ??? None indicates an error (the last token could not be consumed) ???
    yield test, "high  4", ["4runner"]