Skip to content

Commit

Permalink
[nus-cs2103-AY1718S1#51] Add unit tests to the Utils class (nus-cs210…
Browse files Browse the repository at this point in the history
…3-AY1718S1#59)

* Add unit test for Utils class

* Update comments
  • Loading branch information
zzzzwen authored and pyokagan committed Dec 24, 2016
1 parent 45efdd1 commit 922643c
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions test/java/seedu/addressbook/common/UtilsTest.java
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)));
}
}

0 comments on commit 922643c

Please sign in to comment.