-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5358 from XiangRongLin/testable_prettytime
Make Localization.relativeTime testable
- Loading branch information
Showing
4 changed files
with
74 additions
and
46 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
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
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
40 changes: 40 additions & 0 deletions
40
app/src/test/java/org/schabi/newpipe/util/LocalizationTest.kt
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,40 @@ | ||
package org.schabi.newpipe.util | ||
|
||
import org.junit.Assert.assertEquals | ||
import org.junit.Test | ||
import org.ocpsoft.prettytime.PrettyTime | ||
import java.text.SimpleDateFormat | ||
import java.time.OffsetDateTime | ||
import java.time.ZoneOffset | ||
import java.util.GregorianCalendar | ||
import java.util.Locale | ||
|
||
class LocalizationTest { | ||
|
||
@Test | ||
fun `After initializing pretty time relativeTime() with a Calendar must work`() { | ||
val reference = SimpleDateFormat("yyyy/MM/dd").parse("2021/1/1") | ||
Localization.initPrettyTime(PrettyTime(reference, Locale.ENGLISH)) | ||
|
||
val actual = Localization.relativeTime(GregorianCalendar(2021, 1, 6)) | ||
|
||
// yes this assertion is true, even if it should be 5 days, it works as it is. Future research required. | ||
assertEquals("1 month from now", actual) | ||
} | ||
|
||
@Test(expected = NullPointerException::class) | ||
fun `relativeTime() must fail without initializing pretty time`() { | ||
Localization.relativeTime(GregorianCalendar(2021, 1, 6)) | ||
} | ||
|
||
@Test | ||
fun `relativeTime() with a OffsetDateTime must work`() { | ||
val reference = SimpleDateFormat("yyyy/MM/dd").parse("2021/1/1") | ||
Localization.initPrettyTime(PrettyTime(reference, Locale.ENGLISH)) | ||
|
||
val offset = OffsetDateTime.of(2021, 1, 6, 0, 0, 0, 0, ZoneOffset.UTC) | ||
val actual = Localization.relativeTime(offset) | ||
|
||
assertEquals("5 days from now", actual) | ||
} | ||
} |