Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When delim=groupmark=x, treat x as delim unless input is quoted #182

Merged
merged 6 commits into from
Nov 9, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More test cases (based on old test cases)
nickrobinson251 committed Nov 9, 2023
commit 8494136f3257f130095a805f3b9e8caa451797b5
34 changes: 34 additions & 0 deletions test/floats.jl
Original file line number Diff line number Diff line change
@@ -386,22 +386,56 @@ end
@test Parsers.parse(T, "100 000 000", groupmark(' ')) ≈ 100_000_000
end
end
@test Parsers.xparse(Float64, "100_000_000.99"; groupmark='_').val == 100_000_000.99
@test Parsers.xparse(Float64, "100_000_000"; groupmark='_').val == 100_000_000.0
@test Parsers.xparse(Float64, "1_0_0_0_0_0_0_0_0.99"; groupmark='_').val == 100_000_000.99

@test Parsers.xparse(Float64, "1 0 0 0 0 0 0 0 0.99"; groupmark=' ').val == 100_000_000.99
@test Parsers.xparse(Float64, "100000000.99"; groupmark=',').val == 100_000_000.99
@test Parsers.xparse(Float64, "100000000.99,aaa"; groupmark=',') == Parsers.Result{Float64}(OK | DELIMITED, 13, 1.0000000099e8)
@test Parsers.xparse(Float64, "\"100,000,000.99\",100"; groupmark=',', openquotechar='"', closequotechar='"') == Parsers.Result{Float64}(Int16(13), 17, 1.0000000099e8)
@test Parsers.xparse(Float64, "100,000,000.99,100"; groupmark=',', openquotechar='"', closequotechar='"') == Parsers.Result{Float64}(Int16(9), 4, 100.0)
@test Parsers.xparse(Float64, "100_000_000.99,100"; groupmark='_', openquotechar='"', closequotechar='"') == Parsers.Result{Float64}(Int16(9), 15, 1.0000000099e8)
@test Parsers.xparse(Float64, "\"100,000,000\",100"; groupmark=',', openquotechar='"', closequotechar='"') == Parsers.Result{Float64}(Int16(13), 14, 1.0e8)
res = Parsers.xparse(Float64, "100,000,000,aaa"; groupmark=',')
@test res.code == OK | DELIMITED
@test res.tlen == 4
res = Parsers.xparse(Float64, "100_000_000,aaa"; groupmark='_')
@test res.code == OK | DELIMITED
@test res.tlen == 12

@test Parsers.xparse(Float32, "100_000_000.99"; groupmark='_').val ≈ 100_000_000.99
@test Parsers.xparse(Float32, "100_000_000"; groupmark='_').val ≈ 100_000_000.0
@test Parsers.xparse(Float32, "1_0_0_0_0_0_0_0_0.99"; groupmark='_').val ≈ 100_000_000.99
@test Parsers.xparse(Float32, "1 0 0 0 0 0 0 0 0.99"; groupmark=' ').val ≈ 100_000_000.99
@test Parsers.xparse(Float32, "100000000.99"; groupmark=',').val ≈ 100_000_000.99
res = Parsers.xparse(Float32, "100000000.99,aaa"; groupmark=',')
@test res.code == OK | DELIMITED
@test res.tlen == 13
@test res.val ≈ 100_000_000.99
res = Parsers.xparse(Float32, "100,000,000,aaa"; groupmark=',')
@test res.code == OK | DELIMITED
@test res.tlen == 4
res = Parsers.xparse(Float32, "100_000_000,aaa"; groupmark='_')
@test res.code == OK | DELIMITED
@test res.tlen == 12

@test Parsers.xparse(Float64, "100,000,00099e-2"; groupmark=',').val == 100.0
@test Parsers.xparse(Float64, "100_000_00099e-2"; groupmark='_').val == 100_000_000.99
@test Parsers.xparse(Float64, "1,0,0,0,0,0,0,0,099e-2"; groupmark=',').val == 1.0
@test Parsers.xparse(Float64, "1_0_0_0_0_0_0_0_099e-2"; groupmark='_').val == 100_000_000.99

