-
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.
- Loading branch information
Showing
9 changed files
with
154 additions
and
125 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package invalid_command | ||
|
||
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/shell-tester/internal/test_cases" | ||
"github.com/codecrafters-io/tester-utils/logger" | ||
"github.com/codecrafters-io/tester-utils/random" | ||
) | ||
|
||
func getExpectedOutputForCommand(invalidCommand string) string { | ||
return fmt.Sprintf("%s: command not found", invalidCommand) | ||
} | ||
|
||
func getExpectedOutputForType(invalidCommand string) string { | ||
return fmt.Sprintf("%s: not found", invalidCommand) | ||
} | ||
|
||
func getFallbackPatternsForCommand(invalidCommand string) []*regexp.Regexp { | ||
return []*regexp.Regexp{regexp.MustCompile(`^(bash: )?` + invalidCommand + `: (command )?not found$`)} | ||
} | ||
|
||
func getFallbackPatternsForType(invalidCommand string) []*regexp.Regexp { | ||
return []*regexp.Regexp{regexp.MustCompile(fmt.Sprintf(`^(bash: type: )?%s[:]? not found$`, invalidCommand))} | ||
} | ||
|
||
func getRandomInvalidCommand() string { | ||
return "invalid_" + random.RandomWord() + "_command" | ||
} | ||
|
||
func getRandomInvalidCommands(n int) []string { | ||
words := random.RandomWords(n) | ||
invalidCommands := make([]string, n) | ||
|
||
for i := 0; i < n; i++ { | ||
invalidCommands[i] = "invalid_" + words[i] + "_command" | ||
} | ||
|
||
return invalidCommands | ||
} | ||
|
||
func TestCommandReflection(asserter *logged_shell_asserter.LoggedShellAsserter, shell *shell_executable.ShellExecutable, logger *logger.Logger) error { | ||
invalidCommand := getRandomInvalidCommand() | ||
|
||
// We are seperating this out because we don't want to assert | ||
// The prompt at the end | ||
testCase := test_cases.CommandReflectionTestCase{ | ||
Command: invalidCommand, | ||
SkipPromptAssertion: true, | ||
} | ||
if err := testCase.Run(asserter, shell, logger, true); err != nil { | ||
return err | ||
} | ||
|
||
asserter.AddAssertion(assertions.SingleLineAssertion{ | ||
ExpectedOutput: getExpectedOutputForCommand(invalidCommand), | ||
FallbackPatterns: getFallbackPatternsForCommand(invalidCommand), | ||
}) | ||
|
||
if err := asserter.AssertWithoutPrompt(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func TestCommandResponse(asserter *logged_shell_asserter.LoggedShellAsserter, shell *shell_executable.ShellExecutable, logger *logger.Logger) error { | ||
return TestCommandResponses(asserter, shell, logger, 1) | ||
} | ||
|
||
func TestCommandResponses(asserter *logged_shell_asserter.LoggedShellAsserter, shell *shell_executable.ShellExecutable, logger *logger.Logger, numberOfCommands int) error { | ||
invalidCommands := getRandomInvalidCommands(numberOfCommands) | ||
|
||
for i := 0; i < numberOfCommands; i++ { | ||
invalidCommand := invalidCommands[i] | ||
|
||
testCase := test_cases.CommandResponseTestCase{ | ||
Command: invalidCommand, | ||
ExpectedOutput: getExpectedOutputForCommand(invalidCommand), | ||
FallbackPatterns: getFallbackPatternsForCommand(invalidCommand), | ||
SuccessMessage: "✓ Received command not found message", | ||
} | ||
|
||
if err := testCase.Run(asserter, shell, logger); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func TestTypeResponses(asserter *logged_shell_asserter.LoggedShellAsserter, shell *shell_executable.ShellExecutable, logger *logger.Logger, numberOfCommands int) error { | ||
invalidCommands := getRandomInvalidCommands(2) | ||
|
||
for _, invalidCommand := range invalidCommands { | ||
command := fmt.Sprintf("type %s", invalidCommand) | ||
|
||
testCase := test_cases.CommandResponseTestCase{ | ||
Command: command, | ||
ExpectedOutput: getExpectedOutputForType(invalidCommand), | ||
FallbackPatterns: getFallbackPatternsForType(invalidCommand), | ||
SuccessMessage: "✓ Received expected response", | ||
} | ||
if err := testCase.Run(asserter, shell, logger); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
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
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
Oops, something went wrong.