Skip to content

Commit 4972555

Browse files
ararslannalimilan
authored andcommitted
String-related 0.7 fixes (JuliaData#114)
Adapt to 0.7 string API changes. Stop using "é" in tests since it triggers failures in Base, and the only goal here is to check that methods are implemented (not that they are correct, which is the job of Base).
1 parent 59bf7f2 commit 4972555

File tree

3 files changed

+82
-55
lines changed

3 files changed

+82
-55
lines changed

REQUIRE

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
julia 0.6
22
Missings
33
Reexport
4-
Compat 0.39.0
5-
JSON
4+
Compat 0.53.0
5+
JSON

src/value.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ end
144144
Base.string(x::CategoricalString) = get(x)
145145
Base.eltype(x::CategoricalString) = Char
146146
Base.length(x::CategoricalString) = length(get(x))
147-
Base.endof(x::CategoricalString) = endof(get(x))
147+
Compat.lastindex(x::CategoricalString) = lastindex(get(x))
148148
Base.sizeof(x::CategoricalString) = sizeof(get(x))
149-
Base.nextind(x::CategoricalString, i::Integer) = nextind(get(x), i)
150-
Base.prevind(x::CategoricalString, i::Integer) = prevind(get(x), i)
149+
Base.nextind(x::CategoricalString, i::Int) = nextind(get(x), i)
150+
Base.prevind(x::CategoricalString, i::Int) = prevind(get(x), i)
151151
Base.next(x::CategoricalString, i::Int) = next(get(x), i)
152152
Base.getindex(x::CategoricalString, i::Int) = getindex(get(x), i)
153153
Base.codeunit(x::CategoricalString, i::Integer) = codeunit(get(x), i)
@@ -157,9 +157,18 @@ Base.isvalid(x::CategoricalString, i::Integer) = isvalid(get(x), i)
157157
Base.match(r::Regex, s::CategoricalString,
158158
idx::Integer=start(s), add_opts::UInt32=UInt32(0)) =
159159
match(r, get(s), idx, add_opts)
160-
Base.matchall(r::Regex, s::CategoricalString, overlap::Bool=false) =
161-
matchall(r, get(s), overlap)
160+
if VERSION > v"0.7.0-DEV.3526"
161+
Base.matchall(r::Regex, s::CategoricalString; overlap::Bool=false) =
162+
matchall(r, get(s); overlap=overlap)
163+
else
164+
Base.matchall(r::Regex, s::CategoricalString; overlap::Bool=false) =
165+
matchall(r, get(s), overlap)
166+
Base.matchall(r::Regex, s::CategoricalString, overlap::Bool) =
167+
matchall(r, get(s), overlap)
168+
end
162169
Base.collect(x::CategoricalString) = collect(get(x))
170+
Base.reverse(x::CategoricalString) = reverse(get(x))
171+
Compat.ncodeunits(x::CategoricalString) = ncodeunits(get(x))
163172

164173
# JSON of CatValue is JSON of the value it refers to
165174
JSON.lower(x::CatValue) = get(x)

test/08_string.jl

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module TestString
22
using Compat
33
using Compat.Test
4+
using Compat.Unicode
45
using CategoricalArrays
56

67
@testset "AbstractString operations on values of CategoricalPool{String}" begin
@@ -40,22 +41,35 @@ using CategoricalArrays
4041
@test sizeof(v1) === 0
4142
@test sizeof(v2) === 5
4243

43-
@test nextind(v1, 1) === 2
44+
if VERSION >= v"0.7.0-DEV.2949"
45+
@test_throws BoundsError nextind(v1, 1)
46+
else
47+
@test nextind(v1, 1) === 2
48+
end
4449
@test nextind(v2, 4) === 6
4550

4651
@test prevind(v1, 1) === 0
4752
@test prevind(v2, 6) === 4
4853

49-
@test endof(v1) === 0
50-
@test endof(v2) === 4
54+
if VERSION >= v"0.7.0-DEV.3583"
55+
@test firstindex(v1) === 1
56+
@test firstindex(v2) === 1
57+
end
58+
59+
@test lastindex(v1) === 0
60+
@test lastindex(v2) === 4
5161

5262
@test collect(v1) == []
5363
@test collect(v2) == collect("café")
5464

5565
@test v2[2] === 'a'
5666
@test v2[4] === 'é'
5767
@test_throws BoundsError v1[1]
58-
@test_throws UnicodeError v2[5]
68+
if VERSION >= v"0.7.0-DEV.2949"
69+
@test_throws StringIndexError v2[5]
70+
else
71+
@test_throws UnicodeError v2[5]
72+
end
5973

6074
@test codeunit(v2, 2) === 0x61
6175
@test codeunit(v2, 5) === 0xa9
@@ -65,9 +79,9 @@ using CategoricalArrays
6579
@test ascii(v1)::String == ""
6680
@test_throws ArgumentError ascii(v2)
6781

68-
@test normalize_string(v1) == ""
69-
@test normalize_string(v2) == "café"
70-
@test normalize_string(v2, :NFKD) == "café"
82+
@test Unicode.normalize_string(v1) == ""
83+
@test Unicode.normalize_string(v2) == "café"
84+
@test Unicode.normalize_string(v2, :NFKD) == "café"
7185

7286
@test isempty(collect(graphemes(v1)))
7387
@test collect(graphemes(v2)) == collect(graphemes("café"))
@@ -78,11 +92,19 @@ using CategoricalArrays
7892
@test isvalid(v2, 4)
7993
@test !isvalid(v2, 5)
8094

81-
@test_throws BoundsError ind2chr(v1, 0)
82-
@test ind2chr(v2, 4) === 4
95+
if VERSION >= v"0.7.0-DEV.2949"
96+
@test_throws BoundsError length(v1, 0, 0)
97+
@test length(v2, 1, 4) === 4
98+
99+
@test_throws BoundsError nextind(v1, 1, 1)
100+
@test nextind(v2, 1, 2) === 3
101+
else
102+
@test_throws BoundsError ind2chr(v1, 0)
103+
@test ind2chr(v2, 4) === 4
83104

84-
@test_throws BoundsError chr2ind(v1, 1)
85-
@test chr2ind(v2, 2) === 2
105+
@test_throws BoundsError chr2ind(v1, 1)
106+
@test chr2ind(v2, 2) === 2
107+
end
86108

87109
@test string(v1) == ""
88110
@test string(v2) == "café"
@@ -108,20 +130,22 @@ using CategoricalArrays
108130
@test repeat(v1, 10) == ""
109131
@test repeat(v2, 2) == "cafécafé"
110132

111-
@test !ismatch(r"", v1)
112-
@test ismatch(r"", v2)
113-
114-
@test isempty(collect(eachmatch(r"", v1)))
115-
@test first(eachmatch(r"", v2)).offset == 3
133+
@test isempty(collect(eachmatch(r"af", v1)))
134+
@test first(eachmatch(r"af", v2)).offset == 2
116135

117-
@test match(r"", v1) === nothing
118-
@test match(r"", v2).offset === 3
119-
@test match(r"", v2, 2).offset === 3
120-
@test match(r"", v2, 2, UInt32(0)).offset === 3
136+
@test match(r"af", v1) === nothing
137+
@test match(r"af", v2).offset === 2
138+
@test match(r"af", v2, 2).offset === 2
139+
@test match(r"af", v2, 2, UInt32(0)).offset === 2
121140

122-
@test matchall(r"", v1) == []
123-
@test matchall(r"", v2) == [""]
124-
@test matchall(r"", v2, true) == [""]
141+
@test matchall(r"af", v1) == []
142+
@test matchall(r"af", v2) == ["af"]
143+
if VERSION > v"0.7.0-DEV.3526"
144+
@test matchall(r"af", v2, overlap=true) == ["af"]
145+
else
146+
@test matchall(r"af", v2, overlap=true) == ["af"]
147+
@test matchall(r"af", v2, true) == ["af"]
148+
end
125149

126150
@test lpad(v1, 1) == " "
127151
@test lpad(v2, 1) == "café"
@@ -131,31 +155,23 @@ using CategoricalArrays
131155
@test rpad(v2, 1) == "café"
132156
@test rpad(v2, 5) == "café "
133157

134-
@test search(v1, "") === 1:0
135-
@test search(v2, "a") === 2:2
136-
@test search(v2, 'a') === 2
137-
@test search(v2, 'a', 3) === 0
158+
@test Compat.findfirst("", v1) === 1:0
159+
@test Compat.findfirst("a", v2) === 2:2
160+
@test Compat.findfirst(equalto('a'), v2) === 2
161+
@test Compat.findnext(equalto('a'), v2, 3) === nothing
138162

139-
@test searchindex(v1, "") === 1
140-
@test searchindex(v2, "a") === 2
141-
@test searchindex(v2, 'a') === 2
142-
@test searchindex(v2, 'a', 3) === 0
143-
144-
@test rsearch(v1, "a") === 0:-1
145-
@test rsearch(v2, "a") === 2:2
146-
@test rsearch(v2, 'a') === 2
147-
@test rsearch(v2, 'a', 1) === 0
148-
149-
@test rsearchindex(v1, "a") === 0
150-
@test rsearchindex(v2, "a") === 2
151-
# Methods not defined even for String
152-
#@test rsearchindex(v2, 'a') === 2
153-
#@test rsearchindex(v2, 'a', 1) === 0
163+
@test Compat.findlast("a", v1) === 0:-1
164+
@test Compat.findlast("a", v2) === 2:2
165+
@test Compat.findlast(equalto('a'), v2) === 2
166+
@test Compat.findprev(equalto('a'), v2, 1) === nothing
154167

155168
@test !contains(v1, "a")
156169
@test contains(v1, "")
157170
@test contains(v2, "")
158171

172+
@test !contains(v1, r"af")
173+
@test contains(v2, r"af")
174+
159175
@test startswith(v1, "")
160176
@test !startswith(v1, "a")
161177
@test startswith(v2, "caf")
@@ -167,9 +183,9 @@ using CategoricalArrays
167183
@test reverse(v1) == ""
168184
@test reverse(v2) == "éfac"
169185

170-
@test replace(v1, "a", "b") == ""
171-
@test replace(v2, 'a', 'b') == "cbfé"
172-
@test replace(v2, "ca", "b", 1) == "bfé"
186+
@test replace(v1, "a"=>"b") == ""
187+
@test replace(v2, 'a'=>'b') == "cbfé"
188+
@test replace(v2, "ca"=>"b", count=1) == "bfé"
173189

174190
@test isempty(split(v1))
175191
@test split(v1, "a") == [""]
@@ -209,14 +225,16 @@ using CategoricalArrays
209225
@test join([v1, "a"]) == "a"
210226
@test join([v1, "a"], v2) == "caféa"
211227

212-
@test chop(v1) == ""
228+
if VERSION >= v"0.7.0-DEV.3688" # Version known to throw an erro
229+
@test_throws BoundsError chop(v1) == ""
230+
end
213231
@test chop(v2) == "caf"
214232

215233
@test chomp(v1) == ""
216234
@test chomp(v2) == "café"
217235

218-
@test strwidth(v1) === 0
219-
@test strwidth(v2) === 4
236+
@test textwidth(v1) === 0
237+
@test textwidth(v2) === 4
220238

221239
@test isascii(v1)
222240
@test !isascii(v2)

0 commit comments

Comments
 (0)