TextState: DebugOutputConvertible (#421)

* Fix debug text

* XML

* wip

* wip

* wip
This commit is contained in:
Stephen Celis
2021-03-10 17:56:13 -05:00
committed by GitHub
parent a116fff6d4
commit cb080b7478
2 changed files with 135 additions and 0 deletions

View File

@@ -302,3 +302,71 @@ extension LocalizedStringKey: CustomDebugOutputConvertible {
self.formatted().debugDescription self.formatted().debugDescription
} }
} }
extension TextState: CustomDebugOutputConvertible {
public var debugOutput: String {
func debugOutputHelp(_ textState: Self) -> String {
var output: String
switch textState.storage {
case let .concatenated(lhs, rhs):
output = debugOutputHelp(lhs) + debugOutputHelp(rhs)
case let .localized(key, tableName, bundle, comment):
output = key.formatted(tableName: tableName, bundle: bundle, comment: comment)
case let .verbatim(string):
output = string
}
for modifier in textState.modifiers {
switch modifier {
case let .baselineOffset(baselineOffset):
output = "<baseline-offset=\(baselineOffset)>\(output)</baseline-offset>"
case .bold, .fontWeight(.some(.bold)):
output = "**\(output)**"
case .font(.some):
break // TODO: capture Font description using DSL similar to TextState and print here
case let .fontWeight(.some(weight)):
func describe(weight: Font.Weight) -> String {
switch weight {
case .black: return "black"
case .bold: return "bold"
case .heavy: return "heavy"
case .light: return "light"
case .medium: return "medium"
case .regular: return "regular"
case .semibold: return "semibold"
case .thin: return "thin"
default: return "\(weight)"
}
}
output = "<font-weight=\(describe(weight: weight))>\(output)</font-weight>"
case let .foregroundColor(.some(color)):
output = "<foreground-color=\(color)>\(output)</foreground-color>"
case .italic:
output = "_\(output)_"
case let .kerning(kerning):
output = "<kerning=\(kerning)>\(output)</kerning>"
case let .strikethrough(active: true, color: .some(color)):
output = "<s color=\(color)>\(output)</s>"
case .strikethrough(active: true, color: .none):
output = "~~\(output)~~"
case let .tracking(tracking):
output = "<tracking=\(tracking)>\(output)</tracking>"
case let .underline(active: true, color):
output = "<u\(color.map { " color=\($0)" } ?? "")>\(output)</u>"
case .font(.none),
.fontWeight(.none),
.foregroundColor(.none),
.strikethrough(active: false, color: _),
.underline(active: false, color: _):
break
}
}
return output
}
return #"""
\#(Self.self)(
\#(debugOutputHelp(self).indent(by: 2))
)
"""#
}
}

View File

@@ -345,6 +345,73 @@ final class DebugTests: XCTestCase {
) )
} }
func testTextState() {
XCTAssertEqual(
debugOutput(
TextState("Hello, world!")
),
"""
TextState(
Hello, world!
)
"""
)
XCTAssertEqual(
debugOutput(
TextState("Hello, ")
+ TextState("world").bold().italic()
+ TextState("!")
),
"""
TextState(
Hello, _**world**_!
)
"""
)
XCTAssertEqual(
debugOutput(
TextState("Offset by 10.5").baselineOffset(10.5)
+ TextState("\n") + TextState("Headline").font(.headline)
+ TextState("\n") + TextState("No font").font(nil)
+ TextState("\n") + TextState("Light font weight").fontWeight(.light)
+ TextState("\n") + TextState("No font weight").fontWeight(nil)
+ TextState("\n") + TextState("Red").foregroundColor(.red)
+ TextState("\n") + TextState("No color").foregroundColor(nil)
+ TextState("\n") + TextState("Italic").italic()
+ TextState("\n") + TextState("Kerning of 2.5").kerning(2.5)
+ TextState("\n") + TextState("Stricken").strikethrough()
+ TextState("\n") + TextState("Stricken green").strikethrough(color: .green)
+ TextState("\n") + TextState("Not stricken blue").strikethrough(false, color: .blue)
+ TextState("\n") + TextState("Tracking of 5.5").tracking(5.5)
+ TextState("\n") + TextState("Underlined").underline()
+ TextState("\n") + TextState("Underlined pink").underline(color: .pink)
+ TextState("\n") + TextState("Not underlined purple").underline(false, color: .pink)
),
"""
TextState(
<baseline-offset=10.5>Offset by 10.5</baseline-offset>
Headline
No font
<font-weight=light>Light font weight</font-weight>
No font weight
<foreground-color=red>Red</foreground-color>
No color
_Italic_
<kerning=2.5>Kerning of 2.5</kerning>
~~Stricken~~
<s color=green>Stricken green</s>
Not stricken blue
<tracking=5.5>Tracking of 5.5</tracking>
<u>Underlined</u>
<u color=pink>Underlined pink</u>
Not underlined purple
)
"""
)
}
func testEffectOutput() { func testEffectOutput() {
// XCTAssertEqual( // XCTAssertEqual(
// Effect<Int, Never>(value: 42) // Effect<Int, Never>(value: 42)