From 73693b184ca950a31bcdc3577e1b5c75dbaa66ac Mon Sep 17 00:00:00 2001 From: Konstantin Kostov Date: Thu, 23 Jun 2022 15:35:55 +0200 Subject: [PATCH] feat(counter): support += and -= operators on Counter #4 --- Source/Proxy/Proxy+Counter.swift | 53 +++++++++++++++++++++++++++++++- Tests/Proxy+CounterTests.swift | 48 +++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 Tests/Proxy+CounterTests.swift diff --git a/Source/Proxy/Proxy+Counter.swift b/Source/Proxy/Proxy+Counter.swift index 927f1af..6f04302 100644 --- a/Source/Proxy/Proxy+Counter.swift +++ b/Source/Proxy/Proxy+Counter.swift @@ -20,8 +20,59 @@ public extension Proxy where Wrapped == Counter { /// Decrements the counter by the value you provide. /// - Parameter delta: The amount to decrement the counter, defaults to `1`. func decrement(_ delta: Int = -1) { - increment(delta) + increment(delta) } + + /// Subtracts the second value from the counter by performing a `decrement`. + /// + /// - Parameters: + /// - lhs: A Counter + /// - rhs: The value to subtract from `lhs`. + static func -=(lhs: Proxy, rhs: Int) { + lhs.decrement(-rhs) + } + + /// Adds the second value to the counter by performing an `increment`. + /// + /// - Parameters: + /// - lhs: A Counter + /// - rhs: The value to add to `lhs`. + static func +=(lhs: Proxy, rhs: Int) { + lhs.increment(rhs) + } +} +public extension Proxy where Wrapped == Optional { + /// Increments the counter by the value you provide. + /// - Parameter delta: The amount to increment the counter, defaults to `1`. + func increment(_ delta: Int = 1) { + var path = self.path + let pathComponent = path.popLast() + context.increment(path: path, key: pathComponent!.key, delta: delta) + } + + /// Decrements the counter by the value you provide. + /// - Parameter delta: The amount to decrement the counter, defaults to `1`. + func decrement(_ delta: Int = -1) { + increment(delta) + } + + /// Subtracts the second value from the counter by performing a `decrement`. + /// + /// - Parameters: + /// - lhs: A Counter + /// - rhs: The value to subtract from `lhs`. + static func -=(lhs: Proxy, rhs: Int) { + lhs.decrement(-rhs) + } + + /// Adds the second value to the counter by performing an `increment`. + /// + /// - Parameters: + /// - lhs: A Counter + /// - rhs: The value to add to `lhs`. + static func +=(lhs: Proxy, rhs: Int) { + lhs.increment(rhs) + } } diff --git a/Tests/Proxy+CounterTests.swift b/Tests/Proxy+CounterTests.swift new file mode 100644 index 0000000..ea38d38 --- /dev/null +++ b/Tests/Proxy+CounterTests.swift @@ -0,0 +1,48 @@ +// +// File.swift +// +// +// Created by Konstantin Kostov on 23/06/2022. +// + +import Foundation +import XCTest +@testable import Automerge + +class ProxyCounterTests: XCTestCase { + func testAssignmentExpressions() { + struct Schema: Codable, Equatable { + var counter: Counter? + var notOptionalCounter: Counter = 3 + } + + var doc1 = Document(Schema()) + let _ = doc1.change { + $0.counter?.set(3) + XCTAssertEqual($0.counter?.get(), 3) + } + + let _ = doc1.change { + $0.counter -= 1 + $0.notOptionalCounter -= 1 + XCTAssertEqual($0.counter?.get(), 2) + XCTAssertEqual($0.notOptionalCounter.get(), 2) + } + + let _ = doc1.change { + $0.counter += 2 + $0.notOptionalCounter += 2 + XCTAssertEqual($0.counter?.get(), 4) + XCTAssertEqual($0.notOptionalCounter.get(), 4) + } + + let _ = doc1.change { + $0.counter += -2 + $0.notOptionalCounter -= 2 + XCTAssertEqual($0.counter?.get(), 2) + XCTAssertEqual($0.notOptionalCounter.get(), 2) + } + + XCTAssertEqual(doc1.content, Schema(counter: 2, notOptionalCounter: 2)) + } +}