Skip to content

Commit af11ad0

Browse files
committed
Add a test for SmartDocumentFilter
1 parent 2f78e78 commit af11ad0

1 file changed

Lines changed: 209 additions & 0 deletions

File tree

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package groovy.console.ui.text
20+
21+
import org.junit.jupiter.api.Test
22+
23+
import javax.swing.text.DefaultStyledDocument
24+
25+
class SmartDocumentFilterTest {
26+
27+
@Test
28+
void testInsertStringAddsText() {
29+
def doc = new DefaultStyledDocument()
30+
def filter = new SmartDocumentFilter(doc)
31+
doc.setDocumentFilter(filter)
32+
33+
doc.insertString(0, 'def x = 1', null)
34+
35+
assert doc.getText(0, doc.getLength()) == 'def x = 1'
36+
}
37+
38+
@Test
39+
void testReplaceText() {
40+
def doc = new DefaultStyledDocument()
41+
def filter = new SmartDocumentFilter(doc)
42+
doc.setDocumentFilter(filter)
43+
44+
doc.insertString(0, 'hello world', null)
45+
doc.replace(0, 5, 'goodbye', null)
46+
47+
assert doc.getText(0, doc.getLength()) == 'goodbye world'
48+
}
49+
50+
@Test
51+
void testRemoveText() {
52+
def doc = new DefaultStyledDocument()
53+
def filter = new SmartDocumentFilter(doc)
54+
doc.setDocumentFilter(filter)
55+
56+
doc.insertString(0, 'abcdef', null)
57+
doc.remove(0, 3)
58+
59+
assert doc.getText(0, doc.getLength()) == 'def'
60+
}
61+
62+
@Test
63+
void testCarriageReturnStripped() {
64+
def doc = new DefaultStyledDocument()
65+
def filter = new SmartDocumentFilter(doc)
66+
doc.setDocumentFilter(filter)
67+
68+
doc.insertString(0, "line1\r\nline2", null)
69+
70+
String text = doc.getText(0, doc.getLength())
71+
assert !text.contains('\r')
72+
assert text.contains('line1\nline2')
73+
}
74+
75+
@Test
76+
void testGroovyKeywordHighlighting() {
77+
def doc = new DefaultStyledDocument()
78+
def filter = new SmartDocumentFilter(doc)
79+
doc.setDocumentFilter(filter)
80+
81+
doc.insertString(0, 'def x = 42', null)
82+
83+
// Verify document has content and no exception thrown during parse
84+
assert doc.getLength() == 10
85+
assert doc.getText(0, doc.getLength()) == 'def x = 42'
86+
}
87+
88+
@Test
89+
void testMultipleInsertsAccumulateText() {
90+
def doc = new DefaultStyledDocument()
91+
def filter = new SmartDocumentFilter(doc)
92+
doc.setDocumentFilter(filter)
93+
94+
doc.insertString(0, 'class Foo {', null)
95+
doc.insertString(doc.getLength(), '\n def bar() {}', null)
96+
doc.insertString(doc.getLength(), '\n}', null)
97+
98+
def text = doc.getText(0, doc.getLength())
99+
assert text.contains('class Foo')
100+
assert text.contains('def bar()')
101+
}
102+
103+
@Test
104+
void testEmptyDocumentParsesWithoutError() {
105+
def doc = new DefaultStyledDocument()
106+
def filter = new SmartDocumentFilter(doc)
107+
doc.setDocumentFilter(filter)
108+
109+
// Just setting the filter with empty document should not throw
110+
assert doc.getLength() == 0
111+
}
112+
113+
@Test
114+
void testIsLatestAfterParse() {
115+
def doc = new DefaultStyledDocument()
116+
def filter = new SmartDocumentFilter(doc)
117+
doc.setDocumentFilter(filter)
118+
119+
doc.insertString(0, 'println "hello"', null)
120+
121+
assert filter.isLatest()
122+
}
123+
124+
@Test
125+
void testGetLatestTokenListPopulatedAfterParse() {
126+
def doc = new DefaultStyledDocument()
127+
def filter = new SmartDocumentFilter(doc)
128+
doc.setDocumentFilter(filter)
129+
130+
doc.insertString(0, 'def x = 1', null)
131+
132+
def tokens = filter.getLatestTokenList()
133+
assert tokens != null
134+
assert !tokens.isEmpty()
135+
}
136+
137+
@Test
138+
void testRenderRangeGetSetNull() {
139+
def doc = new DefaultStyledDocument()
140+
def filter = new SmartDocumentFilter(doc)
141+
142+
assert filter.getRenderRange() == null
143+
144+
filter.setRenderRange(Tuple.tuple(0, 10))
145+
assert filter.getRenderRange() != null
146+
147+
filter.setRenderRange(null)
148+
assert filter.getRenderRange() == null
149+
}
150+
151+
@Test
152+
void testHighlightedTokenTypeListContainsKeywords() {
153+
def list = SmartDocumentFilter.HIGHLIGHTED_TOKEN_TYPE_LIST
154+
assert list != null
155+
assert !list.isEmpty()
156+
// list should contain token type integers (all > 0)
157+
assert list.every { it > 0 }
158+
}
159+
160+
@Test
161+
void testReparseDocumentDoesNotThrow() {
162+
def doc = new DefaultStyledDocument()
163+
def filter = new SmartDocumentFilter(doc)
164+
doc.setDocumentFilter(filter)
165+
166+
doc.insertString(0, 'for (int i = 0; i < 10; i++) { println i }', null)
167+
filter.reparseDocument()
168+
169+
assert filter.isLatest()
170+
assert doc.getText(0, doc.getLength()).contains('for')
171+
}
172+
173+
@Test
174+
void testComplexGroovyCodeParsesCorrectly() {
175+
def doc = new DefaultStyledDocument()
176+
def filter = new SmartDocumentFilter(doc)
177+
doc.setDocumentFilter(filter)
178+
179+
def code = '''\
180+
import java.util.stream.Collectors
181+
182+
@groovy.transform.CompileStatic
183+
class Example {
184+
static List<String> transform(List<Integer> nums) {
185+
return nums.stream()
186+
.filter { it > 0 }
187+
.map { "num: ${it}" }
188+
.collect(Collectors.toList())
189+
}
190+
}'''
191+
doc.insertString(0, code, null)
192+
193+
assert filter.isLatest()
194+
def tokens = filter.getLatestTokenList()
195+
assert tokens.size() > 10
196+
}
197+
198+
@Test
199+
void testReplaceWithNullTextTreatedAsEmpty() {
200+
def doc = new DefaultStyledDocument()
201+
def filter = new SmartDocumentFilter(doc)
202+
doc.setDocumentFilter(filter)
203+
204+
doc.insertString(0, 'abc', null)
205+
doc.replace(1, 1, null, null)
206+
207+
assert doc.getText(0, doc.getLength()) == 'ac'
208+
}
209+
}

0 commit comments

Comments
 (0)