add unit test

This commit is contained in:
hlq07 2018-06-01 20:14:47 +08:00
parent 61cebdc290
commit a9aa0dface
5 changed files with 321 additions and 0 deletions

View File

@ -0,0 +1,56 @@
package org.cutem.cutecalendar.model;
import org.cutem.cutecalendar.testutil.TodoItemGenerator;
import org.junit.Test;
import java.util.Calendar;
import java.util.List;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class TodoItemUtilTest {
@Test
public void testIntersectedAndIn() {
TodoItemGenerator gen = new TodoItemGenerator();
List<TodoItem> items = gen.generate(5);
Calendar c1 = Calendar.getInstance();
c1.add(Calendar.DAY_OF_MONTH, -1);
Calendar c2 = Calendar.getInstance();
c2.add(Calendar.DAY_OF_MONTH, 1);
for (TodoItem i1 : items) {
boolean t1 = TodoItemUtil.isIntersectedDuring(i1, c1, c2);
assertTrue(t1);
boolean t2 = TodoItemUtil.isIn(i1, c1, c2);
assertTrue(t2);
}
for (TodoItem i1 : items) {
for (TodoItem i2 : items) {
boolean t1 = TodoItemUtil.isIntersected(i1, i2);
assertTrue(t1);
boolean t2 = TodoItemUtil.isIn(i1, i2);
assertTrue(t2);
}
}
for (TodoItem i1 : items) {
for (TodoItem i2 : items) {
boolean t1 = TodoItemUtil.isIntersectedConflict(i1, i2);
assertFalse(t1);
boolean t2 = TodoItemUtil.canBind(i1, i2);
assertTrue(t2);
}
}
}
}

View File

@ -0,0 +1,20 @@
package org.cutem.cutecalendar.testutil;
public class Args {
private Object[] mArgs;
private Args() {
// empty
}
public Object[] unwrap() {
return mArgs;
}
public static Args wrap(Object... args) {
Args res = new Args();
res.mArgs = args.clone();
return res;
}
}

View File

@ -0,0 +1,40 @@
package org.cutem.cutecalendar.testutil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class ArgsGroup {
private List<Args> mGroup = new ArrayList<>();
private ArgsGroup() {
// empty
}
public ArgsGroup(Args... group) {
mGroup.addAll(Arrays.asList(group));
}
public ArgsGroup(Collection<Args> group) {
mGroup.addAll(group);
}
public List<Args> get() {
return mGroup;
}
public static ArgsGroup group(Args... argss) {
return new ArgsGroup(argss);
}
public static ArgsGroup join(ArgsGroup... groups) {
ArgsGroup sumGroup = new ArgsGroup();
for (ArgsGroup group : groups) {
sumGroup.mGroup.addAll(group.mGroup);
}
return sumGroup;
}
}

View File

@ -0,0 +1,31 @@
package org.cutem.cutecalendar.testutil;
import org.cutem.cutecalendar.model.TodoItem;
import org.cutem.cutecalendar.model.TodoItemFactory;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class TodoItemGenerator {
public TodoItemGenerator() {
// empty
}
public List<TodoItem> generate(int count) {
List<TodoItem> res = new ArrayList<>();
TodoItemFactory fac = new TodoItemFactory();
fac.wholeDay(Calendar.getInstance());
fac.type(TodoItem.OTHERS);
for (int i = 0; i < count; i++) {
res.add(fac.buildId(i));
}
return res;
}
}

View File

