Skip to content

Commit

Permalink
fix(deps): support predicates in arguments checks
Browse files Browse the repository at this point in the history
  • Loading branch information
dimasmith committed Nov 11, 2023
1 parent 83c6bad commit 71b9866
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/main/java/net/anatolich/iris/util/Arguments.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
package net.anatolich.iris.util;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Objects;
import java.util.function.Predicate;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Arguments {

public static void checkArgument(boolean correct, String message) {
public static <T> T assertArgument(T argument, boolean correct, String message) {
if (!correct) {
throw new IllegalArgumentException(message);
}
return argument;
}

public static <T> T rejectNull(T argument, String message) {
checkArgument(argument != null, message);
public static <T> T assertArgument(T argument, Predicate<? super T> correct, String message) {
if (!correct.test(argument)) {
throw new IllegalArgumentException(message);
}
return argument;
}

public static <T> T rejectNull(T argument, String message) {
return assertArgument(argument, Objects::nonNull, message);
}

public static String rejectEmptyString(String argument, String message) {
checkArgument(argument != null && !argument.isBlank(), message);
return argument;
return assertArgument(argument, argument != null && !argument.isBlank(), message);
}
}
70 changes: 70 additions & 0 deletions src/test/java/net/anatolich/iris/util/ArgumentsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package net.anatolich.iris.util;


import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.*;

class ArgumentsTest {

public static final String ERROR_MESSAGE = "error message";

@Test
@DisplayName("check assertion")
void baseAssertionCheck() {
Object argument = "argument";
assertThatNoException()
.isThrownBy(() -> Arguments.assertArgument(argument, true, ERROR_MESSAGE));

assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> Arguments.assertArgument(argument, false, ERROR_MESSAGE));
}

@Test
@DisplayName("reject null values")
void rejectNullValues() {
Object passing = "passing";
Object failing = null;

assertThatNoException().isThrownBy(() -> Arguments.rejectNull(passing, ERROR_MESSAGE));
assertThat(Arguments.rejectNull(passing, ERROR_MESSAGE))
.isEqualTo(passing);

assertThatExceptionOfType(IllegalArgumentException.class)
.as("null values must be rejected")
.isThrownBy(() -> Arguments.rejectNull(failing, ERROR_MESSAGE));
}

@Test
@DisplayName("reject null strings")
void rejectNullStrings() {
assertThatExceptionOfType(IllegalArgumentException.class)
.as("null strings must be rejected")
.isThrownBy(() -> Arguments.rejectEmptyString(null, ERROR_MESSAGE));
}

@Test
@DisplayName("reject empty strings")
void rejectEmptyStrings() {
assertThatExceptionOfType(IllegalArgumentException.class)
.as("empty strings must be rejected")
.isThrownBy(() -> Arguments.rejectEmptyString("", ERROR_MESSAGE));
}

@Test
@DisplayName("reject blank strings")
void rejectBlankStrings() {
assertThatExceptionOfType(IllegalArgumentException.class)
.as("blank strings must be rejected")
.isThrownBy(() -> Arguments.rejectEmptyString(" ", ERROR_MESSAGE));
}

@Test
@DisplayName("accept correct strings")
void acceptCorrectStrings() {
assertThatNoException()
.as("correct strings must be accepted")
.isThrownBy(() -> Arguments.rejectEmptyString("name", ERROR_MESSAGE));
}
}

0 comments on commit 71b9866

Please sign in to comment.