Skip to content

Commit

Permalink
Add test cases for lambdas functionality
Browse files Browse the repository at this point in the history
Three new unit test files have been added: FileFilterTest, LazySupplierTest, and PredicateTest. These tests will validate various features and behaviors related to Java lambda expressions. Specifically, testing file filtering, the use of suppliers and predicates, and functionality with both expression and block lambda syntax.
  • Loading branch information
kousen committed Feb 13, 2024
1 parent 1af8ec8 commit dfde2d7
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/test/java/lambdas/FileFilterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package lambdas;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileFilter;

import static java.util.Map.*;
import static org.assertj.core.api.Assertions.assertThat;

public class FileFilterTest {
private final File root = new File("src/main/java");

@Test
void listFiles() {
File[] files = root.listFiles();
assert files != null;
assertThat(files.length).isEqualTo(22);
}

@Test
void listDirectories_anonInnerClass() {
File[] directories = root.listFiles(new java.io.FileFilter() {
@Override
public boolean accept(File file) {
return file.isDirectory();
}
});
assert directories != null;
assertThat(directories.length).isEqualTo(14);
}

@Test
void listDirectories_expressionLambda() {
File[] directories = root.listFiles(file -> file.isDirectory());
assert directories != null;
assertThat(directories.length).isEqualTo(14);
}

@Test
void listJavaSrcFiles_fileFilter() {
File[] javaSrcFiles = root.listFiles(file -> file.getName()
.endsWith(".java"));
assert javaSrcFiles != null;
assertThat(javaSrcFiles.length).isEqualTo(8);
}

@Test
void listJavaSrcFiles_fileNameFilter() {
File[] javaSrcFiles = root.listFiles((File dir, String name) -> name.endsWith(".java"));
assert javaSrcFiles != null;
assertThat(javaSrcFiles.length).isEqualTo(8);
}

@Test
void listJavaSrcFiles_fileFilter_variable() {
FileFilter filter = (File file) -> file.getName()
.endsWith(".java");
File[] javaSrcFiles = root.listFiles(filter);
assert javaSrcFiles != null;
assertThat(javaSrcFiles.length).isEqualTo(8);
}

@Test
void forEachMap() {
ofEntries(
entry("a", 1),
entry("b", 2),
entry("c", 2))
.forEach((k, v) -> System.out.println(k + " -> " + v));
}
}
35 changes: 35 additions & 0 deletions src/test/java/lambdas/LazySupplierTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package lambdas;

import org.junit.jupiter.api.Test;

import java.util.logging.Logger;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class LazySupplierTest {
private final Logger logger = Logger.getLogger(LazySupplierTest.class.getName());

private String getErrorMessage() {
System.out.println("calling getErrorMessage()");
return "An error occurred";
}

private String getLogMessage() {
System.out.println("calling getLogMessage()");
return "log message";
}

@Test
void eagerMessages() {
boolean condition = true;
assertTrue(condition, getErrorMessage());
logger.fine(getLogMessage());
}

@Test
void lazyMessages() {
boolean condition = true;
assertTrue(condition, () -> getErrorMessage());
logger.fine(() -> getLogMessage());
}
}
47 changes: 47 additions & 0 deletions src/test/java/lambdas/PredicateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package lambdas;

import org.junit.jupiter.api.Test;

import java.util.function.Predicate;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class PredicateTest {

@Test
void implementPredicateAsAnonInnerClass() {
Predicate<String> p = new Predicate<String>() {
@Override
public boolean test(String s) {
return s.length() > 5;
}
};
assertFalse(p.test("Hello"));
assertTrue(p.test("Hello World"));
}

@Test
void implementPredicateAsExpressionLambda() {
Predicate<String> p = s -> s.length() > 5;
assertFalse(p.test("Hello"));
assertTrue(p.test("Hello World"));
}

@Test
void implementPredicateAsBlockLambda() {
Predicate<String> p = s -> {
System.out.println("Evaluating whether s length > 5: " + s);
return s.length() > 5;
};
assertFalse(p.test("Hello"));
assertTrue(p.test("Hello World"));
}

@Test
void testRunnable() {
Runnable r = () -> System.out.println("Hello World");
Thread thread = new Thread(r);
thread.start();
}
}

0 comments on commit dfde2d7

Please sign in to comment.