Skip to content

Commit

Permalink
Modernize test suite for JSON (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
TotalVerb authored May 11, 2017
1 parent f28c6e7 commit 4f301b0
Show file tree
Hide file tree
Showing 15 changed files with 411 additions and 378 deletions.
107 changes: 107 additions & 0 deletions test/async.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
finished_async_tests = RemoteChannel()

@async begin
s = listen(7777)
s = accept(s)

Base.start_reading(s)

@test JSON.parse(s) != nothing # a
@test JSON.parse(s) != nothing # b
validate_c(s) # c
@test JSON.parse(s) != nothing # d
validate_svg_tviewer_menu(s) # svg_tviewer_menu
@test JSON.parse(s) != nothing # gmaps
@test JSON.parse(s) != nothing # colors1
@test JSON.parse(s) != nothing # colors2
@test JSON.parse(s) != nothing # colors3
@test JSON.parse(s) != nothing # twitter
@test JSON.parse(s) != nothing # facebook
validate_flickr(s) # flickr
@test JSON.parse(s) != nothing # youtube
@test JSON.parse(s) != nothing # iphone
@test JSON.parse(s) != nothing # customer
@test JSON.parse(s) != nothing # product
@test JSON.parse(s) != nothing # interop
validate_unicode(s) # unicode
@test JSON.parse(s) != nothing # issue5
@test JSON.parse(s) != nothing # dollars
@test JSON.parse(s) != nothing # brackets

put!(finished_async_tests, nothing)
end

w = connect("localhost", 7777)

@test JSON.parse(a) != nothing
write(w, a)

@test JSON.parse(b) != nothing
write(w, b)

validate_c(c)
write(w, c)

@test JSON.parse(d) != nothing
write(w, d)

validate_svg_tviewer_menu(svg_tviewer_menu)
write(w, svg_tviewer_menu)

@test JSON.parse(gmaps) != nothing
write(w, gmaps)

@test JSON.parse(colors1) != nothing
write(w, colors1)

@test JSON.parse(colors2) != nothing
write(w, colors2)

@test JSON.parse(colors3) != nothing
write(w, colors3)

@test JSON.parse(twitter) != nothing
write(w, twitter)

@test JSON.parse(facebook) != nothing
write(w, facebook)

validate_flickr(flickr)
write(w, flickr)

@test JSON.parse(youtube) != nothing
write(w, youtube)

@test JSON.parse(iphone) != nothing
write(w, iphone)

@test JSON.parse(customer) != nothing
write(w, customer)

@test JSON.parse(product) != nothing
write(w, product)

@test JSON.parse(interop) != nothing
write(w, interop)

validate_unicode(unicode)
write(w, unicode)

# issue #5
issue5 = "[\"A\",\"B\",\"C\\n\"]"
JSON.parse(issue5)
write(w, issue5)

# $ escaping issue
dollars = ["all of the \$s", "µniçø∂\$"]
json_dollars = json(dollars)
@test JSON.parse(json_dollars) != nothing
write(w, json_dollars)

# unmatched brackets
brackets = Dict("foo"=>"ba}r", "be}e]p"=>"boo{p")
json_brackets = json(brackets)
@test JSON.parse(json_brackets) != nothing
write(w, json_dollars)

fetch(finished_async_tests)
4 changes: 4 additions & 0 deletions test/enum.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@enum Animal zebra aardvark horse
@test json(zebra) == "\"zebra\""
@test json([aardvark, horse, Dict("z" => zebra)]) ==
"[\"aardvark\",\"horse\",{\"z\":\"zebra\"}]"
10 changes: 10 additions & 0 deletions test/indentation.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# check indented json has same final value as non indented
fb = JSON.parse(facebook)
fbjson1 = json(fb, 2)
fbjson2 = json(fb)
@test JSON.parse(fbjson1) == JSON.parse(fbjson2)

ev = JSON.parse(svg_tviewer_menu)
ejson1 = json(ev, 2)
ejson2 = json(ev)
@test JSON.parse(ejson1) == JSON.parse(ejson2)
186 changes: 109 additions & 77 deletions test/json-samples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,41 @@ b="{
}
"

c="{\"widget\": {
\"debug\": \"on\",
\"window\": {
\"title\": \"Sample Konfabulator Widget\",
\"name\": \"main_window\",
\"width\": 500,
\"height\": 500
const c = """
{"widget": {
"debug": "on",
"window": {
"title": "Sample Konfabulator Widget",
"name": "main_window",
"width": 500,
"height": 500
},
\"image\": {
\"src\": \"Images/Sun.png\",
\"name\": \"sun1\",
\"hOffset\": 250,
\"vOffset\": 250,
\"alignment\": \"center\"
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center"
},
\"text\": {
\"data\": \"Click Here\",
\"size\": 36.5,
\"style\": \"bold\",
\"name\": \"text1\",
\"hOffset\": 250,
\"vOffset\": 100,
\"alignment\": \"center\",
\"onMouseUp\": \"sun1.opacity = (sun1.opacity / 100) * 90;\"
"text": {
"data": "Click Here",
"size": 36.5,
"style": "bold",
"name": "text1",
"hOffset": 250,
"vOffset": 100,
"alignment": "center",
"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"
}
}}"
}}"""
function validate_c(c)
j = JSON.parse(c)
@test j != nothing
@test typeof(j["widget"]["image"]["hOffset"]) == Int64
@test j["widget"]["image"]["hOffset"] == 250
@test typeof(j["widget"]["text"]["size"]) == Float64
@test j["widget"]["text"]["size"] == 36.5
end

d = "{\"web-app\": {
\"servlet\": [
Expand Down Expand Up @@ -153,33 +162,48 @@ d = "{\"web-app\": {
\"taglib-uri\": \"cofax.tld\",
\"taglib-location\": \"/WEB-INF/tlds/cofax.tld\"}}}"

e="{\"menu\": {
\"header\": \"SVG\\tViewer\\u03b1\",
\"items\": [
{\"id\": \"Open\"},
{\"id\": \"OpenNew\", \"label\": \"Open New\"},
const svg_tviewer_menu = """
{"menu": {
"header": "SVG\\tViewer\\u03b1",
"items": [
{"id": "Open"},
{"id": "OpenNew", "label": "Open New"},
null,
{\"id\": \"ZoomIn\", \"label\": \"Zoom In\"},
{\"id\": \"ZoomOut\", \"label\": \"Zoom Out\"},
{\"id\": \"OriginalView\", \"label\": \"Original View\"},
{"id": "ZoomIn", "label": "Zoom In"},
{"id": "ZoomOut", "label": "Zoom Out"},
{"id": "OriginalView", "label": "Original View"},
null,
{\"id\": \"Quality\"},
{\"id\": \"Pause\"},
{\"id\": \"Mute\"},
{"id": "Quality"},
{"id": "Pause"},
{"id": "Mute"},
null,
{\"id\": \"Find\", \"label\": \"Find...\"},
{\"id\": \"FindAgain\", \"label\": \"Find Again\"},
{\"id\": \"Copy\"},
{\"id\": \"CopyAgain\", \"label\": \"Copy Again\"},
{\"id\": \"CopySVG\", \"label\": \"Copy SVG\"},
{\"id\": \"ViewSVG\", \"label\": \"View SVG\"},
{\"id\": \"ViewSource\", \"label\": \"View Source\"},
{\"id\": \"SaveAs\", \"label\": \"Save As\"},
{"id": "Find", "label": "Find..."},
{"id": "FindAgain", "label": "Find Again"},
{"id": "Copy"},
{"id": "CopyAgain", "label": "Copy Again"},
{"id": "CopySVG", "label": "Copy SVG"},
{"id": "ViewSVG", "label": "View SVG"},
{"id": "ViewSource", "label": "View Source"},
{"id": "SaveAs", "label": "Save As"},
null,
{\"id\": \"Help\"},
{\"id\": \"About\", \"label\": \"About Adobe SVG Viewer...\"}
{"id": "Help"},
{"id": "About", "label": "About Adobe SVG Viewer..."}
]
}}"
}}"""
function validate_svg_tviewer_menu(str)
j = JSON.parse(str)
@test j != nothing
@test typeof(j) == Dict{String, Any}
@test length(j) == 1
@test typeof(j["menu"]) == Dict{String, Any}
@test length(j["menu"]) == 2
@test j["menu"]["header"] == "SVG\tViewerα"
@test isa(j["menu"]["items"], Vector{Any})
@test length(j["menu"]["items"]) == 22
@test j["menu"]["items"][3] == nothing
@test j["menu"]["items"][2]["id"] == "OpenNew"
@test j["menu"]["items"][2]["label"] == "Open New"
end


#Example JSON strings from http://www.jquery4u.com/json/10-example-json-files/
Expand Down Expand Up @@ -343,27 +367,33 @@ facebook= "{
]
}"

