Permalink
Browse files

Add vim wrapper class with tests

  • Loading branch information...
1 parent 09f080a commit 341c205d6c88cb87959d64e1c6e026000c5c6c0f @joonty committed Apr 6, 2013
Showing with 150 additions and 18 deletions.
  1. +46 −0 plugin/python/vdebug/ui/vimui/api.py
  2. +12 −13 plugin/python/vdebug/ui/vimui/ui.py
  3. +6 −0 tests/helper.py
  4. +3 −5 tests/test_dbgp_api.py
  5. +83 −0 tests/test_ui_vimui_api.py
@@ -0,0 +1,46 @@
+import vim
+
+class Vim:
+ vim = vim
+
+ """ Getter methods for the current buffer and tab """
+
+ @classmethod
+ def current_buffer(cls):
+ return cls.vim.eval("bufname('%')")
+
+ @classmethod
+ def current_buffer_name(cls):
+ return cls.vim.current.buffer.name
+
+ @classmethod
+ def current_tab(cls):
+ return cls.vim.eval("tabpagenr()")
+
+ @classmethod
+ def current_cursor_row(cls):
+ return cls.vim.current.window.cursor[0]
+
+ @classmethod
+ def current_line(cls):
+ return cls.line_at_row(cls.current_cursor_row())
+
+ """ Other getter methods """
+
+ @classmethod
+ def line_at_row(cls, row):
+ return cls.vim.eval("getline(%s)" % str(row))
+
+
+ """ Setter/creation methods """
+
+ @classmethod
+ def create_tab(cls, buffer_name):
+ cls.vim.command("silent tabnew %s" % buffer_name)
+ return cls
+
+ @classmethod
+ def place_breakpoint_sign(cls, sign_id, file, line):
+ cls.vim.command("sign place %s name=breakpt line=%s file=%s"\
+ %(str(sign_id), str(line), file))
+ return cls
@@ -5,6 +5,7 @@
import vdebug.opts
from .window import BreakpointWindow, WatchWindow, StackWindow,\
StatusWindow, LogWindow, SourceWindow
+from .api import Vim
class Ui:
"""Ui layer which manages the Vim windows.
@@ -19,7 +20,7 @@ def __init__(self,breakpoints):
self.tabnr = None
def is_modified(self):
- modified = int(vim.eval('&mod'))
+ modified = int(vim.eval('&mod'))v
if modified:
return True
else:
@@ -31,14 +32,14 @@ def open(self):
self.is_open = True
try:
- cur_buf_name = vim.eval("bufname('%')")
+ cur_buf_name = Vim.current_buffer()
if cur_buf_name is None:
cur_buf_name = ''
- self.current_tab = vim.eval("tabpagenr()")
+ self.current_tab = Vim.current_tab()
- vim.command('silent tabnew ' + cur_buf_name)
- self.tabnr = vim.eval("tabpagenr()")
+ Vim.create_tab(cur_buf_name)
+ self.tabnr = Vim.current_tab()
srcwin_name = self.__get_srcwin_name()
@@ -94,16 +95,16 @@ def set_listener_details(self,addr,port,idekey):
self.statuswin.insert(details,1,True)
def get_current_file(self):
- return vdebug.util.FilePath(vim.current.buffer.name)
+ return vdebug.util.FilePath(Vim.current_buffer_name())
def get_current_row(self):
- return vim.current.window.cursor[0]
+ return Vim.current_cursor_row()
def get_current_line(self):
- return self.get_line(self.get_current_row())
+ return Vim.current_line()
- def get_line(self,row):
- return vim.eval("getline(" + str(row) + ")")
+ def get_line(self, row):
+ return Vim.line_at_row(row)
def register_breakpoint(self,breakpoint):
if breakpoint.type == 'line':
@@ -113,9 +114,7 @@ def register_breakpoint(self,breakpoint):
self.breakpointwin.add_breakpoint(breakpoint)
def place_breakpoint(self,sign_id,file,line):
- vim.command('sign place '+str(sign_id)+\
- ' name=breakpt line='+str(line)+\
- ' file='+file.as_local())
+ Vim.place_breakpoint_sign(sign_id, file.as_local(), line)
def remove_breakpoint(self,breakpoint):
id = breakpoint.id
View
@@ -0,0 +1,6 @@
+try:
+ helper_loaded
+except NameError:
+ import sys
+ sys.path.append('../plugin/python/')
+ helper_loaded = True
View
@@ -1,11 +1,9 @@
-if __name__ == "__main__":
- import sys
- sys.path.append('../plugin/python/')
-import unittest2 as unittest
+import helper
import vdebug.dbgp
+import unittest2 as unittest
from mock import MagicMock, patch
-class ApiTest(unittest.TestCase):
+class ApiTest(unittest.TestCase):
"""Test the Api class in the vdebug.dbgp module."""
init_msg = """<?xml version="1.0"
View
@@ -0,0 +1,83 @@
+import helper
+import unittest2 as unittest
+from vdebug.ui.vimui.api import Vim
+from mock import Mock
+
+class VimTest(unittest.TestCase):
+ def setUp(self):
+ self.module = self.mock_vim()
+
+ def mock_vim(self):
+ vim_module = Mock()
+ Vim.vim = vim_module
+ vim_module.eval = Mock()
+ vim_module.command = Mock()
+ vim_module.current = Mock()
+ vim_module.current.buffer = Mock()
+ vim_module.current.window = Mock()
+ return vim_module
+
+ def test_current_buffer_return(self):
+ self.module.eval.return_value = 3
+ self.assertEqual(3, Vim.current_buffer())
+
+ def test_current_tab_return(self):
+ self.module.eval.return_value = 10
+ self.assertEqual(10, Vim.current_tab())
+
+ def test_current_buffer_name_return(self):
+ self.module.current.buffer.name = "a buffer name"
+ self.assertEqual("a buffer name", Vim.current_buffer_name())
+
+ def test_current_buffer_name_call(self):
+ Vim.current_buffer_name()
+ self.module.current.buffer.name.assert_called()
+
+ def test_current_buffer_eval(self):
+ Vim.current_buffer()
+ self.module.eval.assert_called_with("bufname('%')")
+
+ def test_current_tab_eval(self):
+ Vim.current_tab()
+ self.module.eval.assert_called_with("tabpagenr()")
+
+ def test_create_tab_command(self):
+ retval = Vim.create_tab('my_buffer')
+ self.module.command.assert_called_with("silent tabnew my_buffer")
+ self.assertIs(Vim, retval)
+
+ def test_create_tab_return_class(self):
+ retval = Vim.create_tab('my_buffer')
+ self.assertIs(Vim, retval)
+
+ def test_current_cursor_row(self):
+ self.module.current.window.cursor = [20, 50]
+ self.assertEqual(20, Vim.current_cursor_row())
+
+ def test_current_line_return(self):
+ self.module.current.window.cursor = [20, 50]
+ self.module.eval.return_value = 'this is the line'
+ self.assertEqual('this is the line', Vim.current_line())
+
+ def test_current_line_gets_current_cursor(self):
+ self.module.current.window.cursor = [20, 50]
+ Vim.current_line()
+ self.module.eval.assert_called_with("getline(20)")
+
+ def test_line_at_row_return(self):
+ self.module.eval.return_value = 'this is the line'
+ self.assertEqual('this is the line', Vim.line_at_row(10))
+
+ def test_line_at_row_eval(self):
+ Vim.line_at_row(10)
+ self.module.eval.assert_called_with("getline(10)")
+
+ def test_place_breakpoint_sign_command(self):
+ Vim.place_breakpoint_sign(110, 'the_file', 30)
+
+ self.module.command.assert_called_with(\
+ 'sign place 110 name=breakpt line=30 file=the_file')
+
+ def test_place_breakpoint_sign_returns_class(self):
+ retval = Vim.place_breakpoint_sign(110, 'the_file', 30)
+ self.assertIs(Vim, retval)

0 comments on commit 341c205

Please sign in to comment.