1
1
import sublime
2
2
3
3
from unittest import TestCase
4
+ from .. import DeferrableTestCase
4
5
6
+ __all__ = [
7
+ "DeferrableViewTestCase" ,
8
+ "ViewTestCase" ,
9
+ ]
5
10
6
- class ViewTestCase (TestCase ):
11
+
12
+ class ViewTestCaseMixin :
7
13
def setUp (self ):
8
14
self .view = sublime .active_window ().new_file ()
9
15
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
+
10
20
settings = self .view .settings ()
11
- default_settings = getattr (self .__class__ , ' view_settings' , {})
21
+ default_settings = getattr (self .__class__ , " view_settings" , {})
12
22
for key , value in default_settings .items ():
13
23
settings .set (key , value )
14
24
@@ -17,8 +27,49 @@ def tearDown(self):
17
27
self .view .set_scratch (True )
18
28
self .view .close ()
19
29
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."""
21
49
return self .view .substr (sublime .Region (0 , self .view .size ()))
22
50
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
+
23
65
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