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

Add require functions for Interaction.Data #64

Merged
merged 4 commits into from Jan 21, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 31 additions & 0 deletions Sources/DiscordModels/Types/Interaction.swift
Expand Up @@ -14,6 +14,8 @@

case componentWasNotOfKind(kind: String, component: ActionRow.Component)

case dataWasNotOfKind(kind: String, data: Interaction.Data)

public var description: String {
switch self {
case let .optionNotFoundInCommand(name, command):
Expand All @@ -30,6 +32,8 @@
return "Interaction.Error.componentNotFoundInActionRows(customId: \(customId), actionRows: \(actionRows))"
case let .componentWasNotOfKind(kind, component):
return "Interaction.Error.componentWasNotOfKind(kind: \(kind), component: \(component))"
case let .dataWasNotOfKind(kind, data):
return "Interaction.Error.dataWasNotOfKind(kind: \(kind), data: \(data))"

Check warning on line 36 in Sources/DiscordModels/Types/Interaction.swift

View check run for this annotation

Codecov / codecov/patch

Sources/DiscordModels/Types/Interaction.swift#L35-L36

Added lines #L35 - L36 were not covered by tests
}
}
}
Expand Down Expand Up @@ -262,6 +266,33 @@
case applicationCommand(ApplicationCommand)
case messageComponent(MessageComponent)
case modalSubmit(ModalSubmit)

public func requireApplicationCommand() throws -> ApplicationCommand {
switch self {
case let .applicationCommand(applicationCommand):
return applicationCommand
default:
throw Error.dataWasNotOfKind(kind: "applicationCommand", data: self)
}
}

public func requireMessageComponent() throws -> MessageComponent {
switch self {
case let .messageComponent(messageComponent):
return messageComponent
default:
throw Error.dataWasNotOfKind(kind: "messageComponent", data: self)
}
}

public func requireModalSubmit() throws -> ModalSubmit {
switch self {
case let .modalSubmit(modalSubmit):
return modalSubmit
default:
throw Error.dataWasNotOfKind(kind: "modalSubmit", data: self)
}
}
}

public var id: InteractionSnowflake
Expand Down
25 changes: 24 additions & 1 deletion Tests/DiscordBMTests/DiscordModels.swift
Expand Up @@ -354,6 +354,29 @@ class DiscordModelsTests: XCTestCase {
_ = try decoder.decode(Interaction.ApplicationCommand.ResolvedData.self, from: encoded)
}

func testInteractionDataUtilities() throws {
let applicationCommand: Interaction.Data = .applicationCommand(
.init(id: "", name: "", type: .applicationCommand)
)

let messageComponent: Interaction.Data = .messageComponent(
.init(custom_id: "", component_type: .actionRow)
)

let modalSubmit: Interaction.Data = .modalSubmit(
.init(custom_id: "", components: [[.button(.init(label: "", url: ""))]])
)

XCTAssertNoThrow(try applicationCommand.requireApplicationCommand())
XCTAssertThrowsError(try messageComponent.requireApplicationCommand())

XCTAssertNoThrow(try messageComponent.requireMessageComponent())
XCTAssertThrowsError(try modalSubmit.requireMessageComponent())

XCTAssertNoThrow(try modalSubmit.requireModalSubmit())
XCTAssertThrowsError(try applicationCommand.requireModalSubmit())
}

func testApplicationCommandUtilities() throws {
let command = Interaction.ApplicationCommand(
id: try! .makeFake(),
Expand Down Expand Up @@ -526,7 +549,7 @@ class DiscordModelsTests: XCTestCase {
}
}

func testCollectionisNotEmpty() throws {
func testCollectionIsNotEmpty() throws {
let array1: [String]? = nil
XCTAssertEqual(array1.isNotEmpty, false)

Expand Down
3 changes: 3 additions & 0 deletions Tests/IntegrationTests/GatwayConnection.swift
Expand Up @@ -26,6 +26,7 @@ class GatewayConnectionTests: XCTestCase, @unchecked Sendable {
try! httpClient.syncShutdown()
}

@available(*, deprecated, message: "To avoid deprecation warnings for 'makeEventsParseFailureStream'")
func testConnect() async throws {
/// Make sure last tests don't affect this test's gateway connection
try await Task.sleep(for: .seconds(5))
Expand Down Expand Up @@ -107,6 +108,7 @@ class GatewayConnectionTests: XCTestCase, @unchecked Sendable {
XCTAssertEqual(bot.connectionId.load(ordering: .relaxed), 2)
}

@available(*, deprecated, message: "To avoid deprecation warnings for 'makeEventsStream'")
func testShardingGatewayManager() async throws {
/// Make sure last tests don't affect this test's gateway connection
try await Task.sleep(for: .seconds(5))
Expand Down Expand Up @@ -162,6 +164,7 @@ class GatewayConnectionTests: XCTestCase, @unchecked Sendable {
await counter.waitFulfillment()
}

@available(*, deprecated, message: "To avoid deprecation warnings for 'makeEventsStream'")
func testGatewayStopsOnInvalidToken() async throws {
/// Make sure last tests don't affect this test's gateway connection
try await Task.sleep(for: .seconds(5))
Expand Down