-
Notifications
You must be signed in to change notification settings - Fork 939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tests for DateUtils and FileExtensions #1111
Conversation
95e20be
to
eb61444
Compare
@iSoron These tests pass, but I am a bit confused by the DateUtil implementations for: For the above methods, these implementations are returning a non-offset time/calendar. This differs from the behavior of getStartOfTomorrowWithOffset() (test 1, test 2) If this is not expected, let me know the correct expectation and I can create an issue and resolve. |
c3a90f5
to
4f87dc3
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the tests, @sgallese. I think they are good. I left just some minor comments below.
uhabits-core/src/jvmMain/java/org/isoron/uhabits/core/utils/DateUtils.kt
Outdated
Show resolved
Hide resolved
@Test | ||
fun testGetStartOfTomorrowWithOffset_priorToOffset() { | ||
val hourOffset = 3 | ||
setStartDayOffset(hourOffset, 0) | ||
setFixedTimeZone(TimeZone.getTimeZone("GMT")) | ||
val startOfTomorrowWithOffset = unixTime(2017, Calendar.JANUARY, 1, hourOffset, 0) | ||
val priorToOffset = unixTime(2017, Calendar.JANUARY, 1, hourOffset - 1, 0) | ||
setFixedLocalTime(priorToOffset) | ||
val startOfTomorrow = DateUtils.getStartOfTomorrowWithOffset() | ||
assertThat(startOfTomorrowWithOffset, equalTo(startOfTomorrow)) | ||
} | ||
|
||
@Test | ||
fun testGetStartOfTomorrowWithOffset_afterOffset() { | ||
val hourOffset = 3 | ||
setStartDayOffset(hourOffset, 0) | ||
setFixedTimeZone(TimeZone.getTimeZone("GMT")) | ||
val startOfTomorrowWithOffset = unixTime(2017, Calendar.JANUARY, 2, hourOffset, 0) | ||
val afterOffset = unixTime(2017, Calendar.JANUARY, 1, hourOffset + 1, 0) | ||
setFixedLocalTime(afterOffset) | ||
val startOfTomorrow = DateUtils.getStartOfTomorrowWithOffset() | ||
assertThat(startOfTomorrowWithOffset, equalTo(startOfTomorrow)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two tests share too much code; could you please merge them into one? It's fine to to more than one assertion per test if it removes a lot of duplication.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was able to move the first three lines to a shared helper function, but the rest of the lines are different. If I'm missing something else obvious for DRYer test code, let me know! 707b2b4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uhabits-core/src/jvmTest/java/org/isoron/uhabits/core/utils/DateUtilsTest.kt
Outdated
Show resolved
Hide resolved
@Test | ||
fun testGetStartOfTodayCalendarWithOffset_priorToOffset() { | ||
val hourOffset = 3 | ||
setStartDayOffset(hourOffset, 0) | ||
val priorToOffset = unixTime(2017, Calendar.JANUARY, 2, hourOffset - 1, 0) | ||
setFixedLocalTime(priorToOffset) | ||
setFixedLocale(Locale.GERMANY) | ||
val startOfYesterday = unixTime(2017, Calendar.JANUARY, 2, 0, 0) | ||
val expectedCalendar = GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.GERMANY) | ||
expectedCalendar.timeInMillis = startOfYesterday | ||
val startOfTodayCalendar = DateUtils.getStartOfTodayCalendar() | ||
assertThat(expectedCalendar, equalTo(startOfTodayCalendar)) | ||
} | ||
|
||
@Test | ||
fun testGetStartOfTodayCalendarWithOffset_afterOffset() { | ||
val hourOffset = 3 | ||
setStartDayOffset(hourOffset, 0) | ||
val afterOffset = unixTime(2017, Calendar.JANUARY, 1, hourOffset + 1, 0) | ||
setFixedLocalTime(afterOffset) | ||
setFixedLocale(Locale.GERMANY) | ||
val startOfToday = unixTime(2017, Calendar.JANUARY, 1, 0, 0) | ||
val expectedCalendar = GregorianCalendar(TimeZone.getTimeZone("GMT"), Locale.GERMANY) | ||
expectedCalendar.timeInMillis = startOfToday | ||
val startOfTodayCalendar = DateUtils.getStartOfTodayCalendar() | ||
assertThat(expectedCalendar, equalTo(startOfTodayCalendar)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same comment for these two tests.
It's also unclear to me. I suggest that we write tests for the current behavior. Changing it might introduce subtle bugs. The underlying issue here is that, historically, we have stored all dates as milliseconds since Jan 1, 1970, but, since we don't actually care about hours/minutes/seconds in most situations, we have also implicitly assumed that most millis are a multiple of |
4f87dc3
to
707b2b4
Compare
@iSoron OK to merge this PR? |
5c774cb
to
2615795
Compare
@sgallese Sorry, I missed the 'review requested' notification. It looks good now, thanks! |
Test coverage for FileExtensions and DateUtils
Prepare classes for migration in #1075