flickr = "{
\"title\": \"Talk On Travel Pool\",
\"link\": \"http://www.flickr.com/groups/talkontravel/pool/\",
\"description\": \"Travel and vacation photos from around the world.\",
\"modified\": \"2009-02-02T11:10:27Z\",
\"generator\": \"http://www.flickr.com/\",
\"totalItems\":222,
\"items\": [
const flickr = """{
"title": "Talk On Travel Pool",
"link": "http://www.flickr.com/groups/talkontravel/pool/",
"description": "Travel and vacation photos from around the world.",
"modified": "2009-02-02T11:10:27Z",
"generator": "http://www.flickr.com/",
"totalItems":222,
"items": [
{
\"title\": \"View from the hotel\",
\"link\": \"http://www.flickr.com/photos/33112458@N08/3081564649/in/pool-998875@N22\",
\"media\": {\"m\":\"http://farm4.static.flickr.com/3037/3081564649_4a6569750c_m.jpg\"},
\"date_taken\": \"2008-12-04T04:43:03-08:00\",
\"description\": \"<p><a href=\\\"http://www.flickr.com/people/33112458@N08/\\\"> Talk On Travel<\/a> has added a photo to the pool:<\/p> <p><a href=\\\"http:// www.flickr.com/photos/33112458@N08/3081564649/\\\" title=\\\"View from the hotel\\\"> <img src=\\\"http://farm4.static.flickr.com/3037/3081564649_4a6569750c_m.jpg\\\" width=\\\"240\\\" height=\\\"180\\\" alt=\\\"View from the hotel\\\" /><\/a><\/p> \",
\"published\": \"2008-12-04T12:43:03Z\",
\"author\": \"[email protected] (Talk On Travel)\",
\"author_id\": \"33112458@N08\",
\"tags\": \"spain dolphins tenerife canaries lagomera aqualand playadelasamericas junglepark losgigantos loscristines talkontravel\"
"title": "View from the hotel",
"link": "http://www.flickr.com/photos/33112458@N08/3081564649/in/pool-998875@N22",
"media": {"m":"http://farm4.static.flickr.com/3037/3081564649_4a6569750c_m.jpg"},
"date_taken": "2008-12-04T04:43:03-08:00",
"description": "<p><a href=\\"http://www.flickr.com/people/33112458@N08/\\"> Talk On Travel<\/a> has added a photo to the pool:<\/p> <p><a href=\\"http:// www.flickr.com/photos/33112458@N08/3081564649/\\" title=\\"View from the hotel\\"> <img src=\\"http://farm4.static.flickr.com/3037/3081564649_4a6569750c_m.jpg\\" width=\\"240\\" height=\\"180\\" alt=\\"View from the hotel\\" /><\/a><\/p> ",
"published": "2008-12-04T12:43:03Z",
"author": "[email protected] (Talk On Travel)",
"author_id": "33112458@N08",
"tags": "spain dolphins tenerife canaries lagomera aqualand playadelasamericas junglepark losgigantos loscristines talkontravel"
}
]
}"
}"""
function validate_flickr(str)
k = JSON.parse(str)
@test k != nothing
@test k["totalItems"] == 222
@test k["items"][1]["description"][12] == '\"'
end

youtube = "{\"apiVersion\":\"2.0\",
\"data\":{
Expand Down Expand Up @@ -596,17 +626,19 @@ interop = "{
}
}"

unicode = "{\"অলিম্পিকস\": {
\"অ্যাথলেট\": \"২২টি দেশ থেকে ২,০৩৫ জন প্রতিযোগী\",
\"ইভেন্ট\": \"২২টি ইভেন্টের মধ্যে ছিল দড়ি টানাটানি\",
\"রেকর্ড\": [
{\"১০০মি. স্প্রিন্ট\": \"রেজি ওয়াকার, দক্ষিণ আফ্রিকা\"},
{\"Marathon\": \"জনি হেইস\"},
{\" ফ্রি-স্টাইল সাঁতার\": \"Henry Taylor, Britain\"}
]
}}
"



test21 = "[\r\n{\r\n\"a\": 1,\r\n\"b\": 2\r\n},\r\n{\r\n\"a\": 3,\r\n\"b\": 4\r\n}\r\n]"
const unicode = """
{"অলিম্পিকস": {
"অ্যাথলেট": "২২টি দেশ থেকে ২,০৩৫ জন প্রতিযোগী",
"ইভেন্ট": "২২টি ইভেন্টের মধ্যে ছিল দড়ি টানাটানি",
"রেকর্ড": [
{"১০০মি. স্প্রিন্ট": "রেজি ওয়াকার, দক্ষিণ আফ্রিকা"},
{"Marathon": "জনি হেইস"},
{" ফ্রি-স্টাইল সাঁতার": "Henry Taylor, Britain"}
]
}}
"""
function validate_unicode(str)
u = JSON.parse(str)
@test u != nothing
@test u["অলিম্পিকস"]["রেকর্ড"][2]["Marathon"] == "জনি হেইস"
end
11 changes: 11 additions & 0 deletions test/parser/dicttype.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@testset for T in [
DataStructures.OrderedDict,
Dict{Symbol, Int32}
]
val = JSON.parse("{\"x\": 3}", dicttype=T)
@test isa(val, T)
@test length(val) == 1
key = collect(keys(val))[1]
@test string(key) == "x"
@test val[key] == 3
end
29 changes: 29 additions & 0 deletions test/parser/invalid-input.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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
"[\"🍕\"_\"🍕\""
]

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

# Test streaming parser
@test_throws ErrorException JSON.parse(IOBuffer(fail))
end
10 changes: 10 additions & 0 deletions test/parser/parsefile.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
tmppath, io = mktemp()
write(io, facebook)
close(io)
if is_windows()
# don't use mmap on Windows, to avoid ERROR: unlink: operation not permitted (EPERM)
@test haskey(JSON.parsefile(tmppath; use_mmap=false), "data")
else
@test haskey(JSON.parsefile(tmppath), "data")
end
rm(tmppath)
Loading

0 comments on commit 4f301b0

Please sign in to comment.