🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of JuanMa

JuanMa's solution

to Grep in the Java Track

Published at Dec 18 2020 · 0 comments
Instructions
Test suite
Solution

Search a file for lines matching a regular expression pattern. Return the line number and contents of each matching line.

The Unix grep command can be used to search for lines in one or more files that match a user-provided search query (known as the pattern).

The grep command takes three arguments:

  1. The pattern used to match lines in a file.
  2. Zero or more flags to customize the matching behavior.
  3. One or more files in which to search for matching lines.

Your task is to implement the grep function, which should read the contents of the specified files, find the lines that match the specified pattern and then output those lines as a single string. Note that the lines should be output in the order in which they were found, with the first matching line in the first file being output first.

As an example, suppose there is a file named "input.txt" with the following contents:

hello
world
hello again

If we were to call grep "hello" input.txt, the returned string should be:

hello
hello again

Flags

As said earlier, the grep command should also support the following flags:

  • -n Print the line numbers of each matching line.
  • -l Print only the names of files that contain at least one matching line.
  • -i Match line using a case-insensitive comparison.
  • -v Invert the program -- collect all lines that fail to match the pattern.
  • -x Only match entire lines, instead of lines that contain a match.

If we run grep -n "hello" input.txt, the -n flag will require the matching lines to be prefixed with its line number:

1:hello
3:hello again

And if we run grep -i "HELLO" input.txt, we'll do a case-insensitive match, and the output will be:

hello
hello again

The grep command should support multiple flags at once.

For example, running grep -l -v "hello" file1.txt file2.txt should print the names of files that do not contain the string "hello".

Tips

Since this exercise has difficulty 5 it doesn't come with any starter implementation. This is so that you get to practice creating classes and methods which is an important part of programming in Java. It does mean that when you first try to run the tests, they won't compile. They will give you an error similar to:

 path-to-exercism-dir\exercism\java\name-of-exercise\src\test\java\ExerciseClassNameTest.java:14: error: cannot find symbol
        ExerciseClassName exerciseClassName = new ExerciseClassName();
        ^
 symbol:   class ExerciseClassName
 location: class ExerciseClassNameTest

This error occurs because the test refers to a class that hasn't been created yet (ExerciseClassName). To resolve the error you need to add a file matching the class name in the error to the src/main/java directory. For example, for the error above you would add a file called ExerciseClassName.java.

When you try to run the tests again you will get slightly different errors. You might get an error similar to:

  constructor ExerciseClassName in class ExerciseClassName cannot be applied to given types;
        ExerciseClassName exerciseClassName = new ExerciseClassName("some argument");
                                              ^
  required: no arguments
  found: String
  reason: actual and formal argument lists differ in length

This error means that you need to add a constructor to your new class. If you don't add a constructor, Java will add a default one for you. This default constructor takes no arguments. So if the tests expect your class to have a constructor which takes arguments, then you need to create this constructor yourself. In the example above you could add:

ExerciseClassName(String input) {

}

That should make the error go away, though you might need to add some more code to your constructor to make the test pass!

You might also get an error similar to:

  error: cannot find symbol
        assertEquals(expectedOutput, exerciseClassName.someMethod());
                                                       ^
  symbol:   method someMethod()
  location: variable exerciseClassName of type ExerciseClassName

This error means that you need to add a method called someMethod to your new class. In the example above you would add:

String someMethod() {
  return "";
}

Make sure the return type matches what the test is expecting. You can find out which return type it should have by looking at the type of object it's being compared to in the tests. Or you could set your method to return some random type (e.g. void), and run the tests again. The new error should tell you which type it's expecting.

After having resolved these errors you should be ready to start making the tests pass!

Setup

Go through the setup instructions for Java to install the necessary dependencies:

https://exercism.io/tracks/java/installation

Running the tests

You can run all the tests for an exercise by entering the following in your terminal:

$ gradle test

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the @Ignore("Remove to run test") annotation.

Source

