1- from unittest import TestCase
1+ from __future__ import annotations
2+ from unittesting import ViewTestCase
23
34import sublime
45
56from LaTeXTools .latextools .context_provider import LatextoolsContextListener
67
7- ST_VER = int (sublime .version ())
88
9+ class ContextTest (ViewTestCase ):
10+ view_settings = {
11+ "auto_indent" : False ,
12+ "syntax" : "Packages/LaTeX/LaTeX.sublime-syntax" ,
13+ }
914
10- class ContextTest (TestCase ):
1115 def setUp (self ):
1216 self .context = LatextoolsContextListener ()
13- self .view = sublime .active_window ().new_file ()
14- self .view .set_syntax_file ("Packages/LaTeX/LaTeX.sublime-syntax" )
15- self .view .settings ().set ("auto_indent" , False )
16- self .view .set_scratch (True )
17-
18- def tearDown (self ):
19- if self .view :
20- self .view .close ()
21- self .view = None
22-
23- def query_context (self , key , operator = sublime .OP_EQUAL , operand = True , match_all = False ):
17+ super ().setUp ()
18+
19+ def query_context (
20+ self ,
21+ key : str ,
22+ operator = sublime .OP_EQUAL ,
23+ operand : bool | float | int | str = True ,
24+ match_all = False ,
25+ ):
2426 return self .context .on_query_context (self .view , key , operator , operand , match_all )
2527
26- def set_content (self , content ):
27- self .view .run_command ("select_all" )
28- self .view .run_command ("insert" , {"characters" : content })
29-
3028 def set_sel (self , regions ):
3129 if not hasattr (regions , "__iter__" ):
3230 regions = [regions ]
@@ -35,47 +33,52 @@ def set_sel(self, regions):
3533
3634 def test_version (self ):
3735 ctx = "latextools.st_version"
38- self .assertTrue (self .query_context (ctx , operand = f">={ ST_VER } " ))
39- self .assertTrue (self .query_context (ctx , operand = f"={ ST_VER } " ))
40- self .assertFalse (self .query_context (ctx , operand = f"<{ ST_VER } " ))
41- self .assertTrue (self .query_context (ctx , operand = f"<{ ST_VER + 1 } " ))
42- self .assertTrue (self .query_context (ctx , operand = f"<={ ST_VER + 1 } " ))
43- self .assertFalse (self .query_context (ctx , operand = f"<={ ST_VER - 1 } " ))
36+ ver = int (sublime .version ())
37+ self .assertTrue (self .query_context (ctx , operand = f">={ ver } " ))
38+ self .assertTrue (self .query_context (ctx , operand = f"={ ver } " ))
39+ self .assertFalse (self .query_context (ctx , operand = f"<{ ver } " ))
40+ self .assertTrue (self .query_context (ctx , operand = f"<{ ver + 1 } " ))
41+ self .assertTrue (self .query_context (ctx , operand = f"<={ ver + 1 } " ))
42+ self .assertFalse (self .query_context (ctx , operand = f"<={ ver - 1 } " ))
4443
4544 def test_documentclass (self ):
4645 ctx = "latextools.documentclass"
47- yield self .set_content ("\\ documentclass{article}" )
46+ yield self .setText ("\\ documentclass{article}" )
4847
49- self .assertTrue (self .query_context (
50- ctx , operator = sublime .OP_REGEX_MATCH , operand = "article|beamer" ))
48+ self .assertTrue (
49+ self .query_context (ctx , operator = sublime .OP_REGEX_MATCH , operand = "article|beamer" )
50+ )
5151 self .assertTrue (self .query_context (ctx , operand = "article" ))
5252 self .assertFalse (self .query_context (ctx , operand = "beamer" ))
5353
5454 def test_usepackage (self ):
5555 ctx = "latextools.usepackage"
5656 content = sublime .load_resource ("Packages/LaTeXTools/tests/fixtures/context/main.tex" )
57- yield self .set_content (content )
57+ yield self .setText (content )
5858 self .assertTrue (self .query_context (ctx , operand = "babel" ))
5959 self .assertTrue (self .query_context (ctx , operand = "amsmath" ))
6060 self .assertFalse (self .query_context (ctx , operand = "amsfonts" ))
6161
6262 operand = "\\ b(mathtools|amsmath)\\ b"
63- self .assertTrue (self .query_context (
64- ctx , operator = sublime .OP_REGEX_CONTAINS , operand = operand ))
63+ self .assertTrue (
64+ self .query_context (ctx , operator = sublime .OP_REGEX_CONTAINS , operand = operand )
65+ )
6566
6667 self .assertTrue (self .query_context (ctx , operand = "xcolor" ))
6768 self .assertFalse (self .query_context (ctx , operand = "color" ))
6869 operand = "\\ bx?color\\ b"
69- self .assertTrue (self .query_context (
70- ctx , operator = sublime .OP_REGEX_CONTAINS , operand = operand ))
70+ self .assertTrue (
71+ self .query_context (ctx , operator = sublime .OP_REGEX_CONTAINS , operand = operand )
72+ )
7173 operand = "\\ bx?color\\ b"
72- self .assertFalse (self .query_context (
73- ctx , operator = sublime .OP_NOT_REGEX_CONTAINS , operand = operand ))
74+ self .assertFalse (
75+ self .query_context (ctx , operator = sublime .OP_NOT_REGEX_CONTAINS , operand = operand )
76+ )
7477
7578 def test_env_selector (self ):
7679 ctx = "latextools.env_selector"
7780 content = sublime .load_resource ("Packages/LaTeXTools/tests/fixtures/context/main.tex" )
78- self .set_content (content )
81+ self .setText (content )
7982 yield self .set_sel (self .view .find (r"<1>" , 0 ))
8083 self .assertTrue (self .query_context (ctx , operand = "" ))
8184 self .assertTrue (self .query_context (ctx , operand = ", none" ))
@@ -85,12 +88,10 @@ def test_env_selector(self):
8588 self .assertTrue (self .query_context (ctx , operand = "-(align, equation)" ))
8689
8790 yield self .set_sel (self .view .find (r"<2>" , 0 ))
88- self .assertTrue (self .query_context (
89- ctx , operand = "document itemize - (align, equation)" ))
91+ self .assertTrue (self .query_context (ctx , operand = "document itemize - (align, equation)" ))
9092
9193 yield self .set_sel (self .view .find (r"<3>" , 0 ))
92- self .assertTrue (self .query_context (
93- ctx , operand = "(document itemize & align) - (equation)" ))
94+ self .assertTrue (self .query_context (ctx , operand = "(document itemize & align) - (equation)" ))
9495
9596 yield self .set_sel (self .view .find (r"<4>" , 0 ))
9697 operand = "document itemize & itemize itemize"
@@ -116,15 +117,13 @@ def test_env_selector(self):
116117 yield self .set_sel (self .view .find (r"<1>" , 0 ))
117118 yield self .view .sel ().add (self .view .find (r"<2>" , 0 ))
118119
119- self .assertTrue (self .query_context (
120- ctx , operand = "document itemize" , match_all = False ))
121- self .assertFalse (self .query_context (
122- ctx , operand = "document itemize" , match_all = True ))
120+ self .assertTrue (self .query_context (ctx , operand = "document itemize" , match_all = False ))
121+ self .assertFalse (self .query_context (ctx , operand = "document itemize" , match_all = True ))
123122
124123 def test_command_selector (self ):
125124 ctx = "latextools.command_selector"
126125 content = sublime .load_resource ("Packages/LaTeXTools/tests/fixtures/context/main.tex" )
127- self .set_content (content )
126+ self .setText (content )
128127 yield self .set_sel (self .view .find (r"<c1>" , 0 ))
129128 self .assertTrue (self .query_context (ctx , operand = "" ))
130129 self .assertTrue (self .query_context (ctx , operand = ", none" ))
@@ -141,8 +140,7 @@ def test_command_selector(self):
141140
142141 yield self .set_sel (self .view .find (r"<c4>" , 0 ))
143142 self .assertTrue (self .query_context (ctx , operand = "ensuremath" ))
144- self .assertFalse (self .query_context (
145- ctx , operand = "ensuremath - textnormal" ))
143+ self .assertFalse (self .query_context (ctx , operand = "ensuremath - textnormal" ))
146144
147145 yield self .set_sel (self .view .find (r"<c5>" , 0 ))
148146 self .assertTrue (self .query_context (ctx , operand = "starcommand" ))
0 commit comments