Skip to content

Commit

Permalink
Use arguments to test foreground and background colors
Browse files Browse the repository at this point in the history
  • Loading branch information
danielctull committed Nov 12, 2024
1 parent 5983dcf commit 2396f5e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Sources/TerminalUI/Color.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

public struct Color: Equatable {
public struct Color: Equatable, Sendable {
let foreground: ControlSequence
let background: ControlSequence
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/TerminalUI/Internal/ControlSequence.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

struct ControlSequence: Equatable {
struct ControlSequence: Equatable, Sendable {
fileprivate let raw: String
fileprivate init(_ raw: String) {
self.raw = raw
Expand Down
40 changes: 33 additions & 7 deletions Tests/TerminalUITests/ViewModifiers/BackgroundColor.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
@testable import TerminalUI
import TerminalUI
import TerminalUITesting
import Testing

@Suite("BackgroundColor", .tags(.viewModifier))
struct BackgroundColorTests {

@Test("backgroundColor")
func backgroundColor() {
@Test("Text Output", arguments: [
(Color.`default`, "[49m"),
(Color.black, "[40m"),
(Color.red, "[41m"),
(Color.green, "[42m"),
(Color.yellow, "[43m"),
(Color.blue, "[44m"),
(Color.magenta, "[45m"),
(Color.cyan, "[46m"),
(Color.white, "[47m"),
])
func textOutput(backgroundColor: Color, expected: String) {

let text = Text("x")
.backgroundColor(.blue)
let app = TestApp {
Text("a").backgroundColor(backgroundColor)
}

text.expect([
Position(x: 1, y: 0): Pixel("x", background: .blue),
let stream = TestStream()
app.run(stream: stream)

#expect(stream.controlSequences == [
"[2J", // Clear screen
"[?1049h", // Alternative buffer on
"[?25l", // Cursor visibility off
"[39m", // ForegroundColor default
expected, // BackgroundColor
"[22m", // Bold off
"[23m", // Italic off
"[24m", // Underline off
"[25m", // Blinking off
"[27m", // Inverse off
"[28m", // Hidden off
"[29m", // Strikethrough off
"[0;1Ha", // Position + content
])
}
}
40 changes: 33 additions & 7 deletions Tests/TerminalUITests/ViewModifiers/ForegroundColorTests.swift
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
@testable import TerminalUI
import TerminalUI
import TerminalUITesting
import Testing

@Suite("ForegroundColor", .tags(.viewModifier))
struct ForegroundColorTests {

@Test("foregroundColor")
func foregroundColor() {
@Test("Text Output", arguments: [
(Color.`default`, "[39m"),
(Color.black, "[30m"),
(Color.red, "[31m"),
(Color.green, "[32m"),
(Color.yellow, "[33m"),
(Color.blue, "[34m"),
(Color.magenta, "[35m"),
(Color.cyan, "[36m"),
(Color.white, "[37m"),
])
func textOutput(foregroundColor: Color, expected: String) {

let text = Text("x")
.foregroundColor(.blue)
let app = TestApp {
Text("a").foregroundColor(foregroundColor)
}

text.expect([
Position(x: 1, y: 0): Pixel("x", foreground: .blue),
let stream = TestStream()
app.run(stream: stream)

#expect(stream.controlSequences == [
"[2J", // Clear screen
"[?1049h", // Alternative buffer on
"[?25l", // Cursor visibility off
expected, // ForegroundColor
"[49m", // BackgroundColor default
"[22m", // Bold off
"[23m", // Italic off
"[24m", // Underline off
"[25m", // Blinking off
"[27m", // Inverse off
"[28m", // Hidden off
"[29m", // Strikethrough off
"[0;1Ha", // Position + content
])
}
}

0 comments on commit 2396f5e

Please sign in to comment.