mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Tests for DateUtils and FileExtensions
This commit is contained in:
@@ -77,11 +77,11 @@ abstract class DateUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun getLocalTime(): Long {
|
fun getLocalTime(testTimeInMillis: Long? = null): Long {
|
||||||
if (fixedLocalTime != null) return fixedLocalTime as Long
|
if (fixedLocalTime != null) return fixedLocalTime as Long
|
||||||
|
|
||||||
val tz = getTimeZone()
|
val tz = getTimeZone()
|
||||||
val now = Date().time
|
val now = testTimeInMillis ?: Date().time
|
||||||
return now + tz.getOffset(now)
|
return now + tz.getOffset(now)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,7 +100,7 @@ abstract class DateUtils {
|
|||||||
format: Int,
|
format: Int,
|
||||||
firstWeekDay: Int
|
firstWeekDay: Int
|
||||||
): Array<String> {
|
): Array<String> {
|
||||||
val calendar = GregorianCalendar()
|
val calendar = GregorianCalendar(getLocale())
|
||||||
calendar.set(DAY_OF_WEEK, firstWeekDay)
|
calendar.set(DAY_OF_WEEK, firstWeekDay)
|
||||||
|
|
||||||
val daysNullable = ArrayList<String>()
|
val daysNullable = ArrayList<String>()
|
||||||
@@ -149,7 +149,7 @@ abstract class DateUtils {
|
|||||||
*/
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun getFirstWeekdayNumberAccordingToLocale(): Int {
|
fun getFirstWeekdayNumberAccordingToLocale(): Int {
|
||||||
return GregorianCalendar().firstDayOfWeek
|
return GregorianCalendar(getLocale()).firstDayOfWeek
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -58,6 +58,65 @@ class DateUtilsTest : BaseUnitTest() {
|
|||||||
assertThat(formatted, equalTo("Thu\n31"))
|
assertThat(formatted, equalTo("Thu\n31"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetLocalTime() {
|
||||||
|
setFixedLocalTime(null)
|
||||||
|
setFixedTimeZone(TimeZone.getTimeZone("Australia/Sydney"))
|
||||||
|
val utcTestTimeInMillis = unixTime(2015, Calendar.JANUARY, 11)
|
||||||
|
val localTimeInMillis = DateUtils.getLocalTime(utcTestTimeInMillis)
|
||||||
|
val expectedUnixTimeOffsetForSydney = 11 * 60 * 60 * 1000
|
||||||
|
val expectedUnixTimeForSydney = utcTestTimeInMillis + expectedUnixTimeOffsetForSydney
|
||||||
|
assertThat(expectedUnixTimeForSydney, equalTo(localTimeInMillis))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetWeekdaySequence() {
|
||||||
|
val weekdaySequence = DateUtils.getWeekdaySequence(3)
|
||||||
|
assertThat(arrayOf(3, 4, 5, 6, 7, 1, 2), equalTo(weekdaySequence))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetFirstWeekdayNumberAccordingToLocale_germany() {
|
||||||
|
setFixedLocale(Locale.GERMANY)
|
||||||
|
val firstWeekdayNumber = DateUtils.getFirstWeekdayNumberAccordingToLocale()
|
||||||
|
assertThat(2, equalTo(firstWeekdayNumber))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetFirstWeekdayNumberAccordingToLocale_us() {
|
||||||
|
setFixedLocale(Locale.US)
|
||||||
|
val firstWeekdayNumber = DateUtils.getFirstWeekdayNumberAccordingToLocale()
|
||||||
|
assertThat(1, equalTo(firstWeekdayNumber))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetLongWeekdayNames_germany() {
|
||||||
|
setFixedLocale(Locale.GERMANY)
|
||||||
|
val longWeekdayNames = DateUtils.getLongWeekdayNames(Calendar.SATURDAY)
|
||||||
|
assertThat(arrayOf("Samstag", "Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag"), equalTo(longWeekdayNames))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetLongWeekdayNames_us() {
|
||||||
|
setFixedLocale(Locale.US)
|
||||||
|
val longWeekdayNames = DateUtils.getLongWeekdayNames(Calendar.SATURDAY)
|
||||||
|
assertThat(arrayOf("Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"), equalTo(longWeekdayNames))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetShortWeekdayNames_germany() {
|
||||||
|
setFixedLocale(Locale.GERMANY)
|
||||||
|
val longWeekdayNames = DateUtils.getShortWeekdayNames(Calendar.SATURDAY)
|
||||||
|
assertThat(arrayOf("Sa.", "So.", "Mo.", "Di.", "Mi.", "Do.", "Fr."), equalTo(longWeekdayNames))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetShortWeekdayNames_us() {
|
||||||
|
setFixedLocale(Locale.US)
|
||||||
|
val longWeekdayNames = DateUtils.getShortWeekdayNames(Calendar.SATURDAY)
|
||||||
|
assertThat(arrayOf("Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"), equalTo(longWeekdayNames))
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun testTruncate_dayOfWeek() {
|
fun testTruncate_dayOfWeek() {
|
||||||
val field = DateUtils.TruncateField.WEEK_NUMBER
|
val field = DateUtils.TruncateField.WEEK_NUMBER
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package org.isoron.uhabits.core.utils
|
||||||
|
|
||||||
|
import org.isoron.uhabits.core.BaseUnitTest
|
||||||
|
import org.junit.Test
|
||||||
|
import java.io.File
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
|
class FileExtensionsTest : BaseUnitTest() {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testIsSQLite3File() {
|
||||||
|
val file = File.createTempFile("asset", "")
|
||||||
|
copyAssetToFile("loop.db", file)
|
||||||
|
val isSqlite3File = file.isSQLite3File()
|
||||||
|
assertTrue(isSqlite3File)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user