diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..fb11b2d --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,11 @@ +# Lint.yml +name: Lint +on: pull_request + +jobs: + Lint: + runs-on: macos-latest + steps: + - uses: actions/checkout@v4 + - name: SwiftFormat + run: swiftformat --lint . --reporter github-actions-log diff --git a/Sources/MBAsyncNetworking/AsyncNetworkable+URLRequest.swift b/Sources/MBAsyncNetworking/AsyncNetworkable+URLRequest.swift index 84372f2..9b4ee01 100644 --- a/Sources/MBAsyncNetworking/AsyncNetworkable+URLRequest.swift +++ b/Sources/MBAsyncNetworking/AsyncNetworkable+URLRequest.swift @@ -59,7 +59,7 @@ public extension AsyncNetworkable { request.httpBody = getBody(body) return request } - + /// Creates a URLRequest with a JSON body and query parameters /// - Parameters: /// - queryItems: Dictionary of query parameters to be added to the URL diff --git a/Tests/MBAsyncNetworkingTests/APITests.swift b/Tests/MBAsyncNetworkingTests/APITests.swift new file mode 100644 index 0000000..19cc3fe --- /dev/null +++ b/Tests/MBAsyncNetworkingTests/APITests.swift @@ -0,0 +1,26 @@ +import Foundation +import Testing +@testable import MBAsyncNetworking + +@MainActor struct APITests { + init() { + UserSession.initialize(with: NetworkingStorage()) + } + + @Test func apiTest() async throws { + // Write your test here and use APIs like `#expect(...)` to check expected conditions. + let request: Data = try await TestAPI.google.fetch() + #expect(request.count > 0) + } + + enum TestAPI: AsyncNetworkable { + case google + case amazon + func request() async -> URLRequest { + switch self { + case .google: await getRequest(queryItems: [:], url: URL(forceString: "https://google.com")) + case .amazon: await getRequest(queryItems: [:], url: URL(forceString: "https://amazon.com")) + } + } + } +} diff --git a/Tests/MBAsyncNetworkingTests/MBAsyncNetworkingTests.swift b/Tests/MBAsyncNetworkingTests/MBAsyncNetworkingTests.swift deleted file mode 100644 index 1b256f5..0000000 --- a/Tests/MBAsyncNetworkingTests/MBAsyncNetworkingTests.swift +++ /dev/null @@ -1,6 +0,0 @@ -import Testing -@testable import MBAsyncNetworking - -@Test func example() async throws { - // Write your test here and use APIs like `#expect(...)` to check expected conditions. -} diff --git a/Tests/MBAsyncNetworkingTests/MultipleSceneTests.swift b/Tests/MBAsyncNetworkingTests/MultipleSceneTests.swift new file mode 100644 index 0000000..2eeaba8 --- /dev/null +++ b/Tests/MBAsyncNetworkingTests/MultipleSceneTests.swift @@ -0,0 +1,117 @@ +// +// MultipleSceneTests.swift +// MBAsyncNetworking +// +// Created by Raşid Ramazanov on 5.08.2025. +// + +import Foundation +import Testing +@testable import MBAsyncNetworking + +@MainActor struct MultipleSceneTests { + var sut: XViewController! + init() { + UserSession.initialize(with: NetworkingStorage()) + } + + @Test mutating func interactorFetch() async { + let presentationLogic = XPresentationLogicSpy() + sut = XViewController(presentationLogic: presentationLogic) + await sut.interactor.fetch() + #expect(presentationLogic.content != nil) + #expect(presentationLogic.error == nil) + } + + final class XPresentationLogicSpy: XPresentationLogic { + var viewController: (any MultipleSceneTests.XDisplayLogic)? + var content: Data? + var error: Error? + func present(data: Data) { + content = data + } + + func present(error: any Error) { + self.error = error + } + } + + final class XViewController: XDisplayLogic { + let interactor: XInteractor + var content: Data? + var errorMessage: String? + init() { + let presenter = XPresenter() + interactor = XInteractor(presenter: presenter) + presenter.viewController = self + } + + init(presentationLogic: XPresentationLogic) { + interactor = XInteractor(presenter: presentationLogic) + interactor.presenter.viewController = self + } + + func viewDidLoad() { + Task { + await interactor.fetch() + } + } + + func displayContent(_ data: Data) { + content = data + } + + func displayError(_ message: String) { + errorMessage = message + } + } + + protocol XDisplayLogic { + func displayContent(_ data: Data) + func displayError(_ message: String) + } + + final class XInteractor { + var presenter: XPresentationLogic + init(presenter: XPresentationLogic) { + self.presenter = presenter + } + + func fetch() async { + do { + let response: Data = try await TestAPI.google.fetch() + presenter.present(data: response) + } catch { + presenter.present(error: error) + } + } + } + + final class XPresenter: XPresentationLogic { + var viewController: XDisplayLogic? + func present(data: Data) { + viewController?.displayContent(data) + } + + func present(error: Error) { + viewController?.displayError(error.localizedDescription) + } + } + + protocol XPresentationLogic { + var viewController: XDisplayLogic? { get set } + func present(data: Data) + func present(error: Error) + } + + enum TestAPI: AsyncNetworkable { + case google + case amazon + func request() async -> URLRequest { + switch self { + case .google: await getRequest(queryItems: [:], url: URL(forceString: "https://google.com")) + case .amazon: await getRequest(queryItems: [:], url: URL(forceString: "https://amazon.com")) + } + } + } +} diff --git a/Tests/MBAsyncNetworkingTests/NetworkingStorage.swift b/Tests/MBAsyncNetworkingTests/NetworkingStorage.swift new file mode 100644 index 0000000..021a79f --- /dev/null +++ b/Tests/MBAsyncNetworkingTests/NetworkingStorage.swift @@ -0,0 +1,14 @@ +// +// NetworkingStorage.swift +// MBAsyncNetworking +// +// Created by Raşid Ramazanov on 5.08.2025. +// + +import Foundation +@testable import MBAsyncNetworking + +struct NetworkingStorage: NetworkingStorable { + var refreshToken: String? = UUID().uuidString + var accessToken: String? = UUID().uuidString +}