Skip to content

Commit

Permalink
feat(counter): support += and -= operators on Counter automerge#4
Browse files Browse the repository at this point in the history
  • Loading branch information
kkostov committed Jun 23, 2022
1 parent 8bcd4ca commit 73693b1
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
53 changes: 52 additions & 1 deletion Source/Proxy/Proxy+Counter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Counter> {
/// 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)
}
}

48 changes: 48 additions & 0 deletions Tests/Proxy+CounterTests.swift
Original file line number Diff line number Diff line change
@@ -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))
}
}

0 comments on commit 73693b1

Please sign in to comment.