Skip to content

Commit

Permalink
Add tooling to verify that all tests run on Linux (#9)
Browse files Browse the repository at this point in the history
Add tooling to verify that all tests run on Linux

Since the Swift Package Manager requires all tests to be manually
declared on Linux, add a test that runs on macOS that identifies
all tests using the Objective-C-based APIs, and then asserts that
all of those tests have been declared on Linux.
  • Loading branch information
JohnSundell committed Nov 28, 2019
1 parent cfe1528 commit e2f76b1
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Tests/InkTests/CodeTests.swift
Expand Up @@ -64,7 +64,7 @@ final class CodeTests: XCTestCase {
}

extension CodeTests {
static var allTests: [(String, TestClosure<CodeTests>)] {
static var allTests: Linux.TestList<CodeTests> {
return [
("testInlineCode", testInlineCode),
("testCodeBlockWithJustBackticks", testCodeBlockWithJustBackticks),
Expand Down
2 changes: 1 addition & 1 deletion Tests/InkTests/HTMLTests.swift
Expand Up @@ -96,7 +96,7 @@ final class HTMLTests: XCTestCase {
}

extension HTMLTests {
static var allTests: [(String, TestClosure<HTMLTests>)] {
static var allTests: Linux.TestList<HTMLTests> {
return [
("testTopLevelHTML", testTopLevelHTML),
("testNestedTopLevelHTML", testNestedTopLevelHTML),
Expand Down
2 changes: 1 addition & 1 deletion Tests/InkTests/HeadingTests.swift
Expand Up @@ -49,7 +49,7 @@ final class HeadingTests: XCTestCase {
}

extension HeadingTests {
static var allTests: [(String, TestClosure<HeadingTests>)] {
static var allTests: Linux.TestList<HeadingTests> {
return [
("testHeading", testHeading),
("testHeadingsSeparatedBySingleNewline", testHeadingsSeparatedBySingleNewline),
Expand Down
2 changes: 1 addition & 1 deletion Tests/InkTests/HorizontalLineTests.swift
Expand Up @@ -39,7 +39,7 @@ final class HorizontalLineTests: XCTestCase {
}

extension HorizontalLineTests {
static var allTests: [(String, TestClosure<HorizontalLineTests>)] {
static var allTests: Linux.TestList<HorizontalLineTests> {
return [
("testHorizonalLineWithDashes", testHorizonalLineWithDashes),
("testHorizontalLineWithDashesAtTheStartOfString", testHorizontalLineWithDashesAtTheStartOfString),
Expand Down
2 changes: 1 addition & 1 deletion Tests/InkTests/ImageTests.swift
Expand Up @@ -43,7 +43,7 @@ final class ImageTests: XCTestCase {
}

extension ImageTests {
static var allTests: [(String, TestClosure<ImageTests>)] {
static var allTests: Linux.TestList<ImageTests> {
return [
("testImageWithURL", testImageWithURL),
("testImageWithReference", testImageWithReference),
Expand Down
2 changes: 1 addition & 1 deletion Tests/InkTests/LinkTests.swift
Expand Up @@ -55,7 +55,7 @@ final class LinkTests: XCTestCase {
}

extension LinkTests {
static var allTests: [(String, TestClosure<LinkTests>)] {
static var allTests: Linux.TestList<LinkTests> {
return [
("testLinkWithURL", testLinkWithURL),
("testLinkWithReference", testLinkWithReference),
Expand Down
55 changes: 55 additions & 0 deletions Tests/InkTests/LinuxCompatibility.swift
@@ -0,0 +1,55 @@
/**
* Ink
* Copyright (c) John Sundell 2019
* MIT license, see LICENSE file for details
*/

import XCTest

public enum Linux {}

public extension Linux {
typealias TestCase = (testCaseClass: XCTestCase.Type, allTests: TestManifest)
typealias TestManifest = [(String, TestRunner)]
typealias TestRunner = (XCTestCase) throws -> Void
typealias TestList<T: XCTestCase> = [(String, Test<T>)]
typealias Test<T: XCTestCase> = (T) -> () throws -> Void
}

internal extension Linux {
static func makeTestCase<T: XCTestCase>(using list: TestList<T>) -> TestCase {
let manifest: TestManifest = list.map { name, function in
(name, { type in
try function(type as! T)()
})
}

return (T.self, manifest)
}
}

#if canImport(ObjectiveC)
internal final class LinuxVerificationTests: XCTestCase {
func testAllTestsRunOnLinux() {
for testCase in allTests() {
let type = testCase.testCaseClass

let testNames: [String] = type.defaultTestSuite.tests.map { test in
let components = test.name.components(separatedBy: .whitespaces)
return components[1].replacingOccurrences(of: "]", with: "")
}

let linuxTestNames = Set(testCase.allTests.map { $0.0 })

for name in testNames {
if !linuxTestNames.contains(name) {
XCTFail("""
\(type).\(name) does not run on Linux.
Please add it to \(type).allTests.
""")
}
}
}
}
}
#endif
2 changes: 1 addition & 1 deletion Tests/InkTests/ListTests.swift
Expand Up @@ -97,7 +97,7 @@ final class ListTests: XCTestCase {
}

extension ListTests {
static var allTests: [(String, TestClosure<ListTests>)] {
static var allTests: Linux.TestList<ListTests> {
return [
("testOrderedList", testOrderedList),
("testOrderedListWithoutIncrementedNumbers", testOrderedListWithoutIncrementedNumbers),
Expand Down
2 changes: 1 addition & 1 deletion Tests/InkTests/MetadataTests.swift
Expand Up @@ -69,7 +69,7 @@ final class MetadataTests: XCTestCase {
}

extension MetadataTests {
static var allTests: [(String, TestClosure<MetadataTests>)] {
static var allTests: Linux.TestList<MetadataTests> {
return [
("testParsingMetadata", testParsingMetadata),
("testDiscardingEmptyMetadataValues", testDiscardingEmptyMetadataValues),
Expand Down
2 changes: 1 addition & 1 deletion Tests/InkTests/ModifierTests.swift
Expand Up @@ -79,7 +79,7 @@ final class ModifierTests: XCTestCase {
}

extension ModifierTests {
static var allTests: [(String, TestClosure<ModifierTests>)] {
static var allTests: Linux.TestList<ModifierTests> {
return [
("testModifierInput", testModifierInput),
("testInitializingParserWithModifiers", testInitializingParserWithModifiers),
Expand Down
9 changes: 0 additions & 9 deletions Tests/InkTests/TestClosure.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Tests/InkTests/TextFormattingTests.swift
Expand Up @@ -139,7 +139,7 @@ final class TextFormattingTests: XCTestCase {
}

extension TextFormattingTests {
static var allTests: [(String, TestClosure<TextFormattingTests>)] {
static var allTests: Linux.TestList<TextFormattingTests> {
return [
("testParagraph", testParagraph),
("testItalicText", testItalicText),
Expand Down
24 changes: 11 additions & 13 deletions Tests/InkTests/XCTestManifests.swift
Expand Up @@ -6,19 +6,17 @@

import XCTest

#if !canImport(ObjectiveC)
public func allTests() -> [XCTestCaseEntry] {
public func allTests() -> [Linux.TestCase] {
return [
testCase(CodeTests.allTests),
testCase(HeadingTests.allTests),
testCase(HorizontalLineTests.allTests),
testCase(HTMLTests.allTests),
testCase(ImageTests.allTests),
testCase(LinkTests.allTests),
testCase(ListTests.allTests),
testCase(MetadataTests.allTests),
testCase(ModifierTests.allTests),
testCase(TextFormattingTests.allTests)
Linux.makeTestCase(using: CodeTests.allTests),
Linux.makeTestCase(using: HeadingTests.allTests),
Linux.makeTestCase(using: HorizontalLineTests.allTests),
Linux.makeTestCase(using: HTMLTests.allTests),
Linux.makeTestCase(using: ImageTests.allTests),
Linux.makeTestCase(using: LinkTests.allTests),
Linux.makeTestCase(using: ListTests.allTests),
Linux.makeTestCase(using: MetadataTests.allTests),
Linux.makeTestCase(using: ModifierTests.allTests),
Linux.makeTestCase(using: TextFormattingTests.allTests)
]
}
#endif

0 comments on commit e2f76b1

Please sign in to comment.