Home modules.gotpike.org
Username: Password: [Create Account]
[Forgot Password?]

Modules

ADT
Database
GTK2
GUI
IP
PiJAX
Public
Sql
Stdio
Subversion
System
Tools
Xosd
lua
v4l2
wx

Recent Changes

Public.Parser.XML2 1.50
Public.ZeroMQ 1.1
Public.Template.Mustache 1.0
Public.Protocols.XMPP 1.4
Sql.Provider.jdbc 1.0

Popular Downloads

Public.Parser.JSON2 1.0
Public.Parser.JSON 0.2
GTK2 2.23
Public.Web.FCGI 1.8
Public.Parser.XML2 1.48


Module Information
Public.Parser.JSON2
Viewing contents of Public_Parser_JSON2-0.10/testsuite.in

// vim:syntax=lpc
// all examples are taken from www.json.org
//
test_true(programp(Public.Parser.JSON2.True))
test_true(programp(Public.Parser.JSON2.False))
test_true(programp(Public.Parser.JSON2.Null))
test_true(functionp(Public.Parser.JSON2.parse))
test_true(functionp(Public.Parser.JSON2.render))
test_true(functionp(Public.Parser.JSON2.validate))
// test the special types
test_true(objectp(Public.Parser.JSON2.Unicode))
test_true(Public.Parser.JSON2.true == Public.Parser.JSON2.parse("true"))
test_true(Public.Parser.JSON2.false == Public.Parser.JSON2.parse("false"))
test_true(Public.Parser.JSON2.null == Public.Parser.JSON2.parse("null"))
test_true(Public.Parser.JSON2.parse("true") == ~Public.Parser.JSON2.parse("false"))
// test numbers
test_true(Public.Parser.JSON2.parse("0E4") == 0.0)
// test reference counter.
// not sure how stable that is amoung pike versions....
test_true(_refs(Public.Parser.JSON2.parse("\" It says little, does less, means nothing.\"")) == 1)
test_true(_refs(Public.Parser.JSON2.parse(" { \"asd\" : 45 }  ")) == 1)
test_true(_refs(Public.Parser.JSON2.parse(" [ 45, 34, 3, 4, 5 ] ")) == 1)
// testing if junk at the end breaks
test_true(-1 != Public.Parser.JSON2.validate_utf8("[ 32, 23, 43  , 54   ,45]        uh"))
test_true(-1 == Public.Parser.JSON2.validate_utf8("[ 32, 23, 43  , 54   ,45]       "))
test_true("\\/\"\"\\" == Public.Parser.JSON2.parse(Public.Parser.JSON2.render("\\/\"\"\\")))
test_true("\\/skjdhf\"sdf\"d\\" == Public.Parser.JSON2.parse(Public.Parser.JSON2.render("\\/skjdhf\"sdf\"d\\")))
test_true("\"\\\\\\\""*20 == Public.Parser.JSON2.parse_utf8(Public.Parser.JSON2.parse(Public.Parser.JSON2.parse_utf8(Public.Parser.JSON2.parse(Public.Parser.JSON2.render(Public.Parser.JSON2.render_utf8(Public.Parser.JSON2.render(Public.Parser.JSON2.render_utf8("\"\\\\\\\""*20), Public.Parser.JSON2.HUMAN_READABLE))))))))
// test examples from json.org
test_true(equal(Public.Parser.JSON2.parse(
"{\
    \"glossary\": {\
        \"title\": \"example glossary\",\
	\"GlossDiv\": {\
            \"title\": \"S\",\
	    \"GlossList\": {\
                \"GlossEntry\": {\
                    \"ID\": \"SGML\",\
		    \"SortAs\": \"SGML\",\
		    \"GlossTerm\": \"Standard Generalized Markup Language\",\
		    \"Acronym\": \"SGML\",\
		    \"Abbrev\": \"ISO 8879:1986\",\
		    \"GlossDef\": {\
                        \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\",\
			\"GlossSeeAlso\": [\"GML\", \"XML\"]\
                    },\
		    \"GlossSee\": \"markup\"\
                }\
            }\
        }\
    }\
}\
"), ([
    "glossary": ([
        "title": "example glossary",
	"GlossDiv": ([
            "title": "S",
	    "GlossList": ([
                "GlossEntry": ([
                    "ID": "SGML",
		    "SortAs": "SGML",
		    "GlossTerm": "Standard Generalized Markup Language",
		    "Acronym": "SGML",
		    "Abbrev": "ISO 8879:1986",
		    "GlossDef": ([
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
			"GlossSeeAlso": ({"GML", "XML"})
                    ]),
		    "GlossSee": "markup"
                ])
            ])
        ])
    ])
])))
test_true(equal(Public.Parser.JSON2.parse(Public.Parser.JSON2.render(([
    "glossary": ([
        "title": "example glossary",
	"GlossDiv": ([
            "title": "S",
	    "GlossList": ([
                "GlossEntry": ([
                    "ID": "SGML",
		    "SortAs": "SGML",
		    "GlossTerm": "Standard Generalized Markup Language",
		    "Acronym": "SGML",
		    "Abbrev": "ISO 8879:1986",
		    "GlossDef": ([
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
			"GlossSeeAlso": ({"GML", "XML"})
                    ]),
		    "GlossSee": "markup"
                ])
            ])
        ])
    ])
]))), ([
    "glossary": ([
        "title": "example glossary",
	"GlossDiv": ([
            "title": "S",
	    "GlossList": ([
                "GlossEntry": ([
                    "ID": "SGML",
		    "SortAs": "SGML",
		    "GlossTerm": "Standard Generalized Markup Language",
		    "Acronym": "SGML",
		    "Abbrev": "ISO 8879:1986",
		    "GlossDef": ([
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
			"GlossSeeAlso": ({"GML", "XML"})
                    ]),
		    "GlossSee": "markup"
                ])
            ])
        ])
    ])
])))
test_true(equal(Public.Parser.JSON2.parse("    {	 \"menu\": 	{\
  \"id\": \"file\",\
  \"value\": \"File\",\
  \"popup\": {\
    \"menuitem\": [\
      {\"value\": \"New\", 	\"onclick\": \"CreateNewDoc()\"},\
      {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},\
      {\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}\
    ]\
  }\
}	}\
"), (["menu": ([
  "id": "file",
  "value": "File",
  "popup": ([
    "menuitem": ({
      (["value": "New", "onclick": "CreateNewDoc()"]),
      (["value": "Open", "onclick": "OpenDoc()"]),
      (["value": "Close", "onclick": "CloseDoc()"])
    })
  ])
])])))
test_true(equal(Public.Parser.JSON2.parse("{\"menu\": {\
    \"header\": \"SVG Viewer\",\
    \"items\": [\
        {\"id\": \"Open\"},\
        {\"id\": \"OpenNew\", \"label\": \"Open New\"},\
        null,\
        {\"id\": \"ZoomIn\", \"label\": \"Zoom In\"},\
        {\"id\": \"ZoomOut\", \"label\": \"Zoom Out\"},\
        {\"id\": \"OriginalView\", \"label\": \"Original View\"},\
        null,\
        {\"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\"},\
        null,\
        {\"id\": \"Help\"},\
        {\"id\": \"About\", \"label\": \"About Adobe CVG Viewer...\"}\
    ]\
}}\
"), (["menu": ([
    "header": "SVG Viewer",
    "items": ({
        (["id": "Open"]),
        (["id": "OpenNew", "label": "Open New"]),
        Public.Parser.JSON2.null,
        (["id": "ZoomIn", "label": "Zoom In"]),
        (["id": "ZoomOut", "label": "Zoom Out"]),
        (["id": "OriginalView", "label": "Original View"]),
        Public.Parser.JSON2.null,
        (["id": "Quality"]),
        (["id": "Pause"]),
        (["id": "Mute"]),
        Public.Parser.JSON2.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"]),
        Public.Parser.JSON2.null,
        (["id": "Help"]),
        (["id": "About", "label": "About Adobe CVG Viewer..."])
    })
])]) ))
test_true(
    "" + String.int2char(76) + String.int2char(2000) + String.int2char(10000) + String.int2char(900000) + "" == Public.Parser.JSON2.parse_utf8(string_to_utf8("\"" + String.int2char(76) + String.int2char(2000) + String.int2char(10000) + String.int2char(900000) + "\"")))
