From 49725550410eeb3b827f11ccbff4b35ff5da5fb1 Mon Sep 17 00:00:00 2001 From: Alex Arslan Date: Sun, 11 Feb 2018 07:47:06 -0800 Subject: [PATCH] String-related 0.7 fixes (#114) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- REQUIRE | 4 +- src/value.jl | 19 ++++++-- test/08_string.jl | 114 +++++++++++++++++++++++++++------------------- 3 files changed, 82 insertions(+), 55 deletions(-) diff --git a/REQUIRE b/REQUIRE index dfbac391..ac906253 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,5 +1,5 @@ julia 0.6 Missings Reexport -Compat 0.39.0 -JSON \ No newline at end of file +Compat 0.53.0 +JSON diff --git a/src/value.jl b/src/value.jl index 76ee2296..53881aa2 100644 --- a/src/value.jl +++ b/src/value.jl @@ -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) @@ -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) diff --git a/test/08_string.jl b/test/08_string.jl index 89fa026f..2c640521 100644 --- a/test/08_string.jl +++ b/test/08_string.jl @@ -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 @@ -40,14 +41,23 @@ 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é") @@ -55,7 +65,11 @@ using CategoricalArrays @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 @@ -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é")) @@ -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é" @@ -108,20 +130,22 @@ using CategoricalArrays @test repeat(v1, 10) == "" @test repeat(v2, 2) == "cafécafé" - @test !ismatch(r"fé", v1) - @test ismatch(r"fé", v2) - - @test isempty(collect(eachmatch(r"fé", v1))) - @test first(eachmatch(r"fé", v2)).offset == 3 + @test isempty(collect(eachmatch(r"af", v1))) + @test first(eachmatch(r"af", v2)).offset == 2 - @test match(r"fé", v1) === nothing - @test match(r"fé", v2).offset === 3 - @test match(r"fé", v2, 2).offset === 3 - @test match(r"fé", 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"fé", v1) == [] - @test matchall(r"fé", v2) == ["fé"] - @test matchall(r"fé", v2, true) == ["fé"] + @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é" @@ -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, "fé") + @test !contains(v1, r"af") + @test contains(v2, r"af") + @test startswith(v1, "") @test !startswith(v1, "a") @test startswith(v2, "caf") @@ -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") == [""] @@ -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)