Skip to content

Commit b4b2e64

Browse files
committed
-
Signed-off-by: azjezz <azjezz@protonmail.com>
1 parent ae14444 commit b4b2e64

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

tests/unit/Terminal/Widget/BarChartTest.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Psl\Ansi\Color;
9+
use Psl\Ansi\Style;
910
use Psl\Terminal\Buffer;
1011
use Psl\Terminal\Rect;
1112
use Psl\Terminal\Widget\BarChart;
@@ -127,4 +128,70 @@ public function testLabelStyleApplied(): void
127128
static::assertNotNull($cell);
128129
static::assertNotNull($cell->foreground);
129130
}
131+
132+
public function testBarStyleWithModifier(): void
133+
{
134+
$buffer = new Buffer(3, 3);
135+
$area = new Rect(0, 0, 3, 3);
136+
137+
BarChart::new()
138+
->data([['A', 1.0]])
139+
->barWidth(3)
140+
->barGap(0)
141+
->barStyle(foreground: Color\red(), style: Style\bold())
142+
->render($area, $buffer);
143+
144+
$cell = $buffer->get(0, 0);
145+
static::assertNotNull($cell);
146+
static::assertSame("\u{2588}", $cell->grapheme);
147+
static::assertNotNull($cell->foreground);
148+
}
149+
150+
public function testLabelStyleWithModifier(): void
151+
{
152+
$buffer = new Buffer(3, 3);
153+
$area = new Rect(0, 0, 3, 3);
154+
155+
BarChart::new()
156+
->data([['X', 0.5]])
157+
->barWidth(3)
158+
->barGap(0)
159+
->labelStyle(foreground: Color\white(), style: Style\bold())
160+
->render($area, $buffer);
161+
162+
$cell = $buffer->get(1, 2);
163+
static::assertNotNull($cell);
164+
static::assertNotNull($cell->foreground);
165+
}
166+
167+
public function testBarsClipWhenExceedingAreaWidth(): void
168+
{
169+
$buffer = new Buffer(5, 3);
170+
$area = new Rect(0, 0, 5, 3);
171+
172+
BarChart::new()
173+
->data([['A', 1.0], ['B', 1.0], ['C', 1.0]])
174+
->barWidth(3)
175+
->barGap(1)
176+
->render($area, $buffer);
177+
178+
static::assertSame("\u{2588}", $buffer->get(0, 0)?->grapheme);
179+
static::assertSame("\u{2588}", $buffer->get(4, 0)?->grapheme);
180+
}
181+
182+
public function testBarColumnClipsToAreaRight(): void
183+
{
184+
$buffer = new Buffer(4, 3);
185+
$area = new Rect(0, 0, 4, 3);
186+
187+
BarChart::new()
188+
->data([['A', 1.0]])
189+
->barWidth(6)
190+
->barGap(0)
191+
->render($area, $buffer);
192+
193+
static::assertSame("\u{2588}", $buffer->get(0, 0)?->grapheme);
194+
static::assertSame("\u{2588}", $buffer->get(3, 0)?->grapheme);
195+
static::assertNull($buffer->get(4, 0));
196+
}
130197
}

tests/unit/Terminal/Widget/ParagraphTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,53 @@ public function testEmptyArea(): void
126126

127127
static::assertSame(' ', $buffer->get(0, 0)?->grapheme);
128128
}
129+
130+
public function testCenterAlignment(): void
131+
{
132+
$buffer = new Buffer(20, 1);
133+
$area = new Rect(0, 0, 20, 1);
134+
135+
$paragraph = Paragraph::new([
136+
Line::new([Span::raw('Hi')]),
137+
])->alignment(Alignment::Center);
138+
139+
$paragraph->render($area, $buffer);
140+
141+
static::assertSame('H', $buffer->get(9, 0)?->grapheme);
142+
static::assertSame('i', $buffer->get(10, 0)?->grapheme);
143+
}
144+
145+
public function testTextClipsToAreaWidth(): void
146+
{
147+
$buffer = new Buffer(10, 1);
148+
$area = new Rect(0, 0, 3, 1);
149+
150+
$paragraph = Paragraph::new([
151+
Line::new([Span::raw('ABCDEF')]),
152+
]);
153+
154+
$paragraph->render($area, $buffer);
155+
156+
static::assertSame('A', $buffer->get(0, 0)?->grapheme);
157+
static::assertSame('B', $buffer->get(1, 0)?->grapheme);
158+
static::assertSame('C', $buffer->get(2, 0)?->grapheme);
159+
static::assertSame(' ', $buffer->get(3, 0)?->grapheme);
160+
}
161+
162+
public function testWideCharacterContinuationCell(): void
163+
{
164+
$buffer = new Buffer(10, 1);
165+
$area = new Rect(0, 0, 10, 1);
166+
167+
$paragraph = Paragraph::new([
168+
Line::new([Span::raw('漢字')]),
169+
]);
170+
171+
$paragraph->render($area, $buffer);
172+
173+
static::assertSame('', $buffer->get(0, 0)?->grapheme);
174+
static::assertSame('', $buffer->get(1, 0)?->grapheme);
175+
static::assertSame('', $buffer->get(2, 0)?->grapheme);
176+
static::assertSame('', $buffer->get(3, 0)?->grapheme);
177+
}
129178
}

0 commit comments

Comments
 (0)