From 6ea48684d47faebf97077c78bd923744b4987225 Mon Sep 17 00:00:00 2001 From: Fengyang Wang Date: Mon, 27 Mar 2017 19:42:05 -0400 Subject: [PATCH] Add missing test coverage and remove unneeded internal code (#190) * Add missing test coverage and remove unneeded internal code * Test all failures on both streaming and memory parsers --- src/JSON.jl | 6 ------ src/Parser.jl | 6 +++++- test/lowering.jl | 10 ++++++++++ test/runtests.jl | 51 +++++++++++++++++++++++++++++------------------- 4 files changed, 46 insertions(+), 27 deletions(-) diff --git a/src/JSON.jl b/src/JSON.jl index 4f47863..296c756 100644 --- a/src/JSON.jl +++ b/src/JSON.jl @@ -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 @@ -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) @@ -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) diff --git a/src/Parser.jl b/src/Parser.jl index fd50154..52611c6 100644 --- a/src/Parser.jl +++ b/src/Parser.jl @@ -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 diff --git a/test/lowering.jl b/test/lowering.jl index 99e5dcb..ae3daf3 100644 --- a/test/lowering.jl +++ b/test/lowering.jl @@ -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 @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 14a17fb..f44ec45 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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")) @@ -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")