1 2 3 4 5 6 7 8 9 10 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
/*
* Copyright (c) 2008-2025 Jonathan Schleifer <js@nil.im>
*
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License version 3.0 only,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* version 3.0 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* version 3.0 along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#import "ObjFW.h"
#import "ObjFWTest.h"
@interface OFXMLNodeTests: OTTestCase
@end
@implementation OFXMLNodeTests
- (void)testElementWithName
{
OTAssertEqualObjects(
[[OFXMLElement elementWithName: @"foo"] XMLString],
@"<foo/>");
}
- (void)testElementWithNameStringValue
{
OTAssertEqualObjects(
[[OFXMLElement elementWithName: @"foo"
stringValue: @"b&ar"] XMLString],
@"<foo>b&ar</foo>");
}
- (void)testElementWithNameNamespace
{
OFXMLElement *element;
element = [OFXMLElement elementWithName: @"foo"
namespace: @"urn:objfw:test"];
[element addAttributeWithName: @"test" stringValue: @"test"];
[element setPrefix: @"objfw-test" forNamespace: @"urn:objfw:test"];
OTAssertEqualObjects(element.XMLString,
@"<objfw-test:foo test='test'/>");
element = [OFXMLElement elementWithName: @"foo"
namespace: @"urn:objfw:test"];
[element addAttributeWithName: @"test" stringValue: @"test"];
OTAssertEqualObjects(element.XMLString,
@"<foo xmlns='urn:objfw:test' test='test'/>");
}
- (void)testElementWithNameNamespaceStringValue
{
OFXMLElement *element = [OFXMLElement elementWithName: @"foo"
namespace: @"urn:objfw:test"
stringValue: @"x"];
[element setPrefix: @"objfw-test" forNamespace: @"urn:objfw:test"];
OTAssertEqualObjects(element.XMLString,
@"<objfw-test:foo>x</objfw-test:foo>");
}
- (void)testElementWithXMLStringAndStringValue
{
OTAssertEqualObjects([[OFXMLElement elementWithXMLString:
@"<?xml version='1.0' encoding='UTF-8'?>\r\n<x>foo<![CDATA[bar]]>"
@"<y>b<!-- fooo -->az</y>qux</x>"] stringValue],
@"foobarbazqux");
}
- (void)testCharactersWithString
{
OTAssertEqualObjects(
[[OFXMLCharacters charactersWithString: @"<foo>"] XMLString],
@"<foo>");
}
- (void)testCDATAWithString
{
OTAssertEqualObjects(
[[OFXMLCDATA CDATAWithString: @"<foo>"] XMLString],
@"<![CDATA[<foo>]]>");
}
- (void)testCommentWithText
{
OTAssertEqualObjects(
[[OFXMLComment commentWithText: @" comment "] XMLString],
@"<!-- comment -->");
}
- (void)testIsEqual
{
OTAssertEqualObjects(
[OFXMLElement elementWithXMLString: @"<foo bar='asd'/>"],
[OFXMLElement elementWithXMLString: @"<foo bar='asd'></foo>"]);
OTAssertEqualObjects(
[OFXMLElement elementWithXMLString: @"<x><y/></x>"],
[OFXMLElement elementWithXMLString: @"<x><y></y></x>"]);
OTAssertNotEqualObjects(
[OFXMLElement elementWithXMLString: @"<x><Y/></x>"],
[OFXMLElement elementWithXMLString: @"<x><y></y></x>"]);
}
- (void)testHash
{
OTAssertEqual(
[[OFXMLElement elementWithXMLString: @"<foo bar='asd'/>"] hash],
[[OFXMLElement elementWithXMLString: @"<foo bar='asd'></foo>"]
hash]);
OTAssertEqual(
[[OFXMLElement elementWithXMLString: @"<x><y/></x>"] hash],
[[OFXMLElement elementWithXMLString: @"<x><y></y></x>"] hash]);
OTAssertNotEqual(
[[OFXMLElement elementWithXMLString: @"<x><Y/></x>"] hash],
[[OFXMLElement elementWithXMLString: @"<x><y></y></x>"] hash]);
}
- (void)testAddAttributeWithNameStringValue
{
OFXMLElement *element = [OFXMLElement elementWithName: @"foo"
stringValue: @"b&ar"];
[element setPrefix: @"objfw-test" forNamespace: @"urn:objfw:test"];
[element addAttributeWithName: @"foo"
stringValue: @"b&ar"];
[element addAttributeWithName: @"foo"
namespace: @"urn:objfw:test"
stringValue: @"bar"];
OTAssertEqualObjects(element.XMLString,
@"<foo foo='b&ar' objfw-test:foo='bar'>b&ar</foo>");
}
- (void)testRemoveAttributeForNameNamespace
{
OFXMLElement *element = [OFXMLElement elementWithName: @"foo"
stringValue: @"b&ar"];
[element setPrefix: @"objfw-test" forNamespace: @"urn:objfw:test"];
[element addAttributeWithName: @"foo"
stringValue: @"b&ar"];
[element addAttributeWithName: @"foo"
namespace: @"urn:objfw:test"
stringValue: @"bar"];
[element removeAttributeForName: @"foo"];
OTAssertEqualObjects(element.XMLString,
@"<foo objfw-test:foo='bar'>b&ar</foo>");
[element removeAttributeForName: @"foo" namespace: @"urn:objfw:test"];
OTAssertEqualObjects(element.XMLString, @"<foo>b&ar</foo>");
}
- (void)testAddChild
{
OFXMLElement *element;
element = [OFXMLElement elementWithName: @"foo"];
[element addAttributeWithName: @"foo" stringValue: @"b&ar"];
[element addChild: [OFXMLElement elementWithName: @"bar"]];
OTAssertEqualObjects(element.XMLString,
@"<foo foo='b&ar'><bar/></foo>");
element = [OFXMLElement elementWithName: @"foo"
namespace: @"urn:objfw:test"];
[element setPrefix: @"objfw-test" forNamespace: @"urn:objfw:test"];
[element addAttributeWithName: @"test" stringValue: @"test"];
[element addChild: [OFXMLElement elementWithName: @"bar"
namespace: @"urn:objfw:test"]];
OTAssertEqualObjects(element.XMLString,
@"<objfw-test:foo test='test'><objfw-test:bar/></objfw-test:foo>");
}
- (void)testElementsForNameNamespace
{
OFXMLElement *element = [OFXMLElement elementWithName: @"foo"];
OFXMLElement *bar;
[element addChild: [OFXMLElement elementWithName: @"foo"]];
bar = [OFXMLElement elementWithName: @"bar"
namespace: @"urn:objfw:test"];
[element addChild: bar];
OTAssertEqualObjects([element elementsForName: @"bar"
namespace: @"urn:objfw:test"],
[OFArray arrayWithObject: bar]);
}
- (void)testXMLStringWithIndentation
{
OTAssertEqualObjects([[OFXMLElement
elementWithXMLString: @"<x><y><z>a\nb</z><!-- foo --></y></x>"]
XMLStringWithIndentation: 2],
@"<x>\n <y>\n <z>a\nb</z>\n <!-- foo -->\n </y>\n</x>");
}
@end
|