Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BellAssertion for terminal bell events #109

Merged
merged 6 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions internal/assertions/bell_assertion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package assertions

import (
"time"
)

// BellAssertion asserts that ...
type BellAssertion struct {
// Vt *virtual_terminal.VirtualTerminal
BellChannel chan bool
}

func (a BellAssertion) Inspect() string {
return "BellAssertion"
}

func (a BellAssertion) Run(screenState [][]string, startRowIndex int) (processedRowCount int, err *AssertionError) {
if !checkIfBellReceived(a.BellChannel) {
return 0, &AssertionError{
StartRowIndex: startRowIndex,
ErrorRowIndex: startRowIndex,
Message: "Expected bell to ring, but it didn't",
}
} else {
return 0, nil
}
}

func checkIfBellReceived(bellChannel chan bool) bool {
select {
case <-bellChannel:
return true
case <-time.After(100 * time.Millisecond): // Add reasonable timeout
return false
}
}
34 changes: 12 additions & 22 deletions internal/test_cases/command_autocomplete_test_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package test_cases

import (
"fmt"
"time"

"github.com/codecrafters-io/shell-tester/internal/assertions"
"github.com/codecrafters-io/shell-tester/internal/logged_shell_asserter"
Expand Down Expand Up @@ -82,15 +81,23 @@ func (t CommandAutocompleteTestCase) Run(asserter *logged_shell_asserter.LoggedS

// Only if we attempted to autocomplete, print the success message
logger.Successf("✓ Prompt line matches %q", t.ExpectedReflection)
// The space at the end of the reflection won't be present, so replace that assertion
asserter.PopAssertion()

if t.CheckForBell {
if err := RunBellAssertion(shell, logger); err != nil {
bellChannel := shell.VTBellChannel()
asserter.AddAssertion(assertions.BellAssertion{
BellChannel: bellChannel,
})
// Run the assertion, before sending the enter key
if err := asserter.AssertWithoutPrompt(); err != nil {
return err
}
}

// The space at the end of the reflection won't be present, so replace that assertion
asserter.PopAssertion()
logger.Successf("✓ Received bell")
// Pop the bell assertion after running
asserter.PopAssertion()
}

var assertFuncToRun func() error
if t.SkipPromptAssertion {
Expand All @@ -117,20 +124,3 @@ func logTab(logger *logger.Logger, expectedReflection string) {
func logCommand(logger *logger.Logger, command string) {
logger.Infof("Typed %q", command)
}

func RunBellAssertion(shell *shell_executable.ShellExecutable, logger *logger.Logger) error {
if !checkIfBellReceived(shell) {
return fmt.Errorf("Expected bell to ring, but it didn't")
}
logger.Successf("✓ Received bell")
return nil
}

func checkIfBellReceived(shell *shell_executable.ShellExecutable) bool {
select {
case <-shell.VTBellChannel():
return true
case <-time.After(50 * time.Millisecond): // Add reasonable timeout
return false
}
}
11 changes: 10 additions & 1 deletion internal/test_cases/command_multiple_completions_test_case.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,18 @@ func (t CommandMultipleCompletionsTestCase) Run(asserter *logged_shell_asserter.
}

if t.CheckForBell {
if err := RunBellAssertion(shell, logger); err != nil {
bellChannel := shell.VTBellChannel()
asserter.AddAssertion(assertions.BellAssertion{
BellChannel: bellChannel,
})
// Run the assertion, before sending the enter key
if err := asserter.AssertWithoutPrompt(); err != nil {
return err
}

logger.Successf("✓ Received bell")
// Pop the bell assertion after running
asserter.PopAssertion()
}

commandReflection := t.ExpectedReflection
Expand Down