Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nalimilan committed Feb 11, 2018
1 parent ca75f86 commit f9da21b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 61 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
20 changes: 12 additions & 8 deletions src/value.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,9 @@ 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(x, Int(i))
Base.nextind(x::CategoricalString, i::Int) = nextind(get(x), i)
Base.prevind(x::CategoricalString, i::Integer) = prevind(x, Int(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)
Expand All @@ -159,12 +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)
Base.collect(x::CategoricalString) = collect(get(x))
if isdefined(Base, :ncodeunits)
Base.ncodeunits(x::CategoricalString) = ncodeunits(get(x))
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)
103 changes: 52 additions & 51 deletions test/08_string.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
module TestString
using Compat
using Compat.Test
using Compat.Unicode
using CategoricalArrays

if VERSION >= v"0.7.0-DEV.2915"
using Unicode
end

@testset "AbstractString operations on values of CategoricalPool{String}" begin
pool = CategoricalPool(["", "café"])

Expand Down Expand Up @@ -54,8 +51,13 @@ end
@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é")
Expand All @@ -77,23 +79,26 @@ end
@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)))
if VERSION < v"0.7.0-DEV.2949"
# FIXME: This causes a StackOverflowError after the string overhaul
@test collect(graphemes(v2)) == collect(graphemes("café"))
end
@test collect(graphemes(v2)) == collect(graphemes("café"))

@test isvalid(v1)
@test isvalid(v2)
@test !isvalid(v1, 1)
@test isvalid(v2, 4)
@test !isvalid(v2, 5)

if VERSION < v"0.7.0-DEV.2949"
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

Expand Down Expand Up @@ -125,20 +130,22 @@ end
@test repeat(v1, 10) == ""
@test repeat(v2, 2) == "cafécafé"

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

@test isempty(collect(eachmatch(r"", v1)))
@test first(eachmatch(r"", v2)).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 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 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 @@ -148,31 +155,23 @@ end
@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 searchindex(v1, "") === 1
@test searchindex(v2, "a") === 2
@test searchindex(v2, 'a') === 2
@test searchindex(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 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 @@ -184,9 +183,9 @@ end
@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 @@ -226,14 +225,16 @@ end
@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 f9da21b

Please sign in to comment.