forked from chinedufn/swift-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a new SPM-based test harness
Using the Swift Package Manager instead of xcode allows for developing on Linux and Windows, and could potentially make life easier. Ref. chinedufn#306
- Loading branch information
Showing
28 changed files
with
619 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
.DS_Store | ||
/.build | ||
/Packages | ||
xcuserdata/ | ||
DerivedData/ | ||
.swiftpm/configuration/registries.json | ||
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata | ||
.netrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"version": 1, | ||
"lineLength": 100, | ||
"indentation": { | ||
"spaces": 4 | ||
}, | ||
"maximumBlankLines": 1, | ||
"respectsExistingLineBreaks": true, | ||
"lineBreakBeforeControlFlowKeywords": false, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// swift-tools-version: 6.0 | ||
|
||
import PackageDescription | ||
|
||
let linkerSettings: [LinkerSetting] = [ | ||
.linkedLibrary("swift_integration_tests"), | ||
.unsafeFlags(["-L../target/debug/"]) | ||
] | ||
|
||
let package = Package( | ||
name: "IntegrationTests", | ||
targets: [ | ||
// The compiled static rust library. | ||
.systemLibrary( | ||
name: "RustLib"), | ||
// The generated Swift wrapper code for the Rust library, plus some | ||
// Swift code used by the Rust library. | ||
.target( | ||
name: "SharedLib", | ||
dependencies: ["RustLib"], | ||
linkerSettings: linkerSettings), | ||
.testTarget( | ||
name: "IntegrationTests", | ||
dependencies: ["SharedLib", "RustLib"]), | ||
] | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* | ||
!.gitignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#include "../Generated/SwiftBridgeCore.h" | ||
#include "../Generated/swift-integration-tests/swift-integration-tests.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module RustLib [system] { | ||
header "bridge.h" | ||
link "swift_integration_tests" | ||
export * | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// RustTests.swift | ||
// SwiftRustIntegrationTestRunner | ||
// | ||
// Created by Frankie Nwafili on 11/14/21. | ||
// | ||
|
||
import Foundation | ||
|
||
public class ASwiftStack { | ||
private var stack: [UInt8] = [] | ||
|
||
func push (val: UInt8) { | ||
stack.append(val) | ||
} | ||
|
||
func pop () { | ||
let _ = stack.popLast(); | ||
} | ||
|
||
func as_ptr() -> UnsafePointer<UInt8> { | ||
UnsafePointer(self.stack) | ||
} | ||
|
||
func len () -> UInt { | ||
UInt(stack.count) | ||
} | ||
|
||
func as_slice () -> UnsafeBufferPointer<UInt8> { | ||
UnsafeBufferPointer(start: self.as_ptr(), count: Int(self.len())) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// Callbacks.swift | ||
// SwiftRustIntegrationTestRunner | ||
// | ||
// Created by Frankie Nwafili on 9/11/22. | ||
// | ||
|
||
import Foundation | ||
|
||
func swift_takes_fnonce_callback_no_args_no_return(arg: () -> ()) { | ||
arg() | ||
} | ||
|
||
func swift_takes_fnonce_callback_primitive( | ||
arg: (UInt8) -> UInt8 | ||
) -> UInt8 { | ||
arg(4) | ||
} | ||
|
||
func swift_takes_fnonce_callback_opaque_rust( | ||
arg: (CallbackTestOpaqueRustType) -> CallbackTestOpaqueRustType | ||
) { | ||
let doubled = arg(CallbackTestOpaqueRustType(10)) | ||
if doubled.val() != 20 { | ||
fatalError("Callback not called") | ||
} | ||
} | ||
|
||
func swift_takes_two_fnonce_callbacks( | ||
arg1: () -> (), | ||
arg2: (UInt8) -> UInt16 | ||
) -> UInt16 { | ||
arg1() | ||
return arg2(3) | ||
} | ||
|
||
func swift_takes_fnonce_callback_with_two_params( | ||
arg: (UInt8, UInt16) -> UInt16 | ||
) -> UInt16 { | ||
arg(1, 2) | ||
} | ||
|
||
/// When given an FnOnce callback this should panic. | ||
func swift_calls_rust_fnonce_callback_twice(arg: () -> ()) { | ||
arg() | ||
arg() | ||
} | ||
|
||
class SwiftMethodCallbackTester { | ||
func method_with_fnonce_callback(callback: () -> ()) { | ||
callback() | ||
} | ||
|
||
func method_with_fnonce_callback_primitive(callback: (UInt16) -> UInt16) -> UInt16 { | ||
callback(5) | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
integration-tests/Sources/SharedLib/FunctionAttributes.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// FunctionAttributes.swift | ||
// SwiftRustIntegrationTestRunner | ||
// | ||
// Created by Erik Živković on 2022-12-17. | ||
// | ||
|
||
import Foundation | ||
|
||
func testCallSwiftFromRustByNameAttribute() -> RustString { | ||
return "StringFromSwift".intoRustString() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
* | ||
!.gitignore |
Oops, something went wrong.