Skip to content

Commit

Permalink
Add the ability to visualise the test canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
danielctull committed Dec 17, 2024
2 parents b74605c + e7c3421 commit 4ed49ee
Show file tree
Hide file tree
Showing 22 changed files with 112 additions and 35 deletions.
8 changes: 4 additions & 4 deletions Sources/TerminalUI/Primitives/Dimensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ extension Horizontal: ExpressibleByIntegerLiteral {

extension Horizontal: Strideable {
public func advanced(by n: Int) -> Horizontal {
Horizontal(value: value + n)
Horizontal(value: value.advanced(by: n))
}

public func distance(to other: Horizontal) -> Int {
value - other.value
value.distance(to: other.value)
}
}

Expand Down Expand Up @@ -87,11 +87,11 @@ extension Vertical: ExpressibleByIntegerLiteral {

extension Vertical: Strideable {
public func advanced(by n: Int) -> Vertical {
Vertical(value: value + n)
Vertical(value: value.advanced(by: n))
}

public func distance(to other: Vertical) -> Int {
value - other.value
value.distance(to: other.value)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/TerminalUI/Primitives/Pixel.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

public struct Pixel: Equatable {

let content: Character
package let content: Character
let foreground: Color
let background: Color
let bold: Bold
Expand Down
8 changes: 6 additions & 2 deletions Sources/TerminalUI/Primitives/Position.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@

public struct Position: Equatable, Hashable {
let x: Horizontal
let y: Vertical
package let x: Horizontal
package let y: Vertical

public init(x: Horizontal, y: Vertical) {
self.x = x
self.y = y
}
}

extension Position {
package static var origin: Position { Position(x: 1, y: 1) }
}

extension Position {
var controlSequence: ControlSequence {
"\(y);\(x)H"
Expand Down
4 changes: 2 additions & 2 deletions Sources/TerminalUI/Primitives/Size.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import Foundation

package struct Size {

let width: Horizontal
let height: Vertical
package let width: Horizontal
package let height: Vertical

package init(width: Horizontal, height: Vertical) {
self.width = width
Expand Down
2 changes: 1 addition & 1 deletion Sources/TerminalUI/Views/Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public struct Text: Builtin, View {
hidden: environment.hidden,
strikethrough: environment.strikethrough
)
canvas.draw(pixel, at: Position(x: Horizontal(index), y: 0))
canvas.draw(pixel, at: Position(x: Horizontal(index), y: 1))
}
}
}
23 changes: 23 additions & 0 deletions Sources/TerminalUITesting/TestCanvas.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,26 @@ extension TestCanvas {
content()._render(in: self, size: size)
}
}

extension TestCanvas: CustomStringConvertible {

public var description: String {

let origin = Position.origin
let ys = (origin.y...size.height)
let xs = (origin.x...size.width)

var characters: [[Character]] = ys.map { _ in xs.map { _ in " " } }

for (position, pixel) in pixels {
guard ys.contains(position.y) && xs.contains(position.x) else { continue }
let y = origin.y.distance(to: position.y)
let x = origin.x.distance(to: position.x)
characters[y][x] = pixel.content
}

return characters
.map { String($0) }
.joined(separator: "\n")
}
}
4 changes: 2 additions & 2 deletions Tests/TerminalUITests/AppTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct AppTests {
"[27m", // Inverse off
"[28m", // Hidden off
"[29m", // Strikethrough off
"[0;1Ha", // Position + content
"[1;1Ha", // Position + content
])
}

Expand Down Expand Up @@ -63,7 +63,7 @@ struct AppTests {
"[27m", // Inverse off
"[28m", // Hidden off
"[29m", // Strikethrough off
"[0;1Ha", // Position + content
"[1;1Ha", // Position + content
])
}
}
16 changes: 8 additions & 8 deletions Tests/TerminalUITests/EnvironmentTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ struct EnvironmentTests {
}

#expect(canvas.pixels == [
Position(x: 1, y: 0): Pixel("d"),
Position(x: 2, y: 0): Pixel("e"),
Position(x: 3, y: 0): Pixel("f"),
Position(x: 4, y: 0): Pixel("a"),
Position(x: 5, y: 0): Pixel("u"),
Position(x: 6, y: 0): Pixel("l"),
Position(x: 7, y: 0): Pixel("t"),
Position(x: 1, y: 1): Pixel("d"),
Position(x: 2, y: 1): Pixel("e"),
Position(x: 3, y: 1): Pixel("f"),
Position(x: 4, y: 1): Pixel("a"),
Position(x: 5, y: 1): Pixel("u"),
Position(x: 6, y: 1): Pixel("l"),
Position(x: 7, y: 1): Pixel("t"),
])
}

Expand All @@ -33,7 +33,7 @@ struct EnvironmentTests {
}

#expect(canvas.pixels == [
Position(x: 1, y: 0): Pixel("b"),
Position(x: 1, y: 1): Pixel("b"),
])
}
}
Expand Down
20 changes: 20 additions & 0 deletions Tests/TerminalUITests/Primitives/DimensionsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ import Testing
#expect(horizontals.next() == 3)
#expect(horizontals.next() == nil)
}

@Test("Strideable: advanced(by:)", arguments: [-1, 0, 1], [-1, 0, 1])
func strideable_advancedBy(start: Int, n: Int) {
#expect(Horizontal(start).advanced(by: n) == Horizontal(start.advanced(by: n)))
}

