Skip to content

Commit

Permalink
Feature/truth (#258)
Browse files Browse the repository at this point in the history
* Add Truth, update part of test classes
* Convert other tests to use Truth, fix date parser test
* Add Truth assertions to FormatUtilsTest, fix ktlint issue
* Update assertions to a proper syntax
  • Loading branch information
vbuberen authored Mar 2, 2020
1 parent 4db6ea8 commit b5bc7fc
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 115 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ buildscript {
materialComponentsVersion = '1.1.0'
roomVersion = '2.2.3'
lifecycleVersion = '2.2.0'
androidXCore = '2.1.0'
androidXCoreVersion = '2.1.0'

// Publishing
androidMavenGradleVersion = '2.1'
Expand All @@ -29,6 +29,7 @@ buildscript {
junitGradlePluignVersion = '1.6.0.0'
junitVersion = '5.6.0'
mockkVersion = '1.9.3'
truthVersion = '1.0.1'
}

repositories {
Expand Down
3 changes: 2 additions & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ dependencies {
testImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
testImplementation "io.mockk:mockk:$mockkVersion"
testImplementation "com.squareup.okhttp3:mockwebserver:$okhttp3Version"
testImplementation "androidx.arch.core:core-testing:$androidXCore"
testImplementation "androidx.arch.core:core-testing:$androidXCoreVersion"
testImplementation "com.google.truth:truth:$truthVersion"
}

apply from: rootProject.file('gradle/gradle-mvn-push.gradle')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.chuckerteam.chucker.internal.support

import org.junit.jupiter.api.Assertions.assertEquals
import com.google.common.truth.Truth.assertThat
import org.junit.jupiter.api.Test

class FormatUtilsTest {
Expand All @@ -15,13 +15,12 @@ class FormatUtilsTest {
""".trimIndent()
)

assertEquals(
assertThat(parsedJson).isEqualTo(
"""
{
"field": null
}
""".trimIndent(),
parsedJson
""".trimIndent()
)
}

Expand All @@ -35,13 +34,12 @@ class FormatUtilsTest {
""".trimIndent()
)

assertEquals(
assertThat(parsedJson).isEqualTo(
"""
{
"field": ""
}
""".trimIndent(),
parsedJson
""".trimIndent()
)
}

Expand All @@ -51,14 +49,13 @@ class FormatUtilsTest {
"""{ "field1": "something", "field2": "else" }"""
)

assertEquals(
assertThat(parsedJson).isEqualTo(
"""
{
"field1": "something",
"field2": "else"
}
""".trimIndent(),
parsedJson
""".trimIndent()
)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.chuckerteam.chucker.internal.support

import junit.framework.TestCase.assertEquals
import com.google.common.truth.Truth.assertThat
import okhttp3.HttpUrl
import org.junit.Test

Expand All @@ -11,15 +11,12 @@ class FormattedUrlTest {

val formattedUrl = FormattedUrl.fromHttpUrl(url, encoded = true)

assertEquals("https", formattedUrl.scheme)
assertEquals("www.example.com", formattedUrl.host)
assertEquals("/path/to%20some/resource", formattedUrl.path)
assertEquals("q=%22Hello,%20world!%22", formattedUrl.query)
assertEquals("/path/to%20some/resource?q=%22Hello,%20world!%22", formattedUrl.pathWithQuery)
assertEquals(
"https://www.example.com/path/to%20some/resource?q=%22Hello,%20world!%22",
formattedUrl.url
)
assertThat(formattedUrl.scheme).isEqualTo("https")
assertThat(formattedUrl.host).isEqualTo("www.example.com")
assertThat(formattedUrl.path).isEqualTo("/path/to%20some/resource")
assertThat(formattedUrl.query).isEqualTo("q=%22Hello,%20world!%22")
assertThat(formattedUrl.pathWithQuery).isEqualTo("/path/to%20some/resource?q=%22Hello,%20world!%22")
assertThat(formattedUrl.url).isEqualTo("https://www.example.com/path/to%20some/resource?q=%22Hello,%20world!%22")
}

@Test
Expand All @@ -28,12 +25,12 @@ class FormattedUrlTest {

val formattedUrl = FormattedUrl.fromHttpUrl(url, encoded = true)

assertEquals("https", formattedUrl.scheme)
assertEquals("www.example.com", formattedUrl.host)
assertEquals("", formattedUrl.path)
assertEquals("q=%22Hello,%20world!%22", formattedUrl.query)
assertEquals("?q=%22Hello,%20world!%22", formattedUrl.pathWithQuery)
assertEquals("https://www.example.com?q=%22Hello,%20world!%22", formattedUrl.url)
assertThat(formattedUrl.scheme).isEqualTo("https")
assertThat(formattedUrl.host).isEqualTo("www.example.com")
assertThat(formattedUrl.path).isEmpty()
assertThat(formattedUrl.query).isEqualTo("q=%22Hello,%20world!%22")
assertThat(formattedUrl.pathWithQuery).isEqualTo("?q=%22Hello,%20world!%22")
assertThat(formattedUrl.url).isEqualTo("https://www.example.com?q=%22Hello,%20world!%22")
}

@Test
Expand All @@ -42,12 +39,12 @@ class FormattedUrlTest {

val formattedUrl = FormattedUrl.fromHttpUrl(url, encoded = true)

assertEquals("https", formattedUrl.scheme)
assertEquals("www.example.com", formattedUrl.host)
assertEquals("/path/to%20some/resource", formattedUrl.path)
assertEquals("", formattedUrl.query)
assertEquals("/path/to%20some/resource", formattedUrl.pathWithQuery)
assertEquals("https://www.example.com/path/to%20some/resource", formattedUrl.url)
assertThat(formattedUrl.scheme).isEqualTo("https")
assertThat(formattedUrl.host).isEqualTo("www.example.com")
assertThat(formattedUrl.path).isEqualTo("/path/to%20some/resource")
assertThat(formattedUrl.query).isEmpty()
assertThat(formattedUrl.pathWithQuery).isEqualTo("/path/to%20some/resource")
assertThat(formattedUrl.url).isEqualTo("https://www.example.com/path/to%20some/resource")
}

@Test
Expand All @@ -56,15 +53,12 @@ class FormattedUrlTest {

val formattedUrl = FormattedUrl.fromHttpUrl(url, encoded = false)

assertEquals("https", formattedUrl.scheme)
assertEquals("www.example.com", formattedUrl.host)
assertEquals("/path/to some/resource", formattedUrl.path)
assertEquals("q=\"Hello, world!\"", formattedUrl.query)
assertEquals("/path/to some/resource?q=\"Hello, world!\"", formattedUrl.pathWithQuery)
assertEquals(
"https://www.example.com/path/to some/resource?q=\"Hello, world!\"",
formattedUrl.url
)
assertThat(formattedUrl.scheme).isEqualTo("https")
assertThat(formattedUrl.host).isEqualTo("www.example.com")
assertThat(formattedUrl.path).isEqualTo("/path/to some/resource")
assertThat(formattedUrl.query).isEqualTo("q=\"Hello, world!\"")
assertThat(formattedUrl.pathWithQuery).isEqualTo("/path/to some/resource?q=\"Hello, world!\"")
assertThat(formattedUrl.url).isEqualTo("https://www.example.com/path/to some/resource?q=\"Hello, world!\"")
}

@Test
Expand All @@ -73,12 +67,12 @@ class FormattedUrlTest {

val formattedUrl = FormattedUrl.fromHttpUrl(url, encoded = false)

assertEquals("https", formattedUrl.scheme)
assertEquals("www.example.com", formattedUrl.host)
assertEquals("", formattedUrl.path)
assertEquals("q=\"Hello, world!\"", formattedUrl.query)
assertEquals("?q=\"Hello, world!\"", formattedUrl.pathWithQuery)
assertEquals("https://www.example.com?q=\"Hello, world!\"", formattedUrl.url)
assertThat(formattedUrl.scheme).isEqualTo("https")
assertThat(formattedUrl.host).isEqualTo("www.example.com")
assertThat(formattedUrl.path).isEmpty()
assertThat(formattedUrl.query).isEqualTo("q=\"Hello, world!\"")
assertThat(formattedUrl.pathWithQuery).isEqualTo("?q=\"Hello, world!\"")
assertThat(formattedUrl.url).isEqualTo("https://www.example.com?q=\"Hello, world!\"")
}

@Test
Expand All @@ -87,11 +81,11 @@ class FormattedUrlTest {

val formattedUrl = FormattedUrl.fromHttpUrl(url, encoded = false)

assertEquals("https", formattedUrl.scheme)
assertEquals("www.example.com", formattedUrl.host)
assertEquals("/path/to some/resource", formattedUrl.path)
assertEquals("", formattedUrl.query)
assertEquals("/path/to some/resource", formattedUrl.pathWithQuery)
assertEquals("https://www.example.com/path/to some/resource", formattedUrl.url)
assertThat(formattedUrl.scheme).isEqualTo("https")
assertThat(formattedUrl.host).isEqualTo("www.example.com")
assertThat(formattedUrl.path).isEqualTo("/path/to some/resource")
assertThat(formattedUrl.query).isEmpty()
assertThat(formattedUrl.pathWithQuery).isEqualTo("/path/to some/resource")
assertThat(formattedUrl.url).isEqualTo("https://www.example.com/path/to some/resource")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ package com.chuckerteam.chucker.internal.support

import android.content.Context
import com.chuckerteam.chucker.R
import com.google.common.truth.Truth.assertThat
import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import java.io.EOFException
import java.nio.charset.Charset
import java.util.stream.Stream
import okio.Buffer
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertNotEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
Expand All @@ -28,31 +25,31 @@ class IOUtilsTest {
fun isPlaintext_withEmptyBuffer_returnsTrue() {
val buffer = Buffer()

assertTrue(ioUtils.isPlaintext(buffer))
assertThat(ioUtils.isPlaintext(buffer)).isTrue()
}

@Test
fun isPlaintext_withWhiteSpace_returnsTrue() {
val buffer = Buffer()
buffer.writeString(" ", Charset.defaultCharset())

assertTrue(ioUtils.isPlaintext(buffer))
assertThat(ioUtils.isPlaintext(buffer)).isTrue()
}

@Test
fun isPlaintext_withPlainText_returnsTrue() {
val buffer = Buffer()
buffer.writeString("just a string", Charset.defaultCharset())

assertTrue(ioUtils.isPlaintext(buffer))
assertThat(ioUtils.isPlaintext(buffer)).isTrue()
}

@Test
fun isPlaintext_withCodepoint_returnsFalse() {
val buffer = Buffer()
buffer.writeByte(0x11000000)

assertFalse(ioUtils.isPlaintext(buffer))
assertThat(ioUtils.isPlaintext(buffer)).isFalse()
}

@Test
Expand All @@ -61,17 +58,18 @@ class IOUtilsTest {
every { mockBuffer.size() } returns 100L
every { mockBuffer.copyTo(any<Buffer>(), any(), any()) } throws EOFException()

assertFalse(ioUtils.isPlaintext(mockBuffer))
assertThat(ioUtils.isPlaintext(mockBuffer)).isFalse()
}

@Test
fun readFromBuffer_contentNotTruncated() {
val mockBuffer = mockk<Buffer>()
every { mockBuffer.size() } returns 100L
every { mockBuffer.readString(any(), any()) } returns "{ \"message\": \"just a mock body\"}"

val result = ioUtils.readFromBuffer(mockBuffer, Charset.defaultCharset(), 200L)

assertEquals("{ \"message\": \"just a mock body\"}", result)
assertThat(result).isEqualTo("{ \"message\": \"just a mock body\"}")
verify { mockBuffer.readString(100L, Charset.defaultCharset()) }
}

Expand All @@ -84,7 +82,7 @@ class IOUtilsTest {

val result = ioUtils.readFromBuffer(mockBuffer, Charset.defaultCharset(), 50L)

assertEquals("{ \"message\": \"just a mock body\"}\\n\\n--- Content truncated ---", result)
assertThat(result).isEqualTo("{ \"message\": \"just a mock body\"}\\n\\n--- Content truncated ---")
verify { mockBuffer.readString(50L, Charset.defaultCharset()) }
}

Expand All @@ -97,7 +95,7 @@ class IOUtilsTest {

val result = ioUtils.readFromBuffer(mockBuffer, Charset.defaultCharset(), 200L)

assertEquals("\\n\\n--- Unexpected end of content ---", result)
assertThat(result).isEqualTo("\\n\\n--- Unexpected end of content ---")
}

@Test
Expand All @@ -106,25 +104,24 @@ class IOUtilsTest {

val nativeSource = ioUtils.getNativeSource(mockBuffer, false)

assertEquals(mockBuffer, nativeSource)
assertThat(nativeSource).isEqualTo(mockBuffer)
}

@Test
fun getNativeSource_withGzipped() {
val mockBuffer = mockk<Buffer>()

val nativeSource = ioUtils.getNativeSource(mockBuffer, true)

assertNotEquals(mockBuffer, nativeSource)
assertThat(nativeSource).isNotEqualTo(mockBuffer)
}

@ParameterizedTest(name = "{0} must be supported? {1}")
@MethodSource("supportedEncodingSource")
@DisplayName("Check if body encoding is supported")
fun bodyHasSupportedEncoding(encoding: String?, supported: Boolean) {
fun bodyHasSupportedEncoding(encoding: String?, isSupported: Boolean) {
val result = ioUtils.bodyHasSupportedEncoding(encoding)

assertEquals(supported, result)
assertThat(result).isEqualTo(isSupported)
}

companion object {
Expand Down
Loading

0 comments on commit b5bc7fc

Please sign in to comment.