Skip to content

Commit

Permalink
XMLElement.text will never be nil
Browse files Browse the repository at this point in the history
Per issue #132, XMLElement can't be nil because of how the reduce
implementation works. Instead, it will be an empty string. I think this
makes more sense than allowing for nils anyway.

A lot of the suggestions mentioned on issue #84 are related to reducing
the number of nil checks as well, so this should help alleviate some of
that.
  • Loading branch information
drmohundro committed May 14, 2017
1 parent 40539f8 commit b34e776
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ end
desc 'Clean, build and test SWXMLHash'
task :test do |_t|
# xctool_build_cmd = './scripts/build.sh'
xcode_build_cmd = 'xcodebuild -workspace SWXMLHash.xcworkspace -scheme "SWXMLHash iOS" -destination "OS=10.2,name=iPhone 6S" clean build test -sdk iphonesimulator | xcpretty'
xcode_build_cmd = 'xcodebuild -workspace SWXMLHash.xcworkspace -scheme "SWXMLHash iOS" -destination "OS=10.3,name=iPhone 6S" clean build test -sdk iphonesimulator | xcpretty'

#if system('which xctool')
#run xctool_build_cmd
Expand Down
2 changes: 1 addition & 1 deletion Scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
set -ev

#xctool -scheme "SWXMLHash iOS" clean build test -sdk iphonesimulator
set -o pipefail && xcodebuild -workspace SWXMLHash.xcworkspace -scheme "SWXMLHash iOS" -destination "OS=10.0,name=iPhone 6S" clean build test -sdk iphonesimulator | xcpretty
set -o pipefail && xcodebuild -workspace SWXMLHash.xcworkspace -scheme "SWXMLHash iOS" -destination "OS=10.3,name=iPhone 6S" clean build test -sdk iphonesimulator | xcpretty
10 changes: 4 additions & 6 deletions Source/SWXMLHash+TypeConversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ extension XMLElement {
- returns: The element text
*/
internal func nonEmptyTextOrThrow() throws -> String {
if let textVal = text, !textVal.characters.isEmpty {
let textVal = text
if !textVal.characters.isEmpty {
return textVal
}

Expand Down Expand Up @@ -464,11 +465,8 @@ extension String: XMLElementDeserializable, XMLAttributeDeserializable {
- throws: an XMLDeserializationError.TypeConversionFailed if the element cannot be deserialized
- returns: the deserialized String value
*/
public static func deserialize(_ element: XMLElement) throws -> String {
guard let text = element.text else {
throw XMLDeserializationError.TypeConversionFailed(type: "String", element: element)
}
return text
public static func deserialize(_ element: XMLElement) -> String {
return element.text
}

/**
Expand Down
8 changes: 2 additions & 6 deletions Source/SWXMLHash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ public class XMLElement: XMLContent {
}

/// The inner text of the element, if it exists
public var text: String? {
public var text: String {
return children
.map({ $0 as? TextElement })
.flatMap({ $0 })
Expand Down Expand Up @@ -850,11 +850,7 @@ extension XMLElement: CustomStringConvertible {
return xmlReturn.joined(separator: "")
}

if text != nil {
return "<\(name)\(attributesString)>\(text!)</\(name)>"
} else {
return "<\(name)\(attributesString)/>"
}
return "<\(name)\(attributesString)>\(text)</\(name)>"
}
}

Expand Down
4 changes: 2 additions & 2 deletions Tests/SWXMLHashTests/LazyXMLParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class LazyXMLParsingTests: XCTestCase {
}

func testShouldBeAbleToIterateElementGroups() {
let result = xml!["root"]["catalog"]["book"].all.map({ $0["genre"].element!.text! }).joined(separator: ", ")
let result = xml!["root"]["catalog"]["book"].all.map({ $0["genre"].element!.text }).joined(separator: ", ")
XCTAssertEqual(result, "Computer, Fantasy, Fantasy")
}

Expand Down Expand Up @@ -96,7 +96,7 @@ class LazyXMLParsingTests: XCTestCase {
let interleavedXml = "<html><body><p>one</p><div>two</div><p>three</p><div>four</div></body></html>"
let parsed = SWXMLHash.parse(interleavedXml)

let result = parsed["html"]["body"].children.map({ $0.element!.text! }).joined(separator: ", ")
let result = parsed["html"]["body"].children.map({ $0.element!.text }).joined(separator: ", ")
XCTAssertEqual(result, "one, two, three, four")
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/SWXMLHashTests/XMLParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class XMLParsingTests: XCTestCase {
}

func testShouldBeAbleToIterateElementGroups() {
let result = xml!["root"]["catalog"]["book"].all.map({ $0["genre"].element!.text! }).joined(separator: ", ")
let result = xml!["root"]["catalog"]["book"].all.map({ $0["genre"].element!.text }).joined(separator: ", ")
XCTAssertEqual(result, "Computer, Fantasy, Fantasy")
}

Expand Down Expand Up @@ -102,7 +102,7 @@ class XMLParsingTests: XCTestCase {
let result = element.children.reduce("") { acc, child in
switch child {
case let elm as SWXMLHash.XMLElement:
guard let text = elm.text else { return acc }
let text = elm.text
return acc + text
case let elm as TextElement:
return acc + elm.text
Expand All @@ -120,7 +120,7 @@ class XMLParsingTests: XCTestCase {
let interleavedXml = "<html><body><p>one</p><div>two</div><p>three</p><div>four</div></body></html>"
let parsed = SWXMLHash.parse(interleavedXml)

let result = parsed["html"]["body"].children.map({ $0.element!.text! }).joined(separator: ", ")
let result = parsed["html"]["body"].children.map({ $0.element!.text }).joined(separator: ", ")
XCTAssertEqual(result, "one, two, three, four")
}

Expand Down

0 comments on commit b34e776

Please sign in to comment.