diff --git a/internal/stage2.go b/internal/stage2.go index 43ad8a7..e6bd74c 100644 --- a/internal/stage2.go +++ b/internal/stage2.go @@ -1,10 +1,6 @@ package internal 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" @@ -20,27 +16,10 @@ func testInvalidCommand(stageHarness *test_case_harness.TestCaseHarness) error { return err } - 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 + testCase := test_cases.InvalidCommandTestCase{ + Command: getRandomInvalidCommand(), } - - asserter.AddAssertion(assertions.SingleLineAssertion{ - ExpectedOutput: fmt.Sprintf("%s: command not found", invalidCommand), - FallbackPatterns: []*regexp.Regexp{ - regexp.MustCompile(fmt.Sprintf(`^bash: %s: command not found$`, invalidCommand)), - regexp.MustCompile(fmt.Sprintf(`^%s: command not found$`, invalidCommand)), - }, - }) - - if err := asserter.AssertWithoutPrompt(); err != nil { + if err := testCase.RunAndTestReflection(asserter, shell, logger); err != nil { return err } diff --git a/internal/stage3.go b/internal/stage3.go index dcc952f..e5c0f22 100644 --- a/internal/stage3.go +++ b/internal/stage3.go @@ -1,8 +1,6 @@ package internal import ( - "fmt" - "regexp" "strconv" "github.com/codecrafters-io/shell-tester/internal/logged_shell_asserter" @@ -17,26 +15,17 @@ func testREPL(stageHarness *test_case_harness.TestCaseHarness) error { shell := shell_executable.NewShellExecutable(stageHarness) asserter := logged_shell_asserter.NewLoggedShellAsserter(shell) - numberOfCommands := random.RandomInt(3, 6) - if err := startShellAndAssertPrompt(asserter, shell); err != nil { return err } - for i := 0; i < numberOfCommands; i++ { - command := "invalid_command_" + strconv.Itoa(i+1) + numberOfCommands := random.RandomInt(3, 6) - testCase := test_cases.CommandResponseTestCase{ - Command: command, - ExpectedOutput: fmt.Sprintf("%s: command not found", command), - FallbackPatterns: []*regexp.Regexp{ - regexp.MustCompile(fmt.Sprintf(`^bash: %s: command not found$`, command)), - regexp.MustCompile(fmt.Sprintf(`^%s: command not found$`, command)), - }, - SuccessMessage: "✓ Received command not found message", + for i := 0; i < numberOfCommands; i++ { + testCase := test_cases.InvalidCommandTestCase{ + Command: "invalid_command_" + strconv.Itoa(i+1), } - - if err := testCase.Run(asserter, shell, logger); err != nil { + if err := testCase.RunAndTestResponse(asserter, shell, logger); err != nil { return err } } diff --git a/internal/stage4.go b/internal/stage4.go index cdb9196..72ae514 100644 --- a/internal/stage4.go +++ b/internal/stage4.go @@ -3,7 +3,6 @@ package internal import ( "errors" "fmt" - "regexp" "strings" "github.com/codecrafters-io/shell-tester/internal/logged_shell_asserter" @@ -23,17 +22,10 @@ func testExit(stageHarness *test_case_harness.TestCaseHarness) error { return err } - invalidCommand := getRandomInvalidCommand() - - // We test a nonexistent command first, just to make sure the logic works in a "loop" - testCase := test_cases.CommandResponseTestCase{ - Command: invalidCommand, - ExpectedOutput: invalidCommand + ": command not found", - FallbackPatterns: []*regexp.Regexp{regexp.MustCompile(`^(bash: )?` + invalidCommand + `: (command )?not found$`)}, - SuccessMessage: "✓ Received command not found message", + testCase := test_cases.InvalidCommandTestCase{ + Command: getRandomInvalidCommand(), } - - if err := testCase.Run(asserter, shell, logger); err != nil { + if err := testCase.RunAndTestResponse(asserter, shell, logger); err != nil { return err } diff --git a/internal/stage6.go b/internal/stage6.go index 517f09e..928ed6f 100644 --- a/internal/stage6.go +++ b/internal/stage6.go @@ -38,13 +38,8 @@ func testType1(stageHarness *test_case_harness.TestCaseHarness) error { invalidCommands := getRandomInvalidCommands(2) for _, invalidCommand := range invalidCommands { - command := fmt.Sprintf("type %s", invalidCommand) - - testCase := test_cases.CommandResponseTestCase{ - Command: command, - ExpectedOutput: fmt.Sprintf("%s: not found", invalidCommand), - FallbackPatterns: []*regexp.Regexp{regexp.MustCompile(fmt.Sprintf(`^(bash: type: )?%s[:]? not found$`, invalidCommand))}, - SuccessMessage: "✓ Received expected response", + testCase := test_cases.InvalidCommandTypeTestCase{ + Command: invalidCommand, } if err := testCase.Run(asserter, shell, logger); err != nil { return err diff --git a/internal/stage7.go b/internal/stage7.go index 858c702..f57c4f5 100644 --- a/internal/stage7.go +++ b/internal/stage7.go @@ -66,15 +66,11 @@ func testType2(stageHarness *test_case_harness.TestCaseHarness) error { } } - nonAvailableExecutables := getRandomInvalidCommands(2) + invalidCommands := getRandomInvalidCommands(2) - for _, executable := range nonAvailableExecutables { - command := fmt.Sprintf("type %s", executable) - testCase := test_cases.CommandResponseTestCase{ - Command: command, - ExpectedOutput: fmt.Sprintf(`%s: not found`, executable), - FallbackPatterns: []*regexp.Regexp{regexp.MustCompile(fmt.Sprintf(`^(bash: type: )?%s: not found$`, executable))}, - SuccessMessage: "✓ Received expected response", + for _, command := range invalidCommands { + testCase := test_cases.InvalidCommandTypeTestCase{ + Command: command, } if err := testCase.Run(asserter, shell, logger); err != nil { return err diff --git a/internal/test_cases/invalid_command_test_case.go b/internal/test_cases/invalid_command_test_case.go new file mode 100644 index 0000000..9439fb4 --- /dev/null +++ b/internal/test_cases/invalid_command_test_case.go @@ -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$`)} +} diff --git a/internal/test_cases/invalid_command_type_test_case.go b/internal/test_cases/invalid_command_type_test_case.go new file mode 100644 index 0000000..7de8bf5 --- /dev/null +++ b/internal/test_cases/invalid_command_type_test_case.go @@ -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 expected response", + } + + 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))} +}