Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mikhail Sidnev #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 103 additions & 2 deletions src/test/java/option/OptionalExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import org.junit.Test;

import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Function;
import java.util.function.Predicate;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

public class OptionalExample {
Expand Down Expand Up @@ -52,7 +55,105 @@ public void map() {

private Optional<String> getOptional() {
return ThreadLocalRandom.current().nextBoolean()
? Optional.empty()
: Optional.of("abc");
? Optional.empty()
: Optional.of("abc");
}

@Test
public void orElseGet() throws Exception {

Optional<String> optional = getOptional();

String other = "other";

String expected = optional.orElseGet(() -> other);

String actual = optional.isPresent() ? optional.get() : other;

assertEquals(expected, actual);

}

@Test
public void orElse() throws Exception {

Optional<String> optional = getOptional();

String other = "other";

String expected = optional.orElse(other);

String actual = optional.isPresent() ? optional.get() : other;

assertEquals(expected, actual);

}


@Test
public void orElseTrow() throws Exception {
boolean isOptionalTrow = false;
String extended = "";

Optional<String> optional = getOptional();
try {
extended = optional.orElseThrow(NoSuchElementException::new);
} catch (NoSuchElementException e) {
isOptionalTrow = true;
}

boolean isImplementationThrow = false;

String actual = "";
try {
if (optional.isPresent()) {
actual = optional.get();
} else {
throw new NoSuchElementException();
}
} catch (NoSuchElementException e) {
isImplementationThrow = true;
}


assertEquals(isOptionalTrow, isImplementationThrow);

if (!isOptionalTrow)
assertEquals(extended, actual);

}

@Test
public void filter() throws Exception {
Optional<String> optional = getOptional();

String s = "abc";
Optional<String> abc = optional.filter(Predicate.isEqual(s));

Optional<String> actual = optional.isPresent() ?
optional.get().equals(s) ?
optional :
Optional.empty() :
Optional.empty();

assertEquals(abc, actual);
}

@Test
public void flatMap() throws Exception {

Optional<String> optional = getOptional();

Optional<char[]> expected = optional.flatMap(s1 -> Optional.of(s1.toCharArray()));

Optional<char[]> actual = optional.isPresent() ?
Optional.of(optional.get().toCharArray()) :
Optional.empty();

if (expected.isPresent()) {
assertArrayEquals(expected.get(), actual.get());
} else {
assertEquals(expected, actual);
}
}
}