Index: new_tests/Makefile ================================================================== --- new_tests/Makefile +++ new_tests/Makefile @@ -5,10 +5,11 @@ PROG_NOINST = tests${PROG_SUFFIX} SRCS = OFCharacterSetTests.m \ OFColorTests.m \ OFIRITests.m \ OFInvocationTests.m \ + OFJSONTests.m \ OFMatrix4x4Tests.m \ OFMethodSignatureTests.m \ OFNumberTests.m \ OFPBKDF2Tests.m \ OFPropertyListTests.m \ ADDED new_tests/OFJSONTests.m Index: new_tests/OFJSONTests.m ================================================================== --- new_tests/OFJSONTests.m +++ new_tests/OFJSONTests.m @@ -0,0 +1,135 @@ +/* + * Copyright (c) 2008-2024 Jonathan Schleifer + * + * All rights reserved. + * + * This file is part of ObjFW. It may be distributed under the terms of the + * Q Public License 1.0, which can be found in the file LICENSE.QPL included in + * the packaging of this file. + * + * Alternatively, it may be distributed under the terms of the GNU General + * Public License, either version 2 or 3, which can be found in the file + * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this + * file. + */ + +#include "config.h" + +#import "ObjFW.h" +#import "ObjFWTest.h" + +@interface OFJSONTests: OTTestCase +{ + unsigned long long _hashSeed; + OFDictionary *_dictionary; +} +@end + +extern unsigned long long OFHashSeed; + +static OFString *string = @"{\"foo\"\t:'b\\na\\r', \"x\":/*foo*/ [.5\r,0xF," + @"null//bar\n,\"foo\",false]}"; + +@implementation OFJSONTests +- (void)setUp +{ + _hashSeed = OFHashSeed; + OFHashSeed = 0; + + _dictionary = [[OFDictionary alloc] initWithKeysAndObjects: + @"foo", @"b\na\r", + @"x", [OFArray arrayWithObjects: + [OFNumber numberWithFloat: .5f], + [OFNumber numberWithInt: 0xF], + [OFNull null], + @"foo", + [OFNumber numberWithBool: false], + nil], + nil]; +} + +- (void)tearDown +{ + OFHashSeed = _hashSeed; +} + +- (void)dealloc +{ + [_dictionary release]; + + [super dealloc]; +} + +- (void)testObjectByParsingJSON +{ + OTAssertEqualObjects(string.objectByParsingJSON, _dictionary); +} + +- (void)testJSONRepresentation +{ + OTAssertEqualObjects(_dictionary.JSONRepresentation, + @"{\"x\":[0.5,15,null,\"foo\",false],\"foo\":\"b\\na\\r\"}"); +} + +- (void)testPrettyJSONRepresentation +{ + OTAssertEqualObjects([_dictionary JSONRepresentationWithOptions: + OFJSONRepresentationOptionPretty], + @"{\n\t\"x\": [\n\t\t0.5,\n\t\t15,\n\t\tnull,\n\t\t" + @"\"foo\",\n\t\tfalse\n\t],\n\t\"foo\": \"b\\na\\r\"\n}"); +} + +- (void)testJSON5Representation +{ + OTAssertEqualObjects([_dictionary JSONRepresentationWithOptions: + OFJSONRepresentationOptionJSON5], + @"{x:[0.5,15,null,\"foo\",false],foo:\"b\\\na\\r\"}"); +} + +- (void)testObjectByParsingJSONFailsWithInvalidJSON +{ + OTAssertThrowsSpecific([@"{" objectByParsingJSON], + OFInvalidJSONException); + + OTAssertThrowsSpecific([@"]" objectByParsingJSON], + OFInvalidJSONException); + + OTAssertThrowsSpecific([@"bar" objectByParsingJSON], + OFInvalidJSONException); + + OTAssertThrowsSpecific([@"[\"a\" \"b\"]" objectByParsingJSON], + OFInvalidJSONException); +} + +- (void)testObjectByParsingJSONWithDeepNesting +{ + OTAssertEqualObjects( + @"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[{}]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]" + .objectByParsingJSON, + [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: [OFArray arrayWithObject: + [OFArray arrayWithObject: + [OFDictionary dictionary]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]); +} + +- (void)testObjectByParsingJSONFailsWithTooDeepNesting +{ + OTAssertThrowsSpecific( + [@"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[{}]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]" + objectByParsingJSON], + OFInvalidJSONException); +} +@end Index: tests/Makefile ================================================================== --- tests/Makefile +++ tests/Makefile @@ -22,11 +22,10 @@ OFDataTests.m \ OFDateTests.m \ OFDictionaryTests.m \ OFHMACTests.m \ OFINIFileTests.m \ - OFJSONTests.m \ OFListTests.m \ OFLocaleTests.m \ OFMD5HashTests.m \ OFMemoryStreamTests.m \ OFNotificationCenterTests.m \ DELETED tests/OFJSONTests.m Index: tests/OFJSONTests.m ================================================================== --- tests/OFJSONTests.m +++ tests/OFJSONTests.m @@ -1,92 +0,0 @@ -/* - * Copyright (c) 2008-2024 Jonathan Schleifer - * - * All rights reserved. - * - * This file is part of ObjFW. It may be distributed under the terms of the - * Q Public License 1.0, which can be found in the file LICENSE.QPL included in - * the packaging of this file. - * - * Alternatively, it may be distributed under the terms of the GNU General - * Public License, either version 2 or 3, which can be found in the file - * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this - * file. - */ - -#include "config.h" - -#import "TestsAppDelegate.h" - -static OFString *const module = @"OFJSON"; - -@implementation TestsAppDelegate (JSONTests) -- (void)JSONTests -{ - void *pool = objc_autoreleasePoolPush(); - OFString *string = @"{\"foo\"\t:'b\\na\\r', \"x\":/*foo*/ [.5\r,0xF," - @"null//bar\n,\"foo\",false]}"; - OFDictionary *dict = [OFDictionary dictionaryWithKeysAndObjects: - @"foo", @"b\na\r", - @"x", [OFArray arrayWithObjects: - [OFNumber numberWithFloat: .5f], - [OFNumber numberWithInt: 0xF], - [OFNull null], - @"foo", - [OFNumber numberWithBool: false], - nil], - nil]; - - TEST(@"-[objectByParsingJSON] #1", - [string.objectByParsingJSON isEqual: dict]) - - TEST(@"-[JSONRepresentation]", - [[dict JSONRepresentation] isEqual: - @"{\"x\":[0.5,15,null,\"foo\",false],\"foo\":\"b\\na\\r\"}"]) - - TEST(@"OFJSONRepresentationOptionPretty", - [[dict JSONRepresentationWithOptions: - OFJSONRepresentationOptionPretty] isEqual: - @"{\n\t\"x\": [\n\t\t0.5,\n\t\t15,\n\t\tnull,\n\t\t" - @"\"foo\",\n\t\tfalse\n\t],\n\t\"foo\": \"b\\na\\r\"\n}"]) - - TEST(@"OFJSONRepresentationOptionJSON5", - [[dict JSONRepresentationWithOptions: - OFJSONRepresentationOptionJSON5] isEqual: - @"{x:[0.5,15,null,\"foo\",false],foo:\"b\\\na\\r\"}"]) - - EXPECT_EXCEPTION(@"-[objectByParsingJSON] #2", OFInvalidJSONException, - [@"{" objectByParsingJSON]) - EXPECT_EXCEPTION(@"-[objectByParsingJSON] #3", OFInvalidJSONException, - [@"]" objectByParsingJSON]) - EXPECT_EXCEPTION(@"-[objectByParsingJSON] #4", OFInvalidJSONException, - [@"bar" objectByParsingJSON]) - EXPECT_EXCEPTION(@"-[objectByParsingJSON] #5", OFInvalidJSONException, - [@"[\"a\" \"b\"]" objectByParsingJSON]) - - TEST(@"-[objectByParsingJSON] #6", - [@"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[{}]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]" - .objectByParsingJSON isEqual: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: [OFArray arrayWithObject: - [OFArray arrayWithObject: - [OFDictionary dictionary]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]) - - EXPECT_EXCEPTION(@"-[objectByParsingJSON] #7", OFInvalidJSONException, - [@"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[{}]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]" - objectByParsingJSON]) - - objc_autoreleasePoolPop(pool); -} -@end Index: tests/TestsAppDelegate.h ================================================================== --- tests/TestsAppDelegate.h +++ tests/TestsAppDelegate.h @@ -109,14 +109,10 @@ @interface TestsAppDelegate (OFIPXSocketTests) - (void)IPXSocketTests; @end -@interface TestsAppDelegate (OFJSONTests) -- (void)JSONTests; -@end - @interface TestsAppDelegate (OFHMACTests) - (void)HMACTests; @end @interface TestsAppDelegate (OFKernelEventObserverTests) Index: tests/TestsAppDelegate.m ================================================================== --- tests/TestsAppDelegate.m +++ tests/TestsAppDelegate.m @@ -429,11 +429,10 @@ [self HTTPCookieManagerTests]; #endif [self XMLParserTests]; [self XMLNodeTests]; [self XMLElementBuilderTests]; - [self JSONTests]; #ifdef OF_HAVE_SUBPROCESSES [self subprocessTests]; #endif #ifdef OF_WINDOWS