unit tests
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
@testable import Peered
|
||||
|
||||
@Suite
|
||||
struct NoteMessageCodableTests {
|
||||
@Test
|
||||
func encodesToJSON() throws {
|
||||
let message = NoteMessage(senderID: "host", content: "Content")
|
||||
let data = try JSONEncoder().encode(message)
|
||||
let json = try #require(try? JSONSerialization.jsonObject(with: data) as? [String: String])
|
||||
|
||||
#expect(json["senderID"] == "host")
|
||||
#expect(json["content"] == "Content")
|
||||
}
|
||||
|
||||
@Test
|
||||
func decodesFromJSON() throws {
|
||||
let json = #"{"senderID":"host","content":"contentt"}"#
|
||||
let data = try #require(json.data(using: .utf8))
|
||||
let message = try JSONDecoder().decode(NoteMessage.self, from: data)
|
||||
|
||||
#expect(message.senderID == "host")
|
||||
#expect(message.content == "contentt")
|
||||
}
|
||||
|
||||
@Test("encode → decode")
|
||||
func coding() throws {
|
||||
let original = NoteMessage(senderID: "host", content: "coding test")
|
||||
let data = try JSONEncoder().encode(original)
|
||||
let decoded = try JSONDecoder().decode(NoteMessage.self, from: data)
|
||||
|
||||
#expect(decoded.senderID == original.senderID)
|
||||
#expect(decoded.content == original.content)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user