Added more tests.

This commit is contained in:
Erik C. Thauvin 2021-11-17 08:05:45 -08:00
parent e462a70147
commit 7c270ed872
3 changed files with 29 additions and 8 deletions

View file

@ -158,6 +158,7 @@ sonarqube {
property('sonar.organization', 'ethauvin-github') property('sonar.organization', 'ethauvin-github')
property('sonar.projectKey', 'ethauvin_mobibot') property('sonar.projectKey', 'ethauvin_mobibot')
property('sonar.host.url', 'https://sonarcloud.io') property('sonar.host.url', 'https://sonarcloud.io')
property('sonar.coverage.jacoco.xmlReportPaths', "${project.buildDir}/reports/kover/report.xml")
} }
} }
@ -174,7 +175,7 @@ jacocoTestReport {
} }
tasks.sonarqube { tasks.sonarqube {
dependsOn 'jacocoTestReport' dependsOn 'koverReport'
} }
task copyToDeploy(type: Copy) { task copyToDeploy(type: Copy) {

View file

@ -44,7 +44,6 @@ import org.pircbotx.hooks.events.PrivateMessageEvent
import org.pircbotx.hooks.types.GenericMessageEvent import org.pircbotx.hooks.types.GenericMessageEvent
class View : AbstractCommand() { class View : AbstractCommand() {
private val maxEntries = 6
override val name = VIEW_CMD override val name = VIEW_CMD
override val help = listOf( override val help = listOf(
"To list or search the current URL posts:", "To list or search the current URL posts:",
@ -55,6 +54,7 @@ class View : AbstractCommand() {
override val isVisible = true override val isVisible = true
companion object { companion object {
const val MAX_ENTRIES = 6
const val VIEW_CMD = "view" const val VIEW_CMD = "view"
} }
@ -70,8 +70,8 @@ class View : AbstractCommand() {
internal fun parseArgs(args: String): Pair<Int, String> { internal fun parseArgs(args: String): Pair<Int, String> {
var query = args.lowercase().trim() var query = args.lowercase().trim()
var start = 0 var start = 0
if (query.isEmpty() && entries.links.size > maxEntries) { if (query.isEmpty() && entries.links.size > MAX_ENTRIES) {
start = entries.links.size - maxEntries start = entries.links.size - MAX_ENTRIES
} }
if (query.matches("^\\d+(| .*)".toRegex())) { // view [<start>] [<query>] if (query.matches("^\\d+(| .*)".toRegex())) { // view [<start>] [<query>]
val split = query.split(" ", limit = 2) val split = query.split(" ", limit = 2)
@ -96,7 +96,7 @@ class View : AbstractCommand() {
var index = start var index = start
var entry: EntryLink var entry: EntryLink
var sent = 0 var sent = 0
while (index < entries.links.size && sent < maxEntries) { while (index < entries.links.size && sent < MAX_ENTRIES) {
entry = entries.links[index] entry = entries.links[index]
if (query.isNotBlank()) { if (query.isNotBlank()) {
if (entry.matches(query)) { if (entry.matches(query)) {
@ -108,7 +108,7 @@ class View : AbstractCommand() {
sent++ sent++
} }
index++ index++
if (sent == maxEntries && index < entries.links.size) { if (sent == MAX_ENTRIES && index < entries.links.size) {
event.sendMessage("To view more, try: ") event.sendMessage("To view more, try: ")
event.sendMessage( event.sendMessage(
helpFormat( helpFormat(

View file

@ -44,7 +44,7 @@ class ViewTest {
fun testParseArgs() { fun testParseArgs() {
val view = View() val view = View()
for (i in 1..3) { for (i in 1..10) {
LinksMgr.entries.links.add( LinksMgr.entries.links.add(
EntryLink( EntryLink(
"https://www.example.com/$i", "https://www.example.com/$i",
@ -77,8 +77,28 @@ class ViewTest {
prop(Pair<Int, String>::second).isEqualTo("foo bar") prop(Pair<Int, String>::second).isEqualTo("foo bar")
} }
assertThat(view.parseArgs("5"), "parseArgs(5)").all { assertThat(view.parseArgs("foo bar"), "parseArgs(foo bar)").all {
prop(Pair<Int, String>::first).isEqualTo(0) prop(Pair<Int, String>::first).isEqualTo(0)
prop(Pair<Int, String>::second).isEqualTo("foo bar")
}
assertThat(view.parseArgs("${Int.MAX_VALUE}1"), "parseArgs(overflow)").all {
prop(Pair<Int, String>::first).isEqualTo(0)
prop(Pair<Int, String>::second).isEqualTo("${Int.MAX_VALUE}1")
}
assertThat(view.parseArgs("1a"), "parseArgs(1a)").all {
prop(Pair<Int, String>::first).isEqualTo(0)
prop(Pair<Int, String>::second).isEqualTo("1a")
}
assertThat(view.parseArgs("20"), "parseArgs(20)").all {
prop(Pair<Int, String>::first).isEqualTo(0)
prop(Pair<Int, String>::second).isEqualTo("")
}
assertThat(view.parseArgs(""), "parseArgs()").all {
prop(Pair<Int, String>::first).isEqualTo(LinksMgr.entries.links.size - View.MAX_ENTRIES)
prop(Pair<Int, String>::second).isEqualTo("") prop(Pair<Int, String>::second).isEqualTo("")
} }