Skip to content

Commit

Permalink
Merge pull request #13 from daltonclaybrook/feature/extension-tests
Browse files Browse the repository at this point in the history
Add tests for extensions
  • Loading branch information
daltonclaybrook authored Aug 25, 2020
2 parents 7b98641 + 276df2c commit 927165c
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Tests/SettlerTests/ArrayAdditionsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import XCTest
@testable import SettlerFramework

final class ArrayAdditionsTests: XCTestCase {
func testMutableForEach() {
var nums = [1, 2, 3]
nums.mutableForEach { $0 *= 2 }
XCTAssertEqual(nums, [2, 4, 6])
}

func testMapEachCombination() {
let nums = [1, 2, 3, 4]
let result = nums.mapEachCombination { $0 + $1 }
XCTAssertEqual(result, [3, 4, 5, 5, 6, 7])
}

func testFlatMapEachCombination() {
let nums = [1, 2, 3, 4]
let result = nums.flatMapEachCombination { [$0 + $1, -1] }
XCTAssertEqual(result, [3, -1, 4, -1, 5, -1, 5, -1, 6, -1, 7, -1])
}

func testReduceEachCombinationInto() {
let nums = [1, 2, 3]
let result = nums.reduceEachCombination(into: ["0"]) { result, first, second in
result.append("\(first)\(second)")
}
XCTAssertEqual(result, ["0", "12", "13", "23"])
}

func testCompactMapRemoving() {
var nums = [0, 1, 2, 3, 4, 5, 6]
let result = nums.compactMapRemoving { $0 % 2 == 0 ? $0 : nil }
XCTAssertEqual(nums, [1, 3, 5])
XCTAssertEqual(result, [0, 2, 4, 6])
}
}
50 changes: 50 additions & 0 deletions Tests/SettlerTests/StringAdditionsTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import XCTest
@testable import SettlerFramework

final class StringAdditionsTests: XCTestCase {
func testPrefixIsStripped() {
let key = "Key.Foo"
let stripped = key.strippingPrefix("Key.")
XCTAssertEqual(stripped, "Foo")
}

func testSuffixIsStripped() {
let key = "Key.Foo"
let stripped = key.strippingSuffix(".Foo")
XCTAssertEqual(stripped, "Key")
}

func testPrefixAndSuffixAreStripped() {
let key = "Lazy<Key.Foo>"
let stripped = key.stripping(prefix: "Lazy<", andSuffix: ">")
XCTAssertEqual(stripped, "Key.Foo")
}

func testSuffixIsNotStrippedWithoutPrefix() {
let key = "Key.Foo>"
let stripped = key.stripping(prefix: "Lazy<", andSuffix: ">")
XCTAssertEqual(stripped, "Key.Foo>")
}

func testPrefixIsNotStrippedWithoutSuffix() {
let key = "Lazy<Key.Foo"
let stripped = key.stripping(prefix: "Lazy<", andSuffix: ">")
XCTAssertEqual(stripped, "Lazy<Key.Foo")
}

func testDotPathIsExpandedCorrectly() {
let relativePath = "./Sources"
let absolutePath = relativePath.bridge().absolutePathRepresentation()
let currentDirectory = FileManager.default.currentDirectoryPath
let expected = "\(currentDirectory)/Sources"
XCTAssertEqual(absolutePath, expected)
}

func testTildeIsExpandedCorrectly() {
let tildePath = "~/Sources"
let absolutePath = tildePath.bridge().absolutePathRepresentation()
let homeDirectory = NSHomeDirectory()
let expected = "\(homeDirectory)/Sources"
XCTAssertEqual(absolutePath, expected)
}
}
31 changes: 31 additions & 0 deletions Tests/SettlerTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
#if !canImport(ObjectiveC)
import XCTest

extension ArrayAdditionsTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__ArrayAdditionsTests = [
("testCompactMapRemoving", testCompactMapRemoving),
("testFlatMapEachCombination", testFlatMapEachCombination),
("testMapEachCombination", testMapEachCombination),
("testMutableForEach", testMutableForEach),
("testReduceEachCombinationInto", testReduceEachCombinationInto),
]
}

extension OrderedDefinitionBuilderTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
Expand Down Expand Up @@ -49,6 +62,21 @@ extension ResolverDefinitionBuilderTests {
]
}

extension StringAdditionsTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
// to regenerate.
static let __allTests__StringAdditionsTests = [
("testDotPathIsExpandedCorrectly", testDotPathIsExpandedCorrectly),
("testPrefixAndSuffixAreStripped", testPrefixAndSuffixAreStripped),
("testPrefixIsNotStrippedWithoutSuffix", testPrefixIsNotStrippedWithoutSuffix),
("testPrefixIsStripped", testPrefixIsStripped),
("testSuffixIsNotStrippedWithoutPrefix", testSuffixIsNotStrippedWithoutPrefix),
("testSuffixIsStripped", testSuffixIsStripped),
("testTildeIsExpandedCorrectly", testTildeIsExpandedCorrectly),
]
}

extension TypeAliasDefinitionBuilderTests {
// DO NOT MODIFY: This is autogenerated, use:
// `swift test --generate-linuxmain`
Expand All @@ -68,15 +96,18 @@ extension XcodeErrorDescriptionTests {
// to regenerate.
static let __allTests__XcodeErrorDescriptionTests = [
("testErrorStringIsCorrectWhenPathIsNil", testErrorStringIsCorrectWhenPathIsNil),
("testJoinedErrorStringsAreCorrect", testJoinedErrorStringsAreCorrect),
("testXcodeErrorStringIsCorrect", testXcodeErrorStringIsCorrect),
]
}

public func __allTests() -> [XCTestCaseEntry] {
return [
testCase(ArrayAdditionsTests.__allTests__ArrayAdditionsTests),
testCase(OrderedDefinitionBuilderTests.__allTests__OrderedDefinitionBuilderTests),
testCase(OutputFileContentsBuilderTests.__allTests__OutputFileContentsBuilderTests),
testCase(ResolverDefinitionBuilderTests.__allTests__ResolverDefinitionBuilderTests),
testCase(StringAdditionsTests.__allTests__StringAdditionsTests),
testCase(TypeAliasDefinitionBuilderTests.__allTests__TypeAliasDefinitionBuilderTests),
testCase(XcodeErrorDescriptionTests.__allTests__XcodeErrorDescriptionTests),
]
Expand Down
17 changes: 17 additions & 0 deletions Tests/SettlerTests/XcodeErrorDescriptionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ final class XcodeErrorDescriptionTests: XCTestCase {
let expected = "<nopath>:2:5: error: \(locatedError.value.description)"
XCTAssertEqual(locatedError.description, expected)
}

func testJoinedErrorStringsAreCorrect() {
let contents = "extension TestResolver {}"
let file1 = MockFile(path: "/path/one.swift", contents: contents)
let error1 = DefinitionError.cantFindDeclarationFile
.located(in: file1, offset: 0)
let file2 = MockFile(path: "/path/two.swift", contents: contents)
let error2 = DefinitionError
.resolverFunctionCannotBeThrowingIfResultIsUsedLazily
.located(in: file2, offset: 0)
let result = [error1, error2].errorString
let expected = """
/path/one.swift:1:1: error: \(error1.value.description)
/path/two.swift:1:1: error: \(error2.value.description)
"""
XCTAssertEqual(result, expected)
}
}

struct MockFile: FileType {
Expand Down

0 comments on commit 927165c

Please sign in to comment.