@test Parsers.xparse(Float64, "1 0 0 0 0 0 0 0 099e-2"; groupmark=' ').val == 100_000_000.99
@test Parsers.xparse(Float64, "10000000099e-2"; groupmark=',').val == 100_000_000.99
@test Parsers.xparse(Float64, "10000000099e-2,aaa"; groupmark=',') == Parsers.Result{Float64}(OK | DELIMITED, 15, 1.0000000099e8)
@test Parsers.xparse(Float64, "\"10000000099e-2\",100"; groupmark=',', openquotechar='"', closequotechar='"') == Parsers.Result{Float64}(Int16(13), 17, 1.0000000099e8)
@test Parsers.xparse(Float64, "10000000099e-2,100"; groupmark=',', openquotechar='"', closequotechar='"') == Parsers.Result{Float64}(Int16(9), 15, 1.0000000099e8)

@test Parsers.xparse(Float32, "100,000,00099e-2"; groupmark=',').val ≈ 100.0
@test Parsers.xparse(Float32, "100_000_00099e-2"; groupmark='_').val ≈ 100_000_000.99
@test Parsers.xparse(Float32, "1,0,0,0,0,0,0,0,099e-2"; groupmark=',').val ≈ 1.0
@test Parsers.xparse(Float32, "1_0_0_0_0_0_0_0_099e-2"; groupmark='_').val ≈ 100_000_000.99

@test Parsers.xparse(Float32, "1 0 0 0 0 0 0 0 099e-2"; groupmark=' ').val ≈ 100_000_000.99
@test Parsers.xparse(Float32, "10000000099e-2"; groupmark=',').val ≈ 100_000_000.99
res = Parsers.xparse(Float32, "10000000099e-2,aaa"; groupmark=',')
71 changes: 53 additions & 18 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -368,29 +368,65 @@ end # @testset "Core Parsers.xparse"
end
end
end

### NOTE: for `xparse` by default `delim=','`, so when we also test `groupmark=','`
### we are testing the case where `delim==groupmark`.
### In these cases `,` is interpreted as `groupmark` only appear inside a quoted value.
@testset "Int64" begin
@test Parsers.xparse(Int64, "100,000,000"; groupmark=',').val == 100
@test Parsers.xparse(Int64, "\"100,000,000\""; groupmark=',').val == 100_000_000
@test Parsers.xparse(Int64, "100_000_000"; groupmark='_').val == 100_000_000
@test Parsers.xparse(Int64, "1_0_0_0_0_0_0_0_0"; groupmark='_').val == 100_000_000
@test Parsers.xparse(Int64, "100000000"; groupmark=',').val == 100_000_000
@test Parsers.xparse(Int64, "100000000"; groupmark='_').val == 100_000_000

@test Parsers.xparse(Int64, "9223372036854775807"; groupmark=',').val == 9223372036854775807
@test Parsers.xparse(Int64, "9,2,2,3,3,7,2,0,3,6,8,5,4,7,7,5,8,0,7"; groupmark=',').val == 9
@test Parsers.xparse(Int64, "9_2_2_3_3_7_2_0_3_6_8_5_4_7_7_5_8_0_7"; groupmark='_').val == 9223372036854775807
@test Parsers.xparse(Int64, "9 2 2 3 3 7 2 0 3 6 8 5 4 7 7 5 8 0 7"; groupmark=' ').val == 9223372036854775807
@test Parsers.xparse(Int64, "\"100,000,000\",100"; groupmark=',', openquotechar='"', closequotechar='"') == Parsers.Result{Int64}(Int16(13), 14, 100_000_000)

@test Parsers.xparse(Int32, "100000000"; groupmark=',').val == 100_000_000
@test Parsers.xparse(Int32, "2147483647"; groupmark=',').val == 2147483647
@test Parsers.xparse(Int64, "\"100,000,000\",100"; groupmark=',', openquotechar='"', closequotechar='"') == Parsers.Result{Int64}(Int16(13), 14, 100_000_000)
@test Parsers.xparse(Int32, "\"100_000_000\",100"; groupmark='_', openquotechar='"', closequotechar='"') == Parsers.Result{Int32}(Int16(13), 14, 100_000_000)
@test Parsers.xparse(Int64, "100,000,000,aaa"; groupmark=',').val == 100
@test Parsers.xparse(Int64, "100_000_000,aaa"; groupmark='_').val == 100_000_000
res = Parsers.xparse(Int64, "100_000_000_aaa"; groupmark='_')
@test res.code == EOF | INVALID | INVALID_DELIMITER
@test res.tlen == 15
end # Int64

@testset "Int32" begin
@test Parsers.xparse(Int32, "100_000_000"; groupmark='_').val == 100_000_000
@test Parsers.xparse(Int32, "1_0_0_0_0_0_0_0_0"; groupmark='_').val == 100_000_000
@test Parsers.xparse(Int32, "100000000"; groupmark='_').val == 100_000_000

