Skip to content

Commit 384ce68

Browse files
author
deathaxe
authored
Improve DeferrableViewTestCase (#249)
This commit... 1. adds docstrings to briefly describe test case usage 2. restores preferences after closing view 3. provides basic default settings to apply to a test view via `view_settings`. Functionality, which alters content in maybe unexpected or indirect ways, may cause unwanted side effects. 4. adds unittests for both `DeferrableViewTestCase` and `ViewTestCase`
1 parent 4729661 commit 384ce68

File tree

4 files changed

+240
-10
lines changed

4 files changed

+240
-10
lines changed

tests/_Deferred/tests/test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def test_defer(self):
99
self.setCaretTo(0, 0)
1010
self.defer(100, self.insertText, "foo")
1111
yield 200
12-
self.assertEqual(self.getRowText(0), "foofoo")
12+
self.assertRowContentsEqual(0, "foofoo")
1313

1414
def test_condition(self):
1515
x = []
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import sublime
2+
from unittesting import DeferrableViewTestCase
3+
4+
5+
class TestViewTestCase(DeferrableViewTestCase):
6+
7+
def test_window_object(self):
8+
self.assertIsInstance(self.window, sublime.Window)
9+
10+
def test_view_object(self):
11+
self.assertIsInstance(self.view, sublime.View)
12+
13+
def test_view_settings(self):
14+
for key, value in self.view_settings.items():
15+
self.assertEqual(self.view.settings().get(key), value)
16+
17+
def test_clear_text_equal(self):
18+
self.clearText()
19+
yield 10
20+
self.assertViewContentsEqual("")
21+
22+
def test_assert_view_content_equal(self):
23+
text = "hello world\ni am here"
24+
self.setText(text)
25+
yield 10
26+
self.assertViewContentsEqual(text)
27+
28+
def test_assert_row_content_equal(self):
29+
self.setText("hello world\ni am here")
30+
yield 10
31+
self.assertRowContentsEqual(0, "hello world")
32+
self.assertRowContentsEqual(1, "i am here")
33+
34+
def test_assert_caret_at(self):
35+
self.setText("hello world")
36+
yield 10
37+
self.setCaretTo(0, 5)
38+
yield 10
39+
self.assertCaretAt(0, 5)
40+
41+
def test_assert_carets_at(self):
42+
self.setText("hello world")
43+
yield 10
44+
self.setCaretTo(0, 5)
45+
yield 10
46+
self.addCaretAt(0, 10)
47+
yield 10
48+
self.assertCaretsAt(((0, 5), (0, 10)))

tests/test_view_test_case.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import sublime
2+
from unittesting import ViewTestCase
3+
4+
5+
class TestViewTestCase(ViewTestCase):
6+
7+
def test_window_object(self):
8+
self.assertIsInstance(self.window, sublime.Window)
9+
10+
def test_view_object(self):
11+
self.assertIsInstance(self.view, sublime.View)
12+
13+
def test_view_settings(self):
14+
for key, value in self.view_settings.items():
15+
self.assertEqual(self.view.settings().get(key), value)
16+
17+
def test_clear_text_equal(self):
18+
self.clearText()
19+
self.assertViewContentsEqual("")
20+
21+
def test_assert_view_content_equal(self):
22+
text = "hello world\ni am here"
23+
self.setText(text)
24+
self.assertViewContentsEqual(text)
25+
26+
def test_assert_row_content_equal(self):
27+
self.setText("hello world\ni am here")
28+
self.assertRowContentsEqual(0, "hello world")
29+
self.assertRowContentsEqual(1, "i am here")
30+
31+
def test_assert_caret_at(self):
32+
self.setText("hello world")
33+
self.setCaretTo(0, 5)
34+
self.assertCaretAt(0, 5)
35+
36+
def test_assert_carets_at(self):
37+
self.setText("hello world")
38+
self.setCaretTo(0, 5)
39+
self.addCaretAt(0, 10)
40+
self.assertCaretsAt(((0, 5), (0, 10)))

unittesting/helpers/view_test_case.py

+151-9
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,23 @@
1010

1111

1212
class ViewTestCaseMixin:
13+
view_settings = {
14+
"detect_indentation": False,
15+
"translate_tabs_to_spaces": False,
16+
"word_wrap": False,
17+
}
18+
1319
def setUp(self):
14-
self.view = sublime.active_window().new_file()
20+
self.window = sublime.active_window()
21+
self.view = self.window.new_file()
1522

1623
# make sure we have a window to work with
1724
settings = sublime.load_settings("Preferences.sublime-settings")
18-
settings.set("close_windows_when_empty", False)
25+
self._orig_close_empty_window = settings.get("close_windows_when_empty")
26+
if self._orig_close_empty_window:
27+
settings.set("close_windows_when_empty", False)
1928

29+
# apply pre-defined settings
2030
settings = self.view.settings()
2131
default_settings = getattr(self.__class__, "view_settings", {})
2232
for key, value in default_settings.items():
@@ -27,29 +37,64 @@ def tearDown(self):
2737
self.view.set_scratch(True)
2838
self.view.close()
2939

40+
# restore original settings
41+
settings = sublime.load_settings("Preferences.sublime-settings")
42+
if self._orig_close_empty_window:
43+
settings.set("close_windows_when_empty", self._orig_close_empty_window)
44+
self._orig_close_empty_window = False
45+
3046
def addCaretAt(self, row, col):
31-
"""Add caret to given point (row, col)."""
47+
"""
48+
Add caret to given point (row, col).
49+
50+
:param row: The 0-based row number specifying target caret position.
51+
:param col: The 0-based column number specifying target caret position.
52+
"""
3253
self.view.sel().add(self.textPoint(row, col))
3354

3455
def setCaretTo(self, row, col):
35-
"""Move caret to given point (row, col)."""
56+
"""
57+
Move caret to given position (row, col).
58+
59+
:param row: The 0-based row number specifying target caret position.
60+
:param col: The 0-based column number specifying target caret position.
61+
"""
3662
self.view.sel().clear()
3763
self.view.sel().add(self.textPoint(row, col))
3864

3965
def textPoint(self, row, col):
40-
"""Return textpoint for given row,col coordinats."""
66+
"""
67+
Return textpoint for given row,col coordinats.
68+
69+
This method is an alias for ``self.view.text_point(row, col)``.
70+
71+
:param row: The 0-based row number.
72+
:param col: The 0-based column number.
73+
74+
:returns: Integer value of text point, belowing to row,col combination.
75+
"""
4176
return self.view.text_point(row, col)
4277

4378
def getRowText(self, row):
44-
"""Return given row's content text."""
79+
"""
80+
Return given row's content text.
81+
82+
:param row: The row to return contents for.
83+
84+
:return: unicode string with content of given row.
85+
"""
4586
return self.view.substr(self.view.line(self.view.text_point(row, 0)))
4687

4788
def getText(self):
4889
"""Return view's content text."""
4990
return self.view.substr(sublime.Region(0, self.view.size()))
5091

5192
def setText(self, text):
52-
"""Set whole view's content, replacing anything existing."""
93+
"""
94+
Set whole view's content, replacing anything existing.
95+
96+
:param text: The text to replace the view's content with.
97+
"""
5398
self.clearText()
5499
self.insertText(text)
55100

@@ -59,17 +104,114 @@ def clearText(self):
59104
self.view.run_command("right_delete")
60105

61106
def insertText(self, text):
62-
"""Insert text at current position."""
107+
"""
108+
Insert text at current position.
109+
110+
:param text: The text to insert at current caret position.
111+
"""
63112
self.view.run_command("insert", {"characters": text})
64113

114+
def assertCaretAt(self, row, col):
115+
"""
116+
Assert caret to be located at a certain position.
117+
118+
:param row: The 0-based row number.
119+
:param col: The 0-based column number.
120+
"""
121+
self.assertEqual(self.view.sel()[0].begin(), self.textPoint(row, col))
122+
123+
def assertCaretsAt(self, carets):
124+
"""
125+
Assert carets to be located at a certain positions.
126+
127+
:param carets:
128+
The tuple of (row, col) tuples of 0-based row, col numbers.
129+
"""
130+
expected = tuple(self.textPoint(row, col) for row, col in carets)
131+
found = tuple(sel.begin() for sel in self.view.sel())
132+
self.assertEqual(found, expected)
133+
134+
def assertRowContentsEqual(self, row, text):
135+
"""
136+
Expect given row's content to match ``text``.
137+
138+
:param row: The 0-based row number.
139+
:param col: The 0-based column number.
140+
:param text: The expected content of given row.
141+
"""
142+
self.assertEqual(self.getRowText(row), text)
143+
65144
def assertViewContentsEqual(self, text):
66-
self.assertEqual(self.getText(), text)
145+
"""
146+
Expect view's content to match ``text``.
67147
148+
:param text: The expected content of the view.
149+
"""
150+
self.assertEqual(self.getText(), text)
68151

69152

70153
class ViewTestCase(ViewTestCaseMixin, TestCase):
154+
"""
155+
This class describes a view test case.
156+
157+
This class provides infrastructure to run unit tests on dedicated ``sublime.View()`` objects.
158+
159+
A new ``view`` object is created within the active ``window`` for each ``ViewTestCase``.
160+
161+
The view is accessible via ``self.view`` from within each test method.
162+
163+
The owning window can be accessed via ``self.window``.
164+
165+
```py
166+
class MyTestCase(ViewTestCase):
167+
# settings to apply to the created view
168+
view_settings = {
169+
"detect_indentation": False,
170+
"tab_size": 4,
171+
"translate_tabs_to_spaces": False,
172+
"word_wrap": False,
173+
}
174+
175+
def test_editing(self):
176+
self.setText("foo")
177+
self.setCaretTo(0, 0)
178+
self.assertRowContentsEqual(0, "foofoo")
179+
```
180+
"""
181+
71182
pass
72183

73184

74185
class DeferrableViewTestCase(ViewTestCaseMixin, DeferrableTestCase):
186+
"""
187+
This class describes a deferrable view test case.
188+
189+
This class provides infrastructure to run unit tests on dedicated ``sublime.View()`` objects,
190+
which includes catching asynchronous events.
191+
192+
A new ``view`` object is created within the active ``window`` for each ``ViewTestCase``.
193+
194+
The view is accessible via ``self.view`` from within each test method.
195+
196+
The owning window can be accessed via ``self.window``.
197+
198+
```py
199+
class MyTestCase(DeferrableViewTestCase):
200+
# settings to apply to the created view
201+
view_settings = {
202+
"detect_indentation": False,
203+
"tab_size": 4,
204+
"translate_tabs_to_spaces": False,
205+
"word_wrap": False,
206+
}
207+
208+
def test_editing(self):
209+
self.setText("foo")
210+
self.setCaretTo(0, 0)
211+
self.defer(100, self.insertText, "foo")
212+
yield 200
213+
self.assertRowContentsEqual(0, "foofoo")
214+
```
215+
"""
216+
75217
pass

0 commit comments

Comments
 (0)