16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
#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
{
[super 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;
[super tearDown];
}
- (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);
|
<
<
<
<
<
<
<
<
<
<
<
<
<
|
>
>
>
|
>
>
|
|
>
>
>
|
>
>
>
|
|
>
>
>
|
>
>
|
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#include "config.h"
#import "ObjFW.h"
#import "ObjFWTest.h"
@interface OFJSONTests: OTTestCase
{
OFDictionary *_dictionary;
}
@end
static OFString *string = @"{\"foo\"\t:'b\\na\\r', \"x\":/*foo*/ [.5\r,0xF,"
@"null//bar\n,\"foo\",false]}";
@implementation OFJSONTests
- (void)setUp
{
[super setUp];
_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)dealloc
{
[_dictionary release];
[super dealloc];
}
- (void)testObjectByParsingJSON
{
OTAssertEqualObjects(string.objectByParsingJSON, _dictionary);
}
- (void)testJSONRepresentation
{
OFString *representation = _dictionary.JSONRepresentation;
OTAssert(
[representation isEqual:
@"{\"x\":[0.5,15,null,\"foo\",false],\"foo\":\"b\\na\\r\"}"] ||
[representation isEqual:
@"{\"foo\":\"b\\na\\r\",\"x\":[0.5,15,null,\"foo\",false]}"]);
}
- (void)testPrettyJSONRepresentation
{
OFString *representation = [_dictionary JSONRepresentationWithOptions:
OFJSONRepresentationOptionPretty];
OTAssert(
[representation 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}"] ||
[representation isEqual:
@"{\n\t\"foo\": \"b\\na\\r\",\n\t\"x\": [\n\t\t0.5,\n\t\t15,"
@"\n\t\tnull,\n\t\t\"foo\",\n\t\tfalse\n\t]\n}"]);
}
- (void)testJSON5Representation
{
OFString *representation = [_dictionary JSONRepresentationWithOptions:
OFJSONRepresentationOptionJSON5];
OTAssert(
[representation isEqual:
@"{x:[0.5,15,null,\"foo\",false],foo:\"b\\\na\\r\"}"] ||
[representation isEqual:
@"{foo:\"b\\\na\\r\",x:[0.5,15,null,\"foo\",false]}"]);
}
- (void)testObjectByParsingJSONFailsWithInvalidJSON
{
OTAssertThrowsSpecific([@"{" objectByParsingJSON],
OFInvalidJSONException);
|