Last active
March 13, 2021 16:45
-
-
Save benjiman/99cc51754a06901b420febf11f240f8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.benjiweber.example; | |
import org.junit.Test; | |
import java.util.Optional; | |
import static org.junit.Assert.assertEquals; | |
import static com.benjiweber.recordmixins.OptionalPatternMatchTest.None.*; | |
public class OptionalPatternMatchTest { | |
@Test | |
public void traditional_unwrap() { | |
Optional<String> thisIsPresent = Optional.of("Hello World"); | |
assertEquals( | |
"hello world", | |
thisIsPresent | |
.map(String::toLowerCase) | |
.orElse("absent") | |
); | |
} | |
@Test | |
public void unwrap_optional() { | |
Optional<String> thisIsPresent = Optional.of("Hello World"); | |
assertEquals( | |
"hello world", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_empty_optional() { | |
Optional<String> thisIsPresent = Optional.empty(); | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_wrong_type_optional() { | |
Optional<Integer> thisIsPresent = Optional.of(5); | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_wrong_type_raw_optional() { | |
Optional thisIsPresent = Optional.of(5); | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_null() { | |
Optional<String> thisIsPresent = null; | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_unboxed_null() { | |
String thisIsPresent = null; | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_unboxed_value() { | |
String thisIsPresent = "Hello World"; | |
assertEquals( | |
"hello world", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_unboxed_value_unknown_type() { | |
Object thisIsPresent = "Hello World"; | |
assertEquals( | |
"hello world", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_unboxed_value_wrong_type() { | |
Integer thisIsPresent = 5; | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_unboxed_absent_unknown_type() { | |
Object thisIsPresent = null; | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_object_optional_empty() { | |
Object thisIsPresent = Optional.empty(); | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_object_optional() { | |
Object thisIsPresent = Optional.of("Hello World"); | |
assertEquals( | |
"hello world", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_raw_optional() { | |
Optional thisIsPresent = Optional.of("Hello World"); | |
assertEquals( | |
"hello world", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
@Test | |
public void unwrap_empty_raw_optional() { | |
Optional thisIsPresent = Optional.empty(); | |
assertEquals( | |
"absent", | |
unwrap(thisIsPresent) instanceof String s | |
? s.toLowerCase() | |
: "absent" | |
); | |
} | |
static Object unwrap(Object o) { | |
if (o instanceof Optional<?> opt) { | |
return opt.isPresent() ? opt.get() : None; | |
} else if (o != null) { | |
return o; | |
} else { | |
return None; | |
} | |
} | |
static class None { | |
private None() {} | |
public static final None None = new None(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment