Skip to content

Commit

Permalink
Fixed undo error for name changes on a child
Browse files Browse the repository at this point in the history
  • Loading branch information
Rien committed Jul 12, 2018
1 parent 10c7153 commit 8f988b3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 13 deletions.
39 changes: 29 additions & 10 deletions Sources/VJson/Name.swift
Expand Up @@ -3,7 +3,7 @@
// File: Name.swift
// Project: VJson
//
// Version: 0.13.0
// Version: 0.13.3
//
// Author: Marinus van der Lugt
// Company: http://balancingrock.nl
Expand Down Expand Up @@ -50,6 +50,7 @@
//
// History
//
// 0.13.3 - Fixed problem with undo support for name changes of a child
// 0.13.0 - Improved unicode character support.
// 0.10.8 - Split off from VJson.swift
// =====================================================================================================================
Expand All @@ -70,12 +71,16 @@ public extension VJson {
set {
if let newName = newValue?.stringToJsonString() {
if newName != name {
recordUndoRedoAction()
recordUndoRedoAction(newName)
name = newName
}
} else {
recordUndoRedoAction()
name = nil
if let parent = parent, parent.isObject {
// Cannot remove name
} else {
recordUndoRedoAction()
name = nil
}
}
}
}
Expand All @@ -90,12 +95,16 @@ public extension VJson {
set {
if let newName = newValue?.stringToJsonString(lut: printableJsonStringSequenceLUT) {
if newName != name {
recordUndoRedoAction()
recordUndoRedoAction(newName)
name = newName
}
} else {
recordUndoRedoAction()
name = nil
if let parent = parent, parent.isObject {
// Cannot remove name
} else {
recordUndoRedoAction()
name = nil
}
}
}
}
Expand All @@ -107,9 +116,19 @@ public extension VJson {

public var nameValueRaw: String? {
get { return name }
set { if name != newValue {
recordUndoRedoAction()
name = newValue
set {
if let newName = newValue {
if newName != name {
recordUndoRedoAction(newName)
name = newName
}
} else {
if let parent = parent, parent.isObject {
// Cannot remove name
} else {
recordUndoRedoAction()
name = nil
}
}
}
}
Expand Down
16 changes: 13 additions & 3 deletions Sources/VJson/UndoRedo.swift
Expand Up @@ -3,7 +3,7 @@
// File: UndoRedo.swift
// Project: VJson
//
// Version: 0.13.0
// Version: 0.13.3
//
// Author: Marinus van der Lugt
// Company: http://balancingrock.nl
Expand Down Expand Up @@ -50,6 +50,7 @@
//
// History
//
// 0.13.3 - Fixed problem with undo support for name changes of a child
// 0.13.0 - Added escape sequences support
// 0.12.5 - Made location public
// 0.11.3 - Search for lowest undo manager in the VJson hierarchy.
Expand Down Expand Up @@ -193,13 +194,22 @@ extension VJson {
}


internal func recordUndoRedoAction() {
internal func recordUndoRedoAction(_ forNewName: String? = nil) {

if let undoManager = undoManager {

if #available(OSX 10.11, *) {

if let (root, path) = self.location() {
if let (root, lpath) = self.location() {

var path = lpath

// If the name will be changed, be sure to use the new name in the path
if let newName = forNewName {
if path.count >= 1 {
path[path.count - 1] = newName
}
}

let clone = undoRedoClone()

Expand Down
49 changes: 49 additions & 0 deletions Tests/VJsonTests/Bugfixes.swift
Expand Up @@ -41,4 +41,53 @@ class Bugfixes: XCTestCase {

XCTAssertEqual(json?.children?.items[0].nameValueRaw, "th\\uD83D\\uDE00ree")
}

func testBugfix_0_13_3() {

// This test will fail (crash on assert) with < 0.13.3

let json = VJson()
json["name"] &= "content"
let child = json["name"]
XCTAssertEqual(child.nameValue, "name")

json.undoManager = UndoManager()

child.nameValue = "new"
XCTAssertEqual(child.nameValue, "new")

json.undoManager?.undo()

XCTAssertEqual(child.nameValue, "name")


// Extra test on removing a name from root object

let root = VJson("content", name: "name")

root.undoManager = UndoManager()

XCTAssertEqual(root.nameValue, "name")
root.nameValue = nil
XCTAssertNil(root.nameValue)

root.undoManager?.undo()

XCTAssertEqual(root.nameValue, "name")


// Extra test on removing a name from a child object

let array = VJson.array()
let element = VJson("content", name: "name")
array.append(element)
XCTAssertEqual(array[0].nameValue, "name")
array.undoManager = UndoManager()

array[0].nameValue = nil
XCTAssertNil(array[0].nameValue)

array.undoManager?.undo()
XCTAssertEqual(array[0].nameValue, "name")
}
}

0 comments on commit 8f988b3

Please sign in to comment.