Skip to content
Permalink
Browse files

Combine parseJSON tests and fix style.

We only care about the result of parseJSON so there's no reason to feature detect the entire test.
  • Loading branch information
dmethvin committed Oct 20, 2012
1 parent 3144163 commit 32051e97c1e77d0b678648832d090168b534841c
Showing with 14 additions and 71 deletions.
  1. +14 −71 test/unit/core.js
@@ -1148,34 +1148,27 @@ test("jQuery.parseHTML", function() {
});

test("jQuery.parseJSON", function(){
expect(8);
expect( 9 );

equal( jQuery.parseJSON( null ), null, "Actual null returns null" );
equal( jQuery.isEmptyObject( jQuery.parseJSON("{}") ), true, "Empty object returns empty object" );
deepEqual( jQuery.parseJSON("{\"test\":1}"), { "test": 1 }, "Plain object parses" );
deepEqual( jQuery.parseJSON("\n{\"test\":1}"), { "test": 1 }, "Leading whitespaces are ignored." );
raises(function() {
jQuery.parseJSON();
}, null, "parseJson now matches JSON.parse for empty input." );
equal(jQuery.parseJSON( null ), null, "parseJson now matches JSON.parse on null input." );
}, null, "Undefined raises an error" );
raises( function() {
jQuery.parseJSON( "" );
}, null, "parseJson now matches JSON.parse for empty strings." );

deepEqual( jQuery.parseJSON("{}"), {}, "Plain object parsing." );
deepEqual( jQuery.parseJSON("{\"test\":1}"), {"test":1}, "Plain object parsing." );

deepEqual( jQuery.parseJSON("\n{\"test\":1}"), {"test":1}, "Make sure leading whitespaces are handled." );

try {
}, null, "Empty string raises an error" );
raises(function() {
jQuery.parseJSON("''");
}, null, "Single-quoted string raises an error" );
raises(function() {
jQuery.parseJSON("{a:1}");
ok( false, "Test malformed JSON string." );
} catch( e ) {
ok( true, "Test malformed JSON string." );
}

try {
}, null, "Unquoted property raises an error" );
raises(function() {
jQuery.parseJSON("{'a':1}");
ok( false, "Test malformed JSON string." );
} catch( e ) {
ok( true, "Test malformed JSON string." );
}
}, null, "Single-quoted property raises an error" );
});

test("jQuery.parseXML", 8, function(){
@@ -1350,53 +1343,3 @@ test("jQuery.camelCase()", function() {
equal( jQuery.camelCase( key ), val, "Converts: " + key + " => " + val );
});
});

// Ensure our window.JSON matches behavior of the native one, if it exists
if ( window.JSON ) {
test( "JQuery.parseJSON() equivalence to JSON.parse", function() {
expect( 10 );

var jsonParse = window.JSON;
window.JSON = null;

raises(function() {
jsonParse.parse("''");
});

raises(function() {
jQuery.parseJSON("''");
});

raises(function() {
jsonParse.parse("");
});

raises(function() {
jQuery.parseJSON("");
});

raises(function() {
jsonParse.parse({});
});

raises(function() {
jQuery.parseJSON({});
});

var parsedValue = jsonParse.parse(null);
equal( parsedValue, null, "parsed null" );

parsedValue = jQuery.parseJSON(null);
equal( parsedValue, null, "parsed null" );

parsedValue = jsonParse.parse("{}");
equal( (typeof parsedValue === "object"), true );

parsedValue = jQuery.parseJSON("{}");
equal( (typeof parsedValue === "object"), true );


window.JSON = jsonParse;
});
}

0 comments on commit 32051e9

Please sign in to comment.
You can’t perform that action at this time.