michael@0: gTestsubsuite='JSON'; michael@0: michael@0: function testJSON(str, expectSyntaxError) michael@0: { michael@0: // Leading and trailing whitespace never affect parsing, so test the string michael@0: // multiple times with and without whitespace around it as it's easy and can michael@0: // potentially detect bugs. michael@0: michael@0: // Try the provided string michael@0: try michael@0: { michael@0: JSON.parse(str); michael@0: reportCompare(false, expectSyntaxError, michael@0: "string <" + str + "> " + michael@0: "should" + (expectSyntaxError ? "n't" : "") + " " + michael@0: "have parsed as JSON"); michael@0: } michael@0: catch (e) michael@0: { michael@0: if (!(e instanceof SyntaxError)) michael@0: { michael@0: reportCompare(true, false, michael@0: "parsing string <" + str + "> threw a non-SyntaxError " + michael@0: "exception: " + e); michael@0: } michael@0: else michael@0: { michael@0: reportCompare(true, expectSyntaxError, michael@0: "string <" + str + "> " + michael@0: "should" + (expectSyntaxError ? "n't" : "") + " " + michael@0: "have parsed as JSON, exception: " + e); michael@0: } michael@0: } michael@0: michael@0: // Now try the provided string with trailing whitespace michael@0: try michael@0: { michael@0: JSON.parse(str + " "); michael@0: reportCompare(false, expectSyntaxError, michael@0: "string <" + str + " > " + michael@0: "should" + (expectSyntaxError ? "n't" : "") + " " + michael@0: "have parsed as JSON"); michael@0: } michael@0: catch (e) michael@0: { michael@0: if (!(e instanceof SyntaxError)) michael@0: { michael@0: reportCompare(true, false, michael@0: "parsing string <" + str + " > threw a non-SyntaxError " + michael@0: "exception: " + e); michael@0: } michael@0: else michael@0: { michael@0: reportCompare(true, expectSyntaxError, michael@0: "string <" + str + " > " + michael@0: "should" + (expectSyntaxError ? "n't" : "") + " " + michael@0: "have parsed as JSON, exception: " + e); michael@0: } michael@0: } michael@0: michael@0: // Now try the provided string with leading whitespace michael@0: try michael@0: { michael@0: JSON.parse(" " + str); michael@0: reportCompare(false, expectSyntaxError, michael@0: "string < " + str + "> " + michael@0: "should" + (expectSyntaxError ? "n't" : "") + " " + michael@0: "have parsed as JSON"); michael@0: } michael@0: catch (e) michael@0: { michael@0: if (!(e instanceof SyntaxError)) michael@0: { michael@0: reportCompare(true, false, michael@0: "parsing string < " + str + "> threw a non-SyntaxError " + michael@0: "exception: " + e); michael@0: } michael@0: else michael@0: { michael@0: reportCompare(true, expectSyntaxError, michael@0: "string < " + str + "> " + michael@0: "should" + (expectSyntaxError ? "n't" : "") + " " + michael@0: "have parsed as JSON, exception: " + e); michael@0: } michael@0: } michael@0: michael@0: // Now try the provided string with whitespace surrounding it michael@0: try michael@0: { michael@0: JSON.parse(" " + str + " "); michael@0: reportCompare(false, expectSyntaxError, michael@0: "string < " + str + " > " + michael@0: "should" + (expectSyntaxError ? "n't" : "") + " " + michael@0: "have parsed as JSON"); michael@0: } michael@0: catch (e) michael@0: { michael@0: if (!(e instanceof SyntaxError)) michael@0: { michael@0: reportCompare(true, false, michael@0: "parsing string < " + str + " > threw a non-SyntaxError " + michael@0: "exception: " + e); michael@0: } michael@0: else michael@0: { michael@0: reportCompare(true, expectSyntaxError, michael@0: "string < " + str + " > " + michael@0: "should" + (expectSyntaxError ? "n't" : "") + " " + michael@0: "have parsed as JSON, exception: " + e); michael@0: } michael@0: } michael@0: }