|
| 1 | +/** |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (C) 2016 Yahoo Japan Corporation. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +import XCTest |
| 26 | +import SwiftyXMLParser |
| 27 | + |
| 28 | +class ConverterTests: XCTestCase { |
| 29 | + func testMakeDosument() { |
| 30 | + // no chiled element, text only |
| 31 | + do { |
| 32 | + let element = XML.Element(name: "name", |
| 33 | + text: "text", |
| 34 | + attributes: ["key": "value"]) |
| 35 | + let converter = XML.Converter(XML.Accessor(element)) |
| 36 | + |
| 37 | + guard let result = try? converter.makeDocument() else { |
| 38 | + XCTFail("fail to make document") |
| 39 | + return |
| 40 | + } |
| 41 | + let extpected = """ |
| 42 | + <?xml version="1.0" encoding="UTF-8"?><name key="value">text</name> |
| 43 | + """ |
| 44 | + XCTAssertEqual(result, extpected) |
| 45 | + } |
| 46 | + |
| 47 | + // no text, chiled elements only |
| 48 | + do { |
| 49 | + let childElements = [ |
| 50 | + XML.Element(name: "c_name1", text: "c_text1", attributes: ["c_key1": "c_value1"]), |
| 51 | + XML.Element(name: "c_name2", text: "c_text2", attributes: ["c_key2": "c_value2"]) |
| 52 | + ] |
| 53 | + let element = XML.Element(name: "name", |
| 54 | + text: nil, |
| 55 | + attributes: ["key": "value"], |
| 56 | + childElements: childElements) |
| 57 | + |
| 58 | + let converter = XML.Converter(XML.Accessor(element)) |
| 59 | + guard let result = try? converter.makeDocument() else { |
| 60 | + XCTFail("fail to make document") |
| 61 | + return |
| 62 | + } |
| 63 | + let extpected = """ |
| 64 | + <?xml version="1.0" encoding="UTF-8"?><name key="value"><c_name1 c_key1="c_value1">c_text1</c_name1><c_name2 c_key2="c_value2">c_text2</c_name2></name> |
| 65 | + """ |
| 66 | + XCTAssertEqual(result, extpected) |
| 67 | + } |
| 68 | + |
| 69 | + // both text and chiled element |
| 70 | + do { |
| 71 | + let childElements = [ |
| 72 | + XML.Element(name: "c_name1", text: "c_text1", attributes: ["c_key1": "c_value1"]), |
| 73 | + XML.Element(name: "c_name2", text: "c_text2", attributes: ["c_key2": "c_value2"]) |
| 74 | + ] |
| 75 | + let element = XML.Element(name: "name", |
| 76 | + text: "text", |
| 77 | + attributes: ["key": "value"], |
| 78 | + childElements: childElements) |
| 79 | + let converter = XML.Converter(XML.Accessor(element)) |
| 80 | + guard let result = try? converter.makeDocument() else { |
| 81 | + XCTFail("fail to make document") |
| 82 | + return |
| 83 | + } |
| 84 | + let extpected = """ |
| 85 | + <?xml version="1.0" encoding="UTF-8"?><name key="value">text<c_name1 c_key1="c_value1">c_text1</c_name1><c_name2 c_key2="c_value2">c_text2</c_name2></name> |
| 86 | + """ |
| 87 | + XCTAssertEqual(result, extpected) |
| 88 | + } |
| 89 | + |
| 90 | + // nested child elements |
| 91 | + do { |
| 92 | + let grateGrandchildElements = [ |
| 93 | + XML.Element(name: "ggc_name1", text: "ggc_text1", attributes: ["ggc_key1": "ggc_value1"]) |
| 94 | + ] |
| 95 | + |
| 96 | + let grandchildElements = [ |
| 97 | + XML.Element(name: "gc_name1", text: "gc_text1", attributes: ["gc_key1": "gc_value1"], childElements: grateGrandchildElements) |
| 98 | + ] |
| 99 | + |
| 100 | + let childElements = [ |
| 101 | + XML.Element(name: "c_name1", text: "c_text1", attributes: ["c_key1": "c_value1"]), |
| 102 | + XML.Element(name: "c_name2", text: "c_text2", attributes: ["c_key2": "c_value2"], childElements: grandchildElements) |
| 103 | + ] |
| 104 | + let element = XML.Element(name: "name", |
| 105 | + text: "text", |
| 106 | + attributes: ["key": "value"], |
| 107 | + childElements: childElements) |
| 108 | + let converter = XML.Converter(XML.Accessor(element)) |
| 109 | + guard let result = try? converter.makeDocument() else { |
| 110 | + XCTFail("fail to make document") |
| 111 | + return |
| 112 | + } |
| 113 | + let extpected = """ |
| 114 | + <?xml version="1.0" encoding="UTF-8"?><name key="value">text<c_name1 c_key1="c_value1">c_text1</c_name1><c_name2 c_key2="c_value2">c_text2<gc_name1 gc_key1="gc_value1">gc_text1<ggc_name1 ggc_key1="ggc_value1">ggc_text1</ggc_name1></gc_name1></c_name2></name> |
| 115 | + """ |
| 116 | + XCTAssertEqual(result, extpected) |
| 117 | + } |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +extension XML.Element { |
| 122 | + convenience init(name: String, |
| 123 | + text: String? = nil, |
| 124 | + attributes: [String: String] = [String: String](), |
| 125 | + childElements: [XML.Element] = [XML.Element]()) { |
| 126 | + self.init(name: name) |
| 127 | + self.text = text |
| 128 | + self.attributes = attributes |
| 129 | + self.childElements = childElements |
| 130 | + } |
| 131 | +} |
0 commit comments