@test Parsers.xparse(Int32, "2147483647"; groupmark='_').val == 2147483647
@test Parsers.xparse(Int32, "2_1_4_7_4_8_3_6_4_7"; groupmark='_').val == 2147483647
@test Parsers.xparse(Int32, "2 1 4 7 4 8 3 6 4 7"; groupmark=' ').val == 2147483647
@test Parsers.xparse(Int32, "\"100,000,000\",100"; groupmark=',', openquotechar='"', closequotechar='"') == Parsers.Result{Int32}(Int16(13), 14, 100_000_000)

@test_throws ArgumentError Parsers.xparse(Int64, "42"; groupmark=',', quoted=false, delim=',')
@test_throws ArgumentError Parsers.xparse(Int64, "42"; groupmark=',', quoted=false, delim=UInt8(','))
@test_throws ArgumentError Parsers.xparse(Int64, "42"; groupmark=',', decimal=',')
@test_throws ArgumentError Parsers.xparse(Int64, "42"; groupmark=',', decimal=UInt8(','))
@test_throws ArgumentError Parsers.xparse(Int64, "42"; groupmark='0')
@test_throws ArgumentError Parsers.xparse(Int64, "42"; groupmark=UInt8('0'))
@test_throws ArgumentError Parsers.xparse(Int64, "42"; groupmark='9')
@test_throws ArgumentError Parsers.xparse(Int64, "42"; groupmark='"', openquotechar='"')
@test_throws ArgumentError Parsers.xparse(Int64, "42"; groupmark='"', openquotechar=UInt8('"'))
@test_throws ArgumentError Parsers.xparse(Int64, "42"; groupmark='"', closequotechar='"')
@test_throws ArgumentError Parsers.xparse(Int64, "42"; groupmark='"', closequotechar=UInt8('"'))

@test Parsers.xparse(Int32, "\"100,000,000\",100"; groupmark=',', openquotechar='"', closequotechar='"') == Parsers.Result{Int32}(Int16(13), 14, 100_000_000)
@test Parsers.xparse(Int32, "\"100_000_000\",100"; groupmark='_', openquotechar='"', closequotechar='"') == Parsers.Result{Int32}(Int16(13), 14, 100_000_000)
@test Parsers.xparse(Int64, "100,000,000,aaa"; groupmark=',').val == 100
@test Parsers.xparse(Int64, "100_000_000,aaa"; groupmark='_').val == 100_000_000
res = Parsers.xparse(Int64, "100_000_000_aaa"; groupmark='_')
@test res.code == EOF | INVALID | INVALID_DELIMITER
@test res.tlen == 15
res = Parsers.xparse(Int32, "100_000_000_aaa"; groupmark='_')
@test res.code == EOF | INVALID | INVALID_DELIMITER
@test res.tlen == 15
end # Int32

@testset "$T error cases" for T in (Int32, Int64)
@test_throws ArgumentError Parsers.xparse(T, "42"; groupmark=',', quoted=false, delim=',')
@test_throws ArgumentError Parsers.xparse(T, "42"; groupmark=',', quoted=false, delim=UInt8(','))
@test_throws ArgumentError Parsers.xparse(T, "42"; groupmark=',', decimal=',')
@test_throws ArgumentError Parsers.xparse(T, "42"; groupmark=',', decimal=UInt8(','))
@test_throws ArgumentError Parsers.xparse(T, "42"; groupmark='0')
@test_throws ArgumentError Parsers.xparse(T, "42"; groupmark=UInt8('0'))
@test_throws ArgumentError Parsers.xparse(T, "42"; groupmark='9')
@test_throws ArgumentError Parsers.xparse(T, "42"; groupmark='"', openquotechar='"')
@test_throws ArgumentError Parsers.xparse(T, "42"; groupmark='"', openquotechar=UInt8('"'))
@test_throws ArgumentError Parsers.xparse(T, "42"; groupmark='"', closequotechar='"')
@test_throws ArgumentError Parsers.xparse(T, "42"; groupmark='"', closequotechar=UInt8('"'))
end

@testset "$T groupmark=$(repr(g))" for g in (',',' '), T in (Int32, Int64)
xgroupmark(c::Char) = Parsers._get_default_xoptions(groupmark=UInt8(c))
@@ -414,7 +450,6 @@ end # @testset "Core Parsers.xparse"
end
end


# #168
res = Parsers.parse(Int, "1,729", Parsers._get_default_options(groupmark=UInt8(',')))
@test res == 1729