Skip to content

Commit

Permalink
PIVOT-799, small enhancements and more tests
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/pivot/trunk@1691348 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Sandro Martini committed Jul 16, 2015
1 parent ed55267 commit ab3e003
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
10 changes: 9 additions & 1 deletion core/src/org/apache/pivot/functional/monad/Option.java
Expand Up @@ -58,9 +58,17 @@ public T getValueOrElse(final T alternativeValue) {
return (hasValue() == true) ? getValue() : alternativeValue;
}

/**
* Return the value contained in the Option, or null if it hasn't a value set.
* @return value if set, otherwise null
*/
public T getValueOrNull() {
return getValueOrElse(null);
}

@Override
public String toString() {
return "Monad(" + ((value != null) ? value.toString() : "null") + ")";
return "Option(" + ((value != null) ? value.toString() : "null") + ")";
}

@Override
Expand Down
47 changes: 39 additions & 8 deletions core/test/org/apache/pivot/functional/monad/test/OptionTest.java
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.pivot.functional.monad.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -149,16 +150,13 @@ public void companionRealUsageRandomTest() {

@Test
public void optionSomeTest() {
OptionCompanion<String> o = OptionCompanion.getInstance();
assertNotNull(o);

// sample by direct instancing of Some/None classes, but discouraged

Option<String> os = null;
String tsValue = null;

// store the value in the Option instance (Some if not null, otherwise None)
os = new Some<>("Optional value");
System.out.println("optionSomeTest(), instance variable is " + os);
assertTrue(os != null);

// verify the value stored
Expand All @@ -168,21 +166,23 @@ public void optionSomeTest() {
tsValue = os.getValue();
System.out.println("optionSomeTest(), value stored is " + tsValue);
assertTrue(tsValue != null);
// test with alternative value
tsValue = os.getValueOrElse("Alternative value");
assertEquals("Optional value", tsValue);
tsValue = os.getValueOrNull();
assertEquals("Optional value", tsValue);
}

@Test
public void optionNoneTest() {
OptionCompanion<String> o = OptionCompanion.getInstance();
assertNotNull(o);

// sample by direct instancing of Some/None classes, but discouraged

Option<String> os = null;
String tsValue = null;

// store the value in the Option instance (Some if not null, otherwise None)
// os = new None<>(); // discouraged
os = None.getInstance(); // better
System.out.println("optionNoneTest(), instance variable is " + os);
assertTrue(os != null);

// verify the value stored
Expand All @@ -196,6 +196,37 @@ public void optionNoneTest() {
System.err.println("optionNoneTest(), got RuntimeException " + e);
assertTrue(os.hasValue() == false);
}
// test with alternative value
tsValue = os.getValueOrElse("Alternative value");
assertEquals("Alternative value", tsValue);
tsValue = os.getValueOrNull();
assertEquals(null, tsValue);
}

@Test
public void optionSomeEqualsTest() {
// sample by direct instancing of Some/None classes, but discouraged
Option<String> os1 = new Some<>("Optional value 1");
System.out.println("optionSomeEqualsTest(), instance variable 1 is " + os1);
Option<String> os2 = new Some<>("Optional value 2");
System.out.println("optionSomeEqualsTest(), instance variable 2 is " + os2);

// verify the value stored
System.out.println("optionSomeEqualsTest(), two instances are not the same object " + (os1 != os2));
assertTrue(os1 != os2);
}

@Test
public void optionNoneEqualsTest() {
// sample by direct instancing of Some/None classes, but discouraged
None<String> on1 = None.getInstance();
System.out.println("optionNoneEqualsTest(), instance variable 1 is " + on1);
None<String> on2 = None.getInstance();
System.out.println("optionNoneEqualsTest(), instance variable 2 is " + on2);

// verify the value stored
System.out.println("optionNoneEqualsTest(), two instances are the same object " + (on1 == on2));
assertTrue(on1 == on2);
}

}

0 comments on commit ab3e003

Please sign in to comment.