Cleaned up tests with assertk

This commit is contained in:
Erik C. Thauvin 2022-10-04 12:39:08 -07:00
parent 28d86b8b14
commit 3a37899d0c
3 changed files with 27 additions and 15 deletions

2
.idea/kotlinc.xml generated
View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="KotlinJpsPluginSettings"> <component name="KotlinJpsPluginSettings">
<option name="version" value="1.7.10" /> <option name="version" value="1.7.20" />
</component> </component>
</project> </project>

View file

@ -37,6 +37,7 @@ dependencies {
testImplementation(kotlin("test")) testImplementation(kotlin("test"))
testImplementation(kotlin("test-junit")) testImplementation(kotlin("test-junit"))
testImplementation("com.willowtreeapps.assertk:assertk-jvm:0.25")
} }
java { java {

View file

@ -32,10 +32,17 @@
package net.thauvin.erik.isgd package net.thauvin.erik.isgd
import assertk.all
import assertk.assertThat
import assertk.assertions.contains
import assertk.assertions.isEqualTo
import assertk.assertions.isNotNull
import assertk.assertions.matches
import assertk.assertions.prop
import assertk.assertions.startsWith
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertFailsWith import kotlin.test.assertFailsWith
import kotlin.test.assertTrue
class IsgdTest { class IsgdTest {
private val url = "https://www.example.com" private val url = "https://www.example.com"
@ -45,7 +52,7 @@ class IsgdTest {
@Test @Test
fun testException() { fun testException() {
assertFailsWith( assertFailsWith(
message = "URL is already shorten", message = "shorten(duplicate)",
exceptionClass = IsgdException::class, exceptionClass = IsgdException::class,
block = { Isgd.shorten(shortUrl) } block = { Isgd.shorten(shortUrl) }
) )
@ -53,15 +60,17 @@ class IsgdTest {
try { try {
Isgd.shorten(shortUrl) Isgd.shorten(shortUrl)
} catch (e: IsgdException) { } catch (e: IsgdException) {
assertTrue(e.statusCode == 400, "status code != 400") assertThat(e, "shorten(duplicate)").all {
assertTrue(e.message!!.startsWith("Error: "), "error message is invalid") prop(IsgdException::statusCode).isEqualTo(400)
prop(IsgdException::message).isNotNull().startsWith("Error: ")
}
} }
} }
@Test @Test
fun testLookup() { fun testLookup() {
assertFailsWith( assertFailsWith(
message = "empty url", message = "lookup(empty)",
exceptionClass = IllegalArgumentException::class, exceptionClass = IllegalArgumentException::class,
block = { Isgd.lookup("") } block = { Isgd.lookup("") }
) )
@ -70,7 +79,7 @@ class IsgdTest {
@Test @Test
fun testLookupDefault() { fun testLookupDefault() {
assertEquals(url, Isgd.lookup(shortUrl)) assertEquals(url, Isgd.lookup(shortUrl))
assertEquals(url, Isgd.lookup(shortVgdUrl, isVgd = true), "v.gd") assertEquals(url, Isgd.lookup(shortVgdUrl, isVgd = true), "lookup(isVgd)")
} }
@Test @Test
@ -79,7 +88,7 @@ class IsgdTest {
assertEquals( assertEquals(
"test({ \"url\": \"$url\" });", "test({ \"url\": \"$url\" });",
Isgd.lookup(shortUrl, callback = "test", format = Format.JSON), Isgd.lookup(shortUrl, callback = "test", format = Format.JSON),
"with callback" "lookup(callback)"
) )
} }
@ -94,13 +103,13 @@ class IsgdTest {
@Test @Test
fun testShorten() { fun testShorten() {
assertFailsWith( assertFailsWith(
message = "empty url", message = "shorten(empty)",
exceptionClass = IllegalArgumentException::class, exceptionClass = IllegalArgumentException::class,
block = { Isgd.shorten("") } block = { Isgd.shorten("") }
) )
assertFailsWith( assertFailsWith(
message = "shorturl already take", message = "shorten(shorturl)",
exceptionClass = IsgdException::class, exceptionClass = IsgdException::class,
block = { Isgd.shorten(url, shorturl = "test") } block = { Isgd.shorten(url, shorturl = "test") }
) )
@ -108,9 +117,11 @@ class IsgdTest {
@Test @Test
fun testShortenDefault() { fun testShortenDefault() {
assertEquals(shortUrl, Isgd.shorten(url)) assertEquals(shortUrl, Isgd.shorten(url), "shorten(url)")
assertEquals(shortVgdUrl, Isgd.shorten(url, isVgd = true), "v.gd") assertEquals(shortVgdUrl, Isgd.shorten(url, isVgd = true), "shorten(isVgd)")
assertTrue(Isgd.shorten(url, logstats = true).matches("https://is.gd/\\w{6}".toRegex()), "with logstats") assertThat(Isgd.shorten(url, logstats = true), "shorten(callback)").matches("https://is.gd/\\w{6}".toRegex())
} }
@Test @Test
@ -119,7 +130,7 @@ class IsgdTest {
assertEquals( assertEquals(
"test({ \"shorturl\": \"$shortUrl\" });", "test({ \"shorturl\": \"$shortUrl\" });",
Isgd.shorten(url, callback = "test", format = Format.JSON), Isgd.shorten(url, callback = "test", format = Format.JSON),
"with callback" "shorten(callback,json)"
) )
} }
@ -134,6 +145,6 @@ class IsgdTest {
@Test @Test
fun testShortenWeb() { fun testShortenWeb() {
assertTrue(Isgd.shorten(url, format = Format.WEB).contains(shortUrl)) assertThat(Isgd.shorten(url, format = Format.WEB)).contains(shortUrl)
} }
} }