Skip to content

Commit

Permalink
Add missing test coverage and remove unneeded internal code (#190)
Browse files Browse the repository at this point in the history
* Add missing test coverage and remove unneeded internal code

* Test all failures on both streaming and memory parsers
  • Loading branch information
TotalVerb authored Mar 27, 2017
1 parent 0cd5eea commit 6ea4868
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
6 changes: 0 additions & 6 deletions src/JSON.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,10 @@ function lower(a)
end
end

lower(a::JSONPrimitive) = a

if isdefined(Base, :Dates)
lower(s::Base.Dates.TimeType) = string(s)
end

lower(s::Symbol) = string(s)

if VERSION < v"0.5.0-dev+2396"
lower(f::Function) = "function at $(f.fptr)"
end
Expand Down Expand Up @@ -182,7 +178,6 @@ for kind in ("object", "array")
beginsym = Symbol(uppercase(kind), "_BEGIN")
endfn = Symbol("end_", kind)
endsym = Symbol(uppercase(kind), "_END")
ctxfn = Symbol("json_", kind)
# Begin and end objects
@eval function $beginfn(io::PrettyContext)
write(io, $beginsym)
Expand All @@ -199,7 +194,6 @@ for kind in ("object", "array")
io.first = false
end
@eval $endfn(io::CompactContext) = (write(io, $endsym); io.first = false)
@eval $ctxfn(f, io::JSONContext) = ($beginfn(io); f(io); $endfn(io))
end

function show_string(io::IO, x)
Expand Down
6 changes: 5 additions & 1 deletion src/Parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ end

@inline function byteat(ps::StreamingParserState)
if ps.used
ps.cur = read(ps.io, UInt8)
ps.used = false
if eof(ps.io)
_error(E_UNEXPECTED_EOF, ps)
else
ps.cur = read(ps.io, UInt8)
end
end
ps.cur
end
Expand Down
10 changes: 10 additions & 0 deletions test/lowering.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module TestLowering

using JSON
using Base.Test
using Compat
using FixedPointNumbers: Fixed
import Compat: String

if isdefined(Base, :Dates)
@test JSON.json(Date(2016, 8, 3)) == "\"2016-08-03\""
end
Expand All @@ -18,3 +26,5 @@ JSON.lower{T}(v::Type151{T}) = Dict(:type => T, :value => v.x)

fixednum = Fixed{Int16, 15}(0.1234)
@test JSON.parse(JSON.json(fixednum)) == Float64(fixednum)

end
51 changes: 31 additions & 20 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ using Compat
import Compat: String

import DataStructures
import FixedPointNumbers: Fixed

include("json-checker.jl")
include(joinpath(dirname(@__FILE__),"json_samples.jl"))
Expand Down Expand Up @@ -284,26 +283,38 @@ if isdefined(Base, :Dates)
@test json([DateTime("2016-04-13T00:00:00"), DateTime("2016-04-12T00:00:00")]) == "[\"2016-04-13T00:00:00\",\"2016-04-12T00:00:00\"]"
end

# Test parser failures
# Unexpected character in array
@test_throws ErrorException JSON.parse("[1,2,3/4,5,6,7]")
# Unexpected character in object
@test_throws ErrorException JSON.parse("{\"1\":2, \"2\":3 _ \"4\":5}")
# Invalid escaped character
@test_throws ErrorException JSON.parse("[\"alpha\\α\"]")
# Invalid 'simple' and 'unknown value'
@test JSON.parse("[true]") == [true]
@test_throws ErrorException JSON.parse("[tXXe]")
@test_throws ErrorException JSON.parse("[fail]")
@test_throws ErrorException JSON.parse("")
# Invalid number
@test_throws ErrorException JSON.parse("[5,2,-]")
@test_throws ErrorException JSON.parse("[5,2,+β]")
# Incomplete escape
@test_throws ErrorException JSON.parse("\"\\")

# Test for Issue #99
@test_throws ErrorException JSON.parse("[\"🍕\"_\"🍕\"")

# Test parser failures
const FAILURES = [
# Unexpected character in array
"[1,2,3/4,5,6,7]",
# Unexpected character in object
"{\"1\":2, \"2\":3 _ \"4\":5}",
# Invalid escaped character
"[\"alpha\\α\"]",
# Invalid 'simple' and 'unknown value'
"[tXXe]",
"[fail]",
"",
# Invalid number
"[5,2,-]",
"[5,2,+β]",
# Incomplete escape
"\"\\",
# Control character
"\"\0\"",
# Issue #99
"[\"🍕\"_\"🍕\""
]

for fail in FAILURES
# Test memory parser
@test_throws ErrorException JSON.parse(fail)

# Test streaming parser
@test_throws ErrorException JSON.parse(IOBuffer(fail))
end

# Lowering tests
include("lowering.jl")
Expand Down

0 comments on commit 6ea4868

Please sign in to comment.