forked from JuliaLang/julia
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotated.jl
212 lines (207 loc) · 12.3 KB
/
annotated.jl
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
# This file is a part of Julia. License is MIT: https://julialang.org/license
@testset "AnnotatedString" begin
str = Base.AnnotatedString("some string")
@test str == Base.AnnotatedString(str.string, Tuple{UnitRange{Int}, Pair{Symbol, Any}}[])
@test length(str) == 11
@test ncodeunits(str) == 11
@test eltype(str) == Base.AnnotatedChar{eltype(str.string)}
@test first(str) == Base.AnnotatedChar(first(str.string), Pair{Symbol, Any}[])
@test str[1:4] isa SubString{typeof(str)}
@test str[1:4] == Base.AnnotatedString("some")
@test "a" * str == Base.AnnotatedString("asome string")
@test str * "a" == Base.AnnotatedString("some stringa")
@test str * str == Base.AnnotatedString("some stringsome string")
Base.annotate!(str, 1:4, :thing => 0x01)
Base.annotate!(str, 6:11, :other => 0x02)
Base.annotate!(str, 1:11, :all => 0x03)
# :thing :other
# ┌┸─┐ ┌──┸─┐
# "some string"
# └───┰─────┘
# :all
@test str[3:4] == SubString(str, 3, 4)
@test Base.AnnotatedString(str[3:4]) ==
Base.AnnotatedString("me", [(1:2, :thing => 0x01), (1:2, :all => 0x03)])
@test Base.AnnotatedString(str[3:6]) ==
Base.AnnotatedString("me s", [(1:2, :thing => 0x01), (4:4, :other => 0x02), (1:4, :all => 0x03)])
@test str == Base.AnnotatedString("some string", [(1:4, :thing => 0x01), (6:11, :other => 0x02), (1:11, :all => 0x03)])
@test str != Base.AnnotatedString("some string")
@test str != Base.AnnotatedString("some string", [(1:1, :thing => 0x01), (1:11, :all => 0x03), (6:6, :other => 0x02)])
@test str != Base.AnnotatedString("some string", [(1:4, :thing => 0x11), (1:11, :all => 0x13), (6:11, :other => 0x12)])
@test str != Base.AnnotatedString("some thingg", [(1:4, :thing => 0x01), (1:11, :all => 0x03), (6:11, :other => 0x02)])
@test Base.AnnotatedString([Base.AnnotatedChar('a', [:a => 1]), Base.AnnotatedChar('b', [:b => 2])]) ==
Base.AnnotatedString("ab", [(1:1, :a => 1), (2:2, :b => 2)])
let allstrings =
['a', Base.AnnotatedChar('a'), Base.AnnotatedChar('a', [:aaa => 0x04]),
"a string", Base.AnnotatedString("a string"),
Base.AnnotatedString("a string", [(1:2, :hmm => '%')]),
SubString(Base.AnnotatedString("a string", [(1:2, :hmm => '%')]), 1:1)]
for str1 in repeat(allstrings, 2)
for str2 in repeat(allstrings, 2)
@test String(str1 * str2) ==
String(string(str1, str2)) ==
String(string(str1)) * String(string(str2))
@test Base.annotatedstring(str1 * str2) ==
Base.annotatedstring(str1, str2) ==
Base.annotatedstring(str1) * Base.annotatedstring(str2)
end
end
end
# @test collect(Base.eachstyle(str)) ==
# [("some", [:thing => 0x01, :all => 0x03]),
# (" string", [:all => 0x03, :other => 0x02])]
@test chopprefix(sprint(show, str), "Base.") ==
"AnnotatedString{String}(\"some string\", [(1:4, :thing => 0x01), (6:11, :other => 0x02), (1:11, :all => 0x03)])"
@test eval(Meta.parse(repr(str))) == str
@test sprint(show, MIME("text/plain"), str) == "\"some string\""
end
@testset "AnnotatedChar" begin
chr = Base.AnnotatedChar('c')
@test chr == Base.AnnotatedChar(chr.char, Pair{Symbol, Any}[])
str = Base.AnnotatedString("hmm", [(1:1, :attr => "h0h0"),
(1:2, :attr => "h0m1"),
(2:3, :attr => "m1m2")])
@test str[1] == Base.AnnotatedChar('h', Pair{Symbol, Any}[:attr => "h0h0"])
@test str[2] == Base.AnnotatedChar('m', Pair{Symbol, Any}[:attr => "h0m1", :attr => "m1m2"])
@test str[3] == Base.AnnotatedChar('m', Pair{Symbol, Any}[:attr => "m1m2"])
end
@testset "Styling preservation" begin
str = Base.AnnotatedString("some string", [(1:4, :thing => 0x01), (1:11, :all => 0x03), (6:11, :other => 0x02)])
@test match(r".e", str).match == str[3:4]
@test match(r"(.e)", str).captures == [str[3:4]]
let m0 = match(r"(.)e", str)
m1 = first(eachmatch(r"(.)e", str))
for f in fieldnames(RegexMatch)
@test getfield(m0, f) == getfield(m1, f)
end
end
@test lpad(str, 12) ==
Base.AnnotatedString(" some string", [(2:5, :thing => 0x01),
(2:12, :all => 0x03),
(7:12, :other => 0x02)])
@test rpad(str, 12) ==
Base.AnnotatedString("some string ", [(1:4, :thing => 0x01),
(1:11, :all => 0x03),
(6:11, :other => 0x02)])
str1 = Base.AnnotatedString("test", [(1:4, :label => 5)])
str2 = Base.AnnotatedString("case", [(2:3, :label => "oomph")])
@test join([str1, str1], ' ') ==
Base.AnnotatedString("test test",
[(1:4, :label => 5),
(6:9, :label => 5)])
@test join([str1, str1], Base.AnnotatedString(" ", [(1:1, :label => 2)])) ==
Base.AnnotatedString("test test",
[(1:4, :label => 5),
(5:5, :label => 2),
(6:9, :label => 5)])
@test repeat(str1, 2) == Base.AnnotatedString("testtest", [(1:8, :label => 5)])
@test repeat(str2, 2) == Base.AnnotatedString("casecase", [(2:3, :label => "oomph"),
(6:7, :label => "oomph")])
@test repeat(str1[1], 3) == Base.AnnotatedString("ttt", [(1:3, :label => 5)])
@test reverse(str1) == Base.AnnotatedString("tset", [(1:4, :label => 5)])
@test reverse(str2) == Base.AnnotatedString("esac", [(2:3, :label => "oomph")])
end
@testset "Unicode" begin
for words in (["ᲃase", "cɦɒnɡeȿ", "can", "CHⱯNGE", "Сodeunıts"],
["Сodeunıts", "ᲃase", "cɦɒnɡeȿ", "can", "CHⱯNGE"])
ann_words = [Base.AnnotatedString(w, [(1:ncodeunits(w), :i => i)])
for (i, w) in enumerate(words)]
ann_str = join(ann_words, '-')
for transform in (lowercase, uppercase, titlecase)
t_words = map(transform, words)
ann_t_words = [Base.AnnotatedString(w, [(1:ncodeunits(w), :i => i)])
for (i, w) in enumerate(t_words)]
ann_t_str = join(ann_t_words, '-')
t_ann_str = transform(ann_str)
@test String(ann_t_str) == String(t_ann_str)
@test Base.annotations(ann_t_str) == Base.annotations(t_ann_str)
end
for transform in (uppercasefirst, lowercasefirst)
t_words = vcat(transform(first(words)), words[2:end])
ann_t_words = [Base.AnnotatedString(w, [(1:ncodeunits(w), :i => i)])
for (i, w) in enumerate(t_words)]
ann_t_str = join(ann_t_words, '-')
t_ann_str = transform(ann_str)
@test String(ann_t_str) == String(t_ann_str)
@test Base.annotations(ann_t_str) == Base.annotations(t_ann_str)
end
end
end
@testset "AnnotatedIOBuffer" begin
aio = Base.AnnotatedIOBuffer()
# Append-only writing
@test write(aio, Base.AnnotatedString("hello", [(1:5, :tag => 1)])) == 5
@test write(aio, ' ') == 1
@test write(aio, Base.AnnotatedString("world", [(1:5, :tag => 2)])) == 5
@test Base.annotations(aio) == [(1:5, :tag => 1), (7:11, :tag => 2)]
# Check `annotate!`, including region sorting
@test truncate(aio, 0).io.size == 0
@test write(aio, "hello world") == ncodeunits("hello world")
@test Base.annotate!(aio, 1:5, :tag => 1) === aio
@test Base.annotate!(aio, 7:11, :tag => 2) === aio
@test Base.annotations(aio) == [(1:5, :tag => 1), (7:11, :tag => 2)]
# Reading
@test read(seekstart(deepcopy(aio.io)), String) == "hello world"
@test read(seekstart(deepcopy(aio)), String) == "hello world"
@test read(seek(aio, 0), Base.AnnotatedString) == Base.AnnotatedString("hello world", [(1:5, :tag => 1), (7:11, :tag => 2)])
@test read(seek(aio, 1), Base.AnnotatedString) == Base.AnnotatedString("ello world", [(1:4, :tag => 1), (6:10, :tag => 2)])
@test read(seek(aio, 4), Base.AnnotatedString) == Base.AnnotatedString("o world", [(1:1, :tag => 1), (3:7, :tag => 2)])
@test read(seek(aio, 5), Base.AnnotatedString) == Base.AnnotatedString(" world", [(2:6, :tag => 2)])
@test read(seekend(aio), Base.AnnotatedString) == Base.AnnotatedString("")
@test read(seekstart(truncate(deepcopy(aio), 5)), Base.AnnotatedString) == Base.AnnotatedString("hello", [(1:5, :tag => 1)])
@test read(seekstart(truncate(deepcopy(aio), 6)), Base.AnnotatedString) == Base.AnnotatedString("hello ", [(1:5, :tag => 1)])
@test read(seekstart(truncate(deepcopy(aio), 7)), Base.AnnotatedString) == Base.AnnotatedString("hello w", [(1:5, :tag => 1), (7:7, :tag => 2)])
@test read(seek(aio, 0), Base.AnnotatedChar) == Base.AnnotatedChar('h', [:tag => 1])
@test read(seek(aio, 5), Base.AnnotatedChar) == Base.AnnotatedChar(' ', Pair{Symbol, Any}[])
@test read(seek(aio, 6), Base.AnnotatedChar) == Base.AnnotatedChar('w', [:tag => 2])
# Check method compatibility with IOBuffer
@test position(aio) == 7
@test seek(aio, 4) === aio
@test skip(aio, 2) === aio
@test Base.annotations(copy(aio)) == Base.annotations(aio)
@test take!(copy(aio).io) == take!(copy(aio.io))
# Writing into the middle of the buffer
@test write(seek(aio, 6), "alice") == 5 # Replace 'world' with 'alice'
@test read(seekstart(aio), String) == "hello alice"
@test Base.annotations(aio) == [(1:5, :tag => 1), (7:11, :tag => 2)] # Should be unchanged
@test write(seek(aio, 0), Base.AnnotatedString("hey-o", [(1:5, :hey => 'o')])) == 5
@test read(seekstart(aio), String) == "hey-o alice"
@test Base.annotations(aio) == [(7:11, :tag => 2), (1:5, :hey => 'o')] # First annotation should have been entirely replaced
@test write(seek(aio, 7), Base.AnnotatedString("bbi", [(1:3, :hey => 'a')])) == 3 # a[lic => bbi]e ('alice' => 'abbie')
@test read(seekstart(aio), String) == "hey-o abbie"
@test Base.annotations(aio) == [(7:7, :tag => 2), (11:11, :tag => 2), (1:5, :hey => 'o'), (8:10, :hey => 'a')]
@test write(seek(aio, 0), Base.AnnotatedString("ab")) == 2 # Check first annotation's region is adjusted correctly
@test read(seekstart(aio), String) == "aby-o abbie"
@test Base.annotations(aio) == [(7:7, :tag => 2), (11:11, :tag => 2), (3:5, :hey => 'o'), (8:10, :hey => 'a')]
@test write(seek(aio, 3), Base.AnnotatedString("ss")) == 2
@test read(seekstart(aio), String) == "abyss abbie"
@test Base.annotations(aio) == [(7:7, :tag => 2), (11:11, :tag => 2), (3:3, :hey => 'o'), (8:10, :hey => 'a')]
# Writing one buffer to another
newaio = Base.AnnotatedIOBuffer()
@test write(newaio, seekstart(aio)) == 11
@test read(seekstart(newaio), String) == "abyss abbie"
@test Base.annotations(newaio) == Base.annotations(aio)
@test write(seek(newaio, 5), seek(aio, 5)) == 6
@test sort(Base.annotations(newaio)) == sort(Base.annotations(aio))
@test write(newaio, seek(aio, 5)) == 6
@test read(seekstart(newaio), String) == "abyss abbie abbie"
@test sort(Base.annotations(newaio)) == sort(vcat(Base.annotations(aio), [(13:13, :tag => 2), (14:16, :hey => 'a'), (17:17, :tag => 2)]))
# The `_insert_annotations!` cautious-merging optimisation
aio = Base.AnnotatedIOBuffer()
@test write(aio, Base.AnnotatedChar('a', [:a => 1, :b => 2])) == 1
@test Base.annotations(aio) == [(1:1, :a => 1), (1:1, :b => 2)]
@test write(aio, Base.AnnotatedChar('b', [:a => 1, :b => 2])) == 1
@test Base.annotations(aio) == [(1:2, :a => 1), (1:2, :b => 2)]
let aio2 = copy(aio) # A different start makes merging too risky to do.
@test write(aio2, Base.AnnotatedChar('c', [:a => 0, :b => 2])) == 1
@test Base.annotations(aio2) == [(1:2, :a => 1), (1:2, :b => 2), (3:3, :a => 0), (3:3, :b => 2)]
end
let aio2 = copy(aio) # Merging some run of the most recent annotations is fine though.
@test write(aio2, Base.AnnotatedChar('c', [:b => 2])) == 1
@test Base.annotations(aio2) == [(1:2, :a => 1), (1:3, :b => 2)]
end
let aio2 = copy(aio) # ...and any subsequent annotations after a matching run can just be copied over.
@test write(aio2, Base.AnnotatedChar('c', [:b => 2, :c => 3, :d => 4])) == 1
@test Base.annotations(aio2) == [(1:2, :a => 1), (1:3, :b => 2), (3:3, :c => 3), (3:3, :d => 4)]
end
end