Skip to content

Commit

Permalink
Multiplatform: Move most of the tests into commonTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
vanniktech committed May 26, 2022
1 parent 54c4e99 commit bae3b28
Showing 11 changed files with 226 additions and 206 deletions.
Original file line number Diff line number Diff line change
@@ -20,7 +20,6 @@ import android.text.SpannableString
import com.vanniktech.emoji.internal.EmojiSpan
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertThrows
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@@ -30,109 +29,21 @@ import org.robolectric.annotation.Config

@Config(manifest = Config.NONE)
@RunWith(RobolectricTestRunner::class)
class EmojiManagerTest {
class EmojiManagerAndroidTest {
private lateinit var provider: EmojiProvider

@Before
fun setUp() {
@Before fun setUp() {
val emoji1 = TestEmoji(intArrayOf(0x1234), listOf("test"), false)
val emoji2 = TestEmoji(intArrayOf(0x4321), listOf("test"), false)
val emoji3 = TestEmoji(intArrayOf(0x5678), listOf("test"), false)
val emoji4 = TestEmoji(intArrayOf(0x1234, 0x4321, 0x9999), listOf("test"), false)
provider = TestEmojiProvider(emoji1, emoji2, emoji3, emoji4)
}

@After
fun tearDown() {
@After fun tearDown() {
EmojiManager.destroy()
}

@Test fun installNormalCategory() {
EmojiManager.install(provider)
assertEquals(true, EmojiManager.categories().isNotEmpty())
}

@Test fun noProviderInstalled() {
assertThrows("Please install an EmojiProvider through the EmojiManager.install() method first.", IllegalStateException::class.java) {
EmojiManager.findEmoji("test")
}
}

@Test fun installEmptyProvider() {
assertThrows("Your EmojiProvider must at least have one category with at least one emoji.", IllegalArgumentException::class.java) {
EmojiManager.install(EmptyCategories)
}
}

@Test fun installEmptyCategory() {
assertThrows("Your EmojiProvider must at least have one category with at least one emoji.", IllegalArgumentException::class.java) {
EmojiManager.install(EmptyEmojiProvider)
}
}

@Test fun installNormalEmoji() {
EmojiManager.install(provider)
assertEquals(
TestEmoji(intArrayOf(0x1234), listOf("test"), false),
EmojiManager.findEmoji(String(intArrayOf(0x1234), 0, 1)),
)
}

@Test fun installMultiple() {
EmojiManager.install(provider)
EmojiManager.install(provider)

// No duplicate categories.
assertEquals(1, EmojiManager.categories().size)
}

@Test fun destroy() {
EmojiManager.destroy()
assertThrows("Please install an EmojiProvider through the EmojiManager.install() method first.", IllegalStateException::class.java) {
EmojiManager.findEmoji("test")
}
}

@Test fun findEmojiNormal() {
EmojiManager.install(provider)
assertEquals(
TestEmoji(intArrayOf(0x5678), listOf("test"), false),
EmojiManager.findEmoji(String(intArrayOf(0x5678), 0, 1)),
)
}

@Test fun findEmojiEmpty() {
EmojiManager.install(provider)
assertEquals(null, EmojiManager.findEmoji(""))
}

@Test fun findAllEmojisNormal() {
EmojiManager.install(provider)
val text = (
"te" + String(intArrayOf(0x5678), 0, 1) +
"st" + String(intArrayOf(0x1234), 0, 1)
)
val firstExpectedRange = EmojiRange(TestEmoji(intArrayOf(0x5678), listOf("test"), false), 2..3)
val secondExpectedRange = EmojiRange(TestEmoji(intArrayOf(0x1234), listOf("test"), false), 5..6)
assertEquals(
listOf(
firstExpectedRange,
secondExpectedRange,
),
EmojiManager.findAllEmojis(text),
)
}

@Test fun findAllEmojisEmpty() {
EmojiManager.install(provider)
assertEquals(true, EmojiManager.findAllEmojis("").isEmpty())
}

@Test fun findAllEmojisNull() {
EmojiManager.install(provider)
assertEquals(true, EmojiManager.findAllEmojis(null).isEmpty())
}

@Suppress("DEPRECATION")
@Test fun simple() {
EmojiManager.install(provider)

This file was deleted.

Original file line number Diff line number Diff line change
@@ -16,18 +16,18 @@

package com.vanniktech.emoji

import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Before
import org.junit.Test
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals

class EmojiInformationTest {
private lateinit var empty: EmojiInformation
private lateinit var empty2: EmojiInformation
private lateinit var one: EmojiInformation
private lateinit var one2: EmojiInformation

@Before fun setUp() {
@BeforeTest fun setUp() {
val emptyList: List<EmojiRange> = emptyList()
empty = EmojiInformation(false, emptyList)
empty2 = EmojiInformation(false, emptyList)
@@ -37,16 +37,16 @@ class EmojiInformationTest {
}

@Test fun equality() {
assertEquals(empty2, empty)
assertEquals(one2, one)
assertNotEquals(empty, one)
assertNotEquals(one, empty)
assertEquals(expected = empty2, actual = empty)
assertEquals(expected = one2, actual = one)
assertNotEquals(illegal = empty, actual = one)
assertNotEquals(illegal = one, actual = empty)
}

@Test fun hashy() {
assertEquals(empty2.hashCode(), empty.hashCode())
assertEquals(one2.hashCode(), one.hashCode())
assertNotEquals(empty.hashCode(), one.hashCode())
assertNotEquals(one.hashCode(), empty.hashCode())
assertEquals(expected = empty2.hashCode(), actual = empty.hashCode())
assertEquals(expected = one2.hashCode(), actual = one.hashCode())
assertNotEquals(illegal = empty.hashCode(), actual = one.hashCode())
assertNotEquals(illegal = one.hashCode(), actual = empty.hashCode())
}
}
125 changes: 125 additions & 0 deletions emoji/src/commonTest/kotlin/com/vanniktech/emoji/EmojiManagerTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Copyright (C) 2016 - Niklas Baudy, Ruben Gees, Mario Đanić and contributors
*
* 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 com.vanniktech.emoji

import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith

class EmojiManagerTest {
private lateinit var provider: EmojiProvider

@BeforeTest fun setUp() {
val emoji1 = TestEmoji(intArrayOf(0x1234), listOf("test"), false)
val emoji2 = TestEmoji(intArrayOf(0x4321), listOf("test"), false)
val emoji3 = TestEmoji(intArrayOf(0x5678), listOf("test"), false)
val emoji4 = TestEmoji(intArrayOf(0x1234, 0x4321, 0x9999), listOf("test"), false)
provider = TestEmojiProvider(emoji1, emoji2, emoji3, emoji4)
}

@AfterTest fun tearDown() {
EmojiManager.destroy()
}

@Test fun installNormalCategory() {
EmojiManager.install(provider)
assertEquals(true, EmojiManager.categories().isNotEmpty())
}

@Test fun noProviderInstalled() {
assertFailsWith<IllegalStateException>("Please install an EmojiProvider through the EmojiManager.install() method first.") {
EmojiManager.findEmoji("test")
}
}

@Test fun installEmptyProvider() {
assertFailsWith<IllegalArgumentException>("Your EmojiProvider must at least have one category with at least one emoji.") {
EmojiManager.install(EmptyCategories)
}
}

@Test fun installEmptyCategory() {
assertFailsWith<IllegalArgumentException>("Your EmojiProvider must at least have one category with at least one emoji.") {
EmojiManager.install(EmptyEmojiProvider)
}
}

@Test fun installNormalEmoji() {
EmojiManager.install(provider)
assertEquals(
TestEmoji(intArrayOf(0x1234), listOf("test"), false),
EmojiManager.findEmoji(String(intArrayOf(0x1234), 0, 1)),
)
}

@Test fun installMultiple() {
EmojiManager.install(provider)
EmojiManager.install(provider)

// No duplicate categories.
assertEquals(1, EmojiManager.categories().size)
}

@Test fun destroy() {
EmojiManager.destroy()
assertFailsWith<IllegalStateException>("Please install an EmojiProvider through the EmojiManager.install() method first.") {
EmojiManager.findEmoji("test")
}
}

@Test fun findEmojiNormal() {
EmojiManager.install(provider)
assertEquals(
TestEmoji(intArrayOf(0x5678), listOf("test"), false),
EmojiManager.findEmoji(String(intArrayOf(0x5678), 0, 1)),
)
}

@Test fun findEmojiEmpty() {
EmojiManager.install(provider)
assertEquals(null, EmojiManager.findEmoji(""))
}

@Test fun findAllEmojisNormal() {
EmojiManager.install(provider)
val text = (
"te" + String(intArrayOf(0x5678), 0, 1) +
"st" + String(intArrayOf(0x1234), 0, 1)
)
val firstExpectedRange = EmojiRange(TestEmoji(intArrayOf(0x5678), listOf("test"), false), 2..3)
val secondExpectedRange = EmojiRange(TestEmoji(intArrayOf(0x1234), listOf("test"), false), 5..6)
assertEquals(
listOf(
firstExpectedRange,
secondExpectedRange,
),
EmojiManager.findAllEmojis(text),
)
}

@Test fun findAllEmojisEmpty() {
EmojiManager.install(provider)
assertEquals(true, EmojiManager.findAllEmojis("").isEmpty())
}

@Test fun findAllEmojisNull() {
EmojiManager.install(provider)
assertEquals(true, EmojiManager.findAllEmojis(null).isEmpty())
}
}
48 changes: 48 additions & 0 deletions emoji/src/commonTest/kotlin/com/vanniktech/emoji/EmojiRangeTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2016 - Niklas Baudy, Ruben Gees, Mario Đanić and contributors
*
* 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 com.vanniktech.emoji

import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotEquals

class EmojiRangeTest {
private lateinit var emoji: Emoji
private lateinit var emoji2: Emoji

@BeforeTest fun setUp() {
emoji = TestEmoji(intArrayOf(0x1234), listOf("test"), false)
emoji2 = TestEmoji(intArrayOf(0x5678), listOf("test"), false)
}

@Test fun equality() {
assertEquals(EmojiRange(emoji = emoji, range = 0..1), EmojiRange(emoji, 0..1))
assertEquals(EmojiRange(emoji = emoji2, range = 0..1), EmojiRange(emoji2, 0..1))
assertNotEquals(EmojiRange(emoji = emoji2, range = 0..1), EmojiRange(emoji2, 0..0))
assertNotEquals(EmojiRange(emoji = emoji2, range = 1..10), EmojiRange(emoji2, 0..10))
assertNotEquals(EmojiRange(emoji = emoji2, range = 0..1), EmojiRange(emoji, 0..1))
}

@Test fun hashy() {
assertEquals(EmojiRange(emoji = emoji, range = 0..1).hashCode(), EmojiRange(emoji, 0..1).hashCode())
assertEquals(EmojiRange(emoji = emoji2, range = 0..1).hashCode(), EmojiRange(emoji2, 0..1).hashCode())
assertNotEquals(EmojiRange(emoji = emoji2, range = 0..1).hashCode(), EmojiRange(emoji2, 0..0).hashCode())
assertNotEquals(EmojiRange(emoji = emoji2, range = 1..10).hashCode(), EmojiRange(emoji2, 0..10).hashCode())
assertNotEquals(EmojiRange(emoji = emoji2, range = 0..1).hashCode(), EmojiRange(emoji, 0..1).hashCode())
}
}
Loading

0 comments on commit bae3b28

Please sign in to comment.