test_true(Public.Parser.JSON2.validate("{\"menu\": {\
    \"header\": \"SVG Viewer\",\
    \"items\": [\
        {\"id\": \"Open\"},\
        {\"id\": \"OpenNew\", \"label\": \"Open New\"},\
        null,\
        {\"id\": \"ZoomIn\", \"label\": \"Zoom In\"},\
        {\"id\": \"ZoomOut\", \"label\": \"Zoom Out\"},\
        {\"id\": \"OriginalView\", \"label\": \"Original View\"},\
        null,\
        {\"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\"},\
        null,\
        {\"id\": \"Help\"},\
        {\"id\": \"About\", \"label\": \"About Adobe CVG Viewer...\"}\
    ]\
}}\
") == -1 )
test_true((string)(map(enumerate(4700, sizeof(Public.Parser.JSON2.Unicode)/4700, 0, `+) + ({sizeof(Public.Parser.JSON2.Unicode)-1}), Public.Parser.JSON2.Unicode->`[])) == Public.Parser.JSON2.parse_utf8(Public.Parser.JSON2.render_utf8((string)(map(enumerate(4700, sizeof(Public.Parser.JSON2.Unicode)/4700, 0, `+) + ({sizeof(Public.Parser.JSON2.Unicode)-1}), Public.Parser.JSON2.Unicode->`[])))))
test_true(8 == String.width(Public.Parser.JSON2.render_utf8((string)(map(enumerate(4700, sizeof(Public.Parser.JSON2.Unicode)/4700, 0, `+) + ({sizeof(Public.Parser.JSON2.Unicode)-1}), Public.Parser.JSON2.Unicode->`[])))))
test_true(equal(String.width(Public.Parser.JSON2.render_utf8(([
    "glossary": ([
        "title": (string)(map(enumerate(4700, sizeof(Public.Parser.JSON2.Unicode)/4700, 0, `+) + ({sizeof(Public.Parser.JSON2.Unicode)-1}), Public.Parser.JSON2.Unicode->`[])),
	"GlossDiv": ([
            "title": "S",
	    "GlossList": ([
                "GlossEntry": ([
                    "ID": "SGML",
		    "SortAs": "SGML",
		    "GlossTerm": "Standard Generalized Markup Language",
		    "Acronym": "SGML",
		    "Abbrev": "ISO 8879:1986",
		    "GlossDef": ([
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
			"GlossSeeAlso": ({"GML", "XML"})
                    ]),
		    "GlossSee": "markup"
                ])
            ])
        ])
    ])
]))), 8))
test_true(equal(Public.Parser.JSON2.parse_utf8(Public.Parser.JSON2.render_utf8(([
    "glossary": ([
        "title": (string)(map(enumerate(4700, sizeof(Public.Parser.JSON2.Unicode)/4700, 0, `+) + ({sizeof(Public.Parser.JSON2.Unicode)-1}), Public.Parser.JSON2.Unicode->`[])),
	"GlossDiv": ([
            "title": "S",
	    "GlossList": ([
                "GlossEntry": ([
                    "ID": "SGML",
		    "SortAs": "SGML",
		    "GlossTerm": "Standard Generalized Markup Language",
		    "Acronym": "SGML",
		    "Abbrev": "ISO 8879:1986",
		    "GlossDef": ([
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
			"GlossSeeAlso": ({"GML", "XML"})
                    ]),
		    "GlossSee": "markup"
                ])
            ])
        ])
    ])
]))), ([
    "glossary": ([
        "title": (string)(map(enumerate(4700, sizeof(Public.Parser.JSON2.Unicode)/4700, 0, `+) + ({sizeof(Public.Parser.JSON2.Unicode)-1}), Public.Parser.JSON2.Unicode->`[])),
	"GlossDiv": ([
            "title": "S",
	    "GlossList": ([
                "GlossEntry": ([
                    "ID": "SGML",
		    "SortAs": "SGML",
		    "GlossTerm": "Standard Generalized Markup Language",
		    "Acronym": "SGML",
		    "Abbrev": "ISO 8879:1986",
		    "GlossDef": ([
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
			"GlossSeeAlso": ({"GML", "XML"})
                    ]),
		    "GlossSee": "markup"
                ])
            ])
        ])
    ])
])))
test_any([[
	string s = "sdfsdf  skldjf ";
	s[10] = 0xdfff;
	mixed e = catch {
		mixed a = Public.Parser.JSON2.parse(Public.Parser.JSON2.render(s));
	};
	return !!e;
]], 1)
test_any([[
	string s = "sdfsdf  skldjf ";
	s[10] = 0xdfff;
	mixed e = catch {
		mixed a = Public.Parser.JSON2.parse_utf8(Public.Parser.JSON2.render(string_to_utf8(s)));
	};
	return !!e;
]], 1)
test_any([[
	string s = "   ";
	s[1] = 0xe000;
	return (s == Public.Parser.JSON2.parse_utf8(Public.Parser.JSON2.render_utf8(s)));
]], 1)
test_any([[
	string s = "{ \"key\": null }";
	mixed e = catch {
		mixed a = Public.Parser.JSON2.parse(s);
	};
	mixed e1 = catch {
		mixed a = Public.Parser.JSON2.parse_utf8(s);
	};
	return !e && !e1;
]], 1)
test_any([[
	string s = "\"http:\\/\\/foobar\\/\"";
	string t = "http://foobar/";
	int a = (t == Public.Parser.JSON2.parse(s));
	int b = (t == Public.Parser.JSON2.parse_utf8(s));
	return a && b;
]], 1)


gotpike.org | Copyright © 2004 - 2019 | Pike is a trademark of Department of Computer and Information Science, Linköping University