forked from nus-cs2103-AY1718S1/addressbook-level3-old
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[nus-cs2103-AY1718S1#51] Add unit tests to the Utils class (nus-cs210…
…3-AY1718S1#59) * Add unit test for Utils class * Update comments
- Loading branch information
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package seedu.addressbook.common; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import org.junit.Test; | ||
|
||
public class UtilsTest { | ||
@Test | ||
public void isAnyNull() { | ||
// empty list | ||
assertFalse(Utils.isAnyNull()); | ||
|
||
// Any non-empty list | ||
assertFalse(Utils.isAnyNull(new Object(), new Object())); | ||
assertFalse(Utils.isAnyNull("test")); | ||
assertFalse(Utils.isAnyNull("")); | ||
|
||
// non empty list with just one null at the beginning | ||
assertTrue(Utils.isAnyNull((Object) null)); | ||
assertTrue(Utils.isAnyNull(null, "", new Object())); | ||
assertTrue(Utils.isAnyNull(null, new Object(), new Object())); | ||
|
||
// non empty list with nulls in the middle | ||
assertTrue(Utils.isAnyNull(new Object(), null, null, "test")); | ||
assertTrue(Utils.isAnyNull("", null, new Object())); | ||
|
||
// non empty list with one null as the last element | ||
assertTrue(Utils.isAnyNull("", new Object(), null)); | ||
assertTrue(Utils.isAnyNull(new Object(), new Object(), null)); | ||
|
||
// confirms nulls inside the list are not considered | ||
List<Object> nullList = Arrays.asList((Object) null); | ||
assertFalse(Utils.isAnyNull(nullList)); | ||
} | ||
|
||
@Test | ||
public void elementsAreUnique() throws Exception { | ||
// empty list | ||
assertAreUnique(); | ||
|
||
// only one object | ||
assertAreUnique((Object) null); | ||
assertAreUnique(1); | ||
assertAreUnique(""); | ||
assertAreUnique("abc"); | ||
|
||
// all objects unique | ||
assertAreUnique("abc", "ab", "a"); | ||
assertAreUnique(1, 2); | ||
|
||
// some identical objects | ||
assertNotUnique("abc", "abc"); | ||
assertNotUnique("abc", "", "abc", "ABC"); | ||
assertNotUnique("", "abc", "a", "abc"); | ||
assertNotUnique(1, new Integer(1)); | ||
assertNotUnique(null, 1, new Integer(1)); | ||
assertNotUnique(null, null); | ||
assertNotUnique(null, "a", "b", null); | ||
} | ||
|
||
private void assertAreUnique(Object... objects) { | ||
assertTrue(Utils.elementsAreUnique(Arrays.asList(objects))); | ||
} | ||
|
||
private void assertNotUnique(Object... objects) { | ||
assertFalse(Utils.elementsAreUnique(Arrays.asList(objects))); | ||
} | ||
} |