Made Amiibo models Codable and Identifiable, and filters Hashable.
This commit is contained in:
1 parent
8fa4d50734
commit
a28842eb0a
24 files changed
+352
-190
No files matched your search
@@ -0,0 +1,61 @@
|
||||
// ===----------------------------------------------------------------------===
|
||||
//
|
||||
// This source file is part of the Amiibo Service open source project
|
||||
//
|
||||
// Copyright (c) 2026 Röck+Cöde VoF. and the Amiibo Service project authors
|
||||
// Licensed under Apache license v2.0
|
||||
//
|
||||
// See LICENSE for license information
|
||||
// See CONTRIBUTORS for the list of Amiibo Service project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
// ===----------------------------------------------------------------------===
|
||||
|
||||
import AmiiboService
|
||||
import Testing
|
||||
|
||||
@Suite("Filter conformances")
|
||||
struct FilterConformanceTests {
|
||||
|
||||
// MARK: Equatable tests
|
||||
|
||||
@Test("compares amiibo filters by value")
|
||||
func amiiboFilterEquality() {
|
||||
// GIVEN
|
||||
// WHEN
|
||||
// THEN
|
||||
#expect(AmiiboFilter() == AmiiboFilter())
|
||||
#expect(AmiiboFilter(name: "zelda") == AmiiboFilter(name: "zelda"))
|
||||
#expect(AmiiboFilter(name: "zelda") != AmiiboFilter(name: "mario"))
|
||||
#expect(AmiiboFilter() != AmiiboFilter(showGames: true))
|
||||
}
|
||||
|
||||
@Test("compares key-name filters by value")
|
||||
func keyNameFilterEquality() {
|
||||
// GIVEN
|
||||
// WHEN
|
||||
// THEN
|
||||
#expect(AmiiboSeriesFilter() == AmiiboSeriesFilter())
|
||||
#expect(AmiiboSeriesFilter(key: "0x01") == AmiiboSeriesFilter(key: "0x01"))
|
||||
#expect(AmiiboSeriesFilter(key: "0x01") != AmiiboSeriesFilter(name: "0x01"))
|
||||
#expect(AmiiboTypeFilter(name: "Card") != AmiiboTypeFilter(name: "Figure"))
|
||||
#expect(GameCharacterFilter(key: "0x0100") != GameCharacterFilter())
|
||||
#expect(GameSeriesFilter(name: "Pikmin") == GameSeriesFilter(name: "Pikmin"))
|
||||
}
|
||||
|
||||
// MARK: Hashable tests
|
||||
|
||||
@Test("hashes equal filters alike")
|
||||
func filterHashing() {
|
||||
// GIVEN
|
||||
let amiiboFilters: Set = [AmiiboFilter(name: "zelda"), AmiiboFilter(name: "zelda"), AmiiboFilter(name: "mario")]
|
||||
let seriesFilters: Set = [AmiiboSeriesFilter(key: "0x01"), AmiiboSeriesFilter(key: "0x01"), AmiiboSeriesFilter()]
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
#expect(amiiboFilters.count == 2)
|
||||
#expect(seriesFilters.count == 2)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
// ===----------------------------------------------------------------------===
|
||||
//
|
||||
// This source file is part of the Amiibo Service open source project
|
||||
//
|
||||
// Copyright (c) 2026 Röck+Cöde VoF. and the Amiibo Service project authors
|
||||
// Licensed under Apache license v2.0
|
||||
//
|
||||
// See LICENSE for license information
|
||||
// See CONTRIBUTORS for the list of Amiibo Service project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
// ===----------------------------------------------------------------------===
|
||||
|
||||
import AmiiboService
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
@Suite("Model conformances")
|
||||
struct ModelConformanceTests {
|
||||
|
||||
// MARK: Identifiable tests
|
||||
|
||||
@Test("identifies an amiibo by its identifier")
|
||||
func amiiboID() {
|
||||
// GIVEN
|
||||
let amiibos = Fixtures.amiibos
|
||||
|
||||
// WHEN
|
||||
// THEN
|
||||
#expect(amiibos.map(\.id) == amiibos.map(\.identifier))
|
||||
}
|
||||
|
||||
@Test("identifies key-name models by their keys")
|
||||
func keyNameIDs() {
|
||||
// GIVEN
|
||||
// WHEN
|
||||
// THEN
|
||||
#expect(Fixtures.amiiboSeries.map(\.id) == Fixtures.amiiboSeries.map(\.key))
|
||||
#expect(Fixtures.amiiboTypes.map(\.id) == Fixtures.amiiboTypes.map(\.key))
|
||||
#expect(Fixtures.gameCharacters.map(\.id) == Fixtures.gameCharacters.map(\.key))
|
||||
#expect(Fixtures.gameSeries.map(\.id) == Fixtures.gameSeries.map(\.key))
|
||||
}
|
||||
|
||||
// MARK: Codable tests
|
||||
|
||||
@Test("round-trips amiibos through JSON")
|
||||
func amiibosRoundTrip() throws {
|
||||
// GIVEN
|
||||
let amiibos = Fixtures.amiibos
|
||||
|
||||
// WHEN
|
||||
let decoded = try roundTrip(amiibos)
|
||||
|
||||
// THEN
|
||||
#expect(decoded == amiibos)
|
||||
#expect(decoded.first?.platform?.switch.first?.usages?.isEmpty == false)
|
||||
}
|
||||
|
||||
@Test("round-trips key-name models through JSON")
|
||||
func keyNameRoundTrip() throws {
|
||||
// GIVEN
|
||||
// WHEN
|
||||
// THEN
|
||||
#expect(try roundTrip(Fixtures.amiiboSeries) == Fixtures.amiiboSeries)
|
||||
#expect(try roundTrip(Fixtures.amiiboTypes) == Fixtures.amiiboTypes)
|
||||
#expect(try roundTrip(Fixtures.gameCharacters) == Fixtures.gameCharacters)
|
||||
#expect(try roundTrip(Fixtures.gameSeries) == Fixtures.gameSeries)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Helpers
|
||||
|
||||
private extension ModelConformanceTests {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
/// Encodes a value to JSON and decodes it back.
|
||||
func roundTrip<Value: Codable>(_ value: Value) throws -> Value {
|
||||
try JSONDecoder().decode(Value.self, from: JSONEncoder().encode(value))
|
||||
}
|
||||
|
||||
}
|
||||
@@ -326,71 +326,3 @@ private enum Errors {
|
||||
.unknown("An underlying error description.")
|
||||
]
|
||||
}
|
||||
|
||||
private enum Fixtures {
|
||||
/// Amiibo items; the first one has release dates and related games with usages.
|
||||
static let amiibos: [Amiibo] = [
|
||||
.init(
|
||||
head: "01000000",
|
||||
tail: "00040002",
|
||||
name: "Zelda",
|
||||
type: "Figure",
|
||||
series: "Super Smash Bros.",
|
||||
gameCharacter: "Zelda",
|
||||
gameSeries: "The Legend of Zelda",
|
||||
image: "https://raw.githubusercontent.com/N3evin/AmiiboAPI/master/images/icon_01000000-00040002.png",
|
||||
release: .init(
|
||||
america: Date(timeIntervalSince1970: 1_416_441_600),
|
||||
australia: Date(timeIntervalSince1970: 1_417_564_800),
|
||||
europe: Date(timeIntervalSince1970: 1_417_132_800),
|
||||
japan: Date(timeIntervalSince1970: 1_417_564_800)
|
||||
),
|
||||
platform: .init(
|
||||
switch: [
|
||||
.init(
|
||||
identifiers: ["01007EF00011E000"],
|
||||
name: "The Legend of Zelda: Breath of the Wild",
|
||||
usages: [.init(explanation: "Spawns a random selection of items.", isWriteable: false)]
|
||||
)
|
||||
],
|
||||
wiiU: [
|
||||
.init(
|
||||
identifiers: ["0005000010145000"],
|
||||
name: "Super Smash Bros. for Wii U",
|
||||
usages: [.init(explanation: "Unlocks a figure player.", isWriteable: true)]
|
||||
)
|
||||
]
|
||||
)
|
||||
),
|
||||
.init(
|
||||
head: "00000000",
|
||||
tail: "00000002",
|
||||
name: "Mario",
|
||||
type: "Figure",
|
||||
series: "Super Smash Bros.",
|
||||
gameCharacter: "Mario",
|
||||
gameSeries: "Super Mario",
|
||||
image: "https://raw.githubusercontent.com/N3evin/AmiiboAPI/master/images/icon_00000000-00000002.png"
|
||||
)
|
||||
]
|
||||
/// Amiibo series.
|
||||
static let amiiboSeries: [AmiiboSeries] = [
|
||||
.init(key: "0x00", name: "Super Smash Bros."),
|
||||
.init(key: "0x01", name: "Super Mario")
|
||||
]
|
||||
/// Amiibo types.
|
||||
static let amiiboTypes: [AmiiboType] = [
|
||||
.init(key: "0x00", name: "Figure"),
|
||||
.init(key: "0x01", name: "Card")
|
||||
]
|
||||
/// Game characters.
|
||||
static let gameCharacters: [GameCharacter] = [
|
||||
.init(key: "0x0000", name: "Mario"),
|
||||
.init(key: "0x0100", name: "Zelda")
|
||||
]
|
||||
/// Game series.
|
||||
static let gameSeries: [GameSeries] = [
|
||||
.init(key: "0x000", name: "Super Mario"),
|
||||
.init(key: "0x010", name: "The Legend of Zelda")
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
// ===----------------------------------------------------------------------===
|
||||
//
|
||||
// This source file is part of the Amiibo Service open source project
|
||||
//
|
||||
// Copyright (c) 2026 Röck+Cöde VoF. and the Amiibo Service project authors
|
||||
// Licensed under Apache license v2.0
|
||||
//
|
||||
// See LICENSE for license information
|
||||
// See CONTRIBUTORS for the list of Amiibo Service project authors
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
//
|
||||
// ===----------------------------------------------------------------------===
|
||||
|
||||
import AmiiboService
|
||||
import Foundation
|
||||
|
||||
/// Ready-made models for testing purposes.
|
||||
enum Fixtures {
|
||||
/// Amiibo items; the first one has release dates and related games with usages.
|
||||
static let amiibos: [Amiibo] = [
|
||||
.init(
|
||||
head: "01000000",
|
||||
tail: "00040002",
|
||||
name: "Zelda",
|
||||
type: "Figure",
|
||||
series: "Super Smash Bros.",
|
||||
gameCharacter: "Zelda",
|
||||
gameSeries: "The Legend of Zelda",
|
||||
image: "https://raw.githubusercontent.com/N3evin/AmiiboAPI/master/images/icon_01000000-00040002.png",
|
||||
release: .init(
|
||||
america: Date(timeIntervalSince1970: 1_416_441_600),
|
||||
australia: Date(timeIntervalSince1970: 1_417_564_800),
|
||||
europe: Date(timeIntervalSince1970: 1_417_132_800),
|
||||
japan: Date(timeIntervalSince1970: 1_417_564_800)
|
||||
),
|
||||
platform: .init(
|
||||
switch: [
|
||||
.init(
|
||||
identifiers: ["01007EF00011E000"],
|
||||
name: "The Legend of Zelda: Breath of the Wild",
|
||||
usages: [.init(explanation: "Spawns a random selection of items.", isWriteable: false)]
|
||||
)
|
||||
],
|
||||
wiiU: [
|
||||
.init(
|
||||
identifiers: ["0005000010145000"],
|
||||
name: "Super Smash Bros. for Wii U",
|
||||
usages: [.init(explanation: "Unlocks a figure player.", isWriteable: true)]
|
||||
)
|
||||
]
|
||||
)
|
||||
),
|
||||
.init(
|
||||
head: "00000000",
|
||||
tail: "00000002",
|
||||
name: "Mario",
|
||||
type: "Figure",
|
||||
series: "Super Smash Bros.",
|
||||
gameCharacter: "Mario",
|
||||
gameSeries: "Super Mario",
|
||||
image: "https://raw.githubusercontent.com/N3evin/AmiiboAPI/master/images/icon_00000000-00000002.png"
|
||||
)
|
||||
]
|
||||
/// Amiibo series.
|
||||
static let amiiboSeries: [AmiiboSeries] = [
|
||||
.init(key: "0x00", name: "Super Smash Bros."),
|
||||
.init(key: "0x01", name: "Super Mario")
|
||||
]
|
||||
/// Amiibo types.
|
||||
static let amiiboTypes: [AmiiboType] = [
|
||||
.init(key: "0x00", name: "Figure"),
|
||||
.init(key: "0x01", name: "Card")
|
||||
]
|
||||
/// Game characters.
|
||||
static let gameCharacters: [GameCharacter] = [
|
||||
.init(key: "0x0000", name: "Mario"),
|
||||
.init(key: "0x0100", name: "Zelda")
|
||||
]
|
||||
/// Game series.
|
||||
static let gameSeries: [GameSeries] = [
|
||||
.init(key: "0x000", name: "Super Mario"),
|
||||
.init(key: "0x010", name: "The Legend of Zelda")
|
||||
]
|
||||
}
|
||||
Reference in new issue
Block a user