main

square/leakcanary

Last updated at: 29/12/2023 09:38

TestDescriptionHolderTest.kt

TLDR

The file TestDescriptionHolderTest.kt is a test file in the leakcanary package that contains tests related to retrieving test descriptions. It includes functionality for retrieving test descriptions and verifying their accuracy.

Methods

No methods found in the file

Classes

TestDescriptionHolderTest

The TestDescriptionHolderTest class is responsible for testing the retrieval of test descriptions. It includes the following tests:

  • retrievingTestDescriptionDoesNotThrowWhileTestEvaluating: Tests that retrieving a test description does not throw an exception while the test is being evaluated.
  • currentTestDescriptionIsAccurate: Tests that the current test description is accurate by comparing it to the stack trace of a RuntimeException.
  • testDescriptionThrowsBeforeClass: Tests that a test description throws an exception before the @BeforeClass method is executed.
  • testDescriptionThrowsBeforeOuterEvaluate: Tests that a test description throws an exception before the outer evaluate method is executed.
  • testDescriptionDoesNotThrowBeforeInnerEvaluate: Tests that a test description does not throw an exception before the inner evaluate method is executed.

AttemptsRetrievingTestDescription

The AttemptsRetrievingTestDescription class implements the TestRule interface and is used within the TestDescriptionHolderTest class. It applies a rule that attempts to retrieve the test description and captures any exceptions that occur.

package leakcanary

import org.assertj.core.api.Assertions.assertThat
import org.junit.BeforeClass
import org.junit.Rule
import org.junit.Test
import org.junit.rules.RuleChain
import org.junit.rules.TestRule
import org.junit.runner.Description
import org.junit.runners.model.Statement

class TestDescriptionHolderTest {

  companion object {
    var beforeClassThrowable: Throwable? = null

    @BeforeClass @JvmStatic fun beforeClass() {
      beforeClassThrowable = try {
        TestDescriptionHolder.testDescription
        null
      } catch (throwable: Throwable) {
        throwable
      }
    }
  }

  class AttemptsRetrievingTestDescription : TestRule {

    var beforeEvaluateThrowable: Throwable? = null

    override fun apply(base: Statement, description: Description): Statement {
      return object : Statement() {
        override fun evaluate() {
          beforeEvaluateThrowable = try {
            TestDescriptionHolder.testDescription
            null
          } catch (throwable: Throwable) {
            throwable
          }
          base.evaluate()
        }
      }
    }
  }

  private val outerRule = AttemptsRetrievingTestDescription()
  private val innerRule = AttemptsRetrievingTestDescription()

  @get:Rule
  val rule = RuleChain.outerRule(outerRule).around(TestDescriptionHolder).around(innerRule)!!

  @Test
  fun retrievingTestDescriptionDoesNotThrowWhileTestEvaluating() {
    TestDescriptionHolder.testDescription
  }

  @Test
  fun currentTestDescriptionIsAccurate() {
    val stackTop = RuntimeException().stackTrace.first()
    val testDescription = TestDescriptionHolder.testDescription
    assertThat(testDescription.className).isEqualTo(stackTop.className)
    assertThat(testDescription.methodName).isEqualTo(stackTop.methodName)
  }

  @Test
  fun testDescriptionThrowsBeforeClass() {
    // On API 16, assertThat(Throwable) causes a TypeComparators class init failure.
    assertThat(beforeClassThrowable == null).isFalse()
  }

  @Test
  fun testDescriptionThrowsBeforeOuterEvaluate() {
    // On API 16, assertThat(Throwable) causes a TypeComparators class init failure.
    assertThat(outerRule.beforeEvaluateThrowable == null).isFalse()
  }

  @Test
  fun testDescriptionDoesNotThrowBeforeInnerEvaluate() {
    // On API 16, assertThat(Throwable) causes a TypeComparators class init failure.
    assertThat(innerRule.beforeEvaluateThrowable == null).isTrue()
  }
}