Skip to content

Commit

Permalink
Fix Testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
humdrum committed Aug 1, 2024
1 parent 3119af7 commit 99c8d93
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
6 changes: 3 additions & 3 deletions Sources/Document/Json/JSONTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public struct JSONTreeElementNode: JSONTreeNode {
childrenString = self.children.compactMap { $0.toJSONString }.joined(separator: ",")
}

var resultString = "{\"type\":\(self.type.toJSONString),\"children\":[\(childrenString)]"
var resultString = "{"

if self.attributes.isEmpty == false {
let sortedKeys = self.attributes.keys.sorted()
Expand All @@ -118,10 +118,10 @@ public struct JSONTreeElementNode: JSONTreeNode {
}
}.joined(separator: ",")

resultString += ",\"attributes\":{\(attrsString)}"
resultString += "\"attributes\":{\(attrsString)},"
}

resultString += "}"
resultString += "\"children\":[\(childrenString)],\"type\":\(self.type.toJSONString)}"

return resultString
}
Expand Down
11 changes: 6 additions & 5 deletions Tests/Integration/TreeIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,22 +85,22 @@ final class TreeIntegrationTests: XCTestCase {
root.t = JSONTree()
try (root.t as? JSONTree)?.edit(0, 0, JSONTreeElementNode(type: "p"))
XCTAssertEqual((root.t as? JSONTree)?.toXML(), /* html */ "<root><p></p></root>")
XCTAssertEqual(root.toJSON(), "{\"t\":{\"type\":\"root\",\"children\":[{\"type\":\"p\",\"children\":[]}]}}")
XCTAssertEqual(root.toJSON(), "{\"t\":{\"children\":[{\"children\":[],\"type\":\"p\"}],\"type\":\"root\"}}")

// 02. Create a text into the paragraph.
try (root.t as? JSONTree)?.edit(1, 1, JSONTreeTextNode(value: "AB"))
XCTAssertEqual((root.t as? JSONTree)?.toXML(), /* html */ "<root><p>AB</p></root>")
XCTAssertEqual(root.toJSON(), "{\"t\":{\"type\":\"root\",\"children\":[{\"type\":\"p\",\"children\":[{\"type\":\"text\",\"value\":\"AB\"}]}]}}")
XCTAssertEqual(root.toJSON(), "{\"t\":{\"children\":[{\"children\":[{\"type\":\"text\",\"value\":\"AB\"}],\"type\":\"p\"}],\"type\":\"root\"}}")

// 03. Insert a text into the paragraph.
try (root.t as? JSONTree)?.edit(3, 3, JSONTreeTextNode(value: "CD"))
XCTAssertEqual((root.t as? JSONTree)?.toXML(), /* html */ "<root><p>ABCD</p></root>")
XCTAssertEqual(root.toJSON(), "{\"t\":{\"type\":\"root\",\"children\":[{\"type\":\"p\",\"children\":[{\"type\":\"text\",\"value\":\"AB\"},{\"type\":\"text\",\"value\":\"CD\"}]}]}}")
XCTAssertEqual(root.toJSON(), "{\"t\":{\"children\":[{\"children\":[{\"type\":\"text\",\"value\":\"AB\"},{\"type\":\"text\",\"value\":\"CD\"}],\"type\":\"p\"}],\"type\":\"root\"}}")

// 04. Replace ABCD with Yorkie
try (root.t as? JSONTree)?.edit(1, 5, JSONTreeTextNode(value: "Yorkie"))
XCTAssertEqual((root.t as? JSONTree)?.toXML(), /* html */ "<root><p>Yorkie</p></root>")
XCTAssertEqual(root.toJSON(), "{\"t\":{\"type\":\"root\",\"children\":[{\"type\":\"p\",\"children\":[{\"type\":\"text\",\"value\":\"Yorkie\"}]}]}}")
XCTAssertEqual(root.toJSON(), "{\"t\":{\"children\":[{\"children\":[{\"type\":\"text\",\"value\":\"Yorkie\"}],\"type\":\"p\"}],\"type\":\"root\"}}")
}
}

Expand Down Expand Up @@ -931,7 +931,8 @@ final class TreeIntegrationStyleTests: XCTestCase {
try (root.t as? JSONTree)?.styleByPath([0, 0, 0], ["z": "m"])
XCTAssertEqual((root.t as? JSONTree)?.toXML(), /* html */ "<doc><tc><p a=\"b\" c=\"q\"><tn z=\"m\"></tn></p></tc></doc>")

XCTAssertEqual(root.toJSON(), /* html */ "{\"t\":{\"type\":\"doc\",\"children\":[{\"type\":\"tc\",\"children\":[{\"type\":\"p\",\"children\":[{\"type\":\"tn\",\"children\":[],\"attributes\":{\"z\":\"m\"}}],\"attributes\":{\"a\":\"b\",\"c\":\"q\"}}]}]}}")
XCTAssertEqual(root.toJSON(), /* html */
"{\"t\":{\"children\":[{\"children\":[{\"attributes\":{\"a\":\"b\",\"c\":\"q\"},\"children\":[{\"attributes\":{\"z\":\"m\"},\"children\":[],\"type\":\"tn\"}],\"type\":\"p\"}],\"type\":\"tc\"}],\"type\":\"doc\"}}")
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/Unit/Document/Json/JONSTreeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class JONSTreeTests: XCTestCase {
"point": ["x": 100, "y": 200]
])

XCTAssertEqual(elementNode.toJSONString, "{\"type\":\"doc\",\"children\":[{\"type\":\"p\",\"children\":[]},{\"type\":\"text\",\"value\":\"Y\"}],\"attributes\":{\"boolean\":true,\"doubleValue\":10.5,\"intValue\":100,\"point\":{\"x\":100,\"y\":200},\"string\":\"testString\"}}")
XCTAssertEqual(elementNode.toJSONString, "{\"attributes\":{\"boolean\":true,\"doubleValue\":10.5,\"intValue\":100,\"point\":{\"x\":100,\"y\":200},\"string\":\"testString\"},\"children\":[{\"children\":[],\"type\":\"p\"},{\"type\":\"text\",\"value\":\"Y\"}],\"type\":\"doc\"}")
}

func test_json_newline_string() throws {
Expand Down

0 comments on commit 99c8d93

Please sign in to comment.