Example #1
0
 async def test(input, output, start=0):
     arg = await mod.Arg(field, input, 0, None)
     words = await field.get_completions(arg)
     assert all(isinstance(w, CompleteWord) for w in words), \
         repr([w for w in words if not isinstance(w, CompleteWord)])
     eq_([w.complete() for w in words], output)
     eq_([w.start for w in words], [start] * len(words), words)
Example #2
0
 def test(command, expected, selection=(0, 0), sel_after=None):
     tv = FakeTextView(TEXT, selection)
     do = CommandTester(mod.unique_lines, textview=tv)
     do(command)
     eq_(tv.text, expected, tv.text)
     if sel_after is not None:
         eq_(do.editor.selection, sel_after)
Example #3
0
 async def check_completions(argstr,
                             expected,
                             start=None,
                             parser=radio_parser):
     words = await parser.get_completions(argstr)
     eq_(words, expected)
     if start is not None:
         eq_([w.start for w in words], [start] * len(words), words)
Example #4
0
    async def type_checker_test(text, start, expect):
        if isinstance(expect, Exception):

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

            with assert_raises(type(expect), msg=check):
                await field.consume(text, start)
        else:
            eq_(await field.consume(text, start), expect)
Example #5
0
    async def test_get_argstring(value, argstr):
        if isinstance(argstr, Exception):

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

            with assert_raises(type(argstr), msg=check):
                await field.arg_string(value)
        else:
            eq_(await field.arg_string(value), argstr)
Example #6
0
    async def test(options, argstr):
        if isinstance(argstr, Exception):

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

            with assert_raises(type(argstr), msg=check):
                await parser.arg_string(options)
        else:
            result = await parser.arg_string(options)
            eq_(result, argstr)
Example #7
0
    async def test_parser(argstr, options, parser):
        if isinstance(options, Exception):

            def check(err):
                eq_(type(err), type(options))
                eq_(str(err), str(options))
                eq_(err.errors, options.errors)
                eq_(err.parse_index, options.parse_index)

            with assert_raises(type(options), msg=check):
                await parser.parse(argstr)
        else:
            opts = parser.default_options()
            opts.__dict__.update(options)
            eq_(await parser.parse(argstr), opts)
Example #8
0
def do_method_pass_through(attr, inner_obj_class, outer_obj, token, method,
        ext_args=(), int_args=None, returns=None):
    from mocker import Mocker # late import so mockerext is installed
    def inject_wc(args):
        return [(outer_obj if a is token else a) for a in args]
    if int_args is None:
        int_args = ext_args
    ext_args = inject_wc(ext_args)
    int_args = inject_wc(int_args)
    m = Mocker()
    if isinstance(method, basestring):
        method = (method, method)
    outer_method, inner_method = method
    inner_obj = m.replace(outer_obj, attr, spec=inner_obj_class)
    setattr(outer_obj, attr, inner_obj)
    getattr(inner_obj, inner_method)(*int_args) >> returns
    with m:
        rval = getattr(outer_obj, outer_method)(*ext_args)
        eq_(rval, returns)
Example #9
0
def test_Int():
    field = Int('num')
    eq_(str(field), 'num')
    eq_(repr(field), "Int('num')")

    test = make_consume_checker(field)
    yield test, '', 0, (None, 1)
    yield test, '3', 0, (3, 2)
    yield test, '42', 0, (42, 3)
    yield test, '100 99', 0, (100, 4)
    yield test, '1077 ', 1, (77, 5)
    yield test, 'a 99', 0, \
        ParseError("invalid literal for int() with base 10: 'a'", field, 0, 1)

    test = make_arg_string_checker(field)
    yield test, 42, "42"
    yield test, -42, "-42"
    yield test, None, ""
    yield test, "arg", Error("invalid value: num='arg'")
Example #10
0
    def test(c):
        opts = SortOptions(**{opt: c.opts[abbr]
            for abbr, opt in optmap if abbr in c.opts})
        m = Mocker()
        editor = m.mock()
        text = editor.text >> Text(c.text)
        if opts.selection:
            sel = editor.selection >> c.sel
            sel = text.line_range(sel)
        else:
            sel = c.sel
        output = []

        def put(text, range, select=False):
            output.append(text)
        (editor.put(ANY, sel, select=opts.selection) << True).call(put)
        with m:
            sortlines(editor, opts)
            eq_(sort_result(output[0]), c.result, output[0])
