From ea1fa1eb1a72042f3aa7a892cd9dcdad919479b4 Mon Sep 17 00:00:00 2001 From: Russell Stephens Date: Tue, 4 Sep 2018 13:07:28 -0400 Subject: [PATCH 1/3] separate map on variable and unique --- Snail/Unique.swift | 6 ++++++ Snail/Variable.swift | 4 ++-- SnailTests/VariableTests.swift | 11 +++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Snail/Unique.swift b/Snail/Unique.swift index 37d82f0..9e4b19b 100644 --- a/Snail/Unique.swift +++ b/Snail/Unique.swift @@ -22,4 +22,10 @@ public class Unique: Variable { super.init(value) subject.on(.next(value)) } + + public override func map(transform: @escaping (T) -> U) -> Unique { + let newVariable = Unique(transform(value)) + asObservable().subscribe(onNext: { _ in newVariable.value = transform(self.value) }) + return newVariable + } } diff --git a/Snail/Variable.swift b/Snail/Variable.swift index 56376c3..57bb92e 100644 --- a/Snail/Variable.swift +++ b/Snail/Variable.swift @@ -31,8 +31,8 @@ public class Variable { return subject } - public func map(transform: @escaping (T) -> U) -> Unique { - let newVariable = Unique(transform(value)) + public func map(transform: @escaping (T) -> U) -> Variable { + let newVariable = Variable(transform(value)) asObservable().subscribe(onNext: { _ in newVariable.value = transform(self.value) }) return newVariable } diff --git a/SnailTests/VariableTests.swift b/SnailTests/VariableTests.swift index 7530d9d..5fe22a6 100644 --- a/SnailTests/VariableTests.swift +++ b/SnailTests/VariableTests.swift @@ -64,4 +64,15 @@ class VariableTests: XCTestCase { XCTAssertEqual(subject.value.count, subjectCharactersCount) } + + func testMapToVoid() { + let subject = Variable("initial") + var fired = false + + subject.map { _ in return () }.asObservable().subscribe(onNext: { _ in + fired = true + }) + + XCTAssertTrue(fired) + } } From 50900697dc8217de92b585fb87597f3d667c6f31 Mon Sep 17 00:00:00 2001 From: Russell Stephens Date: Tue, 4 Sep 2018 13:17:33 -0400 Subject: [PATCH 2/3] add unique map test --- SnailTests/VariableTests.swift | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/SnailTests/VariableTests.swift b/SnailTests/VariableTests.swift index 5fe22a6..e2b2f9e 100644 --- a/SnailTests/VariableTests.swift +++ b/SnailTests/VariableTests.swift @@ -65,6 +65,19 @@ class VariableTests: XCTestCase { XCTAssertEqual(subject.value.count, subjectCharactersCount) } + func testUniqueFireCounts() { + let subject = Unique("one") + var firedCount = 0 + + subject.map { $0.count }.asObservable().subscribe(onNext: { _ in + firedCount += 1 + }) + + subject.value = "two" + + XCTAssertTrue(firedCount == 1) + } + func testMapToVoid() { let subject = Variable("initial") var fired = false From 2d2b156930d4d9cda74763ae12a8cb46b4670062 Mon Sep 17 00:00:00 2001 From: Russell Stephens Date: Tue, 4 Sep 2018 13:33:25 -0400 Subject: [PATCH 3/3] add test for variable map --- SnailTests/VariableTests.swift | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/SnailTests/VariableTests.swift b/SnailTests/VariableTests.swift index e2b2f9e..d6c596a 100644 --- a/SnailTests/VariableTests.swift +++ b/SnailTests/VariableTests.swift @@ -78,6 +78,19 @@ class VariableTests: XCTestCase { XCTAssertTrue(firedCount == 1) } + func testVariableFireCounts() { + let subject = Variable("one") + var firedCount = 0 + + subject.map { $0.count }.asObservable().subscribe(onNext: { _ in + firedCount += 1 + }) + + subject.value = "two" + + XCTAssertTrue(firedCount == 2) + } + func testMapToVoid() { let subject = Variable("initial") var fired = false