From 27fe4e3680193641f1ff03a9cb4b0d620ac5d4f8 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 29 Jan 2020 09:44:53 +0100 Subject: [PATCH] Fix build and add tests --- .../composer/rainbow/RainbowGenerator.kt | 6 +- .../composer/rainbow/RainbowGeneratorTest.kt | 60 ++++++++++--------- .../java/im/vector/riotx/test/Extensions.kt | 19 ++++++ 3 files changed, 54 insertions(+), 31 deletions(-) create mode 100644 vector/src/test/java/im/vector/riotx/test/Extensions.kt diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/composer/rainbow/RainbowGenerator.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/composer/rainbow/RainbowGenerator.kt index 9662e2d372..3868be4e2e 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/composer/rainbow/RainbowGenerator.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/composer/rainbow/RainbowGenerator.kt @@ -28,10 +28,10 @@ import kotlin.math.roundToInt class RainbowGenerator @Inject constructor() { fun generate(text: String): String { - val frequency = 360f / text.length + val split = text.splitEmoji() + val frequency = 360f / split.size - return text - .splitEmoji() + return split .mapIndexed { idx, letter -> // Do better than React-Sdk: Avoid adding font color for spaces if (letter == " ") { diff --git a/vector/src/test/java/im/vector/riotx/features/home/room/detail/composer/rainbow/RainbowGeneratorTest.kt b/vector/src/test/java/im/vector/riotx/features/home/room/detail/composer/rainbow/RainbowGeneratorTest.kt index 3e1092d288..5a9fdc0ab7 100644 --- a/vector/src/test/java/im/vector/riotx/features/home/room/detail/composer/rainbow/RainbowGeneratorTest.kt +++ b/vector/src/test/java/im/vector/riotx/features/home/room/detail/composer/rainbow/RainbowGeneratorTest.kt @@ -16,9 +16,11 @@ package im.vector.riotx.features.home.room.detail.composer.rainbow +import im.vector.riotx.test.trimIndentOneLine import org.junit.Assert.assertEquals import org.junit.Test +@Suppress("SpellCheckingInspection") class RainbowGeneratorTest { private val rainbowGenerator = RainbowGenerator() @@ -35,8 +37,17 @@ class RainbowGeneratorTest { @Test fun testAscii2() { - val expected = - """ + val expected = """ + a + b + """.trimIndentOneLine() + + assertEquals(expected, rainbowGenerator.generate("ab")) + } + + @Test + fun testAscii3() { + val expected = """ T h i @@ -55,8 +66,7 @@ class RainbowGeneratorTest { o w ! - """ - .trimIndentOnLine() + """.trimIndentOneLine() assertEquals(expected, rainbowGenerator.generate("This is a rainbow!")) } @@ -75,9 +85,8 @@ class RainbowGeneratorTest { fun testEmoji3() { val expected = """ 🤞 - 🙂 - """ - .trimIndentOnLine() + 🙂 + """.trimIndentOneLine() assertEquals(expected, rainbowGenerator.generate("🤞🙂")) } @@ -86,21 +95,20 @@ class RainbowGeneratorTest { fun testEmojiMix1() { val expected = """ H - e - l - l - o + e + l + l + o - 🤞 + 🤞 - w - o - r - l - d - ! - """ - .trimIndentOnLine() + w + o + r + l + d + ! + """.trimIndentOneLine() assertEquals(expected, rainbowGenerator.generate("Hello 🤞 world!")) } @@ -109,9 +117,8 @@ class RainbowGeneratorTest { fun testEmojiMix2() { val expected = """ a - 🤞 - """ - .trimIndentOnLine() + 🤞 + """.trimIndentOneLine() assertEquals(expected, rainbowGenerator.generate("a🤞")) } @@ -120,9 +127,8 @@ class RainbowGeneratorTest { fun testEmojiMix3() { val expected = """ 🤞 - a - """ - .trimIndentOnLine() + a + """.trimIndentOneLine() assertEquals(expected, rainbowGenerator.generate("🤞a")) } @@ -132,5 +138,3 @@ class RainbowGeneratorTest { assertEquals("\uD83E", rainbowGenerator.generate("\uD83E")) } } - -fun String.trimIndentOnLine() = trimIndent().replace("\n", "") \ No newline at end of file diff --git a/vector/src/test/java/im/vector/riotx/test/Extensions.kt b/vector/src/test/java/im/vector/riotx/test/Extensions.kt new file mode 100644 index 0000000000..31781ce00e --- /dev/null +++ b/vector/src/test/java/im/vector/riotx/test/Extensions.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2020 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.riotx.test + +fun String.trimIndentOneLine() = trimIndent().replace("\n", "")