Example #11
0
def test_DynamicList():
    def get_items(editor):
        return [
            "Hammer",
            "Hammer Drill",
            "Scewer",
            "Screw Driver",
        ]

    field = mod.DynamicList('tool', get_items, lambda n: n, default="Hammer")
    eq_(str(field), 'tool')
    # eq_(repr(field), "DynamicList('tool', default='Hammer')")

    test = make_consume_checker(field)
    yield test, '', 0, (field.default, 1)
    yield test, 'a', 0, ParseError(
        "'a' does not match any of: Hammer, Hammer Drill, Scewer, Screw Driver",
        field, 0, 1)
    yield test, 'h', 0, ("Hammer", 2)
    yield test, 'H', 0, ("Hammer", 2)
    yield test, 'Ha', 0, ("Hammer", 3)
    yield test, 's', 0, ("Scewer", 2)
    yield test, 'Sc', 0, ("Scewer", 3)
    yield test, ' ', 0, ("Hammer", 1)

    test = make_completions_checker(field)
    yield test, "", [x.replace(" ", "\\ ") for x in get_items(None)]
    yield test, "a", []
    yield test, "h", ["Hammer", "Hammer\\ Drill"]
    yield test, "H", ["Hammer", "Hammer\\ Drill"]

    test = make_placeholder_checker(field)
    yield test, "", 0, ("", "Hammer")
    yield test, " ", 0, ("Hammer", "")
    yield test, "a", 0, ("", None)
    yield test, "h", 0, ("Hammer", "")
    yield test, "sc", 0, ("Scewer", "")
    yield test, "scr", 0, ("Screw Driver", "")

    test = make_arg_string_checker(field)
    yield test, "Hammer", ""
Example #12
0
def test_Float():
    field = mod.Float('num')
    eq_(str(field), 'num')
    eq_(repr(field), "Float('num')")

    test = make_consume_checker(field)
    yield test, '', 0, (None, 1)
    yield test, '3', 0, (3.0, 2)
    yield test, '3.', 0, (3.0, 3)
    yield test, '3.1', 0, (3.1, 4)
    yield test, '42', 0, (42.0, 3)
    yield test, '1.2 99', 0, (1.2, 4)
    yield test, '10.7 ', 1, (0.7, 5)
    yield test, 'a 99', 0, \
        ParseError("could not convert string to float: 'a'", field, 0, 1)

    test = make_arg_string_checker(field)
    yield test, 42.0, "42.0"
    yield test, -42.0, "-42.0"
    yield test, None, ""
    yield test, "arg", Error("invalid value: num='arg'")
Example #13
0
def test_Choice_default_first():
    field = Choice(('true on', True), ('false off', False))
    eq_(str(field), 'true')
    eq_(field.name, 'true')
    eq_(repr(field), "Choice(('true on', True), ('false off', False))")

    test = make_consume_checker(field)
    yield test, '', 0, (True, 1)
    yield test, 't', 0, (True, 2)
    yield test, 'true', 0, (True, 5)
    yield test, 'false', 0, (False, 6)
    yield test, 'f', 0, (False, 2)
    yield test, 'True', 0, \
        ParseError("'True' does not match any of: true, false", field, 0, 4)
    yield test, 'False', 0, \
        ParseError("'False' does not match any of: true, false", field, 0, 5)

    test = make_placeholder_checker(field)
    yield test, '', 0, ("", "true")
    yield test, 't', 0, ("true", "")
    yield test, 'true', 0, ("true", "")
    yield test, 'false', 0, ("false", "")
    yield test, 'f', 0, ("false", "")
    yield test, 'o', 0, ("", None)
    yield test, 'on', 0, ("on", "")
    yield test, 'of', 0, ("off", "")
Example #14
0
def test_VarArgs():
    field = VarArgs("var", Choice('arg', 'nope', 'nah'))
    eq_(str(field), 'arg ...')
    eq_(repr(field), "VarArgs('var', Choice('arg', 'nope', 'nah'))")

    test = make_completions_checker(field)
    yield test, "", ['arg', 'nope', 'nah']
    yield test, "a", ["arg"]
    yield test, "a ", ['arg', 'nope', 'nah']
    yield test, "b ", []
    yield test, "nah", ["nah"]
    yield test, "nah ", ['arg', 'nope', 'nah']
    yield test, "arg a", ['arg']
    yield test, "arg b", []

    test = make_placeholder_checker(field)
    yield test, "", 0, ("", "arg ...")
    yield test, "a", 0, ("arg", "...")
    yield test, "a ", 0, ("arg", "...")
    yield test, "a ", 2, ("", "arg ...")
    yield test, "arg", 0, ("arg", "...")
    yield test, "arg ", 0, ("arg", "...")
    yield test, "arg a", 0, ("arg arg", "...")
    yield test, "arg x", 0, ("", None)
    yield test, "x", 0, ("", None)
    yield test, "x ", 0, ("", None)

    test = make_consume_checker(field)
    yield test, '', 0, (['arg'], 1)
    yield test, 'x', 0, ParseError("'x' does not match any of: arg, nope, nah",
                                   field.field, 0, 1)
    yield test, 'a', 0, (['arg'], 2)
    yield test, 'a na no', 0, (['arg', 'nah', 'nope'], 8)

    test = make_arg_string_checker(field)
    yield test, ["arg"], ""
    yield test, ["nope"], "nope"
    yield test, ["nah", "arg", "nah"], 'nah  nah'
