Skip to content

Commit

Permalink
String-related 0.7 fixes (#114)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
ararslan authored and nalimilan committed Feb 11, 2018
1 parent 59bf7f2 commit 4972555
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 55 deletions.
4 changes: 2 additions & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
julia 0.6
Missings
Reexport
Compat 0.39.0
JSON
Compat 0.53.0
JSON
19 changes: 14 additions & 5 deletions src/value.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ end
Base.string(x::CategoricalString) = get(x)
Base.eltype(x::CategoricalString) = Char
Base.length(x::CategoricalString) = length(get(x))
Base.endof(x::CategoricalString) = endof(get(x))
Compat.lastindex(x::CategoricalString) = lastindex(get(x))
Base.sizeof(x::CategoricalString) = sizeof(get(x))
Base.nextind(x::CategoricalString, i::Integer) = nextind(get(x), i)
Base.prevind(x::CategoricalString, i::Integer) = prevind(get(x), i)
Base.nextind(x::CategoricalString, i::Int) = nextind(get(x), i)
Base.prevind(x::CategoricalString, i::Int) = prevind(get(x), i)
Base.next(x::CategoricalString, i::Int) = next(get(x), i)
Base.getindex(x::CategoricalString, i::Int) = getindex(get(x), i)
Base.codeunit(x::CategoricalString, i::Integer) = codeunit(get(x), i)
Expand All @@ -157,9 +157,18 @@ Base.isvalid(x::CategoricalString, i::Integer) = isvalid(get(x), i)
Base.match(r::Regex, s::CategoricalString,
idx::Integer=start(s), add_opts::UInt32=UInt32(0)) =
match(r, get(s), idx, add_opts)
Base.matchall(r::Regex, s::CategoricalString, overlap::Bool=false) =
matchall(r, get(s), overlap)
if VERSION > v"0.7.0-DEV.3526"
Base.matchall(r::Regex, s::CategoricalString; overlap::Bool=false) =
matchall(r, get(s); overlap=overlap)
else
Base.matchall(r::Regex, s::CategoricalString; overlap::Bool=false) =
matchall(r, get(s), overlap)
Base.matchall(r::Regex, s::CategoricalString, overlap::Bool) =
matchall(r, get(s), overlap)
end
Base.collect(x::CategoricalString) = collect(get(x))
Base.reverse(x::CategoricalString) = reverse(get(x))
Compat.ncodeunits(x::CategoricalString) = ncodeunits(get(x))

# JSON of CatValue is JSON of the value it refers to
JSON.lower(x::CatValue) = get(x)
114 changes: 66 additions & 48 deletions test/08_string.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module TestString
using Compat
using Compat.Test
using Compat.Unicode
using CategoricalArrays

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

@test nextind(v1, 1) === 2
if VERSION >= v"0.7.0-DEV.2949"
@test_throws BoundsError nextind(v1, 1)
else
@test nextind(v1, 1) === 2
end
@test nextind(v2, 4) === 6

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

@test endof(v1) === 0
@test endof(v2) === 4
if VERSION >= v"0.7.0-DEV.3583"
@test firstindex(v1) === 1
@test firstindex(v2) === 1
end

@test lastindex(v1) === 0
@test lastindex(v2) === 4

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

@test v2[2] === 'a'
@test v2[4] === 'é'
@test_throws BoundsError v1[1]
@test_throws UnicodeError v2[5]
if VERSION >= v"0.7.0-DEV.2949"
@test_throws StringIndexError v2[5]
else
@test_throws UnicodeError v2[5]
end

@test codeunit(v2, 2) === 0x61
@test codeunit(v2, 5) === 0xa9
Expand All @@ -65,9 +79,9 @@ using CategoricalArrays
@test ascii(v1)::String == ""
@test_throws ArgumentError ascii(v2)

@test normalize_string(v1) == ""
@test normalize_string(v2) == "café"
@test normalize_string(v2, :NFKD) == "café"
@test Unicode.normalize_string(v1) == ""
@test Unicode.normalize_string(v2) == "café"
@test Unicode.normalize_string(v2, :NFKD) == "café"

@test isempty(collect(graphemes(v1)))
@test collect(graphemes(v2)) == collect(graphemes("café"))
Expand All @@ -78,11 +92,19 @@ using CategoricalArrays
@test isvalid(v2, 4)
@test !isvalid(v2, 5)

@test_throws BoundsError ind2chr(v1, 0)
@test ind2chr(v2, 4) === 4
if VERSION >= v"0.7.0-DEV.2949"
@test_throws BoundsError length(v1, 0, 0)
@test length(v2, 1, 4) === 4

@test_throws BoundsError nextind(v1, 1, 1)
@test nextind(v2, 1, 2) === 3
else
@test_throws BoundsError ind2chr(v1, 0)
@test ind2chr(v2, 4) === 4

@test_throws BoundsError chr2ind(v1, 1)
@test chr2ind(v2, 2) === 2
@test_throws BoundsError chr2ind(v1, 1)
@test chr2ind(v2, 2) === 2
end

@test string(v1) == ""
@test string(v2) == "café"
Expand All @@ -108,20 +130,22 @@ using CategoricalArrays
@test repeat(v1, 10) == ""
@test repeat(v2, 2) == "cafécafé"

@test !ismatch(r"", v1)
@test ismatch(r"", v2)

@test isempty(collect(eachmatch(r"", v1)))
@test first(eachmatch(r"", v2)).offset == 3
@test isempty(collect(eachmatch(r"af", v1)))
@test first(eachmatch(r"af", v2)).offset == 2

@test match(r"", v1) === nothing
@test match(r"", v2).offset === 3
@test match(r"", v2, 2).offset === 3
@test match(r"", v2, 2, UInt32(0)).offset === 3
@test match(r"af", v1) === nothing
@test match(r"af", v2).offset === 2
@test match(r"af", v2, 2).offset === 2
@test match(r"af", v2, 2, UInt32(0)).offset === 2

@test matchall(r"", v1) == []
@test matchall(r"", v2) == [""]
@test matchall(r"", v2, true) == [""]
@test matchall(r"af", v1) == []
@test matchall(r"af", v2) == ["af"]
if VERSION > v"0.7.0-DEV.3526"
@test matchall(r"af", v2, overlap=true) == ["af"]
else
@test matchall(r"af", v2, overlap=true) == ["af"]
@test matchall(r"af", v2, true) == ["af"]
end

@test lpad(v1, 1) == " "
@test lpad(v2, 1) == "café"
Expand All @@ -131,31 +155,23 @@ using CategoricalArrays
@test rpad(v2, 1) == "café"
@test rpad(v2, 5) == "café "

@test search(v1, "") === 1:0
@test search(v2, "a") === 2:2
@test search(v2, 'a') === 2
@test search(v2, 'a', 3) === 0
@test Compat.findfirst("", v1) === 1:0
@test Compat.findfirst("a", v2) === 2:2
@test Compat.findfirst(equalto('a'), v2) === 2
@test Compat.findnext(equalto('a'), v2, 3) === nothing

@test searchindex(v1, "") === 1
@test searchindex(v2, "a") === 2
@test searchindex(v2, 'a') === 2
@test searchindex(v2, 'a', 3) === 0

@test rsearch(v1, "a") === 0:-1
@test rsearch(v2, "a") === 2:2
@test rsearch(v2, 'a') === 2
@test rsearch(v2, 'a', 1) === 0

@test rsearchindex(v1, "a") === 0
@test rsearchindex(v2, "a") === 2
# Methods not defined even for String
#@test rsearchindex(v2, 'a') === 2
#@test rsearchindex(v2, 'a', 1) === 0
@test Compat.findlast("a", v1) === 0:-1
@test Compat.findlast("a", v2) === 2:2
@test Compat.findlast(equalto('a'), v2) === 2
@test Compat.findprev(equalto('a'), v2, 1) === nothing

@test !contains(v1, "a")
@test contains(v1, "")
@test contains(v2, "")

@test !contains(v1, r"af")
@test contains(v2, r"af")

@test startswith(v1, "")
@test !startswith(v1, "a")
@test startswith(v2, "caf")
Expand All @@ -167,9 +183,9 @@ using CategoricalArrays
@test reverse(v1) == ""
@test reverse(v2) == "éfac"

@test replace(v1, "a", "b") == ""
@test replace(v2, 'a', 'b') == "cbfé"
@test replace(v2, "ca", "b", 1) == "bfé"
@test replace(v1, "a"=>"b") == ""
@test replace(v2, 'a'=>'b') == "cbfé"
@test replace(v2, "ca"=>"b", count=1) == "bfé"

@test isempty(split(v1))
@test split(v1, "a") == [""]
Expand Down Expand Up @@ -209,14 +225,16 @@ using CategoricalArrays
@test join([v1, "a"]) == "a"
@test join([v1, "a"], v2) == "caféa"

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

@test chomp(v1) == ""
@test chomp(v2) == "café"

@test strwidth(v1) === 0
@test strwidth(v2) === 4
@test textwidth(v1) === 0
@test textwidth(v2) === 4

@test isascii(v1)
@test !isascii(v2)
Expand Down

0 comments on commit 4972555

Please sign in to comment.