Skip to content

Commit

Permalink
Convenience init for ParserError using NSError
Browse files Browse the repository at this point in the history
  • Loading branch information
theRealRobG committed Apr 21, 2021
1 parent 2130f79 commit 7babc5f
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ public struct InvalidATSCContentIdentifierInUPIDInfo: Equatable {
public let upidLength: Int
public let staticBytesLength = 4
public var calculatedContentIDByteCount: Int { upidLength - staticBytesLength }

public init(upidLength: Int) {
self.upidLength = upidLength
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ public struct InvalidMPUInSegmentationUPIDInfo: Equatable {
public let upidLength: Int
public let staticBytesLength = 4
public var calculatedPrivateDataByteCount: Int { upidLength - staticBytesLength }

public init(upidLength: Int) {
self.upidLength = upidLength
}
}
56 changes: 56 additions & 0 deletions Sources/SCTE35Parser/Errors/ParserError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,60 @@ public enum ParserError: Equatable, LocalizedError, CustomNSError {
case .encryptedMessageNotSupported: return [:]
}
}

public init?(fromNSError nsError: NSError) {
guard nsError.domain == ParserError.errorDomain, let code = Code(rawValue: nsError.code) else {
return nil
}
switch code {
case .encryptedMessageNotSupported:
self = .encryptedMessageNotSupported
case .invalidATSCContentIdentifierInUPID:
guard let info = nsError.userInfo[Self.invalidATSCContentIdentifierInUPIDUserInfoKey] as? InvalidATSCContentIdentifierInUPIDInfo else { return nil }
self = .invalidATSCContentIdentifierInUPID(info)
case .invalidBitStreamMode:
guard let info = nsError.userInfo[Self.invalidBitStreamModeUserInfoKey] as? InvalidBitStreamModeErrorInfo else { return nil }
self = .invalidBitStreamMode(info)
case .invalidInputString:
guard let string = nsError.userInfo[Self.invalidInputStringUserInfoKey] as? String else { return nil }
self = .invalidInputString(string)
case .invalidMPUInSegmentationUPID:
guard let info = nsError.userInfo[Self.invalidMPUInSegmentationUPIDUserInfoKey] as? InvalidMPUInSegmentationUPIDInfo else { return nil }
self = .invalidMPUInSegmentationUPID(info)
case .invalidPrivateIndicator:
self = .invalidPrivateIndicator
case .invalidSectionSyntaxIndicator:
self = .invalidSectionSyntaxIndicator
case .invalidSegmentationDescriptorIdentifier:
guard let id = nsError.userInfo[Self.invalidSegmentationDescriptorIdentifierUserInfoKey] as? Int else { return nil }
self = .invalidSegmentationDescriptorIdentifier(id)
case .invalidURLInSegmentationUPID:
guard let string = nsError.userInfo[Self.invalidURLInSegmentationUPIDUserInfoKey] as? String else { return nil }
self = .invalidURLInSegmentationUPID(string)
case .invalidUUIDInSegmentationUPID:
guard let string = nsError.userInfo[Self.invalidUUIDInSegmentationUPIDUserInfoKey] as? String else { return nil }
self = .invalidUUIDInSegmentationUPID(string)
case .unexpectedEndOfData:
guard let info = nsError.userInfo[Self.unexpectedEndOfDataUserInfoKey] as? UnexpectedEndOfDataErrorInfo else { return nil }
self = .unexpectedEndOfData(info)
case .unexpectedSegmentationUPIDLength:
guard let info = nsError.userInfo[Self.unexpectedSegmentationUPIDLengthUserInfoKey] as? UnexpectedSegmentationUPIDLengthErrorInfo else { return nil }
self = .unexpectedSegmentationUPIDLength(info)
case .unrecognisedAudioCodingMode:
guard let mode = nsError.userInfo[Self.unrecognisedAudioCodingModeUserInfoKey] as? Int else { return nil }
self = .unrecognisedAudioCodingMode(mode)
case .unrecognisedSegmentationTypeID:
guard let id = nsError.userInfo[Self.unrecognisedSegmentationTypeIDUserInfoKey] as? Int else { return nil }
self = .unrecognisedSegmentationTypeID(id)
case .unrecognisedSegmentationUPIDType:
guard let id = nsError.userInfo[Self.unrecognisedSegmentationUPIDTypeUserInfoKey] as? Int else { return nil }
self = .unrecognisedSegmentationUPIDType(id)
case .unrecognisedSpliceCommandType:
guard let id = nsError.userInfo[Self.unrecognisedSpliceCommandTypeUserInfoKey] as? Int else { return nil }
self = .unrecognisedSpliceCommandType(id)
case .unrecognisedSpliceDescriptorTag:
guard let id = nsError.userInfo[Self.unrecognisedSpliceDescriptorTagUserInfoKey] as? Int else { return nil }
self = .unrecognisedSpliceDescriptorTag(id)
}
}
}
10 changes: 10 additions & 0 deletions Sources/SCTE35Parser/Errors/UnexpectedEndOfDataErrorInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ public struct UnexpectedEndOfDataErrorInfo: Equatable {
public let actualBitsLeft: Int
/// A description of what was being attempted to be parsed that resulted in error.
public let description: String

public init(
expectedMinimumBitsLeft: Int,
actualBitsLeft: Int,
description: String
) {
self.expectedMinimumBitsLeft = expectedMinimumBitsLeft
self.actualBitsLeft = actualBitsLeft
self.description = description
}
}
113 changes: 113 additions & 0 deletions Tests/SCTE35ParserTests/Errors/ParserErrorTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//
// ParserErrorTests.swift
//
//
// Created by Robert Galluccio on 21/04/2021.
//