Example #15
0
 def check(err):
     arg = Arg(None, 'num x', 0, Options())
     eq_(
         str(err), "invalid arguments: num x\n"
         "invalid literal for int() with base 10: 'x'")
     eq_(err.options, Options(var=arg))
     eq_(err.errors, [
         ParseError("invalid literal for int() with base 10: 'x'",
                    Int("num"), 4, 5)
     ])
Example #16
0
    async def regex_test(text, start, expect, flags=0):
        if isinstance(expect, Exception):

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

            with assert_raises(type(expect), msg=check):
                await field.consume(text, start)
            return
        value = await field.consume(text, start)
        if expect[0] in [None, (None, None)]:
            eq_(value, expect)
            return
        expr, index = value
        if field.replace:
            (expr, replace) = expr
            got = ((expr, replace), index)
        else:
            got = (expr, index)
        eq_(got, expect)
        eq_(expr.flags, flags | re.UNICODE | re.MULTILINE)
Example #17
0
 async def test(text, result):
     eq_(await parser.get_placeholder(text), result)
Example #18
0
 def test(hist, opts):
     with test_app() as app:
         editor = base.Options(app=app)
         app.text_commander.history.append(hist)
         ctl = SortLinesController(editor)
         eq_(ctl.options._target, SortOptions(**opts))
Example #19
0
 async def test(input, output):
     if input.startswith("/"):
         input = tmp + "/"
     with replattr(os.path, "expanduser", expanduser):
         arg = await mod.Arg(field, input, 0, None)
         eq_(await field.get_completions(arg), output)
