Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

292 lines (286 sloc) 11.459 kB
{-# LANGUAGE OverloadedStrings #-}
import Control.Applicative
import Control.Exception
import Control.Monad
import qualified Data.ByteString as S
import Data.ByteString.Char8 (singleton, append)
import Data.Maybe
import System.IO
import Text.JSON.AttoJSON
runTest :: (S.ByteString, Maybe S.ByteString) -> IO Bool
runTest (test, expected) = do
let actual = (fmap showJSON . readJSON) test
if actual /= expected
then do
S.putStrLn $ "FAIL: " `append` test
S.putStrLn $ " expected " `append` showResult expected
S.putStrLn $ " actual " `append` showResult actual
return False
else return True
showResult :: Maybe S.ByteString -> S.ByteString
showResult = fromMaybe "(invalid)"
main = do
(passCount, totalCount) <-
foldM (\(p,t) b -> do
ok <- runTest b
return (p + fromEnum ok, t + 1)
) (0,0) testcases
if passCount == totalCount
then putStrLn $ "Passed all " ++ show passCount ++ " tests"
else putStrLn $ "Passed only " ++ show passCount ++ "/" ++ show totalCount ++ " tests"
testcases :: [(S.ByteString, Maybe S.ByteString)]
testcases =
[("",Nothing)
,(" ",Nothing)
,("\"",Nothing)
,("[,]",Nothing)
,("[)",Nothing)
,("[]]",Nothing)
,("[}",Nothing)
,("{,}",Nothing)
,("{]",Nothing)
,("[\"1\":2]",Nothing)
,("[1,2,]",Nothing)
,("[1:2}",Nothing)
,("{\"1\":2,}",Nothing)
,("{1:2}",Nothing)
,("{\"1\":2, \"2.5\" : [3, 4, {}, {\"5\": [\"6\"], [7 ]}]}",Nothing)
,("{\"1\":2, \"2.5\" : [3, 4, {}, {\"5\": [\"6\"], [7]}]}",Nothing)
,("{\"1\":2, \"2.5\" : [3, 4, {}, {\"5\": [\"6\"], \"7\" :[8 ]}]",Nothing)
,("{\"1\":2, \"2.5\" : [3, 4, {}, {\"5\": [\"6\"], \"7\" :[8 ]}]]",Nothing)
,("{\"1\":2, \"3\":4",Nothing)
,("\"1\\u2\"",Nothing)
,("[,2]",Nothing)
,("\"3",Nothing)
,("\"3\" \"4\"",Nothing)
,("[3[4]",Nothing)
,("[3[4]]",Nothing)
,("[3, [4, [5], 6] 7, 8 9]",Nothing)
,("[3, [4, [5], 6] 7, 8, 9]",Nothing)
,("[3, [4, [5], 6], 7, 8 9]",Nothing)
,("{\"hello\":true, \"bye\":false, null}",Nothing)
,("{\"hello\":true, \"bye\":false, null:null}",Nothing)
,("\"hi",Nothing)
,("\"hi\"\"\"",Nothing)
,("{\"hi\": \"bye\"]",Nothing)
,("\"\226\153\171\"", Just "\"\226\153\171\"")
,("\"\\u266B\"", Just "\"\226\153\171\"")
,("\"\\uD800\\uD800\"",Just "\"\239\191\189\239\191\189\"")
,("\"\\uD800\\uDBFF\"",Just "\"\239\191\189\239\191\189\"")
,("\"\\UD834\\UDD1E\"",Nothing)
,("\"\\uDB00\"",Just "\"\239\191\189\"")
,("\"\\uDB00\\uDBFF\"",Just "\"\239\191\189\239\191\189\"")
,("\"\\uFFFE\"",Just "\"\239\191\189\"")
,("\"\\uFFFF\"",Just "\"\239\191\189\"")
,(".",Nothing)
,("\"\"",Just "\"\"")
,("[]",Just "[]")
,("{}",Just "{}")
,("+.",Nothing)
,("0.5",Just "0.5")
,("0.e1",Nothing)
,("{\"1\":{}}",Just "{\"1\":{}}")
,("{\"1\":2}",Just "{\"1\":2}")
,("{\"1\":2, \"2.5\" : [3, 4, {}, {\"5\": [\"6\"]}]}",Just "{\"1\":2,\"2.5\":[3, 4, {}, {\"5\":[\"6\"]}]}")
,("{\"1\":2, \"2.5\" : [3, 4, {}, {\"5\": [\"6\"], \"7\" :[8 ]}]}",Just "{\"1\":2,\"2.5\":[3, 4, {}, {\"5\":[\"6\"],\"7\":[8]}]}")
,("1234",Just "1234")
,("-1234",Just "-1234")
,("{\"1\":2, \"3\":4}",Just "{\"1\":2,\"3\":4}")
,("+1234",Nothing)
,("++1234",Nothing)
,("123.456e78",Just "123456000000000000000000000000000000000000000000000000000000000000000000000000000")
,("123.456e-78",Just "1.23456e-76")
,("123.456e+78",Just "123456000000000000000000000000000000000000000000000000000000000000000000000000000")
,("123.e-14234",Nothing)
,("\"1\\u2000\"",Just "\"1\226\128\128\"")
,("\"1\\u20001\"",Just "\"1\226\128\128\&1\"")
,("2",Just "2")
,(".246e-14234",Nothing)
,(".2e-14234",Nothing)
,("3",Just "3")
,(".3",Nothing)
,("\"3\"",Just "\"3\"")
,("[3]",Just "[3]")
,("+3.",Nothing)
,("3.2e+1",Just "32")
,("[3, [4]]",Just "[3, [4]]")
,("[3, [4, [5]]]",Just "[3, [4, [5]]]")
,("[3, [4, [5], 6]]",Just "[3, [4, [5], 6]]")
,("[3, [4, [5], 6], 7]",Just "[3, [4, [5], 6], 7]")
,("[3, [4, [5], 6], 7, 8]",Just "[3, [4, [5], 6], 7, 8]")
,("[3, [4, [5], 6], 7, 8, 9]",Just "[3, [4, [5], 6], 7, 8, 9]")
,("+3.5",Nothing)
,(".3e",Nothing)
,(".3e1",Nothing)
,(".3e-1",Nothing)
,(".3e+1",Nothing)
,("3.e1",Nothing)
,("3.e+1",Nothing)
,("3e+1",Just "30")
,(".5",Nothing)
,("+.5",Nothing)
,(".5e+1",Nothing)
,("[ 7]",Just "[7]")
,("[7 ]",Just "[7]")
,("[7]",Just "[7]")
,(".e-14234",Nothing)
,("\"hello\"",Just "\"hello\"")
,("[\"hello\"]",Just "[\"hello\"]")
,("[\"hello\", \"bye\"]",Just "[\"hello\", \"bye\"]")
,("[\"hello\", \"bye\\n\"]",Just "[\"hello\", \"bye\\n\"]")
,("[\"hello\", \"bye\\n\\r\\t\"]",Just "[\"hello\", \"bye\\n\\r\\t\"]")
,("[\"hello\", \"bye\\n\\r\\t\\b\"]",Just "[\"hello\", \"bye\\n\\r\\t\\b\"]")
,("[\"hello\", \"bye\\n\\r\\t\\b\",true]",Just "[\"hello\", \"bye\\n\\r\\t\\b\", true]")
,("[\"hello\", \"bye\\n\\r\\t\\b\",true , false]",Just "[\"hello\", \"bye\\n\\r\\t\\b\", true, false]")
,("[\"hello\", \"bye\\n\\r\\t\\b\",true , false, null]",Just "[\"hello\", \"bye\\n\\r\\t\\b\", true, false, null]")
,("[\"hello\", \"bye\\n\\r\\t\\v\"]",Nothing)
,("{\"hello\":true}",Just "{\"hello\":true}")
,("{\"hello\":true, \"bye\":false}",Just "{\"bye\":false,\"hello\":true}")
,("{\"hello\":true, \"bye\":false, \"foo\":[\"one\",\"two\",\"three\"]}",Just "{\"bye\":false,\"foo\":[\"one\", \"two\", \"three\"],\"hello\":true}")
,("\"hi\"",Just "\"hi\"")
,("[\"hi\"]",Just "[\"hi\"]")
,("[\"hi\", \"bye\"]",Just "[\"hi\", \"bye\"]")
,("{\"hi\": \"bye\"}",Just "{\"hi\":\"bye\"}")
,("[\"hi\", \"bye\", 3]",Just "[\"hi\", \"bye\", 3]")
,("[\"hi\", \"bye[\", 3]",Just "[\"hi\", \"bye[\", 3]")
,("\"\\u0007\"",Just "\"\\u0007\"")
,("\"\\u0008\"",Just "\"\\b\"")
,("\"\\u0009\"",Just "\"\\t\"")
,("\"\\u0010\"",Just "\"\\u0010\"")
,("\"\\u0020\"",Just "\" \"")
,("\"\\u10000\"",Just "\"\225\128\128\&0\"")
,("\"\\u1234\"",Just "\"\225\136\180\"")
,("\"\\u99999\"",Just "\"\233\166\153\&9\"")
,("\"\\ud800\\udc00\"",Just "\"\240\144\128\128\"")
,("\"\\uD800\\uDC00\"",Just "\"\240\144\128\128\"")
,("\"\\uD834\\uDD1E\"",Just "\"\240\157\132\158\"")
,("\"\\uDBFF\\uDFFF\"",Just "\"\243\191\191\191\"")
,("\"\\uFFFD\"",Just "\"\239\191\189\"")
,("\"\\uFFFF\"",Just "\"\239\191\189\"")
,("hello",Nothing)
,("[32, 1]",Just "[32, 1]")
,("[32, ",Nothing)
,("\"\\uD800\\uDC00\"",Just "\"\240\144\128\128\"")
,("\"\\n\"",Just "\"\\n\"")
,("\"hello\"",Just "\"hello\"")
,("\"hello\\u0009world\"",Just "\"hello\\tworld\"")
,("\"hello\"",Just "\"hello\"")
,("\"hello\\n\"",Just "\"hello\\n\"")
,("\"hello\"",Just "\"hello\"")
,("3",Just "3")
,("3.",Nothing)
,(".3",Nothing)
,("0.3",Just "0.3")
,("0.3e",Nothing)
,("0.3e+",Nothing)
,("0.3e+5",Just "30000")
,("0.3e-5",Just "3.0e-6")
,("0.3e5",Just "30000")
,("\"hello\"",Just "\"hello\"")
,("+3",Nothing)
,("-3",Just "-3")
,("-3.",Nothing)
,("-3.1",Just "-3.1")
,("-0",Just "0")
,("-0.",Nothing)
,("-0.0",Just "0")
,(".5",Nothing)
,("5.",Nothing)
,("5.e1",Nothing)
,("0.5",Just "0.5")
,(".3e1",Nothing)
,(".3e+1",Nothing)
,(".3e-1",Nothing)
,(".3e-1 .5",Nothing)
,(".3e-1.5",Nothing)
,(".3e+1.5",Nothing)
,(".3e+.",Nothing)
,(".3e+.5",Nothing)
,(".3e+1.5",Nothing)
,("9.3e+1.5",Nothing)
,("9.e+1.5",Nothing)
,("9.e+",Nothing)
,("9.e+1",Nothing)
,("\"\\\"\"",Just "\"\\\"\"")
,("\"\\\"3.5\"",Just "\"\\\"3.5\"")
,("\"\\\".\"",Just "\"\\\".\"")
,("\"\\\".\".",Nothing)
,("\"\\\".....\"",Just "\"\\\".....\"")
,("\"\\\"\\\"\\\"\\\"\"\"",Nothing)
,("\"newline\n\"",Nothing)
,("[\"\\\"\\\"\\\"\\\"\", .5]",Nothing)
,("[.5]",Nothing)
,("[\"\\\"\\\"\\\"\\\"\", 0.5]",Just "[\"\\\"\\\"\\\"\\\"\", 0.5]")
,("[\"\\\"\\\"\\\"\\\"\", .5]",Nothing)
,("[\"\\\"\\\"\\\"\\\"\",.5]",Nothing)
,("[\"\\\"\",.5]",Nothing)
,("[\"\\\".5\",.5]",Nothing)
,("[\"\\\".5\",\".5\\\"\".5]",Nothing)
,("[\"\\\".5\",\".5\\\"\", .5]",Nothing)
,("[\"\\\".5\",\".5\\\"\",.5]",Nothing)
,("[\"\\\".5\",\".5\\\"\",0.5]",Just "[\"\\\".5\", \".5\\\"\", 0.5]")
,("{\"key\":/*comment*/\"value\"}",Nothing)
,("{\"key\":/*comment\"value\"}",Nothing)
,("{\"key\":\"value\"}/*",Nothing)
,("{\"key\":\"value\"}/**/",Nothing)
,("{\"key\":\"value\"}/***/",Nothing)
,("{\"key\":\"value\"}/**//",Nothing)
,("{\"key\":\"value\"}/**///",Nothing)
,("{\"key\":\"value\"}/**///----",Nothing)
,("{\"key\":\"value\"}#",Nothing)
,("{\"key\":\"value\"}#{",Nothing)
,("{\"key\":\"value\"}#{}",Nothing)
,("{\"key\":\"value\"}#,",Nothing)
,("{\"key\":\"value\"/**/, \"k2\":\"v2\"}",Nothing)
,("\"\\u0027\"",Just "\"'\"")
,("\"hello\\'\"",Nothing)
,("'hello\\''",Nothing)
,("'hello'",Nothing)
,("'hell\\'o'",Nothing)
,("'\\'hello'",Nothing)
,("'\\'hello\\''",Nothing)
,("\\'hello\\'",Nothing)
,("'hello\\'",Nothing)
,("['hello\\']",Nothing)
,("['hello\\'']",Nothing)
,("['hello\"']",Nothing)
,("['hello\\\"']",Nothing)
,("['hello\"o']",Nothing)
,("['\"']",Nothing)
,("'\"'",Nothing)
,("'\"hello\"'",Nothing)
,("'\"hello'",Nothing)
,("'\"hi\"'",Nothing)
,("\"\SOH\"",Nothing)
,("\"\STX\"",Nothing)
,("\"\ETX\"",Nothing)
,("\"\EOT\"",Nothing)
,("\"\ENQ\"",Nothing)
,("\"\ACK\"",Nothing)
,("\"\a\"",Nothing)
,("\"\b\"",Nothing)
,("\"\t\"",Nothing)
,("\"\n\"",Nothing)
,("\"\v\"",Nothing)
,("\"\f\"",Nothing)
,("\"\r\"",Nothing)
,("\"\SO\"",Nothing)
,("\"\SI\"",Nothing)
,("\"\DLE\"",Nothing)
,("\"\DC1\"",Nothing)
,("\"\DC2\"",Nothing)
,("\"\DC3\"",Nothing)
,("\"\DC4\"",Nothing)
,("\"\NAK\"",Nothing)
,("\"\SYN\"",Nothing)
,("\"\ETB\"",Nothing)
,("\"\CAN\"",Nothing)
,("\"\EM\"",Nothing)
,("\"\SUB\"",Nothing)
,("\"\ESC\"",Nothing)
,("\"\FS\"",Nothing)
,("\"\GS\"",Nothing)
,("\"\RS\"",Nothing)
,("\"\US\"",Nothing)
,("\"\DEL\"",Nothing)
]
Jump to Line
Something went wrong with that request. Please try again.