From 29725a88b527ff7545a2c81936b5b1a2daa8c0c2 Mon Sep 17 00:00:00 2001 From: Firdavs Khaydarov Date: Sat, 30 Nov 2024 18:09:53 +0200 Subject: [PATCH] Tests refactoring --- Sources/EventSource/ServerEvent.swift | 2 +- Tests/EventSourceTests/EventParserTests.swift | 149 +++++++++--------- 2 files changed, 76 insertions(+), 75 deletions(-) diff --git a/Sources/EventSource/ServerEvent.swift b/Sources/EventSource/ServerEvent.swift index bfecce8..8982905 100644 --- a/Sources/EventSource/ServerEvent.swift +++ b/Sources/EventSource/ServerEvent.swift @@ -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 diff --git a/Tests/EventSourceTests/EventParserTests.swift b/Tests/EventSourceTests/EventParserTests.swift index 9203dae..c3b79b0 100644 --- a/Tests/EventSourceTests/EventParserTests.swift +++ b/Tests/EventSourceTests/EventParserTests.swift @@ -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 = """ @@ -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 { @@ -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 = """ @@ -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 { @@ -141,19 +142,19 @@ 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 = """ @@ -161,12 +162,12 @@ struct EventParserTests { """ 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 = """ @@ -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") } }