Example #20
0
def test_File():
    field = File('path')
    eq_(str(field), 'path')
    eq_(repr(field), "File('path')")

    with tempdir() as tmp:
        os.mkdir(join(tmp, "dir"))
        os.mkdir(join(tmp, "space dir"))
        for path in [
                "dir/a.txt",
                "dir/b.txt",
                "dir/B file",
                ".hidden",
                "file.txt",
                "file.doc",
                "space dir/file",
        ]:
            assert not isabs(path), path
            with open(join(tmp, path), "w"):
                pass

        test = make_consume_checker(field)
        yield test, "relative.txt", 0, ("relative.txt", 13)

        test = make_completions_checker(field)
        yield test, "", []

        project_path = join(tmp, "dir")
        editor = FakeEditor(join(tmp, "dir/file.txt"), project_path)
        field = await_coroutine(field.with_context(editor))

        test = make_completions_checker(field)
        yield test, ".../", ["a.txt", "B file", "b.txt"], 4
        with replattr(editor, "_project_path", project_path + "/"):
            yield test, ".../", ["a.txt", "B file", "b.txt"], 4
            yield test, "...//", ["a.txt", "B file", "b.txt"], 5
        with replattr((editor, "_project_path", None),
                      (editor, "_file_path", join(tmp, "space dir/file")),
                      sigcheck=False):
            yield test, "", ["file"], 0
            yield test, "../", ["dir/", "file.doc", "file.txt",
                                "space dir/"], 3
            # yield test, "..//", ["dir", "file.doc", "file.txt", "space dir"], 4
            yield test, "../f", ["file.doc", "file.txt"], 3
            yield test, "../dir/", ["a.txt", "B file", "b.txt"], 7

        test = make_arg_string_checker(field)
        yield test, "/str", "/str"
        yield test, "/a b", '"/a b"'
        yield test, os.path.expanduser("~/a b"), '"~/a b"'
        yield test, join(tmp, "dir/file"), "file"
        yield test, join(tmp, "dir/a b"), '"a b"'
        yield test, join(tmp, "file"), join(tmp, "file")
        yield test, "arg/", Error("not a file: path='arg/'")

        test = make_consume_checker(field)
        yield test, '', 0, (None, 1)
        yield test, 'a', 0, (join(tmp, 'dir/a'), 2)
        yield test, 'abc', 0, (join(tmp, 'dir/abc'), 4)
        yield test, 'abc ', 0, (join(tmp, 'dir/abc'), 4)
        yield test, 'file.txt', 0, (join(tmp, 'dir/file.txt'), 9)
        yield test, '../file.txt', 0, (join(tmp, 'dir/../file.txt'), 12)
        yield test, '/file.txt', 0, ('/file.txt', 10)
        yield test, '~/file.txt', 0, (os.path.expanduser('~/file.txt'), 11)
        yield test, '...', 0, (join(tmp, 'dir'), 4)
        yield test, '.../file.txt', 0, (join(tmp, 'dir/file.txt'), 13)
        yield test, '"ab c"', 0, (join(tmp, 'dir/ab c'), 6)
        yield test, "'ab c'", 0, (join(tmp, 'dir/ab c'), 6)
        yield test, "'ab c/'", 0, (join(tmp, 'dir/ab c/'), 7)

        # completions
        def expanduser(path):
            if path.startswith("~"):
                if len(path) == 1:
                    return tmp
                assert path.startswith("~/"), path
                return tmp + path[1:]
            return path

        @async_test
        async def test(input, output):
            if input.startswith("/"):
                input = tmp + "/"
            with replattr(os.path, "expanduser", expanduser):
                arg = await mod.Arg(field, input, 0, None)
                eq_(await field.get_completions(arg), output)

        yield test, "", ["a.txt", "B file", "b.txt"]
        yield test, "a", ["a.txt"]
        yield test, "a.txt", ["a.txt"]
        yield test, "b", ["B file", "b.txt"]
        yield test, "B", ["B file"]
        yield test, "..", ["../"]
        yield test, "../", ["dir/", "file.doc", "file.txt", "space dir/"]
        yield test, "../.", [".hidden"]
        yield test, "...", [".../"]
        yield test, ".../", ["a.txt", "B file", "b.txt"]
        yield test, "../dir", ["dir/"]
        yield test, "../dir/", ["a.txt", "B file", "b.txt"]
        yield test, "../sp", ["space dir/"]
        yield test, "../space\\ d", ["space dir/"]
        yield test, "../space\\ dir", ["space dir/"]
        yield test, "../space\\ dir/", ["file"]
        yield test, "val", []
        yield test, "/", ["dir/", "file.doc", "file.txt", "space dir/"]
        yield test, "~", ["~/"]
        yield test, "~/", ["dir/", "file.doc", "file.txt", "space dir/"]

        # delimiter completion
        @async_test
        async def test(input, output, start=0):
            arg = await mod.Arg(field, input, 0, None)
            words = await field.get_completions(arg)
            assert all(isinstance(w, CompleteWord) for w in words), \
                repr([w for w in words if not isinstance(w, CompleteWord)])
            eq_([w.complete() for w in words], output)
            eq_([w.start for w in words], [start] * len(words), words)

        yield test, "", ["a.txt ", "B\\ file ", "b.txt "]
        yield test, "x", []
        yield test, "..", ["../"]
        yield test, "../", ["dir/", "file.doc ", "file.txt ",
                            "space\\ dir/"], 3
        yield test, "../dir", ["dir/"], 3
        yield test, "../di", ["dir/"], 3
        yield test, "../sp", ["space\\ dir/"], 3
        yield test, "../space\\ d", ["space\\ dir/"], 3
        yield test, "../space\\ dir", ["space\\ dir/"], 3
        yield test, ".../", ["a.txt ", "B\\ file ", "b.txt "], 4
        yield test, "../space\\ dir/", ["file "], 14
        yield test, "~", ["~/"], None

        field = File('dir', directory=True)
        eq_(str(field), 'dir')
        eq_(repr(field), "File('dir', directory=True)")
        field = await_coroutine(field.with_context(editor))

        test = make_consume_checker(field)
        yield test, '', 0, (None, 1)
        yield test, 'a', 0, (join(tmp, 'dir/a'), 2)
        yield test, 'abc', 0, (join(tmp, 'dir/abc'), 4)
        yield test, 'abc ', 0, (join(tmp, 'dir/abc'), 4)
        yield test, 'abc/', 0, (join(tmp, 'dir/abc/'), 5)
        yield test, '...', 0, (join(tmp, 'dir'), 4)
        yield test, '.../abc/', 0, (join(tmp, 'dir/abc/'), 9)

        test = make_completions_checker(field)
        yield test, "", [], 0
        yield test, "a", [], 0
        yield test, "..", ["../"], 0
        yield test, "../", ["dir/", "space dir/"], 3

        test = make_arg_string_checker(field)
        yield test, "/a", "/a"
        yield test, "/a/", "/a/"
        yield test, "/dir/a", "/dir/a"
        yield test, "/dir/a/", "/dir/a/"

        field = File('dir', default="~/dir")
        check = make_completions_checker(field)

        def test(input, output, *args):
            if input.startswith("/"):
                input = tmp + "/"
            with replattr(os.path, "expanduser", expanduser):
                check(input, output, *args)

        yield test, "", [], 0

        test = make_placeholder_checker(field)
        yield test, "", 0, ("", "~/dir")
        yield test, " ", 0, ("~/dir", "")
