Skip to content

Commit

Permalink
Add DeferrabbleViewTestCase (#247)
Browse files Browse the repository at this point in the history
This commit ...

1. adds `DeferrabbleViewTestCase` class
2. extends `ViewTestCase` class
3. bases various existing unit self tests on them
  • Loading branch information
deathaxe authored Mar 23, 2024
1 parent 673bf2e commit 368364a
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 86 deletions.
35 changes: 7 additions & 28 deletions tests/_Deferred/tests/test.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,14 @@
import sublime
from unittest import TestCase # FIXME Import unused? # noqa: F401
from unittesting import DeferrableTestCase, expectedFailure
from unittesting import DeferrableViewTestCase, expectedFailure


class TestDeferrable(DeferrableTestCase):

def setUp(self):
self.view = sublime.active_window().new_file()
# make sure we have a window to work with
s = sublime.load_settings("Preferences.sublime-settings")
s.set("close_windows_when_empty", False)

def tearDown(self):
if self.view:
self.view.set_scratch(True)
self.view.window().focus_view(self.view)
self.view.window().run_command("close_file")

def setText(self, string):
self.view.run_command("insert", {"characters": string})

def getRow(self, row):
return self.view.substr(self.view.line(self.view.text_point(row, 0)))
class TestDeferrable(DeferrableViewTestCase):

def test_defer(self):
self.setText("foo")
self.view.sel().clear()
self.view.sel().add(sublime.Region(0, 0))
sublime.set_timeout(lambda: self.setText("foo"), 100)
self.setCaretTo(0, 0)
self.defer(100, self.insertText, "foo")
yield 200
self.assertEqual(self.getRow(0), "foofoo")
self.assertEqual(self.getRowText(0), "foofoo")

def test_condition(self):
x = []
Expand All @@ -40,7 +19,7 @@ def append():
def condition():
return len(x) == 1

sublime.set_timeout(append, 100)
self.defer(100, append)

# wait until `condition()` is true
yield condition
Expand All @@ -54,7 +33,7 @@ def test_condition_timeout(self):
def append():
x.append(1)

sublime.set_timeout(append, 100)
self.defer(100, append)

# wait until condition timeout
yield {"condition": lambda: False, "timeout": 500}
Expand Down
21 changes: 4 additions & 17 deletions tests/_Failure/tests/test.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import sublime
from unittest import TestCase
from unittesting import ViewTestCase


class TestHelloWorld(TestCase):

def setUp(self):
self.view = sublime.active_window().new_file()
# make sure we have a window to work with
s = sublime.load_settings("Preferences.sublime-settings")
s.set("close_windows_when_empty", False)

def tearDown(self):
if self.view:
self.view.set_scratch(True)
self.view.window().focus_view(self.view)
self.view.window().run_command("close_file")
class TestHelloWorld(ViewTestCase):

def test_hello_world(self):
self.view.run_command("insert", {"characters": "hello world"})
first_row = self.view.substr(self.view.line(0))
self.setText("hello world")
first_row = self.getRowText(0)
self.assertEqual(first_row, "hello world!")
21 changes: 4 additions & 17 deletions tests/_Output/tests/test.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import sublime
from unittest import TestCase
from unittesting import ViewTestCase


class TestHelloWorld(TestCase):

def setUp(self):
self.view = sublime.active_window().new_file()
# make sure we have a window to work with
s = sublime.load_settings("Preferences.sublime-settings")
s.set("close_windows_when_empty", False)

def tearDown(self):
if self.view:
self.view.set_scratch(True)
self.view.window().focus_view(self.view)
self.view.window().run_command("close_file")
class TestHelloWorld(ViewTestCase):

def test_hello_world(self):
self.view.run_command("insert", {"characters": "hello world"})
first_row = self.view.substr(self.view.line(0))
self.setText("hello world")
first_row = self.getRowText(0)
self.assertEqual(first_row, "hello world")
21 changes: 4 additions & 17 deletions tests/_Success/tests/test.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,9 @@
import sublime
from unittest import TestCase
from unittesting import ViewTestCase


class TestHelloWorld(TestCase):

def setUp(self):
self.view = sublime.active_window().new_file()
# make sure we have a window to work with
s = sublime.load_settings("Preferences.sublime-settings")
s.set("close_windows_when_empty", False)

def tearDown(self):
if self.view:
self.view.set_scratch(True)
self.view.window().focus_view(self.view)
self.view.window().run_command("close_file")
class TestHelloWorld(ViewTestCase):

def test_hello_world(self):
self.view.run_command("insert", {"characters": "hello world"})
first_row = self.view.substr(self.view.line(0))
self.setText("hello world")
first_row = self.getRowText(0)
self.assertEqual(first_row, "hello world")
2 changes: 2 additions & 0 deletions unittesting/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .core import AWAIT_WORKER
from .core import DeferrableTestCase
from .core import expectedFailure
from .helpers import DeferrableViewTestCase
from .helpers import OverridePreferencesTestCase
from .helpers import TempDirectoryTestCase
from .helpers import ViewTestCase
Expand All @@ -12,6 +13,7 @@
"DeferrableTestCase",
"expectedFailure",
"run_scheduler",
"DeferrableViewTestCase",
"OverridePreferencesTestCase",
"TempDirectoryTestCase",
"ViewTestCase",
Expand Down
6 changes: 6 additions & 0 deletions unittesting/core/py33/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import warnings
from functools import wraps
from unittest.case import _ExpectedFailure, _UnexpectedSuccess, SkipTest, _Outcome

from ...utils import isiterable
from .runner import defer


def expectedFailure(func):
Expand Down Expand Up @@ -52,6 +54,10 @@ def _executeTestPart(self, function, outcome, isTest=False):
outcome.success = False
outcome.errors.append(sys.exc_info())

@staticmethod
def defer(delay, callback, *args, **kwargs):
defer(delay, callback, *args, **kwargs)

def run(self, result=None):
orig_result = result
if result is None:
Expand Down
5 changes: 5 additions & 0 deletions unittesting/core/py38/case.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
import unittest
from unittest.case import _Outcome
from .runner import defer
from ...utils import isiterable


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

@staticmethod
def defer(delay, callback, *args, **kwargs):
defer(delay, callback, *args, **kwargs)

def run(self, result=None):
orig_result = result
if result is None:
Expand Down
8 changes: 5 additions & 3 deletions unittesting/helpers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from .override_preferences_test_cast import OverridePreferencesTestCase # noqa: F401
from .temp_directory_test_case import TempDirectoryTestCase # noqa: F401
from .view_test_case import ViewTestCase # noqa: F401
# noqa: F401
from .override_preferences_test_cast import OverridePreferencesTestCase
from .temp_directory_test_case import TempDirectoryTestCase
from .view_test_case import DeferrableViewTestCase
from .view_test_case import ViewTestCase
59 changes: 55 additions & 4 deletions unittesting/helpers/view_test_case.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import sublime

from unittest import TestCase
from .. import DeferrableTestCase

__all__ = [
"DeferrableViewTestCase",
"ViewTestCase",
]

class ViewTestCase(TestCase):

class ViewTestCaseMixin:
def setUp(self):
self.view = sublime.active_window().new_file()

# make sure we have a window to work with
settings = sublime.load_settings("Preferences.sublime-settings")
settings.set("close_windows_when_empty", False)

settings = self.view.settings()
default_settings = getattr(self.__class__, 'view_settings', {})
default_settings = getattr(self.__class__, "view_settings", {})
for key, value in default_settings.items():
settings.set(key, value)

Expand All @@ -17,8 +27,49 @@ def tearDown(self):
self.view.set_scratch(True)
self.view.close()

def _viewContents(self):
def addCaretAt(self, row, col):
"""Add caret to given point (row, col)."""
self.view.sel().add(self.textPoint(row, col))

def setCaretTo(self, row, col):
"""Move caret to given point (row, col)."""
self.view.sel().clear()
self.view.sel().add(self.textPoint(row, col))

def textPoint(self, row, col):
"""Return textpoint for given row,col coordinats."""
return self.view.text_point(row, col)

def getRowText(self, row):
"""Return given row's content text."""
return self.view.substr(self.view.line(self.view.text_point(row, 0)))

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

def setText(self, text):
"""Set whole view's content, replacing anything existing."""
self.clearText()
self.insertText(text)

def clearText(self):
"""Clear whole view's content."""
self.view.run_command("select_all")
self.view.run_command("right_delete")

def insertText(self, text):
"""Insert text at current position."""
self.view.run_command("insert", {"characters": text})

def assertViewContentsEqual(self, text):
self.assertEqual(self._viewContents(), text)
self.assertEqual(self.getText(), text)



class ViewTestCase(ViewTestCaseMixin, TestCase):
pass


class DeferrableViewTestCase(ViewTestCaseMixin, DeferrableTestCase):
pass

0 comments on commit 368364a

Please sign in to comment.