Skip to content

Commit

Permalink
#20: Alternative test to guard against issue #20
Browse files Browse the repository at this point in the history
  • Loading branch information
KyNorthstar committed Jul 30, 2020
1 parent 62bff2f commit 77f0acb
Showing 1 changed file with 60 additions and 39 deletions.
99 changes: 60 additions & 39 deletions Tests/LazyContainersTests/LazyContainersTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,27 @@ import XCTest



var sideEffect: String?

var sideEffectA: String? = nil
func makeLazyA() -> String {
sideEffect = "Side effect A"
sideEffectA = "Side effect A1"
return "lAzy"
}

var sideEffectB: String? = nil
func makeLazyB() -> String {
sideEffectB = "Side effect B"
return "Lazy B (this time with side-effects)"
}



final class LazyContainersTests: XCTestCase {

@Lazy(initializer: makeLazyA)
var lazyInitWithPropertyWrapper: String
var lazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect: String

var lazyInitTraditionally = Lazy<String>() {
sideEffect = "Side effect B"
sideEffectA = "Side effect A2"
return "lazy B"
}

Expand All @@ -43,7 +48,8 @@ final class LazyContainersTests: XCTestCase {


override func setUp() {
sideEffect = nil
sideEffectA = nil
sideEffectB = nil
}


Expand All @@ -55,40 +61,55 @@ final class LazyContainersTests: XCTestCase {

// MARK: - `Lazy`

func testLazyInitWithPropertyWrapper() {
XCTAssertEqual(sideEffect, nil)
XCTAssertFalse(_lazyInitWithPropertyWrapper.isInitialized)
XCTAssertEqual(sideEffect, nil)
XCTAssertEqual("lAzy", lazyInitWithPropertyWrapper)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertTrue(_lazyInitWithPropertyWrapper.isInitialized)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertEqual("lAzy", lazyInitWithPropertyWrapper)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertTrue(_lazyInitWithPropertyWrapper.isInitialized)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertEqual("lAzy", lazyInitWithPropertyWrapper)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertTrue(_lazyInitWithPropertyWrapper.isInitialized)
XCTAssertEqual(sideEffect, "Side effect A")
func testLazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect() {
XCTAssertEqual(sideEffectA, nil)
XCTAssertFalse(_lazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect.isInitialized)
XCTAssertEqual(sideEffectA, nil)
XCTAssertEqual("lAzy", lazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect)
XCTAssertEqual(sideEffectA, "Side effect A1")
XCTAssertTrue(_lazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect.isInitialized)
XCTAssertEqual(sideEffectA, "Side effect A1")
XCTAssertEqual("lAzy", lazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect)
XCTAssertEqual(sideEffectA, "Side effect A1")
XCTAssertTrue(_lazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect.isInitialized)
XCTAssertEqual(sideEffectA, "Side effect A1")
XCTAssertEqual("lAzy", lazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect)
XCTAssertEqual(sideEffectA, "Side effect A1")
XCTAssertTrue(_lazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect.isInitialized)
XCTAssertEqual(sideEffectA, "Side effect A1")

lazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect = "MAnual"

XCTAssertEqual(sideEffectA, "Side effect A1")
XCTAssertTrue(_lazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect.isInitialized)
XCTAssertEqual(sideEffectA, "Side effect A1")
XCTAssertEqual("MAnual", lazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect)
XCTAssertEqual(sideEffectA, "Side effect A1")
XCTAssertTrue(_lazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect.isInitialized)
XCTAssertEqual(sideEffectA, "Side effect A1")
XCTAssertEqual("MAnual", lazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect)
XCTAssertEqual(sideEffectA, "Side effect A1")
XCTAssertTrue(_lazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect.isInitialized)
XCTAssertEqual(sideEffectA, "Side effect A1")
XCTAssertEqual("MAnual", lazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect)
XCTAssertEqual(sideEffectA, "Side effect A1")
XCTAssertTrue(_lazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect.isInitialized)
XCTAssertEqual(sideEffectA, "Side effect A1")
}


func testLazyInitWithPropertyWrapperAndSideEffect() {

struct Test {
@Lazy
var lazyInitWithPropertyWrapperAndSideEffect = makeLazyB()
}


lazyInitWithPropertyWrapper = "MAnual"
let test = Test()

XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertTrue(_lazyInitWithPropertyWrapper.isInitialized)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertEqual("MAnual", lazyInitWithPropertyWrapper)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertTrue(_lazyInitWithPropertyWrapper.isInitialized)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertEqual("MAnual", lazyInitWithPropertyWrapper)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertTrue(_lazyInitWithPropertyWrapper.isInitialized)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertEqual("MAnual", lazyInitWithPropertyWrapper)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertTrue(_lazyInitWithPropertyWrapper.isInitialized)
XCTAssertEqual(sideEffect, "Side effect A")
XCTAssertNil(sideEffectB, "@Lazy eagerly evaluated its initial value")
XCTAssertEqual(test.lazyInitWithPropertyWrapperAndSideEffect, "Lazy B (this time with side-effects)")
}


Expand Down Expand Up @@ -251,7 +272,7 @@ final class LazyContainersTests: XCTestCase {
}

static var allTests = [
("testLazyInitWithPropertyWrapper", testLazyInitWithPropertyWrapper),
("testLazyInitWithPropertyWrapperWithCustomInitializerAndSideEffect", testLazyInitWithPropertyWrapperAndCustomInitializerWithSideEffect),
("testLazyInitTraditionally", testLazyInitTraditionally),

("testResettableLazyInitWithPropertyWrapper", testResettableLazyInitWithPropertyWrapper),
Expand Down

0 comments on commit 77f0acb

Please sign in to comment.