Skip to content

Commit

Permalink
updated tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KazaiMazai committed Sep 18, 2024
1 parent 2d210fa commit cd61d9f
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 20 deletions.
38 changes: 34 additions & 4 deletions Tests/PureduxMacrosTests/InjectedStoreMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import XCTest
@testable import PureduxMacros

final class InjectedStoreMacroTests: XCTestCase {

private let macros = ["StoreEntry": DependencyInjectionMacro.self]

func testEnvironmentValue() {
func test_WhenStoreEntry_StoreInjectionKeyGenerated() {
let macros = ["StoreEntry": StoreInjectionMacro.self]

assertMacroExpansion(
"""
extension SharedStores {
Expand Down Expand Up @@ -43,5 +42,36 @@ final class InjectedStoreMacroTests: XCTestCase {
macros: macros
)
}

func test_WhenDependencyEntry_DepenencyInjectionKeyGenerated() {
let macros = ["DependencyEntry": DependencyInjectionMacro.self]

assertMacroExpansion(
"""
extension Dependencies {
@DependencyEntry var value = 10
}
""",
expandedSource:
"""
extension SharedStores {
var root {
get {
self [_ValueKey.self]
}
set {
self [_ValueKey.self] = newValue
}
}
private enum _ValueKey: DependencyKey {
nonisolated (unsafe) static var currentValue = 10
}
}
}
""",
macros: macros
)
}
}
#endif
36 changes: 27 additions & 9 deletions Tests/PureduxTests/InjectionTests/InjectionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,31 @@
//

import Foundation

import XCTest
@testable import Puredux

#if canImport(PureduxMacros)
extension SharedStores {
@StoreEntry var intValue: Int = 1
@StoreEntry var theStore = StateStore<Int, Int>(1) { _,_ in }
}

extension Injected {
@InjectEntry var anotherIntValue: Int = 1
}

final class InjectionTests: XCTestCase {
final class SharedStoresTests: XCTestCase {
func test_whenReadAndWriteToSharedStoresValue_NoDeadlockOccurs() {
SharedStores[\.intValue] = 1
let exp = expectation(description: "wait for store state")
SharedStores[\.theStore] = StateStore(2) {_,_ in }


SharedStores[\.intValue] = SharedStores[\.intValue] + 1
XCTAssertEqual(SharedStores[\.intValue], 2)

let store = SharedStores[\.theStore]
store.subscribe(observer: Observer { state in
XCTAssertEqual(state, 2)
exp.fulfill()
return .active
})

wait(for: [exp], timeout: 3.0)
}

func test_whenReadAndWriteToInjectedValue_NoDeadlockOccurs() {
Expand All @@ -36,4 +41,17 @@ final class InjectionTests: XCTestCase {

}
}
#endif

extension Dependencies {
@DependencyEntry var intValue: Int = 1
}

final class DependenciesTests: XCTestCase {
func test_whenReadAndWriteToSharedStoresValue_NoDeadlockOccurs() {
XCTAssertEqual(Dependency[\.intValue], 1)

Dependencies[\.intValue] = Dependencies[\.intValue] + 1
XCTAssertEqual(Dependencies[\.intValue], 2)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import XCTest
@testable import Puredux
import SwiftUI
import UIKit

import Dispatch

final class StoreViewAlwaysEqualDeduplicationPropsTests: XCTestCase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import XCTest
@testable import Puredux
import SwiftUI
import UIKit
import Dispatch

final class StoreViewContentRenderTests: XCTestCase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import XCTest
@testable import Puredux
import SwiftUI
import UIKit
import Dispatch

final class StoreViewDeduplicationPropsTests: XCTestCase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import XCTest
@testable import Puredux
import SwiftUI
import UIKit
import Dispatch

final class StoreViewNoDeduplicationPropsTests: XCTestCase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import XCTest
@testable import Puredux
import SwiftUI
import UIKit
import Dispatch

final class StoreViewPresentationQueueTest: XCTestCase {
Expand Down
3 changes: 1 addition & 2 deletions Tests/PureduxTests/TestUtils/TestRootAppStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
import XCTest
@testable import Puredux
import SwiftUI
import UIKit
import PureduxMacros

extension Stores {
extension SharedStores {
@InjectEntry var rootStore = StateStore<Int, Int>(0) { state, action in state += action }
}
#endif

0 comments on commit cd61d9f

Please sign in to comment.