Skip to content

Commit

Permalink
Add SExpPrinter (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle-Ye authored Oct 22, 2024
1 parent d7fb516 commit fc6ad55
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 0 deletions.
67 changes: 67 additions & 0 deletions Sources/OpenSwiftUICore/Util/SExpPrinter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// SExpPrinter.swift
// OpenSwiftUICore
//
// Audited for RELEASE_2024
// Status: Complete

package struct SExpPrinter {
var output: String
var depth: Int
package internal(set) var indent: String

package init(tag: String, singleLine: Bool = false) {
output = "(\(tag)"
depth = singleLine ? 0 : 1
indent = singleLine ? "" : " "
}

package mutating func end() -> String {
if depth == 0 {
output.append(")")
return output
} else {
depth -= 1
indent.removeLast(2)
output.append(")")
return output
}
}

package mutating func print(_ string: String, newline: Bool = true) {
if newline, depth != 0 {
output.append("\n\(indent)")
} else {
output.append(" ")
}
output.append(string)
}

package mutating func newline() {
guard depth != 0 else {
return
}
output.append("\n")
output.append(indent)
}

package mutating func push(_ tag: String) {
if depth == 0 {
output.append("(\(tag)")
} else {
output.append("\n\(indent)(\(tag)")
depth += 1
indent.append(" ")
}
}

package mutating func pop() {
if depth == 0 {
output.append(")")
} else {
depth -= 1
indent.removeLast(2)
output.append(")")
}
}
}
135 changes: 135 additions & 0 deletions Tests/OpenSwiftUICoreTests/Util/SExpPrinterTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
//
// SExpPrinterTests.swift
// OpenSwiftUICoreTests

import OpenSwiftUICore
import OpenGraphShims
import Testing

struct SExpPrinterTests {
@Test
func singleLine() {
var printer = SExpPrinter(tag: "(test", singleLine: true)
printer.push("A")
printer.push("B")
printer.pop()
printer.pop()
let end = printer.end()
#expect(end == "((test(A(B)))")
}

@Test
func multiLine() {
var printer = SExpPrinter(tag: "(test", singleLine: false)
printer.push("A")
printer.push("B")
printer.pop()
printer.pop()
let end = printer.end()
#expect(end == """
((test
(A
(B)))
""")
}

@Test
func newline() {
var printer = SExpPrinter(tag: "(test", singleLine: false)
printer.push("A")
printer.newline()
printer.push("B")
printer.newline()
printer.pop()
printer.pop()
let end = printer.end()
#expect(end == """
((test
(A
(B
)))
""")
printer.newline()
let newEnd = printer.end()
#expect(newEnd == """
((test
(A
(B
))))
""")
}

@Test
func printString() {
do {
var printer = SExpPrinter(tag: "(test", singleLine: false)
printer.push("A")
printer.print("B")
printer.print("C")
printer.pop()
let end = printer.end()
#expect(end == """
((test
(A
B
C))
""")
}
do {
var printer = SExpPrinter(tag: "(test", singleLine: false)
printer.push("A")
printer.print("B")
printer.pop()
_ = printer.end()
printer.print("C")
let end = printer.end()
#expect(end == """
((test
(A
B)) C)
""")
}
do {
var printer = SExpPrinter(tag: "(test", singleLine: true)
printer.push("A")
printer.print("B")
printer.print("C")
printer.pop()
let end = printer.end()
#expect(end == """
((test(A B C))
""")
}
do {
var printer = SExpPrinter(tag: "(test", singleLine: true)
printer.push("A")
printer.print("B")
printer.pop()
printer.print("C")
let end = printer.end()
#expect(end == """
((test(A B) C)
""")
}
}

@Test
func pushPop() {
var printer = SExpPrinter(tag: "(test", singleLine: false)
printer.push("A")
printer.push("B")
printer.pop()
printer.push("C")
printer.pop()
printer.pop()
let end = printer.end()
#expect(end == """
((test
(A
(B)
(C)))
""")
}
}

0 comments on commit fc6ad55

Please sign in to comment.