Skip to content

Commit 9507ec2

Browse files
committed
WIP: Add a test fixture for testing Swift and Clang executable targets.
1 parent 52a4766 commit 9507ec2

File tree

8 files changed

+116
-0
lines changed

8 files changed

+116
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// swift-tools-version:5.3
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "TestableExe",
6+
targets: [
7+
.target(
8+
name: "TestableExe1"
9+
),
10+
.target(
11+
name: "TestableExe2"
12+
),
13+
.target(
14+
name: "TestableExe3"
15+
),
16+
.testTarget(
17+
name: "TestableExeTests",
18+
dependencies: [
19+
"TestableExe1",
20+
"TestableExe2",
21+
"TestableExe3",
22+
]
23+
),
24+
]
25+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public func GetGreeting1() -> String {
2+
return "Hello, world"
3+
}
4+
5+
print("\(GetGreeting1())!")
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public func GetGreeting2() -> String {
2+
return "Hello, planet"
3+
}
4+
5+
print("\(GetGreeting2())!")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const char * GetGreeting3();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <stdio.h>
2+
#include "include/TestableExe3.h"
3+
4+
const char * GetGreeting3() {
5+
return "Hello, universe";
6+
}
7+
8+
int main() {
9+
printf("%s!\n", GetGreeting3());
10+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import XCTest
2+
3+
import TestableExeTests
4+
5+
var tests = [XCTestCaseEntry]()
6+
tests += TestableExeTests.allTests()
7+
XCTMain(tests)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import XCTest
2+
import TestableExe1
3+
import TestableExe2
4+
import TestableExe3
5+
import class Foundation.Bundle
6+
7+
final class TestableExeTests: XCTestCase {
8+
func testExample() throws {
9+
// This is an example of a functional test case.
10+
// Use XCTAssert and related functions to verify your tests produce the correct
11+
// results.
12+
13+
XCTAssertEqual(GetGreeting1(), "Hello, world")
14+
XCTAssertEqual(GetGreeting2(), "Hello, planet")
15+
XCTAssertEqual(String(cString: GetGreeting3()), "Hello, universe")
16+
17+
// Some of the APIs that we use below are available in macOS 10.13 and above.
18+
guard #available(macOS 10.13, *) else {
19+
return
20+
}
21+
22+
let fooBinary = productsDirectory.appendingPathComponent("TestableExe1")
23+
24+
let process = Process()
25+
process.executableURL = fooBinary
26+
27+
let pipe = Pipe()
28+
process.standardOutput = pipe
29+
30+
try process.run()
31+
process.waitUntilExit()
32+
33+
let data = pipe.fileHandleForReading.readDataToEndOfFile()
34+
let output = String(data: data, encoding: .utf8)
35+
36+
XCTAssertEqual(output, "Hello, world!\n")
37+
}
38+
39+
/// Returns path to the built products directory.
40+
var productsDirectory: URL {
41+
#if os(macOS)
42+
for bundle in Bundle.allBundles where bundle.bundlePath.hasSuffix(".xctest") {
43+
return bundle.bundleURL.deletingLastPathComponent()
44+
}
45+
fatalError("couldn't find the products directory")
46+
#else
47+
return Bundle.main.bundleURL
48+
#endif
49+
}
50+
51+
static var allTests = [
52+
("testExample", testExample),
53+
]
54+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import XCTest
2+
3+
#if !canImport(ObjectiveC)
4+
public func allTests() -> [XCTestCaseEntry] {
5+
return [
6+
testCase(TestableExeTests.allTests),
7+
]
8+
}
9+
#endif

0 commit comments

Comments
 (0)