/* * 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 OFXMLNodeTests: OTTestCase @end @implementation OFXMLNodeTests - (void)testElementWithName { OTAssertEqualObjects( [[OFXMLElement elementWithName: @"foo"] XMLString], @""); } - (void)testElementWithNameStringValue { OTAssertEqualObjects( [[OFXMLElement elementWithName: @"foo" stringValue: @"b&ar"] XMLString], @"b&ar"); } - (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, @""); element = [OFXMLElement elementWithName: @"foo" namespace: @"urn:objfw:test"]; [element addAttributeWithName: @"test" stringValue: @"test"]; OTAssertEqualObjects(element.XMLString, @""); } - (void)testElementWithNameNamespaceStringValue { OFXMLElement *element = [OFXMLElement elementWithName: @"foo" namespace: @"urn:objfw:test" stringValue: @"x"]; [element setPrefix: @"objfw-test" forNamespace: @"urn:objfw:test"]; OTAssertEqualObjects(element.XMLString, @"x"); } - (void)testElementWithXMLStringAndStringValue { OTAssertEqualObjects([[OFXMLElement elementWithXMLString: @"\r\nfoo" @"bazqux"] stringValue], @"foobarbazqux"); } - (void)testCharactersWithString { OTAssertEqualObjects( [[OFXMLCharacters charactersWithString: @""] XMLString], @"<foo>"); } - (void)testCDATAWithString { OTAssertEqualObjects( [[OFXMLCDATA CDATAWithString: @""] XMLString], @"]]>"); } - (void)testCommentWithText { OTAssertEqualObjects( [[OFXMLComment commentWithText: @" comment "] XMLString], @""); } - (void)testIsEqual { OTAssertEqualObjects( [OFXMLElement elementWithXMLString: @""], [OFXMLElement elementWithXMLString: @""]); OTAssertEqualObjects( [OFXMLElement elementWithXMLString: @""], [OFXMLElement elementWithXMLString: @""]); OTAssertNotEqualObjects( [OFXMLElement elementWithXMLString: @""], [OFXMLElement elementWithXMLString: @""]); } - (void)testHash { OTAssertEqual( [[OFXMLElement elementWithXMLString: @""] hash], [[OFXMLElement elementWithXMLString: @""] hash]); OTAssertEqual( [[OFXMLElement elementWithXMLString: @""] hash], [[OFXMLElement elementWithXMLString: @""] hash]); OTAssertNotEqual( [[OFXMLElement elementWithXMLString: @""] hash], [[OFXMLElement elementWithXMLString: @""] 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, @"b&ar"); } - (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, @"b&ar"); [element removeAttributeForName: @"foo" namespace: @"urn:objfw:test"]; OTAssertEqualObjects(element.XMLString, @"b&ar"); } - (void)testAddChild { OFXMLElement *element; element = [OFXMLElement elementWithName: @"foo"]; [element addAttributeWithName: @"foo" stringValue: @"b&ar"]; [element addChild: [OFXMLElement elementWithName: @"bar"]]; OTAssertEqualObjects(element.XMLString, @""); 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, @""); } - (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: @"a\nb"] XMLStringWithIndentation: 2], @"\n \n a\nb\n \n \n"); } @end