Skip to content

Commit

Permalink
Fix display of submitted multiselect selections (#53)
Browse files Browse the repository at this point in the history
* Improve test helpers

* Add failing test

* Fix display of selected multiselect options
  • Loading branch information
jessarcher authored Aug 16, 2023
1 parent b0cd4ce commit f1950cb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
44 changes: 42 additions & 2 deletions src/Concerns/FakesInputOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,51 @@ public static function fake(array $keys = []): void
* Assert that the output contains the given string.
*/
public static function assertOutputContains(string $string): void
{
Assert::assertStringContainsString($string, static::content());
}

/**
* Assert that the output doesn't contain the given string.
*/
public static function assertOutputDoesntContain(string $string): void
{
Assert::assertStringNotContainsString($string, static::content());
}

/**
* Assert that the stripped output contains the given string.
*/
public static function assertStrippedOutputContains(string $string): void
{
Assert::assertStringContainsString($string, static::strippedContent());
}

/**
* Assert that the stripped output doesn't contain the given string.
*/
public static function assertStrippedOutputDoesntContain(string $string): void
{
Assert::assertStringNotContainsString($string, static::strippedContent());
}

/**
* Get the buffered console output.
*/
public static function content(): string
{
if (! static::output() instanceof BufferedConsoleOutput) {
throw new RuntimeException('Prompt must be faked before asserting output.');
throw new RuntimeException('Prompt must be faked before accessing content.');
}

Assert::assertStringContainsString($string, static::output()->content());
return static::output()->content();
}

/**
* Get the buffered console output, stripped of escape sequences.
*/
public static function strippedContent(): string
{
return preg_replace("/\e\[[0-9;?]*[A-Za-z]/", '', static::content());
}
}
2 changes: 1 addition & 1 deletion src/MultiSelectPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function value(): array
public function labels(): array
{
if (array_is_list($this->options)) {
return array_values(array_intersect_key($this->options, $this->values));
return array_map(fn ($value) => (string) $value, $this->values);
}

return array_values(array_intersect_key($this->options, array_flip($this->values)));
Expand Down
4 changes: 4 additions & 0 deletions tests/Feature/MultiselectPromptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
);

expect($result)->toBe(['Green', 'Blue']);

Prompt::assertStrippedOutputDoesntContain('│ Red');
Prompt::assertStrippedOutputContains('│ Green');
Prompt::assertStrippedOutputContains('│ Blue');
});

it('accepts an array of keys and labels', function () {
Expand Down

0 comments on commit f1950cb

Please sign in to comment.