Skip to content

Commit

Permalink
fix: swiftlint fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
drmohundro committed Aug 16, 2024
1 parent ab54cd5 commit 4a8eda9
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 123 deletions.
1 change: 0 additions & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ included:
- Tests
- ./Package.swift
opt_in_rules:
- anyobject_protocol
- array_init
- attributes
- closure_end_indentation
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ let package = Package(
)
],
dependencies: [
.package(url: "https://github.com/apple/swift-testing.git", from: "0.10.0"),
.package(url: "https://github.com/apple/swift-testing.git", from: "0.10.0")
],
targets: [
.target(
Expand Down
12 changes: 8 additions & 4 deletions Tests/SWXMLHashTests/LazyTypesConversionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import SWXMLHash
import Testing

@Suite("LazyTypesConversionTests") struct LazyTypesConversionTests {
@Suite("LazyTypesConversionTests")
struct LazyTypesConversionTests {
var parser: XMLIndexer?
let xmlWithBasicTypes = """
<root>
Expand All @@ -49,7 +50,8 @@ import Testing
parser = XMLHash.config { cfg in cfg.shouldProcessLazily = true }.parse(xmlWithBasicTypes)
}

@Test func shouldConvertValueToNonOptional() {
@Test
func shouldConvertValueToNonOptional() {
do {
let value: String = try parser!["root"]["string"].value()
#expect(value == "the string value")
Expand All @@ -58,7 +60,8 @@ import Testing
}
}

@Test func shouldConvertAttributeToNonOptional() {
@Test
func shouldConvertAttributeToNonOptional() {
do {
let value: Int = try parser!["root"]["attribute"].value(ofAttribute: "int")
#expect(value == 1)
Expand All @@ -67,7 +70,8 @@ import Testing
}
}

@Test func shouldBeAbleToGetUserInfoDuringDeserialization() {
@Test
func shouldBeAbleToGetUserInfoDuringDeserialization() {
let parser = XMLHash.config { config in
let options = SampleUserInfo(apiVersion: .v1)
config.userInfo = [ SampleUserInfo.key: options ]
Expand Down
19 changes: 12 additions & 7 deletions Tests/SWXMLHashTests/LazyWhiteSpaceParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,28 @@ struct LazyWhiteSpaceParsingTests {
init() {
// Put setup code here. This method is called before the invocation of each test method in the class.

#if SWIFT_PACKAGE
// #if SWIFT_PACKAGE
// let path = URL(fileURLWithPath: #file).deletingLastPathComponent().appendingPathComponent("test.xml").path
// #else
// let bundle = Bundle(for: WhiteSpaceParsingTests.self)
// let path = bundle.path(forResource: "test", ofType: "xml")!
// #endif

let path = URL(fileURLWithPath: #file).deletingLastPathComponent().appendingPathComponent("test.xml").path
#else
let bundle = Bundle(for: WhiteSpaceParsingTests.self)
let path = bundle.path(forResource: "test", ofType: "xml")!
#endif

// swiftlint:disable:next force_try
let data = try! Data(contentsOf: URL(fileURLWithPath: path))
xml = XMLHash.lazy(data)
}

// issue #6
@Test func shouldBeAbleToPullTextBetweenElementsWithoutWhitespace() {
@Test
func shouldBeAbleToPullTextBetweenElementsWithoutWhitespace() {
#expect(xml!["niotemplate"]["section"][0]["constraint"][1].element?.text == "H:|-15-[title]-15-|")
}

@Test func shouldBeAbleToCorrectlyParseCDATASectionsWithWhitespace() {
@Test
func shouldBeAbleToCorrectlyParseCDATASectionsWithWhitespace() {
#expect(xml!["niotemplate"]["other"].element?.text == "\n \n this\n has\n white\n space\n \n ")
}
}
Expand Down
45 changes: 30 additions & 15 deletions Tests/SWXMLHashTests/LazyXMLParsingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,29 @@ struct LazyXMLParsingTests {
xml = XMLHash.config { config in config.shouldProcessLazily = true }.parse(xmlToParse)
}

@Test func shouldBeAbleToParseIndividualElements() {
@Test
func shouldBeAbleToParseIndividualElements() {
#expect(xml!["root"]["header"]["title"].element?.text == "Test Title Header")
}

@Test func shouldBeAbleToHandleSubsequentParseCalls() {
@Test
func shouldBeAbleToHandleSubsequentParseCalls() {
#expect(xml!["root"]["header"]["title"].element?.text == "Test Title Header")
#expect(xml!["root"]["catalog"]["book"][1]["author"].element?.text == "Ralls, Kim")
}

@Test func shouldBeAbleToParseElementGroups() {
@Test
func shouldBeAbleToParseElementGroups() {
#expect(xml!["root"]["catalog"]["book"][1]["author"].element?.text == "Ralls, Kim")
}

@Test func shouldBeAbleToParseAttributes() {
@Test
func shouldBeAbleToParseAttributes() {
#expect(xml!["root"]["catalog"]["book"][1].element?.attribute(by: "id")?.text == "bk102")
}

@Test func shouldBeAbleToLookUpElementsByNameAndAttribute() {
@Test
func shouldBeAbleToLookUpElementsByNameAndAttribute() {
do {
let value = try xml!["root"]["catalog"]["book"].withAttribute("id", "bk102")["author"].element?.text
#expect(value == "Ralls, Kim")
Expand All @@ -92,20 +97,24 @@ struct LazyXMLParsingTests {
}
}

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

@Test func shouldBeAbleToIterateElementGroupsEvenIfOnlyOneElementIsFound() {
@Test
func shouldBeAbleToIterateElementGroupsEvenIfOnlyOneElementIsFound() {
#expect(xml!["root"]["header"]["title"].all.count == 1)
}

@Test func shouldBeAbleToIndexElementGroupsEvenIfOnlyOneElementIsFound() {
@Test
func shouldBeAbleToIndexElementGroupsEvenIfOnlyOneElementIsFound() {
#expect(xml!["root"]["header"]["title"][0].element?.text == "Test Title Header")
}

@Test func shouldBeAbleToIterateUsingForIn() {
@Test
func shouldBeAbleToIterateUsingForIn() {
var count = 0
for _ in xml!["root"]["catalog"]["book"].all {
count += 1
Expand All @@ -114,35 +123,41 @@ struct LazyXMLParsingTests {
#expect(count == 3)
}

@Test func shouldBeAbleToEnumerateChildren() {
@Test
func shouldBeAbleToEnumerateChildren() {
let result = xml!["root"]["catalog"]["book"][0].children.map({ $0.element!.name }).joined(separator: ", ")
#expect(result == "author, title, genre, price, publish_date, description")
}

@Test func shouldBeAbleToHandleMixedContent() {
@Test
func shouldBeAbleToHandleMixedContent() {
#expect(xml!["root"]["header"].element?.text == "header mixed contentmore mixed content")
}

@Test func shouldHandleInterleavingXMLElements() {
@Test
func shouldHandleInterleavingXMLElements() {
let interleavedXml = "<html><body><p>one</p><div>two</div><p>three</p><div>four</div></body></html>"
let parsed = XMLHash.parse(interleavedXml)

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

@Test func shouldBeAbleToProvideADescriptionForTheDocument() {
@Test
func shouldBeAbleToProvideADescriptionForTheDocument() {
let descriptionXml = "<root><foo><what id=\"myId\">puppies</what></foo></root>"
let parsed = XMLHash.parse(descriptionXml)

#expect(parsed.description == "<root><foo><what id=\"myId\">puppies</what></foo></root>")
}

@Test func shouldReturnNilWhenKeysDontMatch() {
@Test
func shouldReturnNilWhenKeysDontMatch() {
#expect(xml!["root"]["what"]["header"]["foo"].element?.name == nil)
}

@Test func shouldBeAbleToFilterOnIndexer() {
@Test
func shouldBeAbleToFilterOnIndexer() {
let subIndexer = xml!["root"]["catalog"]["book"]
.filterAll { elem, _ in elem.attribute(by: "id")!.text == "bk102" }
.filterChildren { _, index in index >= 1 && index <= 3 }
Expand Down
3 changes: 2 additions & 1 deletion Tests/SWXMLHashTests/MixedTextWithXMLElementsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ struct MixedTextWithXMLElementsTests {
xml = XMLHash.parse(xmlContent)
}

@Test func shouldBeAbleToGetAllContentsInsideOfAnElement() {
@Test
func shouldBeAbleToGetAllContentsInsideOfAnElement() {
#expect(xml!["everything"]["news"]["content"].description == "<content>Here is a cool thing <a href=\"google.com\">A</a> and second cool thing <a href=\"fb.com\">B</a></content>")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ struct TypeConversionArrayOfNonPrimitiveTypesTests {
parser = XMLHash.parse(xmlWithArraysOfTypes)
}

@Test func shouldConvertArrayOfGoodBasicItemsToNonOptional() {
@Test
func shouldConvertArrayOfGoodBasicItemsToNonOptional() {
do {
let value: [BasicItem] = try parser!["root"]["arrayOfGoodBasicItems"]["basicItem"].value()
#expect(value == correctBasicItems)
Expand All @@ -99,7 +100,8 @@ struct TypeConversionArrayOfNonPrimitiveTypesTests {
}
}

@Test func shouldConvertArrayOfGoodBasicItemsToOptional() {
@Test
func shouldConvertArrayOfGoodBasicItemsToOptional() {
do {
let value: [BasicItem]? = try parser!["root"]["arrayOfGoodBasicItems"]["basicItem"].value()
#expect(value != nil)
Expand All @@ -111,7 +113,8 @@ struct TypeConversionArrayOfNonPrimitiveTypesTests {
}
}

@Test func shouldConvertArrayOfGoodBasicItemsToArrayOfOptionals() {
@Test
func shouldConvertArrayOfGoodBasicItemsToArrayOfOptionals() {
do {
let value: [BasicItem?] = try parser!["root"]["arrayOfGoodBasicItems"]["basicItem"].value()
#expect(value.compactMap({ $0 }) == correctBasicItems)
Expand All @@ -120,25 +123,29 @@ struct TypeConversionArrayOfNonPrimitiveTypesTests {
}
}

@Test func shouldThrowWhenConvertingArrayOfBadBasicItemsToNonOptional() {
@Test
func shouldThrowWhenConvertingArrayOfBadBasicItemsToNonOptional() {
#expect(throws: XMLDeserializationError.self) {
try (parser!["root"]["arrayOfBadBasicItems"]["basicItem"].value() as [BasicItem])
}
}

@Test func shouldThrowWhenConvertingArrayOfBadBasicItemsToOptional() {
@Test
func shouldThrowWhenConvertingArrayOfBadBasicItemsToOptional() {
#expect(throws: XMLDeserializationError.self) {
try (parser!["root"]["arrayOfBadBasicItems"]["basicItem"].value() as [BasicItem]?)
}
}

@Test func shouldThrowWhenConvertingArrayOfBadBasicItemsToArrayOfOptionals() {
@Test
func shouldThrowWhenConvertingArrayOfBadBasicItemsToArrayOfOptionals() {
#expect(throws: XMLDeserializationError.self) {
try (parser!["root"]["arrayOfBadBasicItems"]["basicItem"].value() as [BasicItem?])
}
}

@Test func shouldConvertArrayOfEmptyMissingToOptional() {
@Test
func shouldConvertArrayOfEmptyMissingToOptional() {
do {
let value: [BasicItem]? = try parser!["root"]["arrayOfMissingBasicItems"]["basicItem"].value()
#expect(value == nil)
Expand All @@ -147,7 +154,8 @@ struct TypeConversionArrayOfNonPrimitiveTypesTests {
}
}

@Test func shouldConvertArrayOfGoodAttributeItemsToNonOptional() {
@Test
func shouldConvertArrayOfGoodAttributeItemsToNonOptional() {
do {
let value: [AttributeItem] = try parser!["root"]["arrayOfGoodAttributeItems"]["attributeItem"].value()
#expect(value == correctAttributeItems)
Expand All @@ -156,7 +164,8 @@ struct TypeConversionArrayOfNonPrimitiveTypesTests {
}
}

@Test func shouldConvertArrayOfGoodAttributeItemsToOptional() {
@Test
func shouldConvertArrayOfGoodAttributeItemsToOptional() {
do {
let value: [AttributeItem]? = try parser!["root"]["arrayOfGoodAttributeItems"]["attributeItem"].value()
#expect(value != nil)
Expand All @@ -168,7 +177,8 @@ struct TypeConversionArrayOfNonPrimitiveTypesTests {
}
}

@Test func shouldConvertArrayOfGoodAttributeItemsToArrayOfOptionals() {
@Test
func shouldConvertArrayOfGoodAttributeItemsToArrayOfOptionals() {
do {
let value: [AttributeItem?] = try parser!["root"]["arrayOfGoodAttributeItems"]["attributeItem"].value()
#expect(value.compactMap({ $0 }) == correctAttributeItems)
Expand All @@ -177,19 +187,22 @@ struct TypeConversionArrayOfNonPrimitiveTypesTests {
}
}

@Test func shouldThrowWhenConvertingArrayOfBadAttributeItemsToNonOptional() {
@Test
func shouldThrowWhenConvertingArrayOfBadAttributeItemsToNonOptional() {
#expect(throws: XMLDeserializationError.self) {
try (parser!["root"]["arrayOfBadAttributeItems"]["attributeItem"].value() as [AttributeItem])
}
}

@Test func shouldThrowWhenConvertingArrayOfBadAttributeItemsToOptional() {
@Test
func shouldThrowWhenConvertingArrayOfBadAttributeItemsToOptional() {
#expect(throws: XMLDeserializationError.self) {
try (parser!["root"]["arrayOfBadAttributeItems"]["attributeItem"].value() as [AttributeItem]?)
}
}

@Test func shouldThrowWhenConvertingArrayOfBadAttributeItemsToArrayOfOptionals() {
@Test
func shouldThrowWhenConvertingArrayOfBadAttributeItemsToArrayOfOptionals() {
#expect(throws: XMLDeserializationError.self) {
try (parser!["root"]["arrayOfBadAttributeItems"]["attributeItem"].value() as [AttributeItem?])
}
Expand Down
Loading

0 comments on commit 4a8eda9

Please sign in to comment.