Skip to content

Commit

Permalink
Migrate some tests to swift-testing
Browse files Browse the repository at this point in the history
Some are more difficult and/or not really possible due to, for instance, swift-syntax not having done the utilities for them (see <swiftlang/swift-syntax#2720>).
  • Loading branch information
Frizlab committed Dec 10, 2024
1 parent 2f1abd5 commit 4bd9e00
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 30 deletions.
15 changes: 9 additions & 6 deletions Tests/GlobalConfTests/DocTests.swift
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
import Foundation
import XCTest
import Testing

/* No @testable import: we mostly test whether the architecture works; we must be as close as possible to a regular client’s use. */
import GlobalConfModule



/* We mostly just check the examples in the Readme compile. */
final class DocTests : XCTestCase {
struct DocTests {

@Test
func testDependencyInjectionUsage() {
Conf.setRootValue(DefaultDocService(), for: \.docService)
docService.doAmazingStuff()
XCTAssertTrue(true)
#expect(true)
}

@InjectedConf(\.docService)
var docService: DocService

@Test
func testConfUsage() {
XCTAssertEqual(Conf[\.docConf], 42)
XCTAssertEqual(Conf.docConf, 42)
#expect(Conf[\.docConf] == 42)
#expect(Conf.docConf == 42)
}

@Test
func testAutoInjectedUsage() {
XCTAssertNotNil(docAutoInjectedService)
#expect(docAutoInjectedService != nil)
}

@InjectedConf()
Expand Down
37 changes: 19 additions & 18 deletions Tests/GlobalConfTests/UsageMainActorTests.swift
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
import Foundation
import XCTest
import Testing

/* No @testable import: we mostly test whether the architecture works; we must be as close as possible to a regular client’s use. */
import GlobalConfModule



final class UsageMainActorTests : XCTestCase {
struct UsageMainActorTests {

@Test
@MainActor
func testUsingMainActorService() {
MainActor.preconditionIsolated()
let c = MainActorContainer()
c.mainActorService.printHello()
c.mainActorServiceFromKeyPath.printHello()
InjectedConf<MainActorService>.value.printHello()
XCTAssertTrue(c.mainActorService === Conf[\.mainActorService])
XCTAssertTrue(c.mainActorService === Conf[key: MainActorService.AutoInjectionKey.self])
XCTAssertTrue(c.mainActorService === Conf.rootValue(for: MainActorService.AutoInjectionKey.self))
XCTAssertTrue(c.mainActorService === InjectedConf<MainActorService>.value)
XCTAssertTrue(c.mainActorServiceFromKeyPath === InjectedConf<MainActorService>.value)
#expect(c.mainActorService === Conf[\.mainActorService])
#expect(c.mainActorService === Conf[key: MainActorService.AutoInjectionKey.self])
#expect(c.mainActorService === Conf.rootValue(for: MainActorService.AutoInjectionKey.self))
#expect(c.mainActorService === InjectedConf<MainActorService>.value)
#expect(c.mainActorServiceFromKeyPath === InjectedConf<MainActorService>.value)

XCTAssertTrue(c.otherMainActorService !== InjectedConf<MainActorService>.value)
#expect(c.otherMainActorService !== InjectedConf<MainActorService>.value)

let oldOtherActor = c.otherMainActorService
Conf.setRootValue(c.mainActorService, for: \.mainActorService2)
XCTAssertTrue(c.otherMainActorService === InjectedConf<MainActorService>.value)
#expect(c.otherMainActorService === InjectedConf<MainActorService>.value)

Conf.withValue(oldOtherActor, for: \.mainActorService2, operation: {
XCTAssertTrue(c.mainActorService === Conf[\.mainActorService])
XCTAssertTrue(c.otherMainActorService === oldOtherActor)
XCTAssertTrue(c.otherMainActorService !== InjectedConf<MainActorService>.value)
#expect(c.mainActorService === Conf[\.mainActorService])
#expect(c.otherMainActorService === oldOtherActor)
#expect(c.otherMainActorService !== InjectedConf<MainActorService>.value)
})

Conf.withValues{
$0[\.mainActorService2] = MainActorService()
}operation:{
XCTAssertTrue(c.otherMainActorService !== oldOtherActor)
XCTAssertTrue(c.otherMainActorService !== InjectedConf<MainActorService>.value)
#expect(c.otherMainActorService !== oldOtherActor)
#expect(c.otherMainActorService !== InjectedConf<MainActorService>.value)
}

InjectedConf<MainActorService>.withValue(MainActorService()){
XCTAssertTrue(c.otherMainActorService !== oldOtherActor)
XCTAssertTrue(c.otherMainActorService !== InjectedConf<MainActorService>.value)
#expect(c.otherMainActorService !== oldOtherActor)
#expect(c.otherMainActorService !== InjectedConf<MainActorService>.value)
}

XCTAssertTrue(c.otherMainActorService === InjectedConf<MainActorService>.value)
#expect(c.otherMainActorService === InjectedConf<MainActorService>.value)

InjectedConf<MainActorService>.rootValue = oldOtherActor
XCTAssertTrue(c.otherMainActorService !== c.mainActorService)
#expect(c.otherMainActorService !== c.mainActorService)
}

@MainActor
Expand Down
14 changes: 8 additions & 6 deletions Tests/GlobalConfTests/UsageTests.swift
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import Foundation
import XCTest
import Testing

/* No @testable import: we mostly test whether the architecture works; we must be as close as possible to a regular client’s use. */
import GlobalConfModule



final class UsageTests : XCTestCase {
struct UsageTests {

@Test
func testUsingNoActorService() {
noActorService.printHello()
noActorServiceFromKeyPath.printHello()
InjectedConf<NoActorService>.value.printHello()
XCTAssertTrue(noActorService === InjectedConf<NoActorService>.value)
XCTAssertTrue(noActorServiceFromKeyPath === InjectedConf<NoActorService>.value)
#expect(noActorService === InjectedConf<NoActorService>.value)
#expect(noActorServiceFromKeyPath === InjectedConf<NoActorService>.value)
}

@Test
func testUsingNoActorServiceWithFactory() {
XCTAssertTrue(noActorService === noActorService)
XCTAssertTrue(noActorFactoryService !== noActorFactoryService)
#expect(noActorService === noActorService)
#expect(noActorFactoryService !== noActorFactoryService)
}

@InjectedConf()
Expand Down

0 comments on commit 4bd9e00

Please sign in to comment.