Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSC-#8 #9

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/CoreOSC/CoreOSC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import Foundation
public enum CoreOSC {

/// This package's semantic version number, mirrored also in git history as a `git tag`.
public static let version: String = "1.2.1"
public static let version: String = "1.3.0"

/// The license agreement this repository is licensed under.
public static let license: String = {
Expand Down
14 changes: 14 additions & 0 deletions Sources/CoreOSC/OSCBundle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,18 @@ public struct OSCBundle: OSCPacket {
return result
}

/// Flatten the elements contained by the bundle, ignoring all `OSCTimeTag`'s.
/// - Returns: An array of `OSCMessage`'s.
public func flatten() -> [OSCMessage] {
elements.reduce([OSCMessage]()) {
if let message = $1 as? OSCMessage {
return $0 + [message]
} else if let bundle = $1 as? OSCBundle {
return $0 + bundle.flatten()
} else {
return $0
}
}
}

}
10 changes: 8 additions & 2 deletions Sources/CoreOSC/OSCMessage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,14 @@
import Foundation

/// An OSC Message.
public struct OSCMessage: OSCPacket {

public struct OSCMessage: OSCPacket, Equatable {

public static func == (lhs: OSCMessage, rhs: OSCMessage) -> Bool {
lhs.addressPattern == rhs.addressPattern &&
lhs.typeTagString == rhs.typeTagString &&
lhs.arguments.map { $0.oscData } == rhs.arguments.map { $0.oscData }
}

/// The OSC Address Pattern associated with the message that represents the full path to one
/// or more OSC Methods through pattern matching.
public private(set) var addressPattern: OSCAddressPattern
Expand Down
3 changes: 3 additions & 0 deletions Sources/CoreOSC/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/* OSC Address Error */

/* OSC Version */
"OSC_VERSION" = "1.3.0";

/* OSC Address Error: Invalid Address */
"OSC_ADDRESS_ERROR_INVALID_ADDRESS" = "Invalid address";

Expand Down
3 changes: 2 additions & 1 deletion Tests/CoreOSCTests/CoreOSCTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import XCTest
class CoreOSCTests: XCTestCase {

func testVersion() {
XCTAssertEqual(CoreOSC.version, "1.2.1")
XCTAssertEqual(CoreOSC.version, "1.3.0")
XCTAssertEqual(NSLocalizedString("OSC_VERSION", bundle: .module, comment: "OSC Version"), CoreOSC.version)
}

func testLicense() {
Expand Down
82 changes: 82 additions & 0 deletions Tests/CoreOSCTests/OSCBundleTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// OSCBundleTests.swift
// CoreOSCTests
//
// Created by Sam Smallman on 14/08/2023.
// Copyright © 2023 Sam Smallman. https://github.com/SammySmallman
//
// This file is part of CoreOSC
//
// CoreOSC is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// CoreOSC is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//

import XCTest

@testable import CoreOSC

final class OSCABundleTests: XCTestCase {

func testSimpleFlatten() throws {
let messages = [
try! OSCMessage(with: "/core/osc/1"),
try! OSCMessage(with: "/core/osc/2"),
try! OSCMessage(with: "/core/osc/3")
]
let bundle = OSCBundle(
messages,
timeTag: .immediate
)
XCTAssertEqual(bundle.flatten(), messages)
}

func testRecursiveFlatten() throws {
let bundle = OSCBundle(
[
try! OSCMessage(with: "/core/osc/1"),
try! OSCMessage(with: "/core/osc/2"),
try! OSCMessage(with: "/core/osc/3"),
OSCBundle(
[
try! OSCMessage(with: "/core/osc/4"),
try! OSCMessage(with: "/core/osc/5"),
try! OSCMessage(with: "/core/osc/6"),
OSCBundle(
[
try! OSCMessage(with: "/core/osc/7"),
try! OSCMessage(with: "/core/osc/8"),
try! OSCMessage(with: "/core/osc/9"),

],
timeTag: .immediate
)
],
timeTag: .immediate
)
],
timeTag: .immediate
)
XCTAssertEqual(bundle.flatten(), [
try! OSCMessage(with: "/core/osc/1"),
try! OSCMessage(with: "/core/osc/2"),
try! OSCMessage(with: "/core/osc/3"),
try! OSCMessage(with: "/core/osc/4"),
try! OSCMessage(with: "/core/osc/5"),
try! OSCMessage(with: "/core/osc/6"),
try! OSCMessage(with: "/core/osc/7"),
try! OSCMessage(with: "/core/osc/8"),
try! OSCMessage(with: "/core/osc/9")
])
}

}
Loading