-
Notifications
You must be signed in to change notification settings - Fork 309
/
DocumentTest.scala
219 lines (175 loc) · 5.81 KB
/
DocumentTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package org.scalaide.core.text
import org.eclipse.jface.text.BadLocationException
import org.eclipse.jface.text.{Document => EDocument}
import org.junit.ComparisonFailure
import org.junit.Test
import org.scalaide.core.internal.text.TextDocument
import org.scalaide.util.eclipse.RegionUtils._
class DocumentTest {
type Op = Document => String
final def document(text: String): Document =
new TextDocument(new EDocument(text))
final implicit class Assert_===[A](actual: A) {
def ===(expected: A): Unit =
if (actual != expected)
throw new ComparisonFailure("", expected.toString(), actual.toString())
}
final implicit class StringAsTest(input: String) {
def becomes(expectedOutput: String) =
document(input) -> expectedOutput
}
final implicit class TestExecutor(testData: (Document, String)) {
def after(operation: Op) = test(testData._1, testData._2, operation)
}
final def test(doc: Document, expectedOutput: String, operation: Op): Unit = {
val actual = operation(doc)
actual === expectedOutput
}
@Test
def length_returns_the_length() = {
val d = document("hello world")
d.length === 11
}
@Test
def text_returns_string() =
"hello world" becomes "hello world" after (_.text)
@Test
def textRange_returns_range() =
"hello world" becomes "lo wo" after (_.textRange(3, 8))
@Test(expected = classOf[BadLocationException])
def textRange_throws_when_start_lower_than_zero() = {
val d = document("hello world")
d.textRange(-1, 8)
()
}
@Test(expected = classOf[BadLocationException])
def textRange_throws_when_end_lower_than_start() = {
val d = document("hello world")
d.textRange(5, 4)
()
}
@Test(expected = classOf[BadLocationException])
def textRange_throws_when_end_greater_than_length() = {
val d = document("hello world")
d.textRange(5, 20)
()
}
@Test
def textRangeOpt_returns_option() = {
val d = document("hello world")
d.textRangeOpt(3, 8) === Some("lo wo")
}
@Test
def textRangeOpt_returns_none_when_start_lower_than_zero() = {
val d = document("hello world")
d.textRangeOpt(-1, 8) === None
}
@Test
def textRangeOpt_returns_none_when_end_lower_than_start() = {
val d = document("hello world")
d.textRangeOpt(5, 4) === None
}
@Test
def textRangeOpt_returns_none_when_end_greater_than_length() = {
val d = document("hello world")
d.textRangeOpt(5, 20) === None
}
@Test
def lines_returns_all_lines() = {
val d = document("""|a
|bc
|def
|
|gh""".stripMargin)
d.lines.map(r => (r.start, r.end)) === Seq(
(0,1),
(2,4),
(5,8),
(9,9),
(10,12))
}
@Test
def lineCount_returns_the_number_of_lines() = {
val d = document("""|a
|bc
|def
|
|gh""".stripMargin)
d.lineCount === 5
}
@Test
def trim_trims_whitespace() =
" \t hello \t " becomes "hello" after (d => d.lineInformation(0).trim(d).text(d))
@Test
def trim_trims_nothing_when_there_is_no_whitespace() =
"hello" becomes "hello" after (d => d.lineInformation(0).trim(d).text(d))
@Test
def trimLeft_trims_left_whitespace() =
" \t hello \t " becomes "hello \t " after (d => d.lineInformation(0).trimLeft(d).text(d))
@Test
def trimLeft_trims_nothing_when_there_is_no_whitespace() =
"hello \t " becomes "hello \t " after (d => d.lineInformation(0).trimLeft(d).text(d))
@Test
def trimRight_trims_right_whitespace() =
" \t hello \t " becomes " \t hello" after (d => d.lineInformation(0).trimRight(d).text(d))
@Test
def trimRight_trims_nothing_when_there_is_no_whitespace() =
" \t hello" becomes " \t hello" after (d => d.lineInformation(0).trimRight(d).text(d))
@Test
def apply_on_non_empty_file_succeeds() =
document("some text").apply(3) === 'e'
@Test(expected = classOf[BadLocationException])
def apply_thrown_when_index_lower_than_zero(): Unit =
document("some text").apply(-1)
@Test(expected = classOf[BadLocationException])
def apply_thrown_when_index_greater_equal_than_length(): Unit =
document("some text").apply(9)
@Test
def head_on_non_empty_file_succeeds() =
document("some text").head === 's'
@Test
def headOpt_on_non_empty_file_succeeds() =
document("some text").headOpt === Some('s')
@Test
def tail_on_non_empty_file_succeeds() =
document("some text").tail === "ome text"
@Test
def tailOpt_on_non_empty_file_succeeds() =
document("some text").tailOpt === Some("ome text")
@Test
def init_on_non_empty_file_succeeds() =
document("some text").init === "some tex"
@Test
def initOpt_on_non_empty_file_succeeds() =
document("some text").initOpt === Some("some tex")
@Test
def last_on_non_empty_file_succeeds() =
document("some text").last === 't'
@Test
def lastOpt_on_non_empty_file_succeeds() =
document("some text").lastOpt === Some('t')
@Test(expected = classOf[BadLocationException])
def head_on_empty_file_throws(): Unit =
document("").head
@Test
def headOpt_on_empty_file_returns_none() =
document("").headOpt === None
@Test(expected = classOf[BadLocationException])
def tail_on_empty_file_throws(): Unit =
document("").tail
@Test
def tailOpt_on_empty_file_returns_none() =
document("").tailOpt === None
@Test(expected = classOf[BadLocationException])
def init_on_empty_file_throws(): Unit =
document("").init
@Test
def initOpt_on_empty_file_returns_none() =
document("").initOpt === None
@Test(expected = classOf[BadLocationException])
def last_on_empty_file_throws(): Unit =
document("").last
@Test
def lastOpt_on_empty_file_returns_none() =
document("").lastOpt === None
}