Skip to content

Commit

Permalink
Tests refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Recouse committed Nov 30, 2024
1 parent bc6cf4b commit 29725a8
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Sources/EventSource/ServerEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fileprivate extension Data {
}
}

fileprivate extension Array {
package extension Array {
subscript(safe index: Int) -> Element? {
guard index >= 0, index < endIndex else {
return nil
Expand Down
149 changes: 75 additions & 74 deletions Tests/EventSourceTests/EventParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Testing
@testable import EventSource

struct EventParserTests {
@Test func messagesParsing() async throws {
@Test func eventParsing() async throws {
var parser = ServerEventParser()

let text = """
Expand All @@ -31,32 +31,32 @@ struct EventParserTests {
"""
let data = Data(text.utf8)

let messages = parser.parse(data)
let events = parser.parse(data)

#expect(messages.count == 5)
#expect(messages[0].data != nil)
#expect(messages[0].data! == "test 1")
#expect(messages[1].data != nil)
#expect(messages[1].data! == "test 2\ncontinued")
#expect(messages[2].event != nil)
#expect(messages[2].data != nil)
#expect(messages[2].event! == "add")
#expect(messages[2].data! == "test 3")
#expect(messages[3].event != nil)
#expect(messages[3].data != nil)
#expect(messages[3].event! == "remove")
#expect(messages[3].data! == "test 4")
#expect(messages[4].id != nil)
#expect(messages[4].event != nil)
#expect(messages[4].data != nil)
#expect(messages[4].id! == "5")
#expect(messages[4].event! == "ping")
#expect(messages[4].data! == "test 5")
#expect(events.count == 5)

let event0Data = try #require(events[safe: 0]?.data)
#expect(event0Data == "test 1")

let event1Data = try #require(events[safe: 1]?.data)
#expect(event1Data == "test 2\ncontinued")

let event2Event = try #require(events[safe: 2]?.event)
let event2Data = try #require(events[safe: 2]?.data)
#expect(event2Event == "add")
#expect(event2Data == "test 3")

let event3Event = try #require(events[safe: 3]?.event)
let event3Data = try #require(events[safe: 3]?.data)
#expect(event3Event == "remove")
#expect(event3Data == "test 4")

let event4ID = try #require(events[safe: 4]?.id)
let event4Event = try #require(events[safe: 4]?.event)
let event4Data = try #require(events[safe: 4]?.data)
#expect(event4ID == "5")
#expect(event4Event == "ping")
#expect(event4Data == "test 5")
}

@Test func emptyData() async {
Expand All @@ -68,12 +68,12 @@ struct EventParserTests {
"""
let data = Data(text.utf8)

let messages = parser.parse(data)
let events = parser.parse(data)

#expect(messages.isEmpty)
#expect(events.isEmpty)
}

@Test func otherMessageFormats() async {
@Test func otherEventFormats() async throws {
var parser = ServerEventParser()

let text = """
Expand All @@ -98,34 +98,35 @@ struct EventParserTests {
"""
let data = Data(text.utf8)

let messages = parser.parse(data)

#expect(messages[0].data != nil)
#expect(messages[0].data! == "test 1")

#expect(messages[1].id != nil)
#expect(messages[1].data != nil)
#expect(messages[1].id! == "2")
#expect(messages[1].data! == "test 2")

#expect(messages[2].event != nil)
#expect(messages[2].data != nil)
#expect(messages[2].event! == "add")
#expect(messages[2].data! == "test 3")

#expect(messages[3].id != nil)
#expect(messages[3].event != nil)
#expect(messages[3].data != nil)
#expect(messages[3].id! == "4")
#expect(messages[3].event! == "ping")
#expect(messages[3].data! == "test 4")

#expect(messages[4].other != nil)
#expect(messages[4].other!["test 5"] == "")

#expect(messages[5].other != nil)
#expect(messages[5].other!["message 6"] == "")
#expect(messages[5].other!["message 6-1"] == "")
let events = parser.parse(data)

let event0Data = try #require(events[safe: 0]?.data)
#expect(event0Data == "test 1")

let event1ID = try #require(events[safe: 1]?.id)
let event1Data = try #require(events[safe: 1]?.data)
#expect(event1ID == "2")
#expect(event1Data == "test 2")

let event2 = try #require(events[safe: 2]?.event)
let event2Data = try #require(events[safe: 2]?.data)
#expect(event2 == "add")
#expect(event2Data == "test 3")

let event3ID = try #require(events[safe: 3]?.id)
let event3 = try #require(events[safe: 3]?.event)
let event3Data = try #require(events[safe: 3]?.data)
#expect(event3ID == "4")
#expect(event3 == "ping")
#expect(event3Data == "test 4")

let event4Other = try #require(events[safe: 4]?.other?["test 5"])
#expect(event4Other == "")

let event5Other1 = try #require(events[safe: 5]?.other?["message 6"])
let event5Other2 = try #require(events[safe: 5]?.other?["message 6-1"])
#expect(event5Other1 == "")
#expect(event5Other2 == "")
}

@Test func dataOnlyMode() async throws {
Expand All @@ -141,32 +142,32 @@ struct EventParserTests {
"""
let data = Data(text.utf8)

let messages = parser.parse(data)
let events = parser.parse(data)

let data1 = Data(try #require(messages[0].data?.utf8))
let data2 = Data(try #require(messages[1].data?.utf8))
let data1 = Data(try #require(events[0].data?.utf8))
let data2 = Data(try #require(events[1].data?.utf8))

let message1 = try jsonDecoder.decode(TestModel.self, from: data1)
let message2 = try jsonDecoder.decode(TestModel.self, from: data2)
let model1 = try jsonDecoder.decode(TestModel.self, from: data1)
let model2 = try jsonDecoder.decode(TestModel.self, from: data2)

#expect(message1.content == "\ntest\n")
#expect(message2.content == "\n\n")
#expect(model1.content == "\ntest\n")
#expect(model2.content == "\n\n")
}

@Test func parseNotCompleteMessage() async throws {
@Test func parseNotCompleteEvent() async throws {
var parser = ServerEventParser()

let text = """
data: test 1
"""
let data = Data(text.utf8)

let messages = parser.parse(data)
let events = parser.parse(data)

#expect(messages.count == 0)
#expect(events.isEmpty)
}

@Test func parseSeparatedMessage() async throws {
@Test func parseSeparatedEvent() async throws {
var parser = ServerEventParser()

let textPart1 = """
Expand All @@ -182,14 +183,14 @@ struct EventParserTests {
let dataPart2 = Data(textPart2.utf8)

let _ = parser.parse(dataPart1)
let messages = parser.parse(dataPart2)
let events = parser.parse(dataPart2)

#expect(messages.count == 1)
#expect(events.count == 1)

#expect(messages[0].event != nil)
#expect(messages[0].data != nil)
#expect(messages[0].event! == "add")
#expect(messages[0].data! == "test 1")
let event = try #require(events.first?.event)
let eventData = try #require(events.first?.data)
#expect(event == "add")
#expect(eventData == "test 1")
}
}

Expand Down

0 comments on commit 29725a8

Please sign in to comment.