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"
def test_CommandParser_with_SubParser(): sub = SubArgs("num", Int("n"), abc="xyz") arg = SubParser("var", sub) parser = CommandParser(arg, yesno) def test(text, result): eq_(parser.get_placeholder(text), result) yield test, "", "var ... yes" yield test, " ", "yes" yield test, " ", "" yield test, "n", "um n yes" yield test, "n ", "n yes" yield test, "num ", "n yes" yield test, "num ", "yes" def test(text, result): eq_(parser.get_completions(text), result) yield test, "", ["num"] yield test, " ", ["yes", "no"] yield test, " ", None yield test, "n", ["num"] yield test, "n ", [] yield test, "num ", [] yield test, "num ", ["yes", "no"]
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)))
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) ])
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
def test_CommandParser_with_SubParser_errors(): sub = SubArgs("num", Int("num"), abc="xyz") arg = SubParser("var", sub) parser = CommandParser(arg) 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) ]) with assert_raises(ArgumentError, msg=check): parser.parse('num x')
def test_Int(): arg = Int('num') eq_(str(arg), 'num') eq_(repr(arg), "Int('num')") test = make_type_checker(arg) yield test, '', 0, (None, 0) yield test, '3', 0, (3, 1) yield test, '42', 0, (42, 2) 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'", arg, 0, 2) test = make_arg_string_checker(arg) yield test, 42, "42" yield test, -42, "-42" yield test, None, "" yield test, "arg", Error("invalid value: num='arg'")
from editxt.command.base import command, objc_delegate, SheetController from editxt.command.parser import Choice, Int, CommandParser, Options from editxt.command.util import has_selection, iterlines log = logging.getLogger(__name__) WHITESPACE = re.compile(r"[ \t]*") @command( name='wrap', title="Hard Wrap...", hotkey=("\\", ak.NSCommandKeyMask | ak.NSShiftKeyMask), is_enabled=has_selection, arg_parser=CommandParser( # TODO test Int('wrap_column', default=const.DEFAULT_RIGHT_MARGIN), Choice(('indent', True), ('no-indent', False)), )) def wrap_lines(textview, sender, args): if args is None: wrapper = WrapLinesController(textview) wrapper.begin_sheet(sender) else: wrap_selected_lines(textview, args) @command( title="Hard Wrap At Margin", hotkey=("\\", ak.NSCommandKeyMask), arg_parser=CommandParser( # TODO test Choice(('indent', True),
) @command(title="Command Bar", hotkey=(";", ak.NSCommandKeyMask)) def show_command_bar(textview, sender, args): """Show the command bar""" from editxt import app editor = app.find_editor_with_document_view(textview.doc_view) if editor is None: ak.NSBeep() else: editor.command.activate() @command(name='goto', title="Goto Line", arg_parser=CommandParser(Int("line")), lookup_with_arg_parser=True) def goto_line(textview, sender, opts): """Jump to a line in the document""" if opts is None or opts.line is None: show_command_bar(textview, sender, None) return textview.goto_line(opts.line) @command(title="(Un)comment Selected Lines", hotkey=(",", ak.NSCommandKeyMask), is_enabled=has_selection) def comment_text(textview, sender, args): """Comment/uncomment the selected text region
import editxt.command.base as mod from editxt.controls.textview import TextView from editxt.command.base import CommandController from editxt.command.base import SheetController, PanelController from editxt.command.parser import ArgumentError, CommandParser, Int, Options from editxt.textcommand import CommandHistory from editxt.util import KVOProxy log = logging.getLogger(__name__) @mod.command(name='abc', title='Title', hotkey=(',', 0), is_enabled=lambda *a: False, arg_parser=CommandParser(Int("value")), lookup_with_arg_parser=True) def dummy_command(textview, sender, args): assert False, "this command is not meant to be executed" def setup(controller_class, nib_name="TestController"): def setup_controller(func): @functools.wraps(func) def wrapper(): assert not hasattr(controller_class, 'COMMAND') assert not hasattr(controller_class, 'NIB_NAME') controller_class.COMMAND = dummy_command controller_class.NIB_NAME = nib_name try: func()
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"]