11
12
13
14
15
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
* 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"
#define PLIST(x) \
@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" \
@"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" " \
@"\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" \
@"<plist version=\"1.0\">\n" \
x @"\n" \
@"</plist>"
static OFString *const module = @"OFPropertyList";
static OFString *const PLIST1 = PLIST(@"<string>Hello</string>");
static OFString *const PLIST2 = PLIST(
@"<array>"
@" <string>Hello</string>"
@" <data>V29ybGQh</data>"
@" <date>2018-03-14T12:34:56Z</date>"
@" <true/>"
@" <false/>"
@" <real>12.25</real>"
@" <integer>-10</integer>"
@"</array>");
static OFString *const PLIST3 = PLIST(
@"<dict>"
@" <key>array</key>"
@" <array>"
@" <string>Hello</string>"
@" <data>V29ybGQh</data>"
@" <date>2018-03-14T12:34:56Z</date>"
@" <true/>"
@" <false/>"
@" <real>12.25</real>"
@" <integer>-10</integer>"
@" </array>"
@" <key>foo</key>"
@" <string>bar</string>"
@"</dict>");
@implementation TestsAppDelegate (OFPLISTParser)
- (void)propertyListTests
{
void *pool = objc_autoreleasePoolPush();
OFArray *array = [OFArray arrayWithObjects:
@"Hello",
[OFData dataWithItems: "World!" count: 6],
[OFDate dateWithTimeIntervalSince1970: 1521030896],
[OFNumber numberWithBool: true],
[OFNumber numberWithBool: false],
[OFNumber numberWithFloat: 12.25f],
[OFNumber numberWithInt: -10],
nil];
TEST(@"-[objectByParsingPropertyList:] #1",
[PLIST1.objectByParsingPropertyList isEqual: @"Hello"])
TEST(@"-[objectByParsingPropertyList:] #2",
[PLIST2.objectByParsingPropertyList isEqual: array])
TEST(@"-[objectByParsingPropertyList:] #3",
[PLIST3.objectByParsingPropertyList isEqual:
[OFDictionary dictionaryWithKeysAndObjects:
@"array", array,
@"foo", @"bar",
nil]])
EXPECT_EXCEPTION(@"Detecting unsupported version",
OFUnsupportedVersionException,
[[PLIST(@"<string/>") stringByReplacingOccurrencesOfString: @"1.0"
withString: @"1.1"]
objectByParsingPropertyList])
EXPECT_EXCEPTION(
@"-[objectByParsingPropertyList] detecting invalid format #1",
OFInvalidFormatException,
[PLIST(@"<string x='b'/>") objectByParsingPropertyList])
EXPECT_EXCEPTION(
@"-[objectByParsingPropertyList] detecting invalid format #2",
OFInvalidFormatException,
[PLIST(@"<string xmlns='foo'/>") objectByParsingPropertyList])
EXPECT_EXCEPTION(
@"-[objectByParsingPropertyList] detecting invalid format #3",
OFInvalidFormatException,
[PLIST(@"<dict count='0'/>") objectByParsingPropertyList])
EXPECT_EXCEPTION(
@"-[objectByParsingPropertyList] detecting invalid format #4",
OFInvalidFormatException,
[PLIST(@"<dict><key/><string/><key/></dict>")
objectByParsingPropertyList])
EXPECT_EXCEPTION(
@"-[objectByParsingPropertyList] detecting invalid format #5",
OFInvalidFormatException,
[PLIST(@"<dict><key x='x'/><string/></dict>")
objectByParsingPropertyList])
objc_autoreleasePoolPop(pool);
}
@end
|
|
>
>
>
>
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
<
<
<
>
|
|
|
>
>
>
>
>
>
>
>
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
|
|
|
|
|
>
>
>
|
|
|
>
|
|
<
|
<
|
<
>
|
|
<
<
>
|
|
<
|
|
|
>
>
|
>
<
<
<
>
|
|
<
|
11
12
13
14
15
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
* 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 OFPropertyListTests: OTTestCase
@end
#define PLIST(x) \
@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" \
@"<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" " \
@"\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">" \
@"<plist version=\"1.0\">\n" \
x @"\n" \
@"</plist>"
@implementation OFPropertyListTests
- (void)testObjectByParsingPropertyList
{
OFArray *array = [OFArray arrayWithObjects:
@"Hello",
[OFData dataWithItems: "World!" count: 6],
[OFDate dateWithTimeIntervalSince1970: 1521030896],
[OFNumber numberWithBool: true],
[OFNumber numberWithBool: false],
[OFNumber numberWithFloat: 12.25f],
[OFNumber numberWithInt: -10],
nil];
OTAssertEqualObjects([PLIST(
@"<string>Hello</string>") objectByParsingPropertyList],
@"Hello");
OTAssertEqualObjects([PLIST(
@"<array>"
@" <string>Hello</string>"
@" <data>V29ybGQh</data>"
@" <date>2018-03-14T12:34:56Z</date>"
@" <true/>"
@" <false/>"
@" <real>12.25</real>"
@" <integer>-10</integer>"
@"</array>") objectByParsingPropertyList],
array);
OTAssertEqualObjects([PLIST(
@"<dict>"
@" <key>array</key>"
@" <array>"
@" <string>Hello</string>"
@" <data>V29ybGQh</data>"
@" <date>2018-03-14T12:34:56Z</date>"
@" <true/>"
@" <false/>"
@" <real>12.25</real>"
@" <integer>-10</integer>"
@" </array>"
@" <key>foo</key>"
@" <string>bar</string>"
@"</dict>") objectByParsingPropertyList],
([OFDictionary dictionaryWithKeysAndObjects:
@"array", array,
@"foo", @"bar",
nil]));
}
- (void)testDetectUnsupportedVersion
{
OTAssertThrowsSpecific(
[[PLIST(@"<string/>")
stringByReplacingOccurrencesOfString: @"1.0"
withString: @"1.1"]
objectByParsingPropertyList],
OFUnsupportedVersionException);
}
- (void)testDetectInvalidFormat
{
OTAssertThrowsSpecific(
[PLIST(@"<string x='b'/>") objectByParsingPropertyList],
OFInvalidFormatException);
OTAssertThrowsSpecific(
[PLIST(@"<string xmlns='foo'/>") objectByParsingPropertyList],
OFInvalidFormatException);
OTAssertThrowsSpecific(
[PLIST(@"<dict count='0'/>") objectByParsingPropertyList],
OFInvalidFormatException);
OTAssertThrowsSpecific(
[PLIST(@"<dict><key/><string/><key/></dict>")
objectByParsingPropertyList],
OFInvalidFormatException);
OTAssertThrowsSpecific(
[PLIST(@"<dict><key x='x'/><string/></dict>")
objectByParsingPropertyList],
OFInvalidFormatException);
}
@end
|