Skip to content

Commit

Permalink
some static tests
Browse files Browse the repository at this point in the history
  • Loading branch information
slpixe committed Jun 2, 2024
1 parent fa34b7e commit 8e822cc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 21 deletions.
30 changes: 13 additions & 17 deletions src/main/java/org/example/PhoneStatic.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,15 @@
*/
public class PhoneStatic {

/**
* A list to store the lines of phone numbers read from the file.
*/
public List<String> lines = null;
public static final String FILE_PATH = "src/main/resources/fileTest.txt";

/**
* Default constructor.
*/
PhoneStatic(){}

/**
* Reads all lines from the file "src/main/resources/fileTest.txt" and stores them in the 'lines' field.
* If an IOException occurs during the reading process, it will be caught and printed to the console.
*/
public void readAllLinesAndStoreInField() {
try {
Path file = Paths.get("src/main/resources/fileTest.txt");
lines = Files.readAllLines(file, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
static public List<String> readAllLinesAndReturn() {
return PhoneStatic.readAllLinesAndReturn(FILE_PATH);
}

/**
Expand All @@ -43,13 +31,21 @@ public void readAllLinesAndStoreInField() {
*
* @return A List of strings representing the lines from the file.
*/
static public List<String> readAllLinesAndReturn() {
static public List<String> readAllLinesAndReturn(String filePath) {
try {
Path file = Paths.get("src/main/resources/fileTest.txt");
Path file = Paths.get(filePath);
return Files.readAllLines(file, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
return List.of();
}

static public List<String> removeComments(List<String> lines) {
return lines.stream().filter(line ->!line.startsWith("#")).toList();
}

static public List<String> removeEmptyLines(List<String> lines) {
return lines.stream().filter(line ->!line.isEmpty()).toList();
}
}
54 changes: 50 additions & 4 deletions src/test/java/org/example/PhoneStaticTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,56 @@
import static org.hamcrest.MatcherAssert.assertThat;

class PhoneStaticTest {

@Test
void readAllLinesAndReturn() {
//arrange
//act
List<String> lines = PhoneStatic.readAllLinesAndReturn();

//assert
assertThat(lines.size(), org.hamcrest.Matchers.greaterThan(5));
assertThat(lines.get(0), org.hamcrest.Matchers.equalTo("#Valid phone numbers:"));
}

@Test
public void readAllLinesFromMethod() {
List<String> expectedList = List.of("Hello, world!", "My Name is bob");
List<String> meow = PhoneStatic.readAllLinesAndReturn();
Assertions.assertIterableEquals(meow, expectedList);
void readAllLinesAndReturnWithArg() {
//arrange
final String FILE_PATH = "src/main/resources/fileTest.txt";

//act
List<String> lines = PhoneStatic.readAllLinesAndReturn(FILE_PATH);

//assert
assertThat(lines.size(), org.hamcrest.Matchers.greaterThan(5));
assertThat(lines.get(0), org.hamcrest.Matchers.equalTo("#Valid phone numbers:"));
}

@Test
void removeComments() {
//arrange
List<String> lines = PhoneStatic.readAllLinesAndReturn();

//act
List<String> linesWithoutComments = PhoneStatic.removeComments(lines);

//assert
Assertions.assertTrue(linesWithoutComments.size() > 5);
Assertions.assertFalse(linesWithoutComments.get(0).startsWith("#"));
}

@Test
void removeEmptyLines() {
//arrange
List<String> lines = PhoneStatic.readAllLinesAndReturn();

//act
List<String> linesWithoutEmptyLines = PhoneStatic.removeEmptyLines(lines);

//assert
// lines starts at 12 rows, after running removeEmptyLines it goes to 11 rows, calling it multiple times keeps it at 11 rows
Assertions.assertEquals(13, lines.size());
Assertions.assertEquals(12, linesWithoutEmptyLines.size());
Assertions.assertFalse(linesWithoutEmptyLines.get(0).isEmpty());
}
}

0 comments on commit 8e822cc

Please sign in to comment.