Skip to content

Commit 460f3c9

Browse files
committed
Migrate to Unittesting test cases
1. use unittesting.ViewTestCase 2. format source with python black 3. prevent Unittesting from reloading LaTeXTools
1 parent f2800d9 commit 460f3c9

3 files changed

Lines changed: 68 additions & 177 deletions

File tree

tests/test_context.py

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,30 @@
1-
from unittest import TestCase
1+
from __future__ import annotations
2+
from unittesting import ViewTestCase
23

34
import sublime
45

56
from 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"))

tests/test_selectors.py

Lines changed: 21 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from unittest import TestCase
22

3-
from LaTeXTools.latextools.utils.selectors import (
4-
AstNode as Node, AstLeaf as Leaf, build_ast
5-
)
3+
from LaTeXTools.latextools.utils.selectors import AstNode as Node, AstLeaf as Leaf, build_ast
64

75

86
class BuildAstTest(TestCase):
@@ -25,138 +23,58 @@ def test_simple(self):
2523

2624
def test_nested(self):
2725
selector = "list equation"
28-
ast = Node(
29-
" ",
30-
Leaf("list"),
31-
Leaf("equation")
32-
)
26+
ast = Node(" ", Leaf("list"), Leaf("equation"))
3327
self.assertEqual(ast, build_ast(selector))
3428

3529
def test_nested_or(self):
3630
selector = "align | list equation"
37-
ast = Node(
38-
"|",
39-
Leaf("align"),
40-
Node(
41-
" ",
42-
Leaf("list"),
43-
Leaf("equation")
44-
)
45-
)
31+
ast = Node("|", Leaf("align"), Node(" ", Leaf("list"), Leaf("equation")))
4632
self.assertEqual(ast, build_ast(selector))
4733

4834
def test_fill_empty1(self):
4935
selector = "- test"
50-
ast = Node(
51-
"-",
52-
Leaf(""),
53-
Leaf("test")
54-
)
36+
ast = Node("-", Leaf(""), Leaf("test"))
5537
self.assertEqual(ast, build_ast(selector))
5638

5739
def test_fill_empty2(self):
5840
selector = "- (- b)"
59-
ast = Node(
60-
"-",
61-
Leaf(""),
62-
Node(
63-
"-",
64-
Leaf(""),
65-
Leaf("b")
66-
)
67-
)
41+
ast = Node("-", Leaf(""), Node("-", Leaf(""), Leaf("b")))
6842
self.assertEqual(ast, build_ast(selector))
6943

7044
def test_fill_empty3(self):
7145
selector = "a, - b"
72-
ast = Node(
73-
",",
74-
Leaf("a"),
75-
Node(
76-
"-",
77-
Leaf(""),
78-
Leaf("b")
79-
)
80-
)
46+
ast = Node(",", Leaf("a"), Node("-", Leaf(""), Leaf("b")))
8147
self.assertEqual(ast, build_ast(selector))
8248

8349
def test_fill_empty4(self):
8450
selector = "a, &"
85-
ast = Node(
86-
",",
87-
Leaf("a"),
88-
Node(
89-
"&",
90-
Leaf(""),
91-
Leaf("")
92-
)
93-
)
51+
ast = Node(",", Leaf("a"), Node("&", Leaf(""), Leaf("")))
9452
self.assertEqual(ast, build_ast(selector))
9553

9654
def test_fill_empty5(self):
9755
selector = "a, - b & (d, )"
9856
ast = Node(
9957
",",
10058
Leaf("a"),
101-
Node(
102-
"&",
103-
Node(
104-
"-",
105-
Leaf(""),
106-
Leaf("b")
107-
),
108-
Node(
109-
",",
110-
Leaf("d"),
111-
Leaf("")
112-
)
113-
)
59+
Node("&", Node("-", Leaf(""), Leaf("b")), Node(",", Leaf("d"), Leaf(""))),
11460
)
11561
self.assertEqual(ast, build_ast(selector))
11662

11763
def test_complex1(self):
11864
selector = "align & enumerate itemize | list equation"
11965
ast = Node(
12066
"|",
121-
Node(
122-
"&",
123-
Leaf("align"),
124-
Node(
125-
" ",
126-
Leaf("enumerate"),
127-
Leaf("itemize")
128-
)
129-
),
130-
Node(
131-
" ",
132-
Leaf("list"),
133-
Leaf("equation")
134-
)
67+
Node("&", Leaf("align"), Node(" ", Leaf("enumerate"), Leaf("itemize"))),
68+
Node(" ", Leaf("list"), Leaf("equation")),
13569
)
13670
self.assertEqual(ast, build_ast(selector))
13771

13872
def test_complex2(self):
13973
selector = "a, b - c, d - e f"
14074
ast = Node(
14175
",",
142-
Node(
143-
",",
144-
Leaf("a"),
145-
Node(
146-
"-",
147-
Leaf("b"),
148-
Leaf("c")
149-
)
150-
),
151-
Node(
152-
"-",
153-
Leaf("d"),
154-
Node(
155-
" ",
156-
Leaf("e"),
157-
Leaf("f")
158-
)
159-
)
76+
Node(",", Leaf("a"), Node("-", Leaf("b"), Leaf("c"))),
77+
Node("-", Leaf("d"), Node(" ", Leaf("e"), Leaf("f"))),
16078
)
16179
self.assertEqual(ast, build_ast(selector))
16280

@@ -172,47 +90,19 @@ def test_complex3(self):
17290
Leaf("b"),
17391
Node(
17492
"-",
175-
Node(
176-
"|",
177-
Leaf("c"),
178-
Node(
179-
" ",
180-
Leaf("d"),
181-
Leaf("e")
182-
)
183-
),
184-
Node(
185-
" ",
186-
Leaf("f"),
187-
Leaf("g")
188-
)
189-
)
190-
)
93+
Node("|", Leaf("c"), Node(" ", Leaf("d"), Leaf("e"))),
94+
Node(" ", Leaf("f"), Leaf("g")),
95+
),
96+
),
19197
),
19298
Node(
19399
"&",
194-
Node(
195-
" ",
196-
Leaf("h"),
197-
Leaf("i")
198-
),
100+
Node(" ", Leaf("h"), Leaf("i")),
199101
Node(
200102
"-",
201-
Node(
202-
" ",
203-
Leaf("j"),
204-
Leaf("k")
205-
),
206-
Node(
207-
"|",
208-
Leaf("l"),
209-
Node(
210-
" ",
211-
Leaf("m"),
212-
Leaf("n")
213-
)
214-
)
215-
)
216-
)
103+
Node(" ", Leaf("j"), Leaf("k")),
104+
Node("|", Leaf("l"), Node(" ", Leaf("m"), Leaf("n"))),
105+
),
106+
),
217107
)
218108
self.assertEqual(ast, build_ast(selector))

unittesting.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"reload_package_on_testing": false
3+
}

0 commit comments

Comments
 (0)