-
Notifications
You must be signed in to change notification settings - Fork 3
Add tests for Multiple Matches #81
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
Open
UdeshyaDhungana
wants to merge
2
commits into
split-grep
Choose a base branch
from
multiple-matches-tests
base: split-grep
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,81 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/codecrafters-io/grep-tester/internal/test_cases" | ||
| "github.com/codecrafters-io/grep-tester/internal/utils" | ||
| "github.com/codecrafters-io/tester-utils/random" | ||
| "github.com/codecrafters-io/tester-utils/test_case_harness" | ||
| ) | ||
|
|
||
| func testMultipleMatchesMultipleLines(stageHarness *test_case_harness.TestCaseHarness) error { | ||
| utils.RelocateSystemGrep(stageHarness) | ||
|
|
||
| animals := random.RandomElementsFromArray(utils.ANIMALS, 2) | ||
| fruits := random.RandomElementsFromArray(utils.FRUITS, 2) | ||
|
|
||
| animal1 := animals[0] | ||
| animal2 := animals[1] | ||
| fruit1 := fruits[0] | ||
| fruit2 := fruits[1] | ||
|
|
||
| testCaseCollection := test_cases.PrintMatchesOnlyTestCaseCollection{ | ||
| { | ||
| Pattern: `\d`, | ||
| InputLines: []string{ | ||
| "a1b", | ||
| "no digits here", | ||
| "2c3d", | ||
| }, | ||
| ExpectedExitCode: 0, | ||
| }, | ||
| { | ||
| Pattern: fmt.Sprintf(`(%s|%s)`, fruit1, fruit2), | ||
| InputLines: []string{ | ||
| fmt.Sprintf("I like %s", fruit1), | ||
| "nothing here", | ||
| fmt.Sprintf("%s and %s are tasty", fruit2, fruit1), | ||
| }, | ||
| ExpectedExitCode: 0, | ||
| }, | ||
| { | ||
| Pattern: `XYZ123`, | ||
| InputLines: []string{ | ||
| "abc", | ||
| "def", | ||
| "ghi", | ||
| }, | ||
| ExpectedExitCode: 1, | ||
| }, | ||
| { | ||
| Pattern: `cat`, | ||
| InputLines: []string{ | ||
| "dogdogdog", | ||
| "elephant", | ||
| "cat-cat-dog", | ||
| }, | ||
| ExpectedExitCode: 0, | ||
| }, | ||
| { | ||
| Pattern: fmt.Sprintf(`I saw \d+ (%s|%s)s?`, animal1, animal2), | ||
| InputLines: []string{ | ||
| fmt.Sprintf("Yesterday I saw 3 %ss.", animal1), | ||
| "Nothing interesting today.", | ||
| fmt.Sprintf("Last week I saw 12 %ss.", animal2), | ||
| }, | ||
| ExpectedExitCode: 0, | ||
| }, | ||
| { | ||
| Pattern: `cats and dogs`, | ||
| InputLines: []string{ | ||
| "today is sunny", | ||
| "no rains here", | ||
| "tomorrow maybe rainy", | ||
| }, | ||
| ExpectedExitCode: 1, | ||
| }, | ||
| } | ||
|
|
||
| return testCaseCollection.Run(stageHarness) | ||
| } |
This file contains hidden or 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,58 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/codecrafters-io/grep-tester/internal/test_cases" | ||
| "github.com/codecrafters-io/grep-tester/internal/utils" | ||
| "github.com/codecrafters-io/tester-utils/random" | ||
| "github.com/codecrafters-io/tester-utils/test_case_harness" | ||
| ) | ||
|
|
||
| func testMultipleMatchesSingleLine(stageHarness *test_case_harness.TestCaseHarness) error { | ||
| utils.RelocateSystemGrep(stageHarness) | ||
|
|
||
| animals := random.RandomElementsFromArray(utils.ANIMALS, 2) | ||
| fruits := random.RandomElementsFromArray(utils.FRUITS, 3) | ||
|
|
||
| animal1 := animals[0] | ||
| animal2 := animals[1] | ||
| fruit1 := fruits[0] | ||
| fruit2 := fruits[1] | ||
| fruit3 := fruits[2] | ||
|
|
||
| testCaseCollection := test_cases.PrintMatchesOnlyTestCaseCollection{ | ||
| { | ||
| Pattern: `\d`, | ||
| InputLines: []string{"a1b2c3"}, | ||
| ExpectedExitCode: 0, | ||
| }, | ||
| { | ||
| Pattern: fmt.Sprintf(`(%s|%s|%s)`, fruit1, fruit2, fruit3), | ||
| InputLines: []string{fmt.Sprintf("%s_%s_%s", fruit3, fruit2, fruit1)}, | ||
| ExpectedExitCode: 0, | ||
| }, | ||
| { | ||
| Pattern: `\d`, | ||
| InputLines: []string{"cherry"}, | ||
| ExpectedExitCode: 1, | ||
| }, | ||
| { | ||
| Pattern: `\w\w`, | ||
| InputLines: []string{"xx, yy, zz"}, | ||
| ExpectedExitCode: 0, | ||
| }, | ||
| { | ||
| Pattern: `\w`, | ||
| InputLines: []string{"##$$%"}, | ||
| ExpectedExitCode: 1, | ||
| }, | ||
| { | ||
| Pattern: fmt.Sprintf(`I see \d+ (%s|%s)s?`, animal1, animal2), | ||
| InputLines: []string{fmt.Sprintf("I see 3 %ss. Also, I see 4 %ss.", animal1, animal2)}, | ||
| ExpectedExitCode: 0, | ||
| }, | ||
| } | ||
|
|
||
| return testCaseCollection.Run(stageHarness) | ||
| } |
This file contains hidden or 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,50 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/codecrafters-io/grep-tester/internal/test_cases" | ||
| "github.com/codecrafters-io/grep-tester/internal/utils" | ||
| "github.com/codecrafters-io/tester-utils/test_case_harness" | ||
| ) | ||
|
|
||
| func testMultipleMatchesSingleMatch(stageHarness *test_case_harness.TestCaseHarness) error { | ||
| utils.RelocateSystemGrep(stageHarness) | ||
|
|
||
| words := utils.RandomWordsWithoutSubstrings(3) | ||
|
|
||
| testCaseCollection := test_cases.PrintMatchesOnlyTestCaseCollection{ | ||
| { | ||
| Pattern: `\d`, | ||
| InputLines: []string{"only1digit"}, | ||
| ExpectedExitCode: 0, | ||
| }, | ||
| { | ||
| Pattern: `\d`, | ||
| InputLines: []string{"cherry"}, | ||
| ExpectedExitCode: 1, | ||
| }, | ||
| { | ||
| Pattern: fmt.Sprintf("^%s", words[0]), | ||
| InputLines: []string{words[0] + "_suffix"}, | ||
| ExpectedExitCode: 0, | ||
| }, | ||
| { | ||
| Pattern: fmt.Sprintf("^%s", words[0]), | ||
| InputLines: []string{"prefix_" + words[0]}, | ||
| ExpectedExitCode: 1, | ||
| }, | ||
| { | ||
| Pattern: "ca?t", | ||
| InputLines: []string{"cat"}, | ||
| ExpectedExitCode: 0, | ||
| }, | ||
| { | ||
| Pattern: `^I see \d+ (cat|dog)s?$`, | ||
| InputLines: []string{"I see 42 dogs"}, | ||
| ExpectedExitCode: 0, | ||
| }, | ||
| } | ||
|
|
||
| return testCaseCollection.Run(stageHarness) | ||
| } |
This file contains hidden or 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,69 @@ | ||
| package test_cases | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "path" | ||
| "strings" | ||
|
|
||
| "github.com/codecrafters-io/grep-tester/internal/assertions" | ||
| "github.com/codecrafters-io/grep-tester/internal/grep" | ||
| "github.com/codecrafters-io/tester-utils/test_case_harness" | ||
| ) | ||
|
|
||
| type PrintMatchesOnlyTestCase struct { | ||
| Pattern string | ||
| InputLines []string | ||
| ExpectedExitCode int | ||
| } | ||
|
|
||
| type PrintMatchesOnlyTestCaseCollection []PrintMatchingLinesTestCase | ||
|
|
||
| func (c PrintMatchesOnlyTestCaseCollection) Run(stageHarness *test_case_harness.TestCaseHarness) error { | ||
| logger := stageHarness.Logger | ||
| executable := stageHarness.Executable | ||
|
|
||
| executable.TimeoutInMilliseconds = 1000 * 3600 | ||
|
|
||
| for _, testCase := range c { | ||
| // Run executable and collect result | ||
| allInputLines := strings.Join(testCase.InputLines, "\n") | ||
| logger.Infof("$ echo -ne %q | ./%s -o -E '%s'", allInputLines, path.Base(executable.Path), testCase.Pattern) | ||
|
|
||
| grepResult := grep.EmulateGrep([]string{"-o", "-E", testCase.Pattern}, []byte(allInputLines)) | ||
| actualResult, err := executable.RunWithStdin([]byte(allInputLines), "-o", "-E", testCase.Pattern) | ||
|
|
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // Compare against grep | ||
| if testCase.ExpectedExitCode != grepResult.ExitCode { | ||
| panic(fmt.Sprintf("CodeCrafters Internal Error: Expected exit code %v, grep returned %v", testCase.ExpectedExitCode, grepResult.ExitCode)) | ||
| } | ||
|
|
||
| // Run assertions | ||
| exitCodeAssertion := assertions.ExitCodeAssertion{ | ||
| ExpectedExitCode: testCase.ExpectedExitCode, | ||
| } | ||
|
|
||
| if err := exitCodeAssertion.Run(actualResult, logger); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| expectedOutput := strings.TrimSpace(string(grepResult.Stdout)) | ||
|
|
||
| expectedOutputLines := strings.FieldsFunc(expectedOutput, func(r rune) bool { | ||
| return r == '\n' | ||
| }) | ||
|
|
||
| orderedLinesAssertion := assertions.OrderedLinesAssertion{ | ||
| ExpectedOutputLines: expectedOutputLines, | ||
| } | ||
|
|
||
| if err := orderedLinesAssertion.Run(actualResult, logger); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -83,6 +83,13 @@ extensions: | |
|
|
||
| Along the way, you'll learn about extracting matched lines, formatting output, and managing line-by-line processing. | ||
|
|
||
| - slug: "multiple-matches" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be done in the challenge PR but noting here: let's order extensions like this:
|
||
| name: "Multiple Matches" | ||
| description_markdown: | | ||
| In this challenge extension, you'll add support for printing the multiple matching results to your Grep implementation. | ||
|
|
||
| Along the way, you'll learn about how to implement the `-o` flag. | ||
|
|
||
| stages: | ||
| - slug: "cq2" | ||
| name: "Match a literal character" | ||
|
|
@@ -324,4 +331,26 @@ stages: | |
| name: "Print multiple matching lines" | ||
| difficulty: easy | ||
| marketing_md: |- | ||
| In this stage, you'll add support for printing multiple input lines if they match the pattern. | ||
| In this stage, you'll add support for printing multiple input lines if they match the pattern. | ||
|
|
||
| # Multiple matches | ||
| - slug: "cj0" | ||
| primary_extension_slug: "multiple-matches" | ||
| name: "Print single match" | ||
| difficulty: medium | ||
| marketing_md: |- | ||
| In this stage, you'll add support for printing a single matching text to your grep implementation. | ||
|
|
||
| - slug: "ss2" | ||
| primary_extension_slug: "multiple-matches" | ||
| name: "Print multiple matches" | ||
| difficulty: medium | ||
| marketing_md: |- | ||
| In this stage, you'll add support for printing multiple matching texts from a single line to your grep implementation. | ||
|
|
||
| - slug: "bo4" | ||
| primary_extension_slug: "multiple-matches" | ||
| name: "Process multiple input lines" | ||
| difficulty: medium | ||
| marketing_md: |- | ||
| In this stage, you'll add support for processing multiple input lines to print all matching texts. | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A whole 60 seconds? That's kinda wild - is there a reason we need to change this from the default @UdeshyaDhungana?