This repository has been archived by the owner on Nov 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #51 from /issues/42
Fix #42: First time?
- Loading branch information
Showing
6 changed files
with
141 additions
and
0 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
66 changes: 66 additions & 0 deletions
66
core/src/main/kotlin/me/madhead/imgmacrobot/core/generators/FirstTime.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,66 @@ | ||
package me.madhead.imgmacrobot.core.generators | ||
|
||
import dev.inmo.tgbotapi.types.InlineQueries.abstracts.InlineQuery | ||
import io.micrometer.core.instrument.MeterRegistry | ||
import me.madhead.imgmacrobot.core.ParagraphsImageMacroGenerator | ||
import me.madhead.imgmacrobot.core.ParsedInlineQuery | ||
import me.madhead.imgmacrobot.core.dao.CachedInlineQueryResultDAO | ||
import me.madhead.imgmacrobot.imgur.Imgur | ||
import org.jetbrains.skija.Canvas | ||
import org.jetbrains.skija.Image | ||
import org.jetbrains.skija.paragraph.FontCollection | ||
import java.nio.file.Path | ||
import kotlin.io.path.ExperimentalPathApi | ||
|
||
/** | ||
* Never try your luck twice. | ||
*/ | ||
@ExperimentalPathApi | ||
class FirstTime( | ||
templatesDir: Path, | ||
imgur: Imgur, | ||
fontCollection: FontCollection, | ||
cachedInlineQueryResultDAO: CachedInlineQueryResultDAO, | ||
registry: MeterRegistry, | ||
) : ParagraphsImageMacroGenerator<FirstTimeParsedInlineQuery>( | ||
templatesDir, | ||
"first time.png", | ||
imgur, | ||
fontCollection, | ||
cachedInlineQueryResultDAO, | ||
registry, | ||
) { | ||
companion object { | ||
private val macroIdRegex = | ||
"(first time):(\\s*)(?<bottom>.+?)" | ||
.toRegex(RegexOption.IGNORE_CASE) | ||
} | ||
|
||
@Suppress("ReturnCount") | ||
override fun parseInlineQuery(inlineQuery: InlineQuery): FirstTimeParsedInlineQuery? { | ||
return macroIdRegex.matchEntire(inlineQuery.query)?.groups?.let { groups -> | ||
FirstTimeParsedInlineQuery( | ||
groups["bottom"]?.value ?: return null, | ||
) | ||
} | ||
} | ||
|
||
override fun drawParagraphs(template: Image, canvas: Canvas, parsedInlineQuery: FirstTimeParsedInlineQuery) { | ||
imageMacroParagraph(parsedInlineQuery.bottom.toUpperCase()) { | ||
layout(@Suppress("MagicNumber") 0.9F * template.width.toFloat()) | ||
paint(canvas, @Suppress("MagicNumber") 0.05F * template.width, template.height - @Suppress("MagicNumber") 10 - height) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* [ParsedInlineQuery] implementation for [FirstTime]. | ||
* | ||
* @property bottom bottom text. | ||
*/ | ||
data class FirstTimeParsedInlineQuery( | ||
val bottom: String, | ||
) : ParsedInlineQuery { | ||
override val discriminator: String | ||
get() = bottom | ||
} |
56 changes: 56 additions & 0 deletions
56
core/src/test/kotlin/me/madhead/imgmacrobot/core/generators/FirstTimeTest.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,56 @@ | ||
package me.madhead.imgmacrobot.core.generators | ||
|
||
import dev.inmo.tgbotapi.types.ChatId | ||
import dev.inmo.tgbotapi.types.CommonUser | ||
import dev.inmo.tgbotapi.types.InlineQueries.abstracts.InlineQuery | ||
import dev.inmo.tgbotapi.types.InlineQueries.query.BaseInlineQuery | ||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry | ||
import io.mockk.mockk | ||
import me.madhead.imgmacrobot.core.dao.CachedInlineQueryResultDAO | ||
import me.madhead.imgmacrobot.imgur.Imgur | ||
import org.jetbrains.skija.paragraph.FontCollection | ||
import org.junit.jupiter.api.Assertions | ||
import org.junit.jupiter.api.TestInstance | ||
import org.junit.jupiter.params.ParameterizedTest | ||
import org.junit.jupiter.params.provider.Arguments | ||
import org.junit.jupiter.params.provider.MethodSource | ||
import java.nio.file.Path | ||
import kotlin.io.path.ExperimentalPathApi | ||
|
||
@ExperimentalPathApi | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
internal class FirstTimeTest { | ||
private val templatesDir = mockk<Path>() | ||
private val imgur = mockk<Imgur>() | ||
private val fontCollection = mockk<FontCollection>() | ||
private val cachedInlineQueryResultDAO = mockk<CachedInlineQueryResultDAO>() | ||
private val registry = SimpleMeterRegistry() | ||
private val sut = FirstTime(templatesDir, imgur, fontCollection, cachedInlineQueryResultDAO, registry) | ||
|
||
@ParameterizedTest | ||
@MethodSource("parseInlineQueryParams") | ||
fun parseInlineQuery(inlineQuery: InlineQuery, parsedInlineQuery: FirstTimeParsedInlineQuery?) { | ||
val actual = sut.parseInlineQuery(inlineQuery) | ||
|
||
Assertions.assertEquals(parsedInlineQuery, actual) | ||
} | ||
|
||
@Suppress("unused") | ||
private fun parseInlineQueryParams(): List<Arguments> { | ||
val user = CommonUser( | ||
id = ChatId(0), | ||
firstName = "user" | ||
) | ||
|
||
return listOf( | ||
Arguments.of( | ||
BaseInlineQuery(id = "", from = user, query = "first time: First time?", offset = ""), | ||
FirstTimeParsedInlineQuery("First time?") | ||
), | ||
Arguments.of( | ||
BaseInlineQuery(id = "", from = user, query = "Молодой человек, мы, русские, не обманываем друг друга!", offset = ""), | ||
null | ||
), | ||
) | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.