import Foundation
import XCTest
import SCTE35Parser

class ParserErrorTests: XCTestCase {
var expectedError: ParserError!

func test_initFromNSError_encryptedMessageNotSupported() {
expectedError = .encryptedMessageNotSupported
XCTAssertEqual(expectedError, ParserError(fromNSError: expectedError as NSError))
}

func test_initFromNSError_invalidATSCContentIdentifierInUPID() {
expectedError = .invalidATSCContentIdentifierInUPID(
InvalidATSCContentIdentifierInUPIDInfo(upidLength: 10)
)
XCTAssertEqual(expectedError, ParserError(fromNSError: expectedError as NSError))
}

func test_initFromNSError_invalidBitStreamMode() {
expectedError = .invalidBitStreamMode(InvalidBitStreamModeErrorInfo(bsmod: 7, acmod: nil))
XCTAssertEqual(expectedError, ParserError(fromNSError: expectedError as NSError))
}

func test_initFromNSError_invalidInputString() {
expectedError = .invalidInputString("!*&@$%?")
XCTAssertEqual(expectedError, ParserError(fromNSError: expectedError as NSError))
}

func test_initFromNSError_invalidMPUInSegmentationUPID() {
expectedError = .invalidMPUInSegmentationUPID(InvalidMPUInSegmentationUPIDInfo(upidLength: 8))
XCTAssertEqual(expectedError, ParserError(fromNSError: expectedError as NSError))
}

func test_initFromNSError_invalidPrivateIndicator() {
expectedError = .invalidPrivateIndicator
XCTAssertEqual(expectedError, ParserError(fromNSError: expectedError as NSError))
}

func test_initFromNSError_invalidSectionSyntaxIndicator() {
expectedError = .invalidSectionSyntaxIndicator
XCTAssertEqual(expectedError, ParserError(fromNSError: expectedError as NSError))
}

func test_initFromNSError_invalidSegmentationDescriptorIdentifier() {
expectedError = .invalidSegmentationDescriptorIdentifier(42)
XCTAssertEqual(expectedError, ParserError(fromNSError: expectedError as NSError))
}

func test_initFromNSError_invalidURLInSegmentationUPID() {
expectedError = .invalidURLInSegmentationUPID("")
XCTAssertEqual(expectedError, ParserError(fromNSError: expectedError as NSError))
}

func test_initFromNSError_invalidUUIDInSegmentationUPID() {
expectedError = .invalidUUIDInSegmentationUPID("")
XCTAssertEqual(expectedError, ParserError(fromNSError: expectedError as NSError))
}

func test_initFromNSError_unexpectedEndOfData() {
expectedError = .unexpectedEndOfData(
UnexpectedEndOfDataErrorInfo(
expectedMinimumBitsLeft: 100,
actualBitsLeft: 50,
description: "Oops"
)
)
XCTAssertEqual(expectedError, ParserError(fromNSError: expectedError as NSError))
}

func test_initFromNSError_unexpectedSegmentationUPIDLength() {
expectedError = .unexpectedSegmentationUPIDLength(
UnexpectedSegmentationUPIDLengthErrorInfo(
declaredSegmentationUPIDLength: 8,
expectedSegmentationUPIDLength: 12,
segmentationUPIDType: .ti
)
)
XCTAssertEqual(expectedError, ParserError(fromNSError: expectedError as NSError))
}

func test_initFromNSError_unrecognisedAudioCodingMode() {
expectedError = .unrecognisedAudioCodingMode(10)
XCTAssertEqual(expectedError, ParserError(fromNSError: expectedError as NSError))
}

func test_initFromNSError_unrecognisedSegmentationTypeID() {
expectedError = .unrecognisedSegmentationTypeID(0x55)
XCTAssertEqual(expectedError, ParserError(fromNSError: expectedError as NSError))
}

func test_initFromNSError_unrecognisedSegmentationUPIDType() {
expectedError = .unrecognisedSegmentationUPIDType(0x22)
XCTAssertEqual(expectedError, ParserError(fromNSError: expectedError as NSError))
}

func test_initFromNSError_unrecognisedSpliceCommandType() {
expectedError = .unrecognisedSpliceCommandType(0x10)
XCTAssertEqual(expectedError, ParserError(fromNSError: expectedError as NSError))
}

func test_initFromNSError_unrecognisedSpliceDescriptorTag() {
expectedError = .unrecognisedSpliceDescriptorTag(0x10)
XCTAssertEqual(expectedError, ParserError(fromNSError: expectedError as NSError))
}
}

0 comments on commit 7babc5f

Please sign in to comment.