From 9174be7e4c13955665f845a1a68ba963b1313d88 Mon Sep 17 00:00:00 2001 From: Brad Howes Date: Thu, 6 Jul 2023 23:25:31 +0200 Subject: [PATCH] Use Makefile for build --- .github/workflows/CI.yml | 15 +++- JoyStickView.xcodeproj/project.pbxproj | 8 ++ .../xcschemes/JoyStickView.xcscheme | 3 +- .../JoyStickView_AppUITests.swift | 12 +++ Sources/JoyStickView/Bundle+Additions.swift | 2 +- .../JoyStickView/CoreGraphics+Additions.swift | 84 ------------------- Sources/JoyStickView/JoyStickView.swift | 10 +-- .../Bundle+AdditionsTests.swift | 14 ++++ .../CoreGraphics+AdditionsTests.swift | 46 ++++++++++ 9 files changed, 97 insertions(+), 97 deletions(-) create mode 100644 Tests/JoyStickViewTests/Bundle+AdditionsTests.swift create mode 100644 Tests/JoyStickViewTests/CoreGraphics+AdditionsTests.swift diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 786aca7..d00a152 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -11,7 +11,14 @@ jobs: runs-on: macos-latest steps: - uses: actions/checkout@v2 - - name: Run monitor tests - run: xcodebuild -workspace JoyStickView.xcworkspace test -destination 'name=iPhone 11' -scheme 'JoyStickView' - - name: Run view tests - run: xcodebuild -workspace JoyStickView.xcworkspace test -destination 'name=iPhone 11' -scheme 'JoyStickView App' + - name: Build, test, and code coverage + run make + - name: Create Coverage Badge + uses: schneegans/dynamic-badges-action@v1.1.0 + with: + auth: ${{ secrets.GIST_UPDATE }} + gistID: 4739895cbf46573cc4b63ab776c74899 + filename: Joystick-coverage.json + label: Coverage + message: ${{ env.PERCENTAGE }} + color: success diff --git a/JoyStickView.xcodeproj/project.pbxproj b/JoyStickView.xcodeproj/project.pbxproj index 85ce78c..72fa1a8 100644 --- a/JoyStickView.xcodeproj/project.pbxproj +++ b/JoyStickView.xcodeproj/project.pbxproj @@ -8,6 +8,8 @@ /* Begin PBXBuildFile section */ BD0E7DB92A55F57E00181929 /* Polar.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD0E7DB82A55F57E00181929 /* Polar.swift */; }; + BD0E7DBC2A575B6500181929 /* CoreGraphics+AdditionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD0E7DBA2A575A9200181929 /* CoreGraphics+AdditionsTests.swift */; }; + BD0E7DBF2A57618100181929 /* Bundle+AdditionsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD0E7DBD2A57617300181929 /* Bundle+AdditionsTests.swift */; }; BD4CAF182210696000DECD93 /* JoyStickViewMonitorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BD4CAF172210696000DECD93 /* JoyStickViewMonitorTests.swift */; }; BDA7F8E8218B1DE400A9AB5B /* Bundle+Additions.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDA7F8E7218B1DE400A9AB5B /* Bundle+Additions.swift */; }; BDBB51BA220ADE1E00C579B2 /* JoyStickViewMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = BDBB51B9220ADE1E00C579B2 /* JoyStickViewMonitor.swift */; }; @@ -30,6 +32,8 @@ /* Begin PBXFileReference section */ BD0E7DB82A55F57E00181929 /* Polar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Polar.swift; sourceTree = ""; }; + BD0E7DBA2A575A9200181929 /* CoreGraphics+AdditionsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CoreGraphics+AdditionsTests.swift"; sourceTree = ""; }; + BD0E7DBD2A57617300181929 /* Bundle+AdditionsTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "Bundle+AdditionsTests.swift"; sourceTree = ""; }; BD4CAF172210696000DECD93 /* JoyStickViewMonitorTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = JoyStickViewMonitorTests.swift; sourceTree = ""; }; BD75800B215B66F000B787D2 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; BDA7F8E7218B1DE400A9AB5B /* Bundle+Additions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Bundle+Additions.swift"; sourceTree = ""; }; @@ -76,6 +80,8 @@ isa = PBXGroup; children = ( BD4CAF172210696000DECD93 /* JoyStickViewMonitorTests.swift */, + BD0E7DBA2A575A9200181929 /* CoreGraphics+AdditionsTests.swift */, + BD0E7DBD2A57617300181929 /* Bundle+AdditionsTests.swift */, BDBB51D3220B0D9900C579B2 /* Info.plist */, ); name = Tests; @@ -252,6 +258,8 @@ buildActionMask = 2147483647; files = ( BD4CAF182210696000DECD93 /* JoyStickViewMonitorTests.swift in Sources */, + BD0E7DBF2A57618100181929 /* Bundle+AdditionsTests.swift in Sources */, + BD0E7DBC2A575B6500181929 /* CoreGraphics+AdditionsTests.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/JoyStickView.xcodeproj/xcshareddata/xcschemes/JoyStickView.xcscheme b/JoyStickView.xcodeproj/xcshareddata/xcschemes/JoyStickView.xcscheme index f2109a8..68795a0 100644 --- a/JoyStickView.xcodeproj/xcshareddata/xcschemes/JoyStickView.xcscheme +++ b/JoyStickView.xcodeproj/xcshareddata/xcschemes/JoyStickView.xcscheme @@ -26,7 +26,8 @@ buildConfiguration = "Debug" selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB" selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB" - shouldUseLaunchSchemeArgsEnv = "YES"> + shouldUseLaunchSchemeArgsEnv = "YES" + codeCoverageEnabled = "YES"> Bundle { + public func podResource(name: String) -> Bundle { guard let bundleUrl = self.url(forResource: name, withExtension: "bundle") else { return self } return Bundle(url: bundleUrl) ?? self } diff --git a/Sources/JoyStickView/CoreGraphics+Additions.swift b/Sources/JoyStickView/CoreGraphics+Additions.swift index 1231ec4..7cf57a5 100644 --- a/Sources/JoyStickView/CoreGraphics+Additions.swift +++ b/Sources/JoyStickView/CoreGraphics+Additions.swift @@ -12,24 +12,6 @@ public extension CGRect { /// Convenience functions for CGVector. public extension CGVector { - /** - Multiply the components of a CGVector by a scalar - - parameter lhs: the CGVector to multiply - - parameter rhs: the scalar to multiply - - returns new CGVector - */ - @inlinable - static func * (lhs: CGVector, rhs: CGFloat) -> CGVector { .init(dx: lhs.dx * rhs, dy: lhs.dy * rhs) } - - /** - Multiply the components of a CGVector by a scalar - - parameter lhs: the CGVector to multiply - - parameter rhs: the scalar to multiply - - returns new CGVector - */ - @inlinable - static func * (lhs: CGVector, rhs: Double) -> CGVector { lhs * CGFloat(rhs) } - /// Obtain the squared magnitude of the CGVector @inlinable var magnitude2: CGFloat { dx * dx + dy * dy } @@ -69,15 +51,6 @@ public extension CGPoint { @inlinable static func - (lhs: CGPoint, rhs: CGVector) -> CGPoint { .init(x: lhs.x - rhs.dx, y: lhs.y - rhs.dy) } - /** - Subtract the components of a CGPoint and a CGSize - - parameter lhs: the CGPoint to subtract - - parameter rhs: the CGSize to subtract - - returns: new CGPoint representing the difference - */ - @inlinable - static func - (lhs: CGPoint, rhs: CGSize) -> CGPoint { .init(x: lhs.x - rhs.width, y: lhs.y - rhs.height) } - /** Subtract the components of two CGPoint values - parameter lhs: the CGPoint to subtract @@ -87,60 +60,3 @@ public extension CGPoint { @inlinable static func - (lhs: CGPoint, rhs: CGPoint) -> CGVector { .init(dx: lhs.x - rhs.x, dy: lhs.y - rhs.y) } } - -/// Convenience functions for CGSize. -public extension CGSize { - - /** - Add two CGSize values - - parameter lhs: the CGSize to add - - parameter rhs: the CGSize to add - - returns: new CGSize representing the sum - */ - @inlinable - static func + (lhs: CGSize, rhs: CGSize) -> CGSize { - .init(width: lhs.width + rhs.width, height: lhs.height + rhs.height) - } - - /** - Multiply the components of a CGSize value by a scalar - - parameter lhs: the CGSize to multiply - - parameter rhs: the scalar to multiply - - returns: new CGSize representing the result - */ - @inlinable - static func * (lhs: CGSize, rhs: CGFloat) -> CGSize { - .init(width: lhs.width * rhs, height: lhs.height * rhs) - } - - /** - Multiply the components of a CGSize value by a scalar - - parameter lhs: the scalar to multiply - - parameter rhs: the CGSize to multiply - - returns: new CGSize representing the result - */ - @inlinable - static func * (lhs: CGFloat, rhs: CGSize) -> CGSize { rhs * lhs } - - /** - Divide the components of a CGSize value by a scalar - - parameter lhs: the CGSize to divide - - parameter rhs: the scalar to divide - - returns: new CGSize representing the result - */ - @inlinable - static func / (lhs: CGSize, rhs: CGFloat) -> CGSize { - .init(width: lhs.width / rhs, height: lhs.height / rhs) - } - - /** - Divide the components of two CGSize values - - parameter lhs: the CGSize to divide - - parameter rhs: the CGSize to divide - - returns: new CGSize representing the result - */ - @inlinable - static func / (lhs: CGSize, rhs: CGSize) -> CGSize { - .init(width: lhs.width / rhs.width, height: lhs.height / rhs.height) - } -} diff --git a/Sources/JoyStickView/JoyStickView.swift b/Sources/JoyStickView/JoyStickView.swift index a5d4c51..811c8f2 100644 --- a/Sources/JoyStickView/JoyStickView.swift +++ b/Sources/JoyStickView/JoyStickView.swift @@ -38,14 +38,10 @@ import CoreGraphics /// use `CGRect(x: 50, y: 0, width: 1, height: 100)` public var handleConstraint: CGRect? { didSet { - handleCenterClamper = { $0 } if case .some(let hc) = handleConstraint { - print("clamper: \(hc)") - handleCenterClamper = { pos in - let clamped = CGPoint(x: min(max(pos.x, hc.minX), hc.maxX), y: min(max(pos.y, hc.minY), hc.maxY)) - print("\(hc) clamping: \(pos) -> \(clamped)") - return clamped - } + handleCenterClamper = { .init(x: min(max($0.x, hc.minX), hc.maxX), y: min(max($0.y, hc.minY), hc.maxY)) } + } else { + handleCenterClamper = { $0 } } } } diff --git a/Tests/JoyStickViewTests/Bundle+AdditionsTests.swift b/Tests/JoyStickViewTests/Bundle+AdditionsTests.swift new file mode 100644 index 0000000..ae700dd --- /dev/null +++ b/Tests/JoyStickViewTests/Bundle+AdditionsTests.swift @@ -0,0 +1,14 @@ +// Copyright © 2023 Brad Howes. All rights reserved. + +import XCTest +import JoyStickView + +class BundleAdditionsTests: XCTestCase { + + func testPodResource() { + let main = Bundle.main + for bundle in Bundle.allBundles { + _ = main.podResource(name: bundle.description) + } + } +} diff --git a/Tests/JoyStickViewTests/CoreGraphics+AdditionsTests.swift b/Tests/JoyStickViewTests/CoreGraphics+AdditionsTests.swift new file mode 100644 index 0000000..a52a0d1 --- /dev/null +++ b/Tests/JoyStickViewTests/CoreGraphics+AdditionsTests.swift @@ -0,0 +1,46 @@ +// Copyright © 2023 Brad Howes. All rights reserved. + +import XCTest +import JoyStickView + +class CoreGraphicsAdditionsTests: XCTestCase { + + func testCGRectMid() { + let rect = CGRect(x: 0, y: 0, width: 100, height: 220) + XCTAssertEqual(rect.mid, CGPoint(x: 50, y: 110)) + } + + func testCGVectorMagnitude() { + let v1 = CGVector(dx: 1.23, dy: 4.5) + XCTAssertEqual(v1.magnitude, sqrt(v1.dx * v1.dx + v1.dy * v1.dy)) + } + + func testCGVectorMagnitudeSquared() { + let v1 = CGVector(dx: 1.23, dy: 4.5) + XCTAssertEqual(v1.magnitude2, v1.dx * v1.dx + v1.dy * v1.dy) + } + + func testCGPointPlusCGVector() { + let p1 = CGPoint(x: 1.2, y: 3.4) + let v1 = CGVector(dx: 5.6, dy: 7.8) + XCTAssertEqual(p1 + v1, CGPoint(x: p1.x + v1.dx, y: p1.y + v1.dy)) + } + + func testCGPointPlusCGSize() { + let p1 = CGPoint(x: 1.2, y: 3.4) + let s1 = CGSize(width: 5.6, height: 7.8) + XCTAssertEqual(p1 + s1, CGPoint(x: p1.x + s1.width, y: p1.y + s1.height)) + } + + func testCGPointMinusCGVector() { + let p1 = CGPoint(x: 1.2, y: 3.4) + let v1 = CGVector(dx: 5.6, dy: 7.8) + XCTAssertEqual(p1 - v1, CGPoint(x: p1.x - v1.dx, y: p1.y - v1.dy)) + } + + func testCGPointMinusCGPoint() { + let p1 = CGPoint(x: 1.2, y: 3.4) + let p2 = CGPoint(x: 5.6, y: 7.8) + XCTAssertEqual(p1 - p2, CGVector(dx: p1.x - p2.x, dy: p1.y - p2.y)) + } +}