Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F2890998
PhutilJSONParserTestCase.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Advanced/Developer...
View Handle
View Hovercard
Size
3 KB
Referenced Files
None
Subscribers
None
PhutilJSONParserTestCase.php
View Options
<?php
final
class
PhutilJSONParserTestCase
extends
PhutilTestCase
{
public
function
testValidJSON
(
)
{
$parser
=
new
PhutilJSONParser
(
)
;
$tests
=
array
(
'{}'
=>
array
(
)
,
'[]'
=>
array
(
)
,
'{"foo": "bar"}'
=>
array
(
'foo'
=>
'bar'
)
,
'[1, "foo", true, null]'
=>
array
(
1
,
'foo'
,
true
,
null
)
,
'{"foo": {"bar": "baz"}}'
=>
array
(
'foo'
=>
array
(
'bar'
=>
'baz'
)
)
,
'{"foo": "bar", "bar": ["baz"]}'
=>
array
(
'foo'
=>
'bar'
,
'bar'
=>
array
(
'baz'
)
)
,
'{"foo": "bar", "bar": {"baz": "foo"}}'
=>
array
(
'foo'
=>
'bar'
,
'bar'
=>
array
(
'baz'
=>
'foo'
)
)
,
'{"": ""}'
=>
array
(
''
=>
''
)
,
'{"test":"\u00c9v\u00e9nement"}'
=>
array
(
'test'
=>
"\xC3\x89v\xC3\xA9nement"
)
,
'["\u00c9v\u00e9nement"]'
=>
array
(
"\xC3\x89v\xC3\xA9nement"
)
,
'{"test":"http:\/\/foo\\\\zomg"}'
=>
array
(
'test'
=>
'http://foo\\zomg'
)
,
'["http:\/\/foo\\\\zomg"]'
=>
array
(
'http://foo\\zomg'
)
,
Filesystem
::
readFile
(
dirname
(
__FILE__
)
.
'/json/base64.json'
)
=>
array
(
'action'
=>
'candidate.create'
,
'actionId'
=>
'80653a26cc46357ff79ff83b47e27c3cb7a668bd'
,
'params'
=>
array
(
'attachments'
=>
array
(
Filesystem
::
readFile
(
dirname
(
__FILE__
)
.
'/json/base64.data'
)
,
)
,
)
,
)
,
)
;
foreach
(
$tests
as
$input
=>
$expect
)
{
$this
->
assertEqual
(
$expect
,
$parser
->
parse
(
$input
)
,
pht
(
'Parsing JSON: %s'
,
$input
)
)
;
}
}
public
function
testInvalidJSON
(
)
{
$parser
=
new
PhutilJSONParser
(
)
;
$tests
=
array
(
'{'
=>
array
(
'line'
=>
1
,
'char'
=>
1
,
'token'
=>
'EOF'
,
)
,
'['
=>
array
(
'line'
=>
1
,
'char'
=>
1
,
'token'
=>
'EOF'
,
)
,
'{"foo":'
=>
array
(
'line'
=>
1
,
'char'
=>
7
,
'token'
=>
'EOF'
,
)
,
'{"foo":"bar",}'
=>
array
(
'line'
=>
1
,
'char'
=>
13
,
'token'
=>
'}'
,
)
,
'{{}'
=>
array
(
'line'
=>
1
,
'char'
=>
1
,
'token'
=>
'{'
,
)
,
'{}}'
=>
array
(
'line'
=>
1
,
'char'
=>
2
,
'token'
=>
'}'
,
)
,
"{\"foo\":\"bar\",\n\"bar\":\"baz\",}"
=>
array
(
'line'
=>
2
,
'char'
=>
12
,
'token'
=>
'}'
,
)
,
"{'foo': 'bar'}"
=>
array
(
'line'
=>
1
,
'char'
=>
1
,
'token'
=>
'INVALID'
,
)
,
"{\"foo\": \"bar\nbaz\"}"
=>
array
(
'line'
=>
1
,
'char'
=>
7
,
'token'
=>
'INVALID'
,
)
,
'{"foo": "bar\z"}'
=>
array
(
'line'
=>
1
,
'char'
=>
7
,
'token'
=>
'INVALID'
,
)
,
)
;
foreach
(
$tests
as
$input
=>
$expected
)
{
$caught
=
null
;
try
{
$parser
->
parse
(
$input
)
;
}
catch
(
Exception
$ex
)
{
$caught
=
$ex
;
}
$this
->
assertTrue
(
$caught
instanceof
PhutilJSONParserException
)
;
$this
->
assertEqual
(
$expected
[
'line'
]
,
$caught
->
getSourceLine
(
)
)
;
$this
->
assertEqual
(
$expected
[
'char'
]
,
$caught
->
getSourceChar
(
)
)
;
$this
->
assertEqual
(
$expected
[
'token'
]
,
$caught
->
getSourceToken
(
)
)
;
}
}
public
function
testDuplicateKeys
(
)
{
$parser
=
new
PhutilJSONParser
(
)
;
$tests
=
array
(
'{"foo": "bar", "foo": "baz"}'
=>
array
(
'foo'
=>
'baz'
)
,
)
;
foreach
(
$tests
as
$input
=>
$expect
)
{
$parser
->
setAllowDuplicateKeys
(
true
)
;
$this
->
assertEqual
(
$expect
,
$parser
->
parse
(
$input
)
,
pht
(
'Parsing JSON: %s'
,
$input
)
)
;
$parser
->
setAllowDuplicateKeys
(
false
)
;
$caught
=
null
;
try
{
$parser
->
parse
(
$input
)
;
}
catch
(
Exception
$ex
)
{
$caught
=
$ex
;
}
$this
->
assertTrue
(
$caught
instanceof
PhutilJSONParserException
)
;
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sun, Jan 19, 14:27 (3 w, 2 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1116140
Default Alt Text
PhutilJSONParserTestCase.php (3 KB)
Attached To
Mode
rARC Arcanist
Attached
Detach File
Event Timeline
Log In to Comment