Files
playdate-kit/Tests/PlayDate/PlayDateTests.swift
T
javierandClaude Fable 5 6485997edc Promote subsystem namespaces to the top level of the module
System, Display, Graphics, Sprite, Sound, File, JSON, Lua, Scoreboards,
Network, Rect, SystemEvent, and AccessReply now live at module scope
instead of being nested in the Playdate enum; consumers write
System.log(...) instead of Playdate.System.log(...), and can qualify with
the module name (PlayDate.System) on collision. The Playdate enum remains
only as the raw C API bootstrap (initialize(with:), api, apiPointer), and
Playdate.Error is renamed PlaydateError so no top-level type shadows
Swift.Error.

Breaking change for any existing consumers.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-24 11:10:25 +02:00

88 lines
3.0 KiB
Swift

import Testing
@testable import PlayDate
// Most of the binding requires a running Playdate (the API struct is handed
// to the game at launch), so these tests cover the pure value types and the
// inline C helpers that work without it.
@Test func buttonsOptionSetMatchesCMasks() {
#expect(System.Buttons.left.rawValue == 1 << 0)
#expect(System.Buttons.right.rawValue == 1 << 1)
#expect(System.Buttons.up.rawValue == 1 << 2)
#expect(System.Buttons.down.rawValue == 1 << 3)
#expect(System.Buttons.b.rawValue == 1 << 4)
#expect(System.Buttons.a.rawValue == 1 << 5)
let combined: System.Buttons = [.a, .up]
#expect(combined.contains(.a))
#expect(!combined.contains(.b))
}
@Test func graphicsRectConvertsBetweenOriginSizeAndEdges() {
let rect = Graphics.Rect(x: 10, y: 20, width: 30, height: 40)
#expect(rect.left == 10)
#expect(rect.right == 40)
#expect(rect.top == 20)
#expect(rect.bottom == 60)
let translated = rect.translated(dx: 5, dy: -5)
#expect(translated.left == 15)
#expect(translated.top == 15)
let roundTripped = Graphics.Rect(rect.cValue)
#expect(roundTripped.left == rect.left && roundTripped.bottom == rect.bottom)
}
@Test func spriteRectRoundTripsThroughC() {
let rect = Rect(x: 1.5, y: 2.5, width: 3, height: 4)
let roundTripped = Rect(rect.cValue)
#expect(roundTripped.x == 1.5)
#expect(roundTripped.y == 2.5)
#expect(roundTripped.width == 3)
#expect(roundTripped.height == 4)
}
@Test func soundFormatPropertiesMatchCMacros() {
#expect(!Sound.Format.mono8bit.isStereo)
#expect(Sound.Format.stereo16bit.isStereo)
#expect(Sound.Format.mono16bit.is16bit)
#expect(!Sound.Format.monoADPCM.is16bit)
#expect(Sound.Format.mono8bit.bytesPerFrame == 1)
#expect(Sound.Format.stereo8bit.bytesPerFrame == 2)
#expect(Sound.Format.mono16bit.bytesPerFrame == 2)
#expect(Sound.Format.stereo16bit.bytesPerFrame == 4)
}
@Test func midiNoteFrequencyConversionRoundTrips() {
// A4 (MIDI 69) is 440 Hz.
let a4 = Sound.frequency(forNote: 69)
#expect(abs(a4 - 440) < 0.01)
let note = Sound.note(forFrequency: 440)
#expect(abs(note - 69) < 0.001)
}
@Test func cStringHelpersRoundTripUTF8() {
let original = "héllo, wörld"
let copy = original.copiedPlaydateCString()
defer { copy.deallocate() }
#expect(String(playdateCString: copy) == original)
let viaClosure = original.withPlaydateCString { String(playdateCString: $0) }
#expect(viaClosure == original)
}
@Test func dateTimeMirrorsCStruct() {
let dateTime = System.DateTime(year: 2026, month: 7, day: 24, weekday: 5,
hour: 12, minute: 34, second: 56)
let roundTripped = System.DateTime(dateTime.cValue)
#expect(roundTripped.year == 2026)
#expect(roundTripped.month == 7)
#expect(roundTripped.day == 24)
#expect(roundTripped.weekday == 5)
#expect(roundTripped.hour == 12)
#expect(roundTripped.minute == 34)
#expect(roundTripped.second == 56)
}