Skip to content

Commit

Permalink
Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
eleonov authored and eleonov committed Dec 4, 2023
1 parent a4b8fcc commit 8cfddfc
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package software.leonov.common.util.function;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static software.leonov.common.util.function.CheckedComparator.evalUnchecked;
import static software.leonov.common.util.function.CheckedComparator.naturalOrder;

import java.util.List;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class NaturalOrderingTest {

@BeforeAll
static void setUpBeforeClass() throws Exception {
}

@AfterAll
static void tearDownAfterClass() throws Exception {
}

@BeforeEach
void setUp() throws Exception {
}

@AfterEach
void tearDown() throws Exception {
}

@Test
void test_naturalOrder_strings() throws Exception {
final List<String> actual = newArrayList("b", "a", "d", "c");

actual.sort(evalUnchecked(naturalOrder()));

assertThat(actual).isEqualTo(newArrayList("a", "b", "c", "d"));
}

@Test
void test_naturalOrder_strings_reversed() throws Exception {
final List<String> actual = newArrayList("b", "a", "d", "c");

actual.sort(evalUnchecked(CheckedComparator.<String>naturalOrder().reversed()));

assertThat(actual).isEqualTo(newArrayList("d", "c", "b", "a"));
}

@Test
void test_naturalOrder_ints() throws Exception {
final List<Integer> actual = newArrayList(3, 7, 25, -6, 8, 5);

actual.sort(evalUnchecked(naturalOrder()));

assertThat(actual).isEqualTo(newArrayList(-6, 3, 5, 7, 8, 25));
}

@Test
void test_naturalOrder_ints_reversed() throws Exception {
final List<Integer> actual = newArrayList(3, 7, 25, -6, 8, 5);

actual.sort(evalUnchecked(CheckedComparator.<Integer>naturalOrder().reversed()));

assertThat(actual).isEqualTo(newArrayList(25, 8, 7, 5, 3, -6));
}

@Test
void test_naturalOrder_ints_npe() throws Exception {
final List<Integer> actual = newArrayList(3, 7, 25, null, -6, 8, 5);

assertThrows(NullPointerException.class, () -> actual.sort(evalUnchecked(naturalOrder())));
}

}
90 changes: 90 additions & 0 deletions src/test/java/software/leonov/common/util/function/NullsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package software.leonov.common.util.function;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.truth.Truth.assertThat;
import static software.leonov.common.util.function.CheckedComparator.evalUnchecked;
import static software.leonov.common.util.function.CheckedComparator.naturalOrder;
import static software.leonov.common.util.function.CheckedComparator.nullsFirst;
import static software.leonov.common.util.function.CheckedComparator.nullsLast;

import java.util.List;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class NullsTest {

@BeforeAll
static void setUpBeforeClass() throws Exception {
}

@AfterAll
static void tearDownAfterClass() throws Exception {
}

@BeforeEach
void setUp() throws Exception {
}

@AfterEach
void tearDown() throws Exception {
}

@Test
void test_nullsFirst_naturalOrder() throws Exception {
final List<String> actual = newArrayList("b", "a", null, "d", "c", null);

actual.sort(evalUnchecked(nullsFirst(naturalOrder())));

assertThat(actual).isEqualTo(newArrayList(null, null, "a", "b", "c", "d"));
}

@Test
void test_nullsFirst_naturalOrder_reversed() throws Exception {
final List<String> actual = newArrayList("b", "a", null, "d", "c", null);

actual.sort(evalUnchecked(nullsFirst(CheckedComparator.<String>naturalOrder()).reversed()));

assertThat(actual).isEqualTo(newArrayList("d", "c", "b", "a", null, null));
}

@Test
void test_nullsLast_naturalOrder() throws Exception {
final List<String> actual = newArrayList("b", "a", null, "d", "c", null);

actual.sort(evalUnchecked(nullsLast(naturalOrder())));

assertThat(actual).isEqualTo(newArrayList("a", "b", "c", "d", null, null));
}

@Test
void test_nullsLast_naturalOrder_reversed() throws Exception {
final List<String> actual = newArrayList("b", "a", null, "d", "c", null);

actual.sort(evalUnchecked(nullsLast(CheckedComparator.<String>naturalOrder()).reversed()));

assertThat(actual).isEqualTo(newArrayList(null, null, "d", "c", "b", "a"));
}

@Test
void test_nullsFirst_null() throws Exception {
final List<String> actual = newArrayList("b", "a", null, "d", "c", null);

actual.sort(evalUnchecked(nullsFirst(null)));

assertThat(actual).isEqualTo(newArrayList(null, null, "b", "a", "d", "c"));
}

@Test
void test_nullsFirst_last() throws Exception {
final List<String> actual = newArrayList("b", "a", null, "d", "c", null);

actual.sort(evalUnchecked(nullsLast(null)));

assertThat(actual).isEqualTo(newArrayList("b", "a", "d", "c", null, null));
}

}
27 changes: 0 additions & 27 deletions src/test/java/software/leonov/common/util/function/Test.java

This file was deleted.

0 comments on commit 8cfddfc

Please sign in to comment.