@Test("Strideable: distance(to:)", arguments: [-1, 0, 1], [-1, 0, 1])
func strideable_distanceTo(x: Int, y: Int) {
#expect(Horizontal(x).distance(to: Horizontal(y)) == x.distance(to: y))
}

@Test("init(some BinaryInteger)")
func initBinaryInteger() {
Expand Down Expand Up @@ -85,6 +95,16 @@ import Testing
#expect(verticals.next() == nil)
}

@Test("Strideable: advanced(by:)", arguments: [-1, 0, 1], [-1, 0, 1])
func strideable_advancedBy(start: Int, n: Int) {
#expect(Vertical(start).advanced(by: n) == Vertical(start.advanced(by: n)))
}

@Test("Strideable: distance(to:)", arguments: [-1, 0, 1], [-1, 0, 1])
func strideable_distanceTo(x: Int, y: Int) {
#expect(Vertical(x).distance(to: Vertical(y)) == x.distance(to: y))
}

@Test("init(some BinaryInteger)")
func initBinaryInteger() {
let value: Int = Int.random(in: 0..<1_000_000)
Expand Down
11 changes: 11 additions & 0 deletions Tests/TerminalUITests/Primitives/PositionTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import TerminalUI
import Testing

@Suite("Position") struct PositionTests {

@Test("origin")
func origin() async throws {
#expect(Position.origin.x == 1)
#expect(Position.origin.y == 1)
}
}
19 changes: 19 additions & 0 deletions Tests/TerminalUITests/Testing/TestCanvasTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import TerminalUI
import TerminalUITesting
import Testing

@Suite("TestCanvas")
struct TestCanvasTests {

@Test("CustomStringConvertible")
func customStringConvertible() async throws {

let canvas = TestCanvas(width: 5, height: 1)

canvas.render {
Text("hello")
}

#expect(canvas.description == "hello")
}
}
2 changes: 1 addition & 1 deletion Tests/TerminalUITests/ViewModifierTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct ViewModifierTests {
"[27m", // Inverse off
"[28m", // Hidden off
"[29m", // Strikethrough off
"[0;1HA", // Position + content
"[1;1HA", // Position + content
])
}
}
2 changes: 1 addition & 1 deletion Tests/TerminalUITests/ViewModifiers/BackgroundColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct BackgroundColorTests {
"[27m", // Inverse off
"[28m", // Hidden off
"[29m", // Strikethrough off
"[0;1Ha", // Position + content
"[1;1Ha", // Position + content
])
}
}
2 changes: 1 addition & 1 deletion Tests/TerminalUITests/ViewModifiers/BlinkingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct BlinkingTests {
"[27m", // Inverse off
"[28m", // Hidden off
"[29m", // Strikethrough off
"[0;1Ha", // Position + content
"[1;1Ha", // Position + content
])
}
}
2 changes: 1 addition & 1 deletion Tests/TerminalUITests/ViewModifiers/BoldTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct BoldTests {
"[27m", // Inverse off
"[28m", // Hidden off
"[29m", // Strikethrough off
"[0;1Ha", // Position + content
"[1;1Ha", // Position + content
])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct ForegroundColorTests {
"[27m", // Inverse off
"[28m", // Hidden off
"[29m", // Strikethrough off
"[0;1Ha", // Position + content
"[1;1Ha", // Position + content
])
}
}
2 changes: 1 addition & 1 deletion Tests/TerminalUITests/ViewModifiers/HiddenTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct HiddenTests {
"[27m", // Inverse off
expected, // Hidden
"[29m", // Strikethrough off
"[0;1Ha", // Position + content
"[1;1Ha", // Position + content
])
}
}
2 changes: 1 addition & 1 deletion Tests/TerminalUITests/ViewModifiers/InverseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct InverseTests {
expected, // Inverse
"[28m", // Hidden off
"[29m", // Strikethrough off
"[0;1Ha", // Position + content
"[1;1Ha", // Position + content
])
}
}
2 changes: 1 addition & 1 deletion Tests/TerminalUITests/ViewModifiers/ItalicTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct ItalicTests {
"[27m", // Inverse off
"[28m", // Hidden off
"[29m", // Strikethrough off
"[0;1Ha", // Position + content
"[1;1Ha", // Position + content
])
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct StrikethroughTests {
"[27m", // Inverse off
"[28m", // Hidden off
expected, // Strikethrough
"[0;1Ha", // Position + content
"[1;1Ha", // Position + content
])
}
}
2 changes: 1 addition & 1 deletion Tests/TerminalUITests/ViewModifiers/UnderlineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct UnderlineTests {
"[27m", // Inverse off
"[28m", // Hidden off
"[29m", // Strikethrough off
"[0;1Ha", // Position + content
"[1;1Ha", // Position + content
])
}
}
10 changes: 5 additions & 5 deletions Tests/TerminalUITests/Views/TextTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ struct TextTests {
}

#expect(canvas.pixels == [
Position(x: 1, y: 0): Pixel("H"),
Position(x: 2, y: 0): Pixel("e"),
Position(x: 3, y: 0): Pixel("l"),
Position(x: 4, y: 0): Pixel("l"),
Position(x: 5, y: 0): Pixel("o"),
Position(x: 1, y: 1): Pixel("H"),
Position(x: 2, y: 1): Pixel("e"),
Position(x: 3, y: 1): Pixel("l"),
Position(x: 4, y: 1): Pixel("l"),
Position(x: 5, y: 1): Pixel("o"),
])
}
}

0 comments on commit 4ed49ee

Please sign in to comment.