Skip to content

Refactor betweenTest and distinctIgnoreCase#1602

Open
wyyl1 wants to merge 3 commits into
apitable:developfrom
wyyl1:develop
Open

Refactor betweenTest and distinctIgnoreCase#1602
wyyl1 wants to merge 3 commits into
apitable:developfrom
wyyl1:develop

Conversation

@wyyl1
Copy link
Copy Markdown

@wyyl1 wyyl1 commented Feb 1, 2024

Submit a pull request for this project.

Why?

betweenTest

  • Make code cleaner:Only one thing per method
  • Testing as documentation

distinctIgnoreCase

  • Use List instead of ArrayList
  • Make code cleaner:Consistency at the abstraction level
  • Code as Documentation:Using Method Names Instead of Comments

What?

betweenTest

@Nested
class BetweenTest {

    private final LocalDateTime startDateTime = LocalDateTime.of(2022, 7, 29, 18, 34, 20);

    @TestFactory
    Stream<DynamicTest> endDateTimeLessStartDateTime() {
        return Stream.of(
            new DifferenceTime(0, 2022, 7, 29, 18, 34, 19),
            new DifferenceTime(0, 2022, 7, 29, 0, 0, 0),
            new DifferenceTime(-1, 2022, 7, 28, 18, 34, 20),
            new DifferenceTime(-2, 2022, 7, 27, 18, 34, 20)
        ).map(differenceTime -> DynamicTest.dynamicTest(String.format("%s natural time difference between %s and %s", differenceTime.expectDifferenceNaturalTime, startDateTime, differenceTime.endDateTime()),
            () -> Assertions.assertEquals(differenceTime.expectDifferenceNaturalTime(), DateTimeUtil.between(startDateTime, differenceTime.endDateTime(), ChronoField.EPOCH_DAY))));
    }

    @Test
    void return0WhenStartDateTimeEqualsEndDateTime() {
        Assertions.assertEquals(0, DateTimeUtil.between(startDateTime, startDateTime, ChronoField.EPOCH_DAY));
    }

    @TestFactory
    Stream<DynamicTest> endDateTimeGreaterStartDateTime() {
        return Stream.of(
            new DifferenceTime(0, 2022, 7, 29, 18, 34, 21),
            new DifferenceTime(1, 2022, 7, 30, 0, 0, 0),
            new DifferenceTime(1, 2022, 7, 30, 18, 34, 19),
            new DifferenceTime(1, 2022, 7, 30, 18, 34, 20),
            new DifferenceTime(1, 2022, 7, 30, 18, 34, 21),
            new DifferenceTime(2, 2022, 7, 31, 0, 0, 0),
            new DifferenceTime(3, 2022, 8, 1, 0, 0, 0)
        ).map(differenceTime -> DynamicTest.dynamicTest(String.format("%s natural time difference between %s and %s", differenceTime.expectDifferenceNaturalTime, startDateTime, differenceTime.endDateTime()),
            () -> Assertions.assertEquals(differenceTime.expectDifferenceNaturalTime(), DateTimeUtil.between(startDateTime, differenceTime.endDateTime(), ChronoField.EPOCH_DAY))));
    }

    private record DifferenceTime(int expectDifferenceNaturalTime, int year, int month, int dayOfMonth, int hour,
                                  int minute, int second) {
        LocalDateTime endDateTime(){
            return LocalDateTime.of(year, month, dayOfMonth, hour, minute, second);
        }

    }
}

distinctIgnoreCase

public static List<String> distinctIgnoreCase(Collection<String> collection) {
    if (CollectionUtils.isEmpty(collection)) {
        return new ArrayList<>();
    }

    Set<String> caseInsensitiveSet = caseInsensitiveSetOf(collection);
    return filterAndCollect(collection, caseInsensitiveSet);
}

private static Set<String> caseInsensitiveSetOf(Collection<String> collection) {
    Set<String> caseInsensitiveSet = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
    caseInsensitiveSet.addAll(collection);
    return caseInsensitiveSet;
}

private static boolean containsSearchStr(String coll, Set<String> sets) {
    return sets.stream().anyMatch(coll::equals);
}

private static List<String> filterAndCollect(Collection<String> collection, Set<String> caseInsensitiveSet) {
    return collection.stream()
        .filter(coll -> containsSearchStr(coll, caseInsensitiveSet))
        .toList();
}

How?

You can see the documentation after running the test code in idea

reactor-between-test

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Feb 1, 2024

CLA assistant check
All committers have signed the CLA.

1. Use List instead of ArrayList
2. Make code cleaner:Consistency at the abstraction level
3. Code as Documentation:Using Method Names Instead of Comments
@wyyl1 wyyl1 changed the title Refactor betweenTest Refactor betweenTest and distinctIgnoreCase Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants