Skip to content

Commit

Permalink
chore: add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
tangcent committed Jul 15, 2023
1 parent fd150c5 commit d4b6c4f
Show file tree
Hide file tree
Showing 13 changed files with 297 additions and 111 deletions.
18 changes: 15 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,22 @@ tasks.create("codeCoverageReport", JacocoReport::class) {
executionData(
fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
)
subprojects.forEach {
sourceDirectories.from(it.file("src/main/kotlin"))
classDirectories.from(it.file("build/classes/kotlin/main"))

val exclusiveDirectories = listOf("**/common/model/**")

subprojects.forEach { project ->
sourceDirectories.from(project.files("src/main/kotlin").map {
fileTree(it).matching {
exclude(exclusiveDirectories)
}
})
classDirectories.from(project.files("build/classes/kotlin/main").map {
fileTree(it).matching {
exclude(exclusiveDirectories)
}
})
}

reports {
xml.required.set(true)
xml.outputLocation.set(file("${buildDir}/reports/jacoco/report.xml").apply { parentFile.mkdirs() })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object HttpMethod {
if (method.isBlank()) {
return NO_METHOD
}
val standardMethod = method.toUpperCase()
val standardMethod = method.uppercase()
if (ALL_METHODS.contains(standardMethod)) {
return standardMethod
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
package com.itangcent.common.constant

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

/**
* Test case for [HttpMethod]
*/
internal class HttpMethodTest {

@Test
fun preferMethod() {

Assertions.assertEquals("ALL", HttpMethod.preferMethod(""))

Assertions.assertEquals("GET", HttpMethod.preferMethod("GET"))
Assertions.assertEquals("POST", HttpMethod.preferMethod("POST"))

Assertions.assertEquals("GET", HttpMethod.preferMethod("XXX.GET"))
Assertions.assertEquals("POST", HttpMethod.preferMethod("XXX.POST"))
Assertions.assertEquals("GET", HttpMethod.preferMethod("POST.GET"))
Assertions.assertEquals("POST", HttpMethod.preferMethod("GET.POST"))

Assertions.assertEquals("GET", HttpMethod.preferMethod("POST_GET"))
Assertions.assertEquals("GET", HttpMethod.preferMethod("GET_POST"))

Assertions.assertEquals("GET", HttpMethod.preferMethod("[GET]"))
Assertions.assertEquals("POST", HttpMethod.preferMethod("[POST]"))
Assertions.assertEquals("GET", HttpMethod.preferMethod("[GET][POST]"))
Assertions.assertEquals("GET", HttpMethod.preferMethod("[POST][GET]"))
fun `test preferMethod function`() {

assertEquals("ALL", HttpMethod.preferMethod(""))
assertEquals("ALL", HttpMethod.preferMethod("foo"))

assertEquals("GET", HttpMethod.preferMethod("GET"))
assertEquals("POST", HttpMethod.preferMethod("POST"))
assertEquals("DELETE", HttpMethod.preferMethod("DELETE"))
assertEquals("PUT", HttpMethod.preferMethod("PUT"))
assertEquals("PATCH", HttpMethod.preferMethod("PATCH"))
assertEquals("OPTIONS", HttpMethod.preferMethod("OPTIONS"))
assertEquals("TRACE", HttpMethod.preferMethod("TRACE"))
assertEquals("HEAD", HttpMethod.preferMethod("HEAD"))

assertEquals("GET", HttpMethod.preferMethod("get"))
assertEquals("POST", HttpMethod.preferMethod("post"))
assertEquals("DELETE", HttpMethod.preferMethod("delete"))
assertEquals("PUT", HttpMethod.preferMethod("put"))
assertEquals("PATCH", HttpMethod.preferMethod("patch"))
assertEquals("OPTIONS", HttpMethod.preferMethod("options"))
assertEquals("TRACE", HttpMethod.preferMethod("trace"))
assertEquals("HEAD", HttpMethod.preferMethod("head"))

assertEquals("GET", HttpMethod.preferMethod("XXX.GET"))
assertEquals("POST", HttpMethod.preferMethod("XXX.POST"))
assertEquals("GET", HttpMethod.preferMethod("POST.GET"))
assertEquals("POST", HttpMethod.preferMethod("GET.POST"))

assertEquals("GET", HttpMethod.preferMethod("POST_GET"))
assertEquals("GET", HttpMethod.preferMethod("GET_POST"))

assertEquals("GET", HttpMethod.preferMethod("[GET]"))
assertEquals("POST", HttpMethod.preferMethod("[POST]"))
assertEquals("GET", HttpMethod.preferMethod("[GET][POST]"))
assertEquals("GET", HttpMethod.preferMethod("[POST][GET]"))
}
}
15 changes: 14 additions & 1 deletion common-api/src/test/kotlin/com/itangcent/utils/AnyKitKtTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.itangcent.utils
import com.itangcent.common.kit.toJson
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.Test
import kotlin.test.assertEquals

class AnyKitKtTest {

Expand Down Expand Up @@ -62,5 +61,19 @@ class AnyKitKtTest {
it.subMutable("x")!!["b"] = "c"
}.toJson()
)

assertNull(
ImmutableMap(hashMapOf("a" to mapOf("x" to "y"))).subMutable("a")
)
assertNull(
ImmutableMap(hashMapOf("a" to mapOf("x" to "y"))).subMutable("b")
)
}

private class ImmutableMap<K, V>(m: MutableMap<out K, out V>?) : HashMap<K, V>(m) {

override fun put(key: K, value: V): V? {
throw UnsupportedOperationException()
}
}
}
56 changes: 56 additions & 0 deletions common-api/src/test/kotlin/com/itangcent/utils/FuncKitKtTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.itangcent.utils

import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class FuncKitKtTest {

@Test
fun `test and function`() {
val isEven = { num: Int -> num % 2 == 0 }
val isPositive = { num: Int -> num > 0 }

val isEvenAndPositive = isEven.and(isPositive)

assertEquals(true, isEvenAndPositive(4))
assertEquals(false, isEvenAndPositive(3))
assertEquals(false, isEvenAndPositive(-4))
assertEquals(false, isEvenAndPositive(0))
}

@Test
fun `test then function with one parameter`() {
var result = ""

val addHello = { str: String -> result = "$result $str Hello" }
val addWorld = { str: String -> result = "$result $str World" }

val addHelloThenWorld = addHello.then(addWorld)

result = ""
addHelloThenWorld("Hi")
assertEquals(" Hi Hello Hi World", result)

result = ""
addHelloThenWorld("")
assertEquals(" Hello World", result)
}

@Test
fun `test then function with three parameters`() {
var result = ""

val addHello = { str: String, num: Int, flag: Boolean -> result = "$result $str Hello $num $flag" }
val addWorld = { str: String, num: Int, flag: Boolean -> result = "$result $str World $num $flag" }

val addHelloThenWorld = addHello.then(addWorld)

result = ""
addHelloThenWorld("Hi", 42, true)
assertEquals(" Hi Hello 42 true Hi World 42 true", result)

result = ""
addHelloThenWorld("", 0, false)
assertEquals(" Hello 0 false World 0 false", result)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.apache.http.util

import org.apache.http.HttpEntity
import org.apache.http.entity.ContentType
import org.apache.http.entity.StringEntity
import org.junit.jupiter.api.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals

class EntityKitsKtTest {

@Test
fun `test HttpEntity toByteArray`() {
val content = "Hello, world!"
val entity = StringEntity(content, ContentType.TEXT_PLAIN)
val bytes = entity.toByteArray()
assertContentEquals(content.toByteArray(), bytes)
}

@Test
fun `test HttpEntity consume`() {
object : HttpEntity {
override fun getContent() = null
override fun getContentLength() = 0L
override fun getContentType() = null
override fun isChunked() = false
override fun isRepeatable() = false
override fun isStreaming() = false
override fun writeTo(output: java.io.OutputStream?) {}
override fun getContentEncoding() = null
override fun consumeContent() {}
}.consume()
object : HttpEntity {
override fun getContent() = "content".byteInputStream(Charsets.UTF_8)
override fun getContentLength() = 0L
override fun getContentType() = null
override fun isChunked() = false
override fun isRepeatable() = false
override fun isStreaming() = true
override fun writeTo(output: java.io.OutputStream?) {}
override fun getContentEncoding() = null
override fun consumeContent() {}
}.consume()
}

@Test
fun `test HttpEntity getContentCharSet`() {
val contentType = ContentType.create("text/plain", "UTF-8")
val entity = StringEntity("Hello, world!", contentType)
val charset = entity.getContentCharSet()
assertEquals("UTF-8", charset)
}

@Test
fun `test HttpEntity getContentMimeType`() {
val contentType = ContentType.create("text/plain", "UTF-8")
val entity = StringEntity("Hello, world!", contentType)
val mimeType = entity.getContentMimeType()
assertEquals("text/plain", mimeType)
}

@Test
fun `test HttpEntity readString`() {
val content = "Hello, world!"
val entity = StringEntity(content, ContentType.TEXT_PLAIN)
assertEquals(content, entity.readString())

val wildcardEntity = StringEntity(content, ContentType.WILDCARD)
assertEquals(content, wildcardEntity.readString())
assertEquals(content, wildcardEntity.readString(Charsets.ISO_8859_1))
}

@Test
fun `test HttpEntity readString with default charset`() {
val content = "Hello, world!"
val entity = StringEntity(content, ContentType.TEXT_PLAIN.withCharset("ISO-8859-1"))
assertEquals(content, entity.readString())
}

@Test
fun `test HttpEntity readString with specified charset`() {
val content = "Hello, world!"
val entity = StringEntity(content, ContentType.TEXT_PLAIN.withCharset("ISO-8859-1"))
val result = entity.readString("ISO-8859-1")
assertEquals(content, result)
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ plugin_version=2.5.9.212.0
kotlin.code.style=official
kotlin_version=1.8.0
junit_version=5.9.2
itangcent_intellij_version=1.5.13-SNAPSHOT
itangcent_intellij_version=1.5.2
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.itangcent.idea.plugin.api.cache

import com.itangcent.common.constant.HttpMethod
import com.itangcent.common.model.*
import com.itangcent.idea.utils.setExts
import com.itangcent.utils.setExts

class FileApiCache {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.itangcent.common.utils.*
import com.itangcent.idea.plugin.api.export.*
import com.itangcent.idea.plugin.api.export.core.*
import com.itangcent.idea.psi.resource
import com.itangcent.idea.utils.setExts
import com.itangcent.utils.setExts
import com.itangcent.intellij.context.ActionContext
import com.itangcent.intellij.jvm.DuckTypeHelper
import com.itangcent.intellij.jvm.JsonOption
Expand Down

This file was deleted.

22 changes: 12 additions & 10 deletions idea-plugin/src/main/kotlin/com/itangcent/utils/ExtensibleKit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ object ExtensibleKit {
val jsonElement = GsonUtils.parseToJsonTree(json)!!
val t = GSON.fromJson(jsonElement, this.java)
jsonElement.asJsonObject.entrySet()
.filter { it.key.startsWith(Attrs.PREFIX) }
.forEach { t.setExt(it.key, it.value.unbox()) }
.filter { it.key.startsWith(Attrs.PREFIX) }
.forEach { t.setExt(it.key, it.value.unbox()) }
return t
}

fun <T : Extensible> KClass<T>.fromJson(json: String, vararg exts: String): T {
val extNames = exts.removePrefix(Attrs.PREFIX)
val extNames = exts.toSet() +
exts.map { it.removePrefix(Attrs.PREFIX) } +
exts.map { it.addPrefix(Attrs.PREFIX) }
val jsonElement = GsonUtils.parseToJsonTree(json)!!
val t = GSON.fromJson(jsonElement, this.java)
jsonElement.asJsonObject.entrySet()
.filter { extNames.contains(it.key) }
.forEach { t.setExt(it.key.addPrefix(Attrs.PREFIX), it.value.unbox()) }
.filter { extNames.contains(it.key) }
.forEach { t.setExt(it.key.addPrefix(Attrs.PREFIX), it.value.unbox()) }
return t
}

Expand All @@ -41,10 +43,10 @@ object ExtensibleKit {
return this
}

/**
* Remove [prefix] from each string in the original array.
*/
private fun Array<out String>.removePrefix(prefix: CharSequence): Array<String> {
return this.mapToTypedArray { it.removePrefix(prefix) }
}

fun Extensible.setExts(exts: Map<String, Any?>) {
exts.forEach { (t, u) ->
this.setExt(t, u)
}
}

This file was deleted.

Loading

0 comments on commit d4b6c4f

Please sign in to comment.