Skip to content

Commit

Permalink
feat: new public getters canUndo/canRedo
Browse files Browse the repository at this point in the history
+ better test coverage for internal state getters
  • Loading branch information
perfaram committed Feb 25, 2023
1 parent a1b26e1 commit 2fb8fe5
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Sources/GRDBUndoRedo/UndoRedoManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,16 @@ extension UndoRedoManager {
return _undoState.freeze >= 0
}
}

public var canUndo: Bool {
get {
return !_undoState.undoStack.isEmpty
}
}

public var canRedo: Bool {
get {
return !_undoState.redoStack.isEmpty
}
}
}
33 changes: 33 additions & 0 deletions Tests/GRDBUndoRedoTests/GRDBUndoRedoTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ final class UndoRedoManagerTests: XCTestCase {
XCTAssertEqual(state, undoRedo._undoState)
}

func testActiveGetterConsistency() throws {
undoRedo = try UndoRedoManager(recordTypes: Tbl1.self, db: dbQueue)

XCTAssertEqual(undoRedo.isActive, undoRedo._undoState.active)
try undoRedo.deactivate()
XCTAssertEqual(undoRedo.isActive, undoRedo._undoState.active)
}

func testFreeze() throws {
undoRedo = try UndoRedoManager(recordTypes: Tbl1.self, db: dbQueue)
XCTAssertEqual(undoRedo._undoState.freeze, -1)
Expand Down Expand Up @@ -235,6 +243,16 @@ final class UndoRedoManagerTests: XCTestCase {
}
}

func testCanUndoGetterConsistency() throws {
undoRedo = try UndoRedoManager(recordTypes: Tbl1.self, db: dbQueue)
XCTAssertFalse(undoRedo.canUndo)

let newItems = [Tbl1(a: 404)]
try insertDummies(newItems)
XCTAssertTrue(try undoRedo.barrier())
XCTAssertTrue(undoRedo.canUndo)
}

func testUndoInsertOne() throws {
undoRedo = try UndoRedoManager(recordTypes: Tbl1.self, db: dbQueue)

Expand Down Expand Up @@ -401,6 +419,21 @@ final class UndoRedoManagerTests: XCTestCase {
}
}

func testCanRedoGetterConsistency() throws {
undoRedo = try UndoRedoManager(recordTypes: Tbl1.self, db: dbQueue)
XCTAssertFalse(undoRedo.canRedo)

let newItems = [Tbl1(a: 404)]
try insertDummies(newItems)
XCTAssertTrue(try undoRedo.barrier())
XCTAssertFalse(undoRedo.canRedo)

try undoRedo.perform(.undo)
XCTAssertTrue(undoRedo.canRedo)
try undoRedo.perform(.redo)
XCTAssertFalse(undoRedo.canRedo)
}

func testRedoInsert() throws {
undoRedo = try UndoRedoManager(recordTypes: Tbl1.self, db: dbQueue)
let newItems = [Tbl1(a: 23)]
Expand Down

0 comments on commit 2fb8fe5

Please sign in to comment.