@ -0,0 +1,174 @@
package org.cutem.cutecalendar.util;
import org.cutem.cutecalendar.testutil.Args;
import org.cutem.cutecalendar.testutil.ArgsGroup;
import org.junit.Test;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Calendar;
import static org.cutem.cutecalendar.testutil.Args.wrap;
import static org.junit.Assert.*;
public class CalendarUtilTest {
private Class<CalendarUtil> mClass = CalendarUtil.class;
private static void checkSMethodReturn(Object expectedRet, Method m, Args args) throws Exception {
System.out.println("method: '" + m + "' with args: " + Arrays.asList(args.unwrap()));
m.invoke(null, args.unwrap());
}
private static void checkSMethodReturn(Object expectedRet, Method m, ArgsGroup group) throws Exception {
System.out.println();
System.out.println("=== check group bgn ===");
for (Args args : group.get()) {
checkSMethodReturn(expectedRet, m, args);
}
System.out.println("=== check group end ===");
System.out.println();
}
@Test
public void testChecker() throws Exception {
Method m1 = mClass.getDeclaredMethod("isValidDateString", String.class);
Method m2 = mClass.getDeclaredMethod("isValidTimeString", String.class);
Method m3 = mClass.getDeclaredMethod("isValidCalendarString", String.class);
ArgsGroup validDateStr = ArgsGroup.group(
wrap("2016-2-29"),
wrap("2018-3-31")
);
ArgsGroup validTimeStr = ArgsGroup.group(
wrap("10:59"),
wrap("00:00"),
wrap("0:00")
);
ArgsGroup validCalendarStr = ArgsGroup.group(
wrap("2016-2-29/10:59")
);
ArgsGroup invalidDateStr = ArgsGroup.group(
wrap("2016-2-30"),
wrap("2018-2-29"),
wrap("2017-0-12")
);
ArgsGroup invalidTimeStr = ArgsGroup.group(
wrap("23:99"),
wrap("0:60")
);
ArgsGroup invalidCalendarStr = ArgsGroup.group(
wrap("2016-2-29/10:78"),
wrap("2016-2-30/1:12"),
wrap("2016-2-30/1:88")
);
ArgsGroup invalid = ArgsGroup.group(
wrap("alsdkjflksjdf"),
wrap("1998*1-12"),
wrap("asd-fasd-aa"),
wrap("gg:22")
);
checkSMethodReturn(true, m1, validDateStr);
checkSMethodReturn(true, m2, validTimeStr);
checkSMethodReturn(
false, m1,
ArgsGroup.join(validTimeStr, invalidDateStr, invalidTimeStr, invalid)
);
checkSMethodReturn(
false, m2,
ArgsGroup.join(validDateStr, invalidDateStr, invalidTimeStr, invalid)
);
checkSMethodReturn(
true, m3,
validCalendarStr
);
}
@Test
public void testSplit() throws Exception {
Calendar c = Calendar.getInstance();
c.set(2018, Calendar.MAY, 25, 11, 8);
int[] sp = CalendarUtil.splitCalendar(c);
int[] exp = new int[]{2018, 5, 25, 11, 8};
assertArrayEquals(exp, sp);
}
@Test
public void construct() throws Exception {
Calendar ans = Calendar.getInstance();
ans.set(2018, Calendar.MAY, 25, 11, 8);
String str = "2018-5-25/11:8";
Calendar c = CalendarUtil.constructCalendar(str);
int[] exp = CalendarUtil.splitCalendar(ans);
int[] cSp = CalendarUtil.splitCalendar(c);
assertArrayEquals(exp, cSp);
}
@Test
public void testAddFields() throws Exception {
Calendar c = Calendar.getInstance();
c.set(2018, Calendar.MAY, 25, 11, 8);
int[] add = new int[]{1, 2, 3, 4, 5};
c = CalendarUtil.addFields(c, add);
Calendar ans = Calendar.getInstance();
ans.set(2019, Calendar.JULY, 28, 15, 13);
int[] exp = CalendarUtil.splitCalendar(ans);
int[] cSp = CalendarUtil.splitCalendar(c);
assertArrayEquals(exp, cSp);
}
@Test
public void testToString() throws Exception {
Calendar c = CalendarUtil.roundToMinute(Calendar.getInstance());
c.set(2018, Calendar.MAY, 25, 11, 8);
String ansStr = "2018-5-25/11:8";
Calendar ans = CalendarUtil.constructCalendar(ansStr);
String c1 = CalendarUtil.toString(c);
String c2 = CalendarUtil.getDatePartString(c);
String c3 = CalendarUtil.getTimePartString(c);
Calendar n1 = CalendarUtil.constructCalendar(c1);
Calendar n2 = CalendarUtil.constructCalendar(c2, c3);
assertEquals(ans.getTimeInMillis(), n1.getTimeInMillis());
assertEquals(ans.getTimeInMillis(), n2.getTimeInMillis());
}
@Test
public void testOthers() throws Exception {
Calendar c = Calendar.getInstance();
Calendar[] whole = CalendarUtil.getWholeDayPeriod(c);
Calendar monthBgn = CalendarUtil.getMonthBegin(c);
Calendar monthEnd = CalendarUtil.getMonthEnd(c);
assertTrue(CalendarUtil.isSameDay(c, whole[0]));
assertTrue(CalendarUtil.isSameDay(c, whole[1]));
assertTrue(CalendarUtil.isSameMonth(c, whole[0]));
assertTrue(CalendarUtil.isSameMonth(c, whole[1]));
assertTrue(CalendarUtil.isSameMonth(c, monthBgn));
assertTrue(CalendarUtil.isSameMonth(c, monthEnd));
}
}