From a678d5eca2154a39b7e88c5e872a8e7e0896f5a7 Mon Sep 17 00:00:00 2001 From: Matthias Schmidt <0x107@pm.me> Date: Mon, 6 Aug 2018 13:14:37 +0200 Subject: [PATCH] Fix notification for initial value on new subscriptions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current implementation of the test „testVariableNotifiesInitialOnSubscribe“ gives a false positive result, because the inital value of the result variable is equal to the expected result. When the inital value is not equal to the initial value of the result variable the test fails. --- Snail/Unique.swift | 5 +++++ SnailTests/UniqueTests.swift | 23 +++++++++++++---------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/Snail/Unique.swift b/Snail/Unique.swift index b4f79b3..37d82f0 100644 --- a/Snail/Unique.swift +++ b/Snail/Unique.swift @@ -17,4 +17,9 @@ public class Unique: Variable { subject.on(.next(newValue)) } } + + public override init(_ value: T) { + super.init(value) + subject.on(.next(value)) + } } diff --git a/SnailTests/UniqueTests.swift b/SnailTests/UniqueTests.swift index a3e17b5..a00d890 100644 --- a/SnailTests/UniqueTests.swift +++ b/SnailTests/UniqueTests.swift @@ -16,10 +16,11 @@ class UniqueTests: XCTestCase { subject.value = "2" subject.value = "2" subject.value = "2" - XCTAssert(events[0] == "1") - XCTAssert(events[1] == "2") + XCTAssert(events[0] == nil) + XCTAssert(events[1] == "1") + XCTAssert(events[2] == "2") XCTAssert(subject.value == "2") - XCTAssert(events.count == 2) + XCTAssert(events.count == 3) } func testVariableNotifiesOnSubscribe() { @@ -35,14 +36,14 @@ class UniqueTests: XCTestCase { } func testVariableNotifiesInitialOnSubscribe() { - let subject = Unique(nil) + let subject = Unique("initial") var result: String? subject.asObservable().subscribe(onNext: { string in result = string }) - XCTAssertEqual(nil, result) + XCTAssertEqual("initial", result) } func testVariableHandlesEquatableArrays() { @@ -55,8 +56,9 @@ class UniqueTests: XCTestCase { subject.value = ["2", "1"] subject.value = ["2", "1"] subject.value = ["1", "2"] - XCTAssert(events[0] == ["2", "1"]) - XCTAssert(events[1] == ["1", "2"]) + XCTAssert(events[0] == ["1", "2"]) + XCTAssert(events[1] == ["2", "1"]) + XCTAssert(events[2] == ["1", "2"]) } func testVariableHandlesOptionalArrays() { @@ -69,8 +71,9 @@ class UniqueTests: XCTestCase { subject.value = nil subject.value = nil subject.value = ["1", "2"] - XCTAssert(events[0] == ["1", "2"]) - XCTAssert(events[1] == nil) - XCTAssert(events[2] == ["1", "2"]) + XCTAssert(events[0] == nil) + XCTAssert(events[1] == ["1", "2"]) + XCTAssert(events[2] == nil) + XCTAssert(events[3] == ["1", "2"]) } }