Skip to content

Commit 368364a

Browse files
author
deathaxe
authored
Add DeferrabbleViewTestCase (#247)
This commit ... 1. adds `DeferrabbleViewTestCase` class 2. extends `ViewTestCase` class 3. bases various existing unit self tests on them
1 parent 673bf2e commit 368364a

File tree

9 files changed

+92
-86
lines changed

9 files changed

+92
-86
lines changed

tests/_Deferred/tests/test.py

+7-28
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,14 @@
1-
import sublime
2-
from unittest import TestCase # FIXME Import unused? # noqa: F401
3-
from unittesting import DeferrableTestCase, expectedFailure
1+
from unittesting import DeferrableViewTestCase, expectedFailure
42

53

6-
class TestDeferrable(DeferrableTestCase):
7-
8-
def setUp(self):
9-
self.view = sublime.active_window().new_file()
10-
# make sure we have a window to work with
11-
s = sublime.load_settings("Preferences.sublime-settings")
12-
s.set("close_windows_when_empty", False)
13-
14-
def tearDown(self):
15-
if self.view:
16-
self.view.set_scratch(True)
17-
self.view.window().focus_view(self.view)
18-
self.view.window().run_command("close_file")
19-
20-
def setText(self, string):
21-
self.view.run_command("insert", {"characters": string})
22-
23-
def getRow(self, row):
24-
return self.view.substr(self.view.line(self.view.text_point(row, 0)))
4+
class TestDeferrable(DeferrableViewTestCase):
255

266
def test_defer(self):
277
self.setText("foo")
28-
self.view.sel().clear()
29-
self.view.sel().add(sublime.Region(0, 0))
30-
sublime.set_timeout(lambda: self.setText("foo"), 100)
8+
self.setCaretTo(0, 0)
9+
self.defer(100, self.insertText, "foo")
3110
yield 200
32-
self.assertEqual(self.getRow(0), "foofoo")
11+
self.assertEqual(self.getRowText(0), "foofoo")
3312

3413
def test_condition(self):
3514
x = []
@@ -40,7 +19,7 @@ def append():
4019
def condition():
4120
return len(x) == 1
4221

43-
sublime.set_timeout(append, 100)
22+
self.defer(100, append)
4423

4524
# wait until `condition()` is true
4625
yield condition
@@ -54,7 +33,7 @@ def test_condition_timeout(self):
5433
def append():
5534
x.append(1)
5635

57-
sublime.set_timeout(append, 100)
36+
self.defer(100, append)
5837

5938
# wait until condition timeout
6039
yield {"condition": lambda: False, "timeout": 500}

tests/_Failure/tests/test.py

+4-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
import sublime
2-
from unittest import TestCase
1+
from unittesting import ViewTestCase
32

43

5-
class TestHelloWorld(TestCase):
6-
7-
def setUp(self):
8-
self.view = sublime.active_window().new_file()
9-
# make sure we have a window to work with
10-
s = sublime.load_settings("Preferences.sublime-settings")
11-
s.set("close_windows_when_empty", False)
12-
13-
def tearDown(self):
14-
if self.view:
15-
self.view.set_scratch(True)
16-
self.view.window().focus_view(self.view)
17-
self.view.window().run_command("close_file")
4+
class TestHelloWorld(ViewTestCase):
185

196
def test_hello_world(self):
20-
self.view.run_command("insert", {"characters": "hello world"})
21-
first_row = self.view.substr(self.view.line(0))
7+
self.setText("hello world")
8+
first_row = self.getRowText(0)
229
self.assertEqual(first_row, "hello world!")

tests/_Output/tests/test.py

+4-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
import sublime
2-
from unittest import TestCase
1+
from unittesting import ViewTestCase
32

43

5-
class TestHelloWorld(TestCase):
6-
7-
def setUp(self):
8-
self.view = sublime.active_window().new_file()
9-
# make sure we have a window to work with
10-
s = sublime.load_settings("Preferences.sublime-settings")
11-
s.set("close_windows_when_empty", False)
12-
13-
def tearDown(self):
14-
if self.view:
15-
self.view.set_scratch(True)
16-
self.view.window().focus_view(self.view)
17-
self.view.window().run_command("close_file")
4+
class TestHelloWorld(ViewTestCase):
185

196
def test_hello_world(self):
20-
self.view.run_command("insert", {"characters": "hello world"})
21-
first_row = self.view.substr(self.view.line(0))
7+
self.setText("hello world")
8+
first_row = self.getRowText(0)
229
self.assertEqual(first_row, "hello world")

tests/_Success/tests/test.py

+4-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
import sublime
2-
from unittest import TestCase
1+
from unittesting import ViewTestCase
32

43

5-
class TestHelloWorld(TestCase):
6-
7-
def setUp(self):
8-
self.view = sublime.active_window().new_file()
9-
# make sure we have a window to work with
10-
s = sublime.load_settings("Preferences.sublime-settings")
11-
s.set("close_windows_when_empty", False)
12-
13-
def tearDown(self):
14-
if self.view:
15-
self.view.set_scratch(True)
16-
self.view.window().focus_view(self.view)
17-
self.view.window().run_command("close_file")
4+
class TestHelloWorld(ViewTestCase):
185

196
def test_hello_world(self):
20-
self.view.run_command("insert", {"characters": "hello world"})
21-
first_row = self.view.substr(self.view.line(0))
7+
self.setText("hello world")
8+
first_row = self.getRowText(0)
229
self.assertEqual(first_row, "hello world")

unittesting/__init__.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from .core import AWAIT_WORKER
22
from .core import DeferrableTestCase
33
from .core import expectedFailure
4+
from .helpers import DeferrableViewTestCase
45
from .helpers import OverridePreferencesTestCase
56
from .helpers import TempDirectoryTestCase
67
from .helpers import ViewTestCase
@@ -12,6 +13,7 @@
1213
"DeferrableTestCase",
1314
"expectedFailure",
1415
"run_scheduler",
16+
"DeferrableViewTestCase",
1517
"OverridePreferencesTestCase",
1618
"TempDirectoryTestCase",
1719
"ViewTestCase",

unittesting/core/py33/case.py

+6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import warnings
44
from functools import wraps
55
from unittest.case import _ExpectedFailure, _UnexpectedSuccess, SkipTest, _Outcome
6+
67
from ...utils import isiterable
8+
from .runner import defer
79

810

911
def expectedFailure(func):
@@ -52,6 +54,10 @@ def _executeTestPart(self, function, outcome, isTest=False):
5254
outcome.success = False
5355
outcome.errors.append(sys.exc_info())
5456

57+
@staticmethod
58+
def defer(delay, callback, *args, **kwargs):
59+
defer(delay, callback, *args, **kwargs)
60+
5561
def run(self, result=None):
5662
orig_result = result
5763
if result is None:

unittesting/core/py38/case.py

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
import unittest
33
from unittest.case import _Outcome
4+
from .runner import defer
45
from ...utils import isiterable
56

67

@@ -26,6 +27,10 @@ def _callCleanup(self, function, *args, **kwargs):
2627
if isiterable(deferred):
2728
yield from deferred
2829

30+
@staticmethod
31+
def defer(delay, callback, *args, **kwargs):
32+
defer(delay, callback, *args, **kwargs)
33+
2934
def run(self, result=None):
3035
orig_result = result
3136
if result is None:

unittesting/helpers/__init__.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
from .override_preferences_test_cast import OverridePreferencesTestCase # noqa: F401
2-
from .temp_directory_test_case import TempDirectoryTestCase # noqa: F401
3-
from .view_test_case import ViewTestCase # noqa: F401
1+
# noqa: F401
2+
from .override_preferences_test_cast import OverridePreferencesTestCase
3+
from .temp_directory_test_case import TempDirectoryTestCase
4+
from .view_test_case import DeferrableViewTestCase
5+
from .view_test_case import ViewTestCase

unittesting/helpers/view_test_case.py

+55-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
import sublime
22

33
from unittest import TestCase
4+
from .. import DeferrableTestCase
45

6+
__all__ = [
7+
"DeferrableViewTestCase",
8+
"ViewTestCase",
9+
]
510

6-
class ViewTestCase(TestCase):
11+
12+
class ViewTestCaseMixin:
713
def setUp(self):
814
self.view = sublime.active_window().new_file()
915

16+
# make sure we have a window to work with
17+
settings = sublime.load_settings("Preferences.sublime-settings")
18+
settings.set("close_windows_when_empty", False)
19+
1020
settings = self.view.settings()
11-
default_settings = getattr(self.__class__, 'view_settings', {})
21+
default_settings = getattr(self.__class__, "view_settings", {})
1222
for key, value in default_settings.items():
1323
settings.set(key, value)
1424

@@ -17,8 +27,49 @@ def tearDown(self):
1727
self.view.set_scratch(True)
1828
self.view.close()
1929

20-
def _viewContents(self):
30+
def addCaretAt(self, row, col):
31+
"""Add caret to given point (row, col)."""
32+
self.view.sel().add(self.textPoint(row, col))
33+
34+
def setCaretTo(self, row, col):
35+
"""Move caret to given point (row, col)."""
36+
self.view.sel().clear()
37+
self.view.sel().add(self.textPoint(row, col))
38+
39+
def textPoint(self, row, col):
40+
"""Return textpoint for given row,col coordinats."""
41+
return self.view.text_point(row, col)
42+
43+
def getRowText(self, row):
44+
"""Return given row's content text."""
45+
return self.view.substr(self.view.line(self.view.text_point(row, 0)))
46+
47+
def getText(self):
48+
"""Return view's content text."""
2149
return self.view.substr(sublime.Region(0, self.view.size()))
2250

51+
def setText(self, text):
52+
"""Set whole view's content, replacing anything existing."""
53+
self.clearText()
54+
self.insertText(text)
55+
56+
def clearText(self):
57+
"""Clear whole view's content."""
58+
self.view.run_command("select_all")
59+
self.view.run_command("right_delete")
60+
61+
def insertText(self, text):
62+
"""Insert text at current position."""
63+
self.view.run_command("insert", {"characters": text})
64+
2365
def assertViewContentsEqual(self, text):
24-
self.assertEqual(self._viewContents(), text)
66+
self.assertEqual(self.getText(), text)
67+
68+
69+
70+
class ViewTestCase(ViewTestCaseMixin, TestCase):
71+
pass
72+
73+
74+
class DeferrableViewTestCase(ViewTestCaseMixin, DeferrableTestCase):
75+
pass

0 commit comments

Comments
 (0)