Conversation with Nate Foster. http://www.cs.cornell.edu/Courses/cs3110/2014sp/hw/0/ps0.pdf

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

GrepToolTest.java

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class GrepToolTest {
    private GrepTool grepTool;

    @Before
    public void setUp() throws IOException {
        List<String> iliadText = Arrays.asList(
            "Achilles sing, O Goddess! Peleus' son;",
            "His wrath pernicious, who ten thousand woes",
            "Caused to Achaia's host, sent many a soul",
            "Illustrious into Ades premature,",
            "And Heroes gave (so stood the will of Jove)",
            "To dogs and to all ravening fowls a prey,",
            "When fierce dispute had separated once",
            "The noble Chief Achilles from the son",
            "Of Atreus, Agamemnon, King of men."
        );
        writeToFile("iliad.txt", iliadText);

        List<String> midsummerNightText = Arrays.asList(
            "I do entreat your grace to pardon me.",
            "I know not by what power I am made bold,",
            "Nor how it may concern my modesty,",
            "In such a presence here to plead my thoughts;",
            "But I beseech your grace that I may know",
            "The worst that may befall me in this case,",
            "If I refuse to wed Demetrius."
        );
        writeToFile("midsummer-night.txt", midsummerNightText);

        List<String> paradiseLostText = Arrays.asList(
            "Of Mans First Disobedience, and the Fruit",
            "Of that Forbidden Tree, whose mortal tast",
            "Brought Death into the World, and all our woe,",
            "With loss of Eden, till one greater Man",
            "Restore us, and regain the blissful Seat,",
            "Sing Heav'nly Muse, that on the secret top",
            "Of Oreb, or of Sinai, didst inspire",
            "That Shepherd, who first taught the chosen Seed"
        );
        writeToFile("paradise-lost.txt", paradiseLostText);

        grepTool = new GrepTool();
    }

    @After
    public void tearDown() throws IOException {
        deleteFile("iliad.txt");
        deleteFile("midsummer-night.txt");
        deleteFile("paradise-lost.txt");
    }

    @Test
    public void oneFileOneMatchNoFlags() {
        String expected = "Of Atreus, Agamemnon, King of men.";

        String actual = grepTool.grep(
            "Agamemnon",
            Collections.emptyList(),
            Collections.singletonList("iliad.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void oneFileOneMatchPrintLineNumbersFlag() {
        String expected = "2:Of that Forbidden Tree, whose mortal tast";

        String actual = grepTool.grep(
            "Forbidden",
            Collections.singletonList("-n"),
            Collections.singletonList("paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void oneFileOneMatchCaseInsensitiveFlag() {
        String expected = "Of that Forbidden Tree, whose mortal tast";

        String actual = grepTool.grep(
            "FORBIDDEN",
            Collections.singletonList("-i"),
            Collections.singletonList("paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void oneFileOneMatchPrintFileNamesFlag() {
        String expected = "paradise-lost.txt";

        String actual = grepTool.grep(
            "Forbidden",
            Collections.singletonList("-l"),
            Collections.singletonList("paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void oneFileOneMatchEntireLinesFlag() {
        String expected = "With loss of Eden, till one greater Man";

        String actual = grepTool.grep(
            "With loss of Eden, till one greater Man",
            Collections.singletonList("-x"),
            Collections.singletonList("paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void oneFileOneMatchMultipleFlags() {
        String expected = "9:Of Atreus, Agamemnon, King of men.";

        String actual = grepTool.grep(
            "OF ATREUS, Agamemnon, KIng of MEN.",
            Arrays.asList("-n", "-i", "-x"),
            Collections.singletonList("iliad.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void oneFileSeveralMatchesNoFlags() {
        String expected = "Nor how it may concern my modesty,\n"
            + "But I beseech your grace that I may know\n"
            + "The worst that may befall me in this case,";

        String actual = grepTool.grep(
            "may",
            Collections.emptyList(),
            Collections.singletonList("midsummer-night.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void oneFileSeveralMatchesPrintLineNumbersFlag() {
        String expected = "3:Nor how it may concern my modesty,\n"
            + "5:But I beseech your grace that I may know\n"
            + "6:The worst that may befall me in this case,";

        String actual = grepTool.grep(
            "may",
            Collections.singletonList("-n"),
            Collections.singletonList("midsummer-night.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void oneFileSeveralMatchesMatchEntireLineFlag() {
        String expected = "";

        String actual = grepTool.grep(
            "may",
            Collections.singletonList("-x"),
            Collections.singletonList("midsummer-night.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void oneFileSeveralMatchesCaseInsensitiveFlag() {
        String expected = "Achilles sing, O Goddess! Peleus' son;\n"
            + "The noble Chief Achilles from the son";

        String actual = grepTool.grep(
            "ACHILLES",
            Collections.singletonList("-i"),
            Collections.singletonList("iliad.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void oneFileSeveralMatchesInvertedFlag() {
        String expected = "Brought Death into the World, and all our woe,\n"
            + "With loss of Eden, till one greater Man\n"
            + "Restore us, and regain the blissful Seat,\n"
            + "Sing Heav'nly Muse, that on the secret top\n"
            + "That Shepherd, who first taught the chosen Seed";

        String actual = grepTool.grep(
            "Of",
            Collections.singletonList("-v"),
            Collections.singletonList("paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void oneFileNoMatchesVariousFlags() {
        String expected = "";

        String actual = grepTool.grep(
            "Gandalf",
            Arrays.asList("-n", "-l", "-x", "-i"),
            Collections.singletonList("iliad.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void oneFileOneMatchFileFlagTakesPrecedenceOverLineFlag() {
        String expected = "iliad.txt";

        String actual = grepTool.grep(
            "ten",
            Arrays.asList("-n", "-l"),
            Collections.singletonList("iliad.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void oneFileSeveralMatchesInvertedAndMatchEntireLinesFlags() {
        String expected = "Achilles sing, O Goddess! Peleus' son;\n"
            + "His wrath pernicious, who ten thousand woes\n"
            + "Caused to Achaia's host, sent many a soul\n"
            + "And Heroes gave (so stood the will of Jove)\n"
            + "To dogs and to all ravening fowls a prey,\n"
            + "When fierce dispute had separated once\n"
            + "The noble Chief Achilles from the son\n"
            + "Of Atreus, Agamemnon, King of men.";

        String actual = grepTool.grep(
            "Illustrious into Ades premature,",
            Arrays.asList("-x", "-v"),
            Collections.singletonList("iliad.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void multipleFilesOneMatchNoFlags() {
        String expected = "iliad.txt:Of Atreus, Agamemnon, King of men.";

        String actual = grepTool.grep(
            "Agamemnon",
            Collections.emptyList(),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void multipleFilesSeveralMatchesNoFlags() {
        String expected = "midsummer-night.txt:Nor how it may concern my modesty,\n"
            + "midsummer-night.txt:But I beseech your grace that I may know\n"
            + "midsummer-night.txt:The worst that may befall me in this case,";

        String actual = grepTool.grep(
            "may",
            Collections.emptyList(),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void multipleFilesSeveralMatchesPrintLineNumbersFlag() {
        String expected = "midsummer-night.txt:5:But I beseech your grace that I may know\n"
            + "midsummer-night.txt:6:The worst that may befall me in this case,\n"
            + "paradise-lost.txt:2:Of that Forbidden Tree, whose mortal tast\n"
            + "paradise-lost.txt:6:Sing Heav'nly Muse, that on the secret top";

        String actual = grepTool.grep(
            "that",
            Collections.singletonList("-n"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void multipleFilesOneMatchPrintFileNamesFlag() {
        String expected = "iliad.txt\n"
            + "paradise-lost.txt";

        String actual = grepTool.grep(
            "who",
            Collections.singletonList("-l"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void multipleFilesSeveralMatchesCaseInsensitiveFlag() {
        String expected = "iliad.txt:Caused to Achaia's host, sent many a soul\n"
            + "iliad.txt:Illustrious into Ades premature,\n"
            + "iliad.txt:And Heroes gave (so stood the will of Jove)\n"
            + "iliad.txt:To dogs and to all ravening fowls a prey,\n"
            + "midsummer-night.txt:I do entreat your grace to pardon me.\n"
            + "midsummer-night.txt:In such a presence here to plead my thoughts;\n"
            + "midsummer-night.txt:If I refuse to wed Demetrius.\n"
            + "paradise-lost.txt:Brought Death into the World, and all our woe,\n"
            + "paradise-lost.txt:Restore us, and regain the blissful Seat,\n"
            + "paradise-lost.txt:Sing Heav'nly Muse, that on the secret top";

        String actual = grepTool.grep(
            "TO",
            Collections.singletonList("-i"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void multipleFilesSeveralMatchesInvertedFlag() {
        String expected = "iliad.txt:Achilles sing, O Goddess! Peleus' son;\n"
            + "iliad.txt:The noble Chief Achilles from the son\n"
            + "midsummer-night.txt:If I refuse to wed Demetrius.";

        String actual = grepTool.grep(
            "a",
            Collections.singletonList("-v"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void multipleFilesOneMatchEntireLinesFlag() {
        String expected = "midsummer-night.txt:But I beseech your grace that I may know";

        String actual = grepTool.grep(
            "But I beseech your grace that I may know",
            Collections.singletonList("-x"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void multipleFilesOneMatchMultipleFlags() {
        String expected = "paradise-lost.txt:4:With loss of Eden, till one greater Man";

        String actual = grepTool.grep(
            "WITH LOSS OF EDEN, TILL ONE GREATER MAN",
            Arrays.asList("-n", "-i", "-x"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void multipleFilesNoMatchesVariousFlags() {
        String expected = "";

        String actual = grepTool.grep(
            "Frodo",
            Arrays.asList("-n", "-l", "-x", "-i"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void multipleFilesSeveralMatchesFileFlagTakesPrecedenceOverLineNumberFlag() {
        String expected = "iliad.txt\n"
            + "paradise-lost.txt";

        String actual = grepTool.grep(
            "who",
            Arrays.asList("-n", "-l"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    @Ignore("Remove to run test")
    @Test
    public void multipleFilesSeveralMatchesInvertedAndMatchEntireLinesFlags() {
        String expected = "iliad.txt:Achilles sing, O Goddess! Peleus' son;\n"
            + "iliad.txt:His wrath pernicious, who ten thousand woes\n"
            + "iliad.txt:Caused to Achaia's host, sent many a soul\n"
            + "iliad.txt:And Heroes gave (so stood the will of Jove)\n"
            + "iliad.txt:To dogs and to all ravening fowls a prey,\n"
            + "iliad.txt:When fierce dispute had separated once\n"
            + "iliad.txt:The noble Chief Achilles from the son\n"
            + "iliad.txt:Of Atreus, Agamemnon, King of men.\n"
            + "midsummer-night.txt:I do entreat your grace to pardon me.\n"
            + "midsummer-night.txt:I know not by what power I am made bold,\n"
            + "midsummer-night.txt:Nor how it may concern my modesty,\n"
            + "midsummer-night.txt:In such a presence here to plead my thoughts;\n"
            + "midsummer-night.txt:But I beseech your grace that I may know\n"
            + "midsummer-night.txt:The worst that may befall me in this case,\n"
            + "midsummer-night.txt:If I refuse to wed Demetrius.\n"
            + "paradise-lost.txt:Of Mans First Disobedience, and the Fruit\n"
            + "paradise-lost.txt:Of that Forbidden Tree, whose mortal tast\n"
            + "paradise-lost.txt:Brought Death into the World, and all our woe,\n"
            + "paradise-lost.txt:With loss of Eden, till one greater Man\n"
            + "paradise-lost.txt:Restore us, and regain the blissful Seat,\n"
            + "paradise-lost.txt:Sing Heav'nly Muse, that on the secret top\n"
            + "paradise-lost.txt:Of Oreb, or of Sinai, didst inspire\n"
            + "paradise-lost.txt:That Shepherd, who first taught the chosen Seed";

        String actual = grepTool.grep(
            "Illustrious into Ades premature,",
            Arrays.asList("-x", "-v"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    private void writeToFile(String filename, List<String> contents) throws IOException {
        Path file = Paths.get(filename);
        Files.write(file, contents, Charset.forName("UTF-8"));
    }

    private void deleteFile(String filename) throws IOException {
        Files.deleteIfExists(Paths.get(filename));
    }
}

src/main/java/GrepTool.java

import java.util.List;
import java.util.Objects;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class GrepTool
{

	public GrepTool(){}

	public String grep(String pattern, List<String> flags, List<String> files)
	{
		StringBuilder result = new StringBuilder();
		boolean putFileName = (files.size() > 1);
		result.append(files.stream().map(fileName -> this.evaluateFile(fileName, pattern, flags, putFileName))
									.filter(s -> !"".equals(s.trim()))
									.collect(Collectors.joining("\n")));
		return result.toString();
	}

	private String evaluateFile(String fileName, String pattern, List<String> flags, boolean putFileName)
	{
		try{
			StringBuilder result = new StringBuilder();
			Path file = Paths.get(fileName);
			List<String> lines = Files.readAllLines(file);

			if (flags.contains("-l")){
				if (Files.lines(file).anyMatch(l -> l.contains(pattern))){
					result.append(fileName);
				}
			} else {
				String text = IntStream.range(0, lines.size()).filter(i -> this.filter(lines.get(i), pattern, flags))
															  .mapToObj(i -> this.map(i, lines.get(i), flags, (putFileName?fileName:null)))
															  .collect(Collectors.joining("\n"));
				result.append(text);
			}
			return result.toString();
		} catch (Exception e) {
			return null;
		}
	}

	private boolean filter(String line, String pattern, List<String> flags)
	{
		boolean filter = true;
		if ((flags.contains("-i")) || flags.contains("-x")){
			if ((flags.contains("-i")) && flags.contains("-x")){
				filter = line.toUpperCase().equals(pattern.toUpperCase());
			} else {
				if (flags.contains("-i")){
					filter = line.toUpperCase().contains(pattern.toUpperCase());
				}
				if (flags.contains("-x")){
					filter = line.equals(pattern);
				}
			}
		} else {
			filter = line.contains(pattern);
		}
		if (flags.contains("-v")){
			filter = !filter;
		}
		return filter;
	}

	private String map(int index, String line, List<String> flags, String fileName)
	{
		String result = "";
		if (Objects.nonNull(fileName)){
			result += fileName + ":";
		}
		if (flags.contains("-n")){
			result += (index+1) + ":";
		}
		result += line;
		return result;
	}

}

src/test/java/GrepToolTest.java

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class GrepToolTest {
    private GrepTool grepTool;

    @Before
    public void setUp() throws IOException {
        List<String> iliadText = Arrays.asList(
            "Achilles sing, O Goddess! Peleus' son;",
            "His wrath pernicious, who ten thousand woes",
            "Caused to Achaia's host, sent many a soul",
            "Illustrious into Ades premature,",
            "And Heroes gave (so stood the will of Jove)",
            "To dogs and to all ravening fowls a prey,",
            "When fierce dispute had separated once",
            "The noble Chief Achilles from the son",
            "Of Atreus, Agamemnon, King of men."
        );
        writeToFile("iliad.txt", iliadText);

        List<String> midsummerNightText = Arrays.asList(
            "I do entreat your grace to pardon me.",
            "I know not by what power I am made bold,",
            "Nor how it may concern my modesty,",
            "In such a presence here to plead my thoughts;",
            "But I beseech your grace that I may know",
            "The worst that may befall me in this case,",
            "If I refuse to wed Demetrius."
        );
        writeToFile("midsummer-night.txt", midsummerNightText);

        List<String> paradiseLostText = Arrays.asList(
            "Of Mans First Disobedience, and the Fruit",
            "Of that Forbidden Tree, whose mortal tast",
            "Brought Death into the World, and all our woe,",
            "With loss of Eden, till one greater Man",
            "Restore us, and regain the blissful Seat,",
            "Sing Heav'nly Muse, that on the secret top",
            "Of Oreb, or of Sinai, didst inspire",
            "That Shepherd, who first taught the chosen Seed"
        );
        writeToFile("paradise-lost.txt", paradiseLostText);

        grepTool = new GrepTool();
    }

    @After
    public void tearDown() throws IOException {
        deleteFile("iliad.txt");
        deleteFile("midsummer-night.txt");
        deleteFile("paradise-lost.txt");
    }

    @Test
    public void oneFileOneMatchNoFlags() {
        String expected = "Of Atreus, Agamemnon, King of men.";

        String actual = grepTool.grep(
            "Agamemnon",
            Collections.emptyList(),
            Collections.singletonList("iliad.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void oneFileOneMatchPrintLineNumbersFlag() {
        String expected = "2:Of that Forbidden Tree, whose mortal tast";

        String actual = grepTool.grep(
            "Forbidden",
            Collections.singletonList("-n"),
            Collections.singletonList("paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void oneFileOneMatchCaseInsensitiveFlag() {
        String expected = "Of that Forbidden Tree, whose mortal tast";

        String actual = grepTool.grep(
            "FORBIDDEN",
            Collections.singletonList("-i"),
            Collections.singletonList("paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void oneFileOneMatchPrintFileNamesFlag() {
        String expected = "paradise-lost.txt";

        String actual = grepTool.grep(
            "Forbidden",
            Collections.singletonList("-l"),
            Collections.singletonList("paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void oneFileOneMatchEntireLinesFlag() {
        String expected = "With loss of Eden, till one greater Man";

        String actual = grepTool.grep(
            "With loss of Eden, till one greater Man",
            Collections.singletonList("-x"),
            Collections.singletonList("paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void oneFileOneMatchMultipleFlags() {
        String expected = "9:Of Atreus, Agamemnon, King of men.";

        String actual = grepTool.grep(
            "OF ATREUS, Agamemnon, KIng of MEN.",
            Arrays.asList("-n", "-i", "-x"),
            Collections.singletonList("iliad.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void oneFileSeveralMatchesNoFlags() {
        String expected = "Nor how it may concern my modesty,\n"
            + "But I beseech your grace that I may know\n"
            + "The worst that may befall me in this case,";

        String actual = grepTool.grep(
            "may",
            Collections.emptyList(),
            Collections.singletonList("midsummer-night.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void oneFileSeveralMatchesPrintLineNumbersFlag() {
        String expected = "3:Nor how it may concern my modesty,\n"
            + "5:But I beseech your grace that I may know\n"
            + "6:The worst that may befall me in this case,";

        String actual = grepTool.grep(
            "may",
            Collections.singletonList("-n"),
            Collections.singletonList("midsummer-night.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void oneFileSeveralMatchesMatchEntireLineFlag() {
        String expected = "";

        String actual = grepTool.grep(
            "may",
            Collections.singletonList("-x"),
            Collections.singletonList("midsummer-night.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void oneFileSeveralMatchesCaseInsensitiveFlag() {
        String expected = "Achilles sing, O Goddess! Peleus' son;\n"
            + "The noble Chief Achilles from the son";

        String actual = grepTool.grep(
            "ACHILLES",
            Collections.singletonList("-i"),
            Collections.singletonList("iliad.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void oneFileSeveralMatchesInvertedFlag() {
        String expected = "Brought Death into the World, and all our woe,\n"
            + "With loss of Eden, till one greater Man\n"
            + "Restore us, and regain the blissful Seat,\n"
            + "Sing Heav'nly Muse, that on the secret top\n"
            + "That Shepherd, who first taught the chosen Seed";

        String actual = grepTool.grep(
            "Of",
            Collections.singletonList("-v"),
            Collections.singletonList("paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void oneFileNoMatchesVariousFlags() {
        String expected = "";

        String actual = grepTool.grep(
            "Gandalf",
            Arrays.asList("-n", "-l", "-x", "-i"),
            Collections.singletonList("iliad.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void oneFileOneMatchFileFlagTakesPrecedenceOverLineFlag() {
        String expected = "iliad.txt";

        String actual = grepTool.grep(
            "ten",
            Arrays.asList("-n", "-l"),
            Collections.singletonList("iliad.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void oneFileSeveralMatchesInvertedAndMatchEntireLinesFlags() {
        String expected = "Achilles sing, O Goddess! Peleus' son;\n"
            + "His wrath pernicious, who ten thousand woes\n"
            + "Caused to Achaia's host, sent many a soul\n"
            + "And Heroes gave (so stood the will of Jove)\n"
            + "To dogs and to all ravening fowls a prey,\n"
            + "When fierce dispute had separated once\n"
            + "The noble Chief Achilles from the son\n"
            + "Of Atreus, Agamemnon, King of men.";

        String actual = grepTool.grep(
            "Illustrious into Ades premature,",
            Arrays.asList("-x", "-v"),
            Collections.singletonList("iliad.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void multipleFilesOneMatchNoFlags() {
        String expected = "iliad.txt:Of Atreus, Agamemnon, King of men.";

        String actual = grepTool.grep(
            "Agamemnon",
            Collections.emptyList(),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void multipleFilesSeveralMatchesNoFlags() {
        String expected = "midsummer-night.txt:Nor how it may concern my modesty,\n"
            + "midsummer-night.txt:But I beseech your grace that I may know\n"
            + "midsummer-night.txt:The worst that may befall me in this case,";

        String actual = grepTool.grep(
            "may",
            Collections.emptyList(),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void multipleFilesSeveralMatchesPrintLineNumbersFlag() {
        String expected = "midsummer-night.txt:5:But I beseech your grace that I may know\n"
            + "midsummer-night.txt:6:The worst that may befall me in this case,\n"
            + "paradise-lost.txt:2:Of that Forbidden Tree, whose mortal tast\n"
            + "paradise-lost.txt:6:Sing Heav'nly Muse, that on the secret top";

        String actual = grepTool.grep(
            "that",
            Collections.singletonList("-n"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void multipleFilesOneMatchPrintFileNamesFlag() {
        String expected = "iliad.txt\n"
            + "paradise-lost.txt";

        String actual = grepTool.grep(
            "who",
            Collections.singletonList("-l"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void multipleFilesSeveralMatchesCaseInsensitiveFlag() {
        String expected = "iliad.txt:Caused to Achaia's host, sent many a soul\n"
            + "iliad.txt:Illustrious into Ades premature,\n"
            + "iliad.txt:And Heroes gave (so stood the will of Jove)\n"
            + "iliad.txt:To dogs and to all ravening fowls a prey,\n"
            + "midsummer-night.txt:I do entreat your grace to pardon me.\n"
            + "midsummer-night.txt:In such a presence here to plead my thoughts;\n"
            + "midsummer-night.txt:If I refuse to wed Demetrius.\n"
            + "paradise-lost.txt:Brought Death into the World, and all our woe,\n"
            + "paradise-lost.txt:Restore us, and regain the blissful Seat,\n"
            + "paradise-lost.txt:Sing Heav'nly Muse, that on the secret top";

        String actual = grepTool.grep(
            "TO",
            Collections.singletonList("-i"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void multipleFilesSeveralMatchesInvertedFlag() {
        String expected = "iliad.txt:Achilles sing, O Goddess! Peleus' son;\n"
            + "iliad.txt:The noble Chief Achilles from the son\n"
            + "midsummer-night.txt:If I refuse to wed Demetrius.";

        String actual = grepTool.grep(
            "a",
            Collections.singletonList("-v"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void multipleFilesOneMatchEntireLinesFlag() {
        String expected = "midsummer-night.txt:But I beseech your grace that I may know";

        String actual = grepTool.grep(
            "But I beseech your grace that I may know",
            Collections.singletonList("-x"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void multipleFilesOneMatchMultipleFlags() {
        String expected = "paradise-lost.txt:4:With loss of Eden, till one greater Man";

        String actual = grepTool.grep(
            "WITH LOSS OF EDEN, TILL ONE GREATER MAN",
            Arrays.asList("-n", "-i", "-x"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void multipleFilesNoMatchesVariousFlags() {
        String expected = "";

        String actual = grepTool.grep(
            "Frodo",
            Arrays.asList("-n", "-l", "-x", "-i"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void multipleFilesSeveralMatchesFileFlagTakesPrecedenceOverLineNumberFlag() {
        String expected = "iliad.txt\n"
            + "paradise-lost.txt";

        String actual = grepTool.grep(
            "who",
            Arrays.asList("-n", "-l"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    //@Ignore("Remove to run test")
    @Test
    public void multipleFilesSeveralMatchesInvertedAndMatchEntireLinesFlags() {
        String expected = "iliad.txt:Achilles sing, O Goddess! Peleus' son;\n"
            + "iliad.txt:His wrath pernicious, who ten thousand woes\n"
            + "iliad.txt:Caused to Achaia's host, sent many a soul\n"
            + "iliad.txt:And Heroes gave (so stood the will of Jove)\n"
            + "iliad.txt:To dogs and to all ravening fowls a prey,\n"
            + "iliad.txt:When fierce dispute had separated once\n"
            + "iliad.txt:The noble Chief Achilles from the son\n"
            + "iliad.txt:Of Atreus, Agamemnon, King of men.\n"
            + "midsummer-night.txt:I do entreat your grace to pardon me.\n"
            + "midsummer-night.txt:I know not by what power I am made bold,\n"
            + "midsummer-night.txt:Nor how it may concern my modesty,\n"
            + "midsummer-night.txt:In such a presence here to plead my thoughts;\n"
            + "midsummer-night.txt:But I beseech your grace that I may know\n"
            + "midsummer-night.txt:The worst that may befall me in this case,\n"
            + "midsummer-night.txt:If I refuse to wed Demetrius.\n"
            + "paradise-lost.txt:Of Mans First Disobedience, and the Fruit\n"
            + "paradise-lost.txt:Of that Forbidden Tree, whose mortal tast\n"
            + "paradise-lost.txt:Brought Death into the World, and all our woe,\n"
            + "paradise-lost.txt:With loss of Eden, till one greater Man\n"
            + "paradise-lost.txt:Restore us, and regain the blissful Seat,\n"
            + "paradise-lost.txt:Sing Heav'nly Muse, that on the secret top\n"
            + "paradise-lost.txt:Of Oreb, or of Sinai, didst inspire\n"
            + "paradise-lost.txt:That Shepherd, who first taught the chosen Seed";

        String actual = grepTool.grep(
            "Illustrious into Ades premature,",
            Arrays.asList("-x", "-v"),
            Arrays.asList("iliad.txt", "midsummer-night.txt", "paradise-lost.txt")
        );

        assertEquals(expected, actual);
    }

    private void writeToFile(String filename, List<String> contents) throws IOException {
        Path file = Paths.get(filename);
        Files.write(file, contents, Charset.forName("UTF-8"));
    }

    private void deleteFile(String filename) throws IOException {
        Files.deleteIfExists(Paths.get(filename));
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?