Example #21
0
 async def test(arg, strval):
     eq_(str(await arg), strval)
Example #22
0
def test_SortLinesController_default_options():
    with test_app() as app:
        editor = base.Options(app=app)
        ctl = SortLinesController(editor)
        for name, value in SortOptions.DEFAULTS.items():
            eq_(getattr(ctl.options, name), value, name)
Example #23
0
 def check(err):
     eq_(err, expect)
Example #24
0
def test_Choice_strings():
    field = Choice('maybe yes no', name='yes')
    eq_(str(field), 'maybe')
    eq_(field.name, 'yes')
    eq_(repr(field), "Choice('maybe yes no', name='yes')")
Example #25
0
 async def test(text, result):
     if isinstance(result, Options):
         eq_(await parser.parse(text), result)
     else:
         with assert_raises(result):
             await parser.parse(text)
Example #26
0
 def test(command, expected):
     m = Mocker()
     do = CommandTester(mod.sort_lines, text=TEXT, sel=(0, 0))
     with m:
         do(command)
         eq_(sort_result(do.editor.text), expected, TEXT)
Example #27
0
def expect_beep(on=True, count=1):
    import editxt.platform.test.app as app
    before_count = app.beep_count
    yield
    eq_(app.beep_count - before_count, count if on else 0, 'beep')
Example #28
0
def test_Choice():
    field = Choice('arg-ument', 'nope', 'nah')
    eq_(str(field), 'arg-ument')
    eq_(field.name, 'arg_ument')

    test = make_consume_checker(field)
    yield test, 'arg-ument', 0, ("arg-ument", 10)
    yield test, 'arg', 0, ("arg-ument", 4)
    yield test, 'a', 0, ("arg-ument", 2)
    yield test, 'a', 1, ("arg-ument", 2)
    yield test, '', 0, ("arg-ument", 1)
    yield test, '', 3, ("arg-ument", 3)
    yield test, 'arg arg', 0, ("arg-ument", 4)
    yield test, 'nope', 0, ("nope", 5)
    yield test, 'nop', 0, ("nope", 4)
    yield test, 'no', 0, ("nope", 3)
    yield test, 'nah', 0, ("nah", 4)
    yield test, 'na', 0, ("nah", 3)

    test = make_arg_string_checker(field)
    yield test, "arg-ument", ""
    yield test, "nope", "nope"
    yield test, "nah", "nah"
    yield test, "arg", Error("invalid value: arg_ument='arg'")

    field = Choice(('arg-ument', True), ('nope', False), ('nah', ""))
    test = make_consume_checker(field)
    yield test, 'arg-ument', 0, (True, 10)
    yield test, 'arg', 0, (True, 4)
    yield test, 'a', 0, (True, 2)
    yield test, 'a', 1, (True, 2)
    yield test, '', 0, (True, 1)
    yield test, '', 3, (True, 3)
    yield test, 'arg arg', 0, (True, 4)
    yield test, 'nope', 0, (False, 5)
    yield test, 'nop', 0, (False, 4)
    yield test, 'no', 0, (False, 3)
    yield test, 'nah', 0, ("", 4)
    yield test, 'na', 0, ("", 3)
    # TODO pass arg instead of field to errors
    yield test, 'n', 0, \
        ParseError("'n' is ambiguous: nope, nah", field, 0, 2)
    yield test, 'arg', 1, \
        ParseError("'rg' does not match any of: arg-ument, nope, nah", field, 1, 3)
    yield test, 'args', 0, \
        ParseError("'args' does not match any of: arg-ument, nope, nah", field, 0, 4)
    yield test, 'args arg', 0, \
        ParseError("'args' does not match any of: arg-ument, nope, nah", field, 0, 4)

    test = make_placeholder_checker(field)
    yield test, '', 0, ("", "arg-ument")
    yield test, 'a', 0, ("arg-ument", "")
    yield test, 'n', 0, ("", None)
    yield test, 'x', 0, ("", None)

    field = Choice("argument parameter", "find search")
    test = make_consume_checker(field)
    yield test, 'a', 0, ("argument", 2)
    yield test, 'arg', 0, ("argument", 4)
    yield test, 'argument', 0, ("argument", 9)
    yield test, 'p', 0, ("argument", 2)
    yield test, 'param', 0, ("argument", 6)
    yield test, 'parameter', 0, ("argument", 10)
    yield test, 'f', 0, ("find", 2)
    yield test, 'find', 0, ("find", 5)
    yield test, 's', 0, ("find", 2)
    yield test, 'search', 0, ("find", 7)
    yield test, 'arg-ument', 0, \
        ParseError("'arg-ument' does not match any of: argument, find", field, 0, 9)

    field = Choice(("argument parameter", True), ("find search", False))
    test = make_consume_checker(field)
    yield test, 'a', 0, (True, 2)
    yield test, 'arg', 0, (True, 4)
    yield test, 'argument', 0, (True, 9)
    yield test, 'p', 0, (True, 2)
    yield test, 'param', 0, (True, 6)
    yield test, 'parameter', 0, (True, 10)
    yield test, 'f', 0, (False, 2)
    yield test, 'find', 0, (False, 5)
    yield test, 's', 0, (False, 2)
    yield test, 'search', 0, (False, 7)
    yield test, 'arg-ument', 0, \
        ParseError("'arg-ument' does not match any of: argument, find", field, 0, 9)
Example #29
0
 def test(name, ident):
     eq_(identifier(name), ident)
Example #30
0
def test_Regex():
    field = Regex('regex')
    eq_(str(field), 'regex')
    eq_(repr(field), "Regex('regex')")

    @async_test
    async def regex_test(text, start, expect, flags=0):
        if isinstance(expect, Exception):

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

            with assert_raises(type(expect), msg=check):
                await field.consume(text, start)
            return
        value = await field.consume(text, start)
        if expect[0] in [None, (None, None)]:
            eq_(value, expect)
            return
        expr, index = value
        if field.replace:
            (expr, replace) = expr
            got = ((expr, replace), index)
        else:
            got = (expr, index)
        eq_(got, expect)
        eq_(expr.flags, flags | re.UNICODE | re.MULTILINE)

    test = regex_test
    yield test, '', 0, (None, 1)
    yield test, '/abc/', 0, ('abc', 6)
    yield test, '/abc/ def', 0, ('abc', 6)
    yield test, '/abc/  def', 0, ('abc', 6)
    yield test, '/abc/i def', 0, ('abc', 7), re.I
    yield test, '/abc/is def', 0, ('abc', 8), re.I | re.S
    yield test, '/abc/is  def', 0, ('abc', 8), re.I | re.S
    yield test, 'abc', 0, ('abc', 4)
    yield test, 'abci', 0, ('abci', 5)
    yield test, '^abc$', 0, ('^abc$', 6)
    yield test, '^abc$ def', 0, ('^abc$', 6)
    yield test, '/abc/X def', 0, ParseError('unknown flag: X', field, 5, 5)

    test = make_placeholder_checker(field)
    yield test, "", 0, ("", "regex")
    yield test, "/", 0, ("//", "")
    yield test, "//", 0, ("//", "")
    # yield test, "// ", 0, None

    test = make_placeholder_checker(Regex('regex', default="1 2"))
    yield test, "", 0, ("", "/1 2/")
    yield test, " ", 0, ("/1 2/", "")

    test = make_arg_string_checker(field)
    yield test, RegexPattern("str"), "str"
    yield test, RegexPattern("str", re.I), "/str/i"
    yield test, RegexPattern("/usr/bin"), ":/usr/bin:"
    yield test, RegexPattern("/usr/bin:"), '"/usr/bin:"'
    yield test, RegexPattern('/usr/bin:"'), "'/usr/bin:\"'"
    yield test, RegexPattern('/usr/bin:\'"'), ":/usr/bin\\:'\":"
    yield test, RegexPattern(r'''//'':""'''), r'''://''\:"":'''
    yield test, RegexPattern(r'''//''\\:""'''), r'''://''\\\:"":'''
    yield test, RegexPattern(r'''://''""'''), r''':\://''"":'''
    yield test, RegexPattern(r'''\://''""'''), r'''\://''""'''
    yield test, RegexPattern(r'''\\://''""'''), r'''\\://''""'''
    # pedantic cases with three or more of all except ':'
    yield test, RegexPattern(r'''///'"'::"'"'''), r''':///'"'\:\:"'":'''
    yield test, RegexPattern(r'''///'"':\\:"'"'''), r''':///'"'\:\\\:"'":'''
    yield test, "str", Error("invalid value: regex='str'")

    field = Regex('regex', replace=True)
    eq_(repr(field), "Regex('regex', replace=True)")
    test = regex_test
    yield test, '', 0, ((None, None), 1)
    yield test, '/abc', 0, (('abc', None), 5)
    yield test, '/abc ', 0, (('abc ', None), 6)
    yield test, '/\\\\', 0, (('\\\\', None), 4)
    yield test, '/\\/', 0, (('\\/', None), 4)
    yield test, '"abc', 0, (('abc', None), 5)
    yield test, '"abc"', 0, (('abc', ''), 6)
    yield test, '"abc""', 0, (('abc', ''), 7)
    yield test, '/abc def', 0, (('abc def', None), 9)
    yield test, '/abc/def', 0, (('abc', 'def'), 9)
    yield test, '/abc/def/', 0, (('abc', 'def'), 10)
    yield test, '/abc/def/ def', 0, (('abc', 'def'), 10)
    yield test, '/abc/def/  def', 0, (('abc', 'def'), 10)
    yield test, '/abc/def/i  def', 0, (('abc', 'def'), 11), re.I
    yield test, '/abc/def/is  def', 0, (('abc', 'def'), 12), re.I | re.S
    yield test, '/(', 0, (("(", None), 3)
    yield test, 'abc', 0, \
        ParseError("invalid search pattern: 'abc'", field, 0, 0)
    yield test, 'abc def', 0, \
        ParseError("invalid search pattern: 'abc def'", field, 0, 0)
    yield test, '/abc/def/y  def', 0, \
        ParseError('unknown flag: y', field, 9, 9)

    test = make_placeholder_checker(field)
    yield test, "", 0, ("", "regex")
    yield test, "/", 0, ("///", "")
    yield test, "/x/", 0, ("/x//", "")
    yield test, "/\\//", 0, ("/\\///", "")
    yield test, "/x//", 0, ("/x//", "")

    field = Regex('regex', replace=True, default=("", ""))
    test = make_placeholder_checker(field)
    yield test, "", 0, ("", "regex")
    yield test, "/", 0, ("///", "")
    yield test, "/x/", 0, ("/x//", "")
    yield test, "/\\//", 0, ("/\\///", "")
    yield test, "/x//", 0, ("/x//", "")
    yield test, " ", 0, ("///", "")

    test = make_arg_string_checker(field)
    yield test, (RegexPattern("str"), 'abc'), "/str/abc/"
    yield test, (RegexPattern("str", re.I), 'abc'), "/str/abc/i"
    yield test, (RegexPattern("/usr/bin"), "abc"), ":/usr/bin:abc:"
    yield test, (RegexPattern("/usr/bin:"), ":"), '"/usr/bin:":"'
    yield test, (RegexPattern(r'''//''\:""'''),
                 r'''/"'\:'''), r'''://''\:"":/"'\::'''
    yield test, \
        (RegexPattern(r'''//''\:""'''), r'''/"'\\:'''), r'''://''\:"":/"'\\\::'''
    yield test, ("str", "abc"), Error("invalid value: regex=('str', 'abc')")
    yield test, ("str", 42), Error("invalid value: regex=('str', 42)")
Example #31
0
 async def test(text, args):
     eq_(await parser.parse(text), args)
Example #32
0
 async def test(text, result):
     eq_(await parser.get_completions(text), result)
Example #33
0
 async def test_placeholder(argstr, expected, parser=radio_parser):
     eq_(await parser.get_placeholder(argstr), expected)
Example #34
0
def test_String():
    field = String('str')
    eq_(str(field), 'str')
    eq_(repr(field), "String('str')")

    test = make_consume_checker(field)
    yield test, '', 0, (None, 1)
    yield test, 'a', 0, ('a', 2)
    yield test, 'abc', 0, ('abc', 4)
    yield test, 'abc def', 0, ('abc', 4)
    yield test, 'abc', 1, ('bc', 4)
    yield test, 'a"c', 0, ('a"c', 4)
    yield test, '\\"c', 0, ('"c', 4)
    yield test, 'a\\ c', 0, ('a c', 5)
    yield test, '"a c"', 0, ('a c', 5)
    yield test, "'a c'", 0, ('a c', 5)
    yield test, "'a c' ", 0, ('a c', 6)
    yield test, "'a c", 0, ('a c', 5)
    yield test, r"'a c\' '", 0, ("a c' ", 8)
    yield test, r"'a c\\' ", 0, ("a c\\", 8)
    yield test, r"'a c\"\' '", 0, ("a c\"\' ", 10)
    yield test, r"'a c\\\' '", 0, ("a c\\' ", 10)
    yield test, r"'a c\a\' '", 0, ("a c\a' ", 10)
    yield test, r"'a c\b\' '", 0, ("a c\b' ", 10)
    yield test, r"'a c\f\' '", 0, ("a c\f' ", 10)
    yield test, r"'a c\n\' '", 0, ("a c\n' ", 10)
    yield test, r"'a c\r\' '", 0, ("a c\r' ", 10)
    yield test, r"'a c\t\' '", 0, ("a c\t' ", 10)
    yield test, r"'a c\v\' '", 0, ("a c\v' ", 10)
    yield test, r"'a c\v\' ' ", 0, ("a c\v' ", 11)
    yield test, '\\', 0, ParseError("unterminated string: \\", field, 0, 1)
    yield test, '\\\\', 0, ("\\", 3)
    yield test, '\\\\\\', 0, ParseError("unterminated string: \\\\\\", field,
                                        0, 3)
    yield test, '\\\\\\\\', 0, ("\\\\", 5)
    yield test, '""', 0, ("", 2)
    yield test, '"\\"', 0, ('"', 4)
    yield test, '"\\\\"', 0, ("\\", 4)
    yield test, '"\\\\\\"', 0, ('\\"', 6)
    yield test, '"\\\\\\\\"', 0, ("\\\\", 6)

    test = make_arg_string_checker(field)
    yield test, "str", "str"
    yield test, "a b", '"a b"'
    yield test, "a 'b", '''"a 'b"'''
    yield test, 'a "b', """'a "b'"""
    yield test, """a"'b""", """a"'b"""
    yield test, """a "'b""", '''"a \\"'b"'''
    yield test, "'ab", '''"'ab"'''
    yield test, '"ab', """'"ab'"""
    yield test, "ab'", "ab'"
    yield test, 'ab"', 'ab"'
    yield test, "\u0168", "\u0168"
    yield test, '\u0168" \u0168', """'\u0168" \u0168'"""
    yield test, "\u0168' \u0168", '''"\u0168' \u0168"'''
    for char, esc in String.ESCAPES.items():
        if char not in "\"\\'":
            yield test, esc, "\\" + char
    yield test, "\\x", "\\\\x"
    yield test, "\\", '\\\\'
    yield test, "\\\\", '\\\\\\\\'
    yield test, "\\\\\\", '\\\\\\\\\\\\'
    yield test, None, ""
    yield test, 5, Error("invalid value: str=5")

    test = make_placeholder_checker(field)
    yield test, "", 0, ("", "str")
    yield test, "a", 0, ("a", "")
    yield test, "s", 0, ("s", "")
    yield test, "'a", 0, ("'a'", "")
    yield test, "'a'", 0, ("'a'", "")
    yield test, '"a', 0, ('"a"', "")

    test = make_placeholder_checker(String('str', default='def'))
    yield test, "", 0, ("", "def")
    yield test, "d", 0, ("d", "")

    test = make_placeholder_checker(String('str', default='d e f'))
    yield test, "", 0, ("", '"d e f"')
    yield test, "a", 0, ("a", "")

    test = make_placeholder_checker(String('str', default=''))
    yield test, "", 0, ("", '""')
    yield test, " ", 0, ('""', '')
Example #35
0
 def test(rep, args):
     eq_(repr(Choice(*args[0], **args[1])), rep)
Example #36
0
 async def test(text, expected_items, index=None):
     items = await parser.get_completions(text)
     eq_(items, expected_items)
     eq_({x.start for x in items}, (set() if index is None else {index}))
Example #37
0
 def test(command, expected, selection=(0, 0), sel_after=None):
     do = CommandTester(mod.unique_lines, text=TEXT, sel=selection)
     do(command)
     eq_(str(do.editor.text), expected, do.editor.text)
     if sel_after is not None:
         eq_(do.editor.selection, sel_after)
Example #38
0
 def check(err):
     eq_(type(err), type(options))
     eq_(str(err), str(options))
     eq_(err.errors, options.errors)
     eq_(err.parse_index, options.parse_index)