-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Abstract invalid command test cases to separate files and a…
…dd reflection handling
- Loading branch information
Showing
7 changed files
with
113 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package test_cases | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/codecrafters-io/shell-tester/internal/assertions" | ||
"github.com/codecrafters-io/shell-tester/internal/logged_shell_asserter" | ||
"github.com/codecrafters-io/shell-tester/internal/shell_executable" | ||
"github.com/codecrafters-io/tester-utils/logger" | ||
) | ||
|
||
type InvalidCommandTestCase struct { | ||
Command string | ||
} | ||
|
||
func (t *InvalidCommandTestCase) RunAndTestReflection(asserter *logged_shell_asserter.LoggedShellAsserter, shell *shell_executable.ShellExecutable, logger *logger.Logger) error { | ||
testCase := CommandReflectionTestCase{ | ||
Command: t.Command, | ||
SkipPromptAssertion: true, | ||
} | ||
if err := testCase.Run(asserter, shell, logger, true); err != nil { | ||
return err | ||
} | ||
|
||
asserter.AddAssertion(assertions.SingleLineAssertion{ | ||
ExpectedOutput: t.getExpectedOutput(), | ||
FallbackPatterns: t.getFallbackPatterns(), | ||
}) | ||
|
||
if err := asserter.AssertWithoutPrompt(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (t *InvalidCommandTestCase) RunAndTestResponse(asserter *logged_shell_asserter.LoggedShellAsserter, shell *shell_executable.ShellExecutable, logger *logger.Logger) error { | ||
testCase := CommandResponseTestCase{ | ||
Command: t.Command, | ||
ExpectedOutput: t.getExpectedOutput(), | ||
FallbackPatterns: t.getFallbackPatterns(), | ||
SuccessMessage: "✓ Received command not found message", | ||
} | ||
|
||
if err := testCase.Run(asserter, shell, logger); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (t *InvalidCommandTestCase) getExpectedOutput() string { | ||
return fmt.Sprintf("%s: command not found", t.Command) | ||
} | ||
|
||
func (t *InvalidCommandTestCase) getFallbackPatterns() []*regexp.Regexp { | ||
return []*regexp.Regexp{regexp.MustCompile(`^(bash: )?` + t.Command + `: (command )?not found$`)} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package test_cases | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
|
||
"github.com/codecrafters-io/shell-tester/internal/logged_shell_asserter" | ||
"github.com/codecrafters-io/shell-tester/internal/shell_executable" | ||
"github.com/codecrafters-io/tester-utils/logger" | ||
) | ||
|
||
type InvalidCommandTypeTestCase struct { | ||
Command string | ||
} | ||
|
||
func (t *InvalidCommandTypeTestCase) Run(asserter *logged_shell_asserter.LoggedShellAsserter, shell *shell_executable.ShellExecutable, logger *logger.Logger) error { | ||
testCase := CommandResponseTestCase{ | ||
Command: "type " + t.Command, | ||
ExpectedOutput: t.getExpectedOutput(), | ||
FallbackPatterns: t.getFallbackPatterns(), | ||
SuccessMessage: "✓ Received command not found message", | ||
} | ||
|
||
if err := testCase.Run(asserter, shell, logger); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (t *InvalidCommandTypeTestCase) getExpectedOutput() string { | ||
return fmt.Sprintf("%s: not found", t.Command) | ||
} | ||
|
||
func (t *InvalidCommandTypeTestCase) getFallbackPatterns() []*regexp.Regexp { | ||
return []*regexp.Regexp{regexp.MustCompile(fmt.Sprintf(`^(bash: type: )?%s[:]? not found$`, t.Command))} | ||
} |