Skip to content
Permalink
Browse files
Followup for #12751, only test on browsers with JSON.parse
  • Loading branch information
dmethvin committed Oct 19, 2012
1 parent c6cf30a commit 3144163f61ea89855818fab597a2b5f1074d10c3
Showing 1 changed file with 36 additions and 33 deletions.
@@ -1351,49 +1351,52 @@ test("jQuery.camelCase()", function() {
});
});

test( "JQuery.parseJSON() test internal parseJson (using fallback) to make sure that it throws like JSON.parse", function() {
expect( 10 );
// 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;
var jsonParse = window.JSON;
window.JSON = null;

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

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

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

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

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

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

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

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

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

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

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

window.JSON = jsonParse;
} );
window.JSON = jsonParse;
});
}

4 comments on commit 3144163

@jaubourg
Copy link
Member

@jaubourg jaubourg commented on 3144163 Oct 19, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't keeping the tests for browsers with JSON.parse the best way to make sure we have the same behaviour across browsers? Or am I missing something here?

@dmethvin
Copy link
Member Author

@dmethvin dmethvin commented on 3144163 Oct 19, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can only compare our results to json.parse on browser that have the native method. I agree it's not optimal to test this everywhere but where we actually use it. Perhaps we dump this test's attempt to compare since we don't care about the native result.

@jaubourg
Copy link
Member

@jaubourg jaubourg commented on 3144163 Oct 20, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I get what you mean now. Why not just put the tests with the expected result using jQuery.parseJSON only? Browsers with JSON.parse support will use this code path, others will use our polyfil. We'd have some sort of comparison... just not in the same browser but between browsers. Makes sense?

@gibson042
Copy link
Member

@gibson042 gibson042 commented on 3144163 Oct 20, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm with @jaubourg on this one... JSON.parse is just another component we abstract, and will have to normalize if a flaw is discovered in some environment's native implementation.

Please sign in to comment.