main

square/leakcanary

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

UnreachableObjectRenderingTest.kt

TLDR

This file contains a test case for rendering unreachable objects in the Shark library.

Classes

UnreachableObjectRenderingTest

This class represents a test case for rendering unreachable objects. It includes a single test method called renders unreachable object. The test creates a heap dump with a watched instance of com.example.SomeClass and performs an analysis on the heap dump. It then asserts that the rendering of the unreachable object matches the expected string.

package shark

import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

class UnreachableObjectRenderingTest {

  @Test fun `renders unreachable object`() {
    val heapDump = dump {
      "com.example.SomeClass" watchedInstance {
      }
    }

    val analysis = heapDump.checkForLeaks<HeapAnalysisSuccess>()

    analysis renders """
    com.example.SomeClass instance
    _  Leaking: YES (ObjectWatcher was watching this because its lifecycle has ended)
    _  key = 39efcc1a-67bf-2040-e7ab-3fc9f94731dc
    _  watchDurationMillis = 25000
    _  retainedDurationMillis = 10000
        """
  }

  private infix fun HeapAnalysisSuccess.renders(expectedString: String) {
    assertThat(unreachableObjects[0].toString()).isEqualTo(
      expectedString.trimIndent().replace('_', LeakTrace.ZERO_WIDTH_SPACE)
    )
  }

}