def test_load(vim_mod, monkeypatch): from completor import _completor vim_mod.eval = mock.Mock(return_value={}) vim_mod.vars = {} with mock.patch.object(_completor, '_type_map', {b'python.django': b'python'}): assert load_completer(b'hello', b'') is None assert get('python') is None c = load_completer(b'python', b'os.') assert c.input_data == 'os.' assert get('python') is c c = load_completer(b'python', b'#') assert c is get('common') c = load_completer(b'python.django', b'os.') assert c is get('python') vim_mod.current.buffer.options.update({ 'omnifunc': b'csscomplete#CompleteCSS'}) vim_mod.vars = { 'completor_css_omni_trigger': b'([\w-]+|@[\w-]*|[\w-]+:\s*[\w-]*)$' } assert load_completer(b'css', b'text') is get('omni')
def test_load(vim_mod, monkeypatch): from completor import _completor vim_mod.eval = mock.Mock(return_value={}) vim_mod.vars = {} with mock.patch.object(_completor, '_type_map', {b'python.django': b'python'}): assert load_completer(b'hello', b'') is None assert get('python') is None c = load_completer(b'python', b'os.') assert c.input_data == 'os.' assert get('python') is c c = load_completer(b'python', b'#') assert c is get('common') c = load_completer(b'python.django', b'os.') assert c is get('python') vim_mod.current.buffer.options.update( {'omnifunc': b'csscomplete#CompleteCSS'}) vim_mod.vars = { 'completor_css_omni_trigger': b'([\w-]+|@[\w-]*|[\w-]+:\s*[\w-]*)$' } assert load_completer(b'css', b'text') is get('omni')
def test_on_data(vim_mod): omnifunc = mock.Mock() vim_mod.current.buffer.options['omnifunc'] = b'csscomplete#CompleteCSS' vim_mod.funcs[b'csscomplete#CompleteCSS'] = omnifunc vim_mod.current.window.cursor = (1, 2) vim_mod.buffers = [] omni = completor.get('omni') omni.trigger_cache = {} omni.ft = 'css' assert omni.on_data(b'complete', b'text') == [] omni.trigger_cache = { 'css': re.compile('([\w-]+|@[\w-]*|[\w-]+:\s*[\w-]*)$', re.X | re.U)} omnifunc.side_effect = [1, [b'text-transform']] assert omni.on_data(b'complete', b'#') == [] omnifunc.side_effect = [0, [b'text-transform']] vim_mod.current.window.cursor = (1, 2) omni.input_data = 'text' assert omni.on_data(b'complete', b'text') == [b'text-transform'] omnifunc.assert_called_with(0, b'text') omnifunc.side_effect = [17, [b'text-transform']] vim_mod.current.window.cursor = (1, 2) omni.input_data = to_unicode('które się nią opiekują', 'utf-8') omni.on_data(b'complete', omni.input_data) omnifunc.assert_called_with(0, b'opiekuj\xc4\x85')
def test_parse(vim_mod): items = [ b'COMPLETION: pclose : [#int#]pclose(<#FILE *#>)', b'COMPLETION: hello : [#int#]hello', b'COMPLETION: b : [#int#]b', b'COMPLETION: Pattern: fake hello', ] cpp = completor.get('cpp') cpp.input_data = to_unicode('b->', 'utf-8') assert cpp.on_data(b'complete', items) == [{ 'dup': 1, 'menu': b'int pclose(FILE *)', 'word': b'pclose' }, { 'dup': 1, 'menu': b'int hello', 'word': b'hello' }, { 'dup': 1, 'menu': b'int b', 'word': b'b' }, { 'dup': 1, 'menu': b'hello', 'word': b'fake' }]
def test_on_data(vim_mod): omnifunc = mock.Mock() vim_mod.current.buffer.options['omnifunc'] = b'csscomplete#CompleteCSS' vim_mod.funcs[b'csscomplete#CompleteCSS'] = omnifunc vim_mod.current.window.cursor = (1, 2) vim_mod.buffers = [] omni = completor.get('omni') omni.trigger_cache = {} omni.ft = 'css' assert omni.on_data(b'complete', b'text') == [] omni.trigger_cache = { 'css': re.compile('([\w-]+|@[\w-]*|[\w-]+:\s*[\w-]*)$', re.X | re.U) } omnifunc.side_effect = [1, [b'text-transform']] assert omni.on_data(b'complete', b'#') == [] omnifunc.side_effect = [0, [b'text-transform']] vim_mod.current.window.cursor = (1, 2) omni.input_data = 'text' assert omni.on_data(b'complete', b'text') == [b'text-transform'] omnifunc.assert_called_with(0, b'text') omnifunc.side_effect = [17, [b'text-transform']] vim_mod.current.window.cursor = (1, 2) omni.input_data = to_unicode('które się nią opiekują', 'utf-8') omni.on_data(b'complete', omni.input_data) omnifunc.assert_called_with(0, b'opiekuj\xc4\x85')
def test_min_chars(vim_mod, monkeypatch): vim_mod.vars = {'completor_min_chars': 3} common = completor.get('common') mock_buffer_parse = mock.Mock(return_value=['hello']) monkeypatch.setattr(completor.get('buffer'), 'parse', mock_buffer_parse) mock_snips_parse = mock.Mock(return_value=['snips']) monkeypatch.setattr(completor.get('ultisnips'), 'parse', mock_snips_parse) assert common.on_data(b'complete', b'he') == [] mock_buffer_parse.assert_not_called() mock_snips_parse.assert_not_called() assert common.on_data(b'complete', b'hello') == ['snips', 'hello'] mock_buffer_parse.assert_called_with('hello') mock_snips_parse.assert_called_with('hello')
def test_on_data(vim_mod, create_buffer): common = completor.get('common') common.input_data = 'urt' vim_mod.current.buffer.number = 1 vim_mod.current.window.cursor = (1, 2) buffer = create_buffer(1) with open(__file__) as f: buffer[:] = f.read().split('\n') vim_mod.buffers = [buffer] assert common.on_data(b'complete', b'urt') == [ {'menu': '[snip] mock snips', 'word': 'urt', 'dup': 1}, {'menu': '[ID]', 'word': 'current'} ] vim_mod.vars = {'completor_disable_ultisnips': 1} assert common.on_data(b'complete', b'urt') == [ {'menu': '[ID]', 'word': 'current'}] vim_mod.vars = { 'completor_disable_buffer': 1, 'completor_disable_ultisnips': 0 } assert common.on_data(b'complete', b'urt') == [ {'menu': '[snip] mock snips', 'word': 'urt', 'dup': 1}]
def test_get_completions(vim_mod): common = completor.get('common') vim_mod.current.buffer.number = 1 vim_mod.current.window.cursor = (1, 2) buffer = Buffer(1) with open(__file__) as f: buffer[:] = f.read().split('\n') vim_mod.buffers = [buffer] assert common.get_completions('urt') == [ {'menu': '[snip] mock snips', 'word': 'ultisnips_trigger'}, {'menu': '[ID]', 'word': 'current'} ] vim_mod.vars = {'completor_disable_ultisnips': 1} assert common.get_completions('urt') == [ {'menu': '[ID]', 'word': 'current'}] vim_mod.vars = { 'completor_disable_buffer': 1, 'completor_disable_ultisnips': 0 } assert common.get_completions('urt') == [ {'menu': '[snip] mock snips', 'word': 'ultisnips_trigger'}]
def test_min_chars(vim_mod, monkeypatch): vim_mod.vars = {'completor_min_chars': 3} common = completor.get('common') mock_buffer_parse = mock.Mock(return_value=['hello']) monkeypatch.setattr(completor.get('buffer'), 'parse', mock_buffer_parse) mock_snips_parse = mock.Mock(return_value=['snips']) monkeypatch.setattr(completor.get('ultisnips'), 'parse', mock_snips_parse) assert common.get_completions('he') == [] mock_buffer_parse.assert_not_called() mock_snips_parse.assert_not_called() assert common.get_completions('hello') == ['snips', 'hello'] mock_buffer_parse.assert_called_with('hello') mock_snips_parse.assert_called_with('hello')
def test_on_data(vim_mod): common = completor.get('common') vim_mod.current.buffer.number = 1 vim_mod.current.window.cursor = (1, 2) buffer = Buffer(1) with open(__file__) as f: buffer[:] = f.read().split('\n') vim_mod.buffers = [buffer] assert common.on_data(b'complete', b'urt') == [{ 'menu': '[snip] mock snips', 'word': 'ultisnips_trigger' }, { 'menu': '[ID]', 'word': 'current' }] vim_mod.vars = {'completor_disable_ultisnips': 1} assert common.on_data(b'complete', b'urt') == [{ 'menu': '[ID]', 'word': 'current' }] vim_mod.vars = { 'completor_disable_buffer': 1, 'completor_disable_ultisnips': 0 } assert common.on_data(b'complete', b'urt') == [{ 'menu': '[snip] mock snips', 'word': 'ultisnips_trigger' }]
def completions(self, completer, base): com = completor.get(completer) if not com: return [] com.ft = self.ft if com.disabled: return [] return com.parse(base)
def test_request(vim_mod): vim_mod.funcs['completor#utils#tempname'] = mock.Mock( return_value=b'/tmp/vJBio2A/2.vim') vim_mod.current.buffer.name = '/home/vagrant/bench.vim' vim_mod.current.window.cursor = (1, 5) racer = completor.get('rust') racer.input_data = to_unicode('self.', 'utf-8') assert racer.prepare_request(b'complete') == \ 'complete 1 5 /home/vagrant/bench.vim /tmp/vJBio2A/2.vim'
def test_get_completions(vim_mod): filename = completor.get('filename') vim_mod.vars = {} data = list(set([item['menu'] for item in filename.get_completions('./')])) assert data == ['[F]'] vim_mod.vars = {'completor_disable_filename': [b'python']} filename.ft = 'python' assert filename.disabled
def test_format_cmd(vim_mod): vim_mod.funcs['line2byte'] = mock.Mock(return_value=20) vim_mod.funcs['completor#utils#tempname'] = mock.Mock( return_value=b'/tmp/vJBio2A/2.vim') vim_mod.current.window.cursor = (1, 5) go = completor.get('go') go.input_data = to_unicode('self.', 'utf-8') assert go.format_cmd() == [ 'gocode', '-f=csv', '--in=/tmp/vJBio2A/2.vim', 'autocomplete', 24]
def test_format_cmd(vim_mod): vim_mod.funcs['line2byte'] = mock.Mock(return_value=20) vim_mod.funcs['completor#utils#tempname'] = mock.Mock( return_value=b'/tmp/vJBio2A/2.vim') vim_mod.current.window.cursor = (1, 5) go = completor.get('go') go.input_data = to_unicode('self.', 'utf-8') assert go.format_cmd() == [ 'gocode', '-f=csv', '--in=/tmp/vJBio2A/2.vim', 'autocomplete', 24 ]
def test_get_completions(vim_mod): filename = completor.get('filename') vim_mod.vars = {} vim_mod.funcs['expand'] = lambda _: os.path.join(os.getcwd(), 'tests') data = list(set([item['menu'] for item in filename.get_completions('./')])) assert data == ['[F]'] vim_mod.vars = {'completor_disable_filename': [b'python']} filename.ft = 'python' assert filename.disabled
def test_has_omnifunc(vim_mod): vim_mod.vars = { 'completor_css_omni_trigger': b'([\w-]+|@[\w-]*|[\w-]+:\s*[\w-]*)$' } vim_mod.current.buffer.options['omnifunc'] = b'' omni = completor.get('omni') assert omni.has_omnifunc('css') is False omni.trigger_cache = {} vim_mod.current.buffer.options['omnifunc'] = b'csscomplete#CompleteCSS' assert omni.has_omnifunc('css') is True
def test_get_cmd_info(vim_mod): vim_mod.funcs['expand'] = mock.Mock(return_value=b'/tmp/vJBio2A') vim_mod.funcs['completor#utils#tempname'] = mock.Mock( return_value=b'/tmp/vJBio2A/2.cpp') vim_mod.current.window.cursor = (1, 5) cpp = completor.get('cpp') cpp.input_data = to_unicode('self.', 'utf-8') assert cpp.get_cmd_info(b'complete')['cmd'] == [ 'clang', '-fsyntax-only', '-I', '/tmp/vJBio2A', '-Xclang', '-code-completion-macros', '-Xclang', '-code-completion-at=/tmp/vJBio2A/2.cpp:1:6', '/tmp/vJBio2A/2.cpp']
def test_format_cmd(vim_mod): vim_mod.funcs['expand'] = mock.Mock(return_value=b'/tmp/vJBio2A') vim_mod.funcs['completor#utils#tempname'] = mock.Mock( return_value=b'/tmp/vJBio2A/2.cpp') vim_mod.current.window.cursor = (1, 5) cpp = completor.get('cpp') cpp.input_data = to_unicode('self.', 'utf-8') assert cpp.format_cmd() == [ 'clang', '-fsyntax-only', '-I', '/tmp/vJBio2A', '-Xclang', '-code-completion-macros', '-Xclang', '-code-completion-at=/tmp/vJBio2A/2.cpp:1:6', '/tmp/vJBio2A/2.cpp']
def test_parse(): items = [ b'MATCH FrameHandler,155,7,./src/event.rs,Struct,struct FrameHandler', b'MATCH tcp_port,1075,4,./src/event.rs,StructField,Option<u16>', b'MATCH run,1092,11,./src/event.rs,Function,pub fn run(&mut self)' ] racer = completor.get('rust') racer.input_data = to_unicode('self.', 'utf-8') assert racer.on_data(b'complete', items) == [ {'offset': 5, 'dup': 0, 'menu': b'struct FrameHandler', 'word': b'FrameHandler'}, # noqa {'offset': 5, 'dup': 0, 'menu': b'Option<u16>', 'word': b'tcp_port'}, {'offset': 5, 'dup': 0, 'menu': b'fn run(&mut self)', 'word': b'run'} ]
def test_parse(): data = [ b'func,,Errorf,,func(format string, a ...interface{}) error', b'func,,Fprint,,func(w io.Writer, a ...interface{}) (n int, err error)', # noqa ] go = completor.get('go') assert go.parse(data) == [{ 'word': b'Errorf', 'menu': b'func(format string, a ...interface{}) error' }, { 'word': b'Fprint', 'menu': b'func(w io.Writer, a ...interface{}) (n int, err error)' }]
def completions(self, completer, base): com = completor.get(completer) if not com: return [] com.ft = self.ft com.input_data = self.input_data if com.disabled: return [] func = getattr(com, 'parse', None) try: return (func or com.on_complete)(base) except AttributeError as e: return []
def test_unicode(vim_mod): buffer = Buffer(1) with open('./tests/test.txt') as f: buffer[:] = f.read().split('\n') vim_mod.buffers = [buffer] vim_mod.current.buffer.number = 1 vim_mod.current.window.cursor = (1, 16) buf = completor.get('buffer') buf.input_data = to_unicode('pielę pielęgn', 'utf-8') assert buf.start_column() == 7 assert buf.get_completions(b'piel\xc4\x99gn') == [ {'menu': '[ID]', 'word': to_unicode('pielęgniarką', 'utf-8')}, {'menu': '[ID]', 'word': to_unicode('pielęgniarkach', 'utf-8')} ]
def test_on_doc(vim_mod): racer = completor.get('rust') data = [ (b'MATCH io;io;1;1;' b'/Users/maralla/Workspace/src/rust/src/libstd/io/mod.rs;Module;' b'/Users/maralla/Workspace/src/rust/src/libstd/io/mod.rs;' b'"Traits, helpers, and type definitions for core I/O functionality.' b'\\n\\nThe `std::io` module contains a number of common things' b" you\\'ll need\\nwhen doing input and output.\"") ] r = racer.on_doc(data) assert r == [b"Traits, helpers, and type definitions for core I/O" b" functionality.\n\nThe `std::io` module contains a number" b" of common things you'll need\nwhen doing input and " b"output."]
def test_get_cmd_info(self, vim_mod, create_buffer, action, monkeypatch): vim_mod.funcs['line2byte'] = mock.Mock(return_value=20) vim_mod.current.buffer = buf = create_buffer(1) vim_mod.current.buffer.name = '/home/vagrant/bench.vim' vim_mod.current.buffer.options = { 'fileencoding': 'utf-8', 'modified': True } vim_mod.current.window.cursor = (1, 5) vim_mod.eval = mock.Mock(return_value='1') buf[:] = ['package main', 'var _ = "哦"'] go = completor.get('go') info = go.get_cmd_info(action) assert info == self.expected[action]
def get_cmd_info(self, action): ft = self.ft_orig args = self.ft_args lsp_cmd = args.get(b'cmd') if not lsp_cmd: return vim.Dictionary() if action == b'format' and ft != self.ft: import_completer(ft) c = get(ft, ft, self.input_data) if not c: return '' return c.get_cmd_info(action) return vim.Dictionary(cmd=lsp_cmd.split(), is_daemon=True, ftype=self.filetype + '_' + ft, is_sync=False)
def test_parse(vim_mod): items = [ b'COMPLETION: pclose : [#int#]pclose(<#FILE *#>)', b'COMPLETION: hello : [#int#]hello', b'COMPLETION: b : [#int#]b', b'COMPLETION: Pattern: fake hello', ] cpp = completor.get('cpp') cpp.input_data = to_unicode('b->', 'utf-8') assert cpp.on_data(b'complete', items) == [ {'dup': 1, 'menu': b'int pclose(FILE *)', 'info': b'int pclose(FILE *)', 'word': b'pclose'}, {'dup': 1, 'menu': b'int hello', 'info': b'int hello', 'word': b'hello'}, {'dup': 1, 'menu': b'int b', 'info': b'int b', 'word': b'b'}, {'dup': 1, 'menu': b'hello', 'info': b'hello', 'word': b'fake'} ]
def completions(self, completer, base): com = completor.get(completer) if not com: return [] self.copy_to(com) if com.disabled: return [] func = getattr(com, 'parse', None) try: items = (func or com.on_complete)(base) if items and 'offset' not in items[0]: if self._start_column is None: self._start_column = self.start_column() items['offset'] = self._start_column return items except AttributeError: return []
def test_on_data(vim_mod, create_buffer): common = completor.get("common") common.input_data = "urt" vim_mod.current.buffer.number = 1 vim_mod.current.window.cursor = (1, 2) buffer = create_buffer(1) with open(__file__) as f: buffer[:] = f.read().split("\n") vim_mod.buffers = [buffer] assert common.on_data(b"complete", b"urt") == [ { "offset": 0, "menu": "[snip] mock snips", "word": "urt", "dup": 1 }, { "offset": 0, "menu": "[ID]", "word": "current" }, ] vim_mod.vars = {"completor_disable_ultisnips": 1} assert common.on_data(b"complete", b"urt") == [{ "offset": 0, "menu": "[ID]", "word": "current" }] vim_mod.vars = { "completor_disable_buffer": 1, "completor_disable_ultisnips": 0, } assert common.on_data(b"complete", b"urt") == [{ "offset": 0, "menu": "[snip] mock snips", "word": "urt", "dup": 1 }]
def test_unicode(vim_mod): buffer = Buffer(1) with open('./tests/test.txt') as f: buffer[:] = f.read().split('\n') vim_mod.buffers = [buffer] vim_mod.current.buffer.number = 1 vim_mod.current.window.cursor = (1, 16) buf = completor.get('buffer') buf.input_data = to_unicode('pielę pielęgn', 'utf-8') assert buf.start_column() == 7 assert buf.on_data(b'complete', b'piel\xc4\x99gn') == [{ 'menu': '[ID]', 'word': to_unicode('pielęgniarką', 'utf-8') }, { 'menu': '[ID]', 'word': to_unicode('pielęgniarkach', 'utf-8') }]
def test_unicode(vim_mod, create_buffer): buffer = create_buffer(1) with open("./tests/test.txt") as f: buffer[:] = f.read().split("\n") vim_mod.buffers = [buffer] vim_mod.current.buffer.number = 1 vim_mod.current.window.cursor = (1, 16) buf = completor.get("buffer") buf.input_data = to_unicode("pielę pielęgn", "utf-8") assert buf.start_column() == 7 assert buf.on_data(b"complete", b"piel\xc4\x99gn") == [ { "offset": 0, "menu": "[ID]", "word": to_unicode("pielęgniarką", "utf-8"), }, { "offset": 0, "menu": "[ID]", "word": to_unicode("pielęgniarkach", "utf-8"), }, ]
def test_on_data(vim_mod): filename = completor.get('filename') vim_mod.vars = {} vim_mod.funcs['expand'] = lambda _: os.path.join(os.getcwd(), 'tests') data = list(set([item['menu'] for item in filename.on_data(b'complete', b'./')])) assert data == ['[F]'] data = list(set([ item['menu'] for item in filename.on_data( b'complete', b'http:// https://example')])) assert data == [] data = list(set([ item['menu'] for item in filename.on_data( b'complete', b'http:// https://example /')])) assert data == ['[F]'] vim_mod.vars = {'completor_disable_filename': [b'python']} filename.ft = 'python' assert filename.disabled
def reset_guru(self): go = completor.get('go') status = go.use_guru_for_def go.use_guru_for_def = False yield go.use_guru_for_def = status
def test_message_end(vim_mod): racer = completor.get('rust') assert racer.is_message_end(b'END')
def test_doc(self, action): go = completor.get('go') ret = getattr(go, action)(self.data) assert ret == self.expected[action]