Repository: square/leakcanary
File: leakcanary-android-instrumentation/src/main/java/leakcanary/DetectLeaksAfterTestSuccess.kt (Kotlin)

Code before:
  override fun apply(base: Statement, description: Description): Statement {
    return TestDescriptionHolder.wrap(object : Statement() {
      override fun evaluate() {
        try {
          base.evaluate()
          // If the test fails, evaluate() will throw and we won't run the analysis (which is good).
          LeakAssertions.assertNoLeaks(tag)
        } finally {
          // Otherwise upstream test failures will be reported as leaks.
          // https://github.com/square/leakcanary/issues/2297
          AppWatcher.objectWatcher.clearWatchedObjects()
        }
      }
    }, description)
  }

  companion object {
    /**
     * A helper function to trigger leak detection twice during test tear down, before and after
     * the tear down of a set of wrapped rule chains. For example, this can be useful to detect
     * leaks both right before and right after the activity under test is destroyed. Before means
     * we can detect detached fragment leaks that go away when the activity is destroyed. After
     * means we can detect activity leaks.
     *
     * ```
     * RuleChain.outerRule(LoginRule())
     *   .detectLeaksAfterTestSuccessWrapping("ActivitiesDestroyed") {
     *     around(ActivityListenerRule)
     *   }
     *   .around(LoadingScreenRule)
     * ```
     */
    fun RuleChain.detectLeaksAfterTestSuccessWrapping(
      tag: String,
      wrapped: RuleChain.() -> RuleChain
    ): RuleChain {
      return around(DetectLeaksAfterTestSuccess("After$tag")).wrapped()
        .around(DetectLeaksAfterTestSuccess("Before$tag"))
    }
  }
}




Review comment (suggestion):
```suggestion
     * ```kotlin
     * RuleChain.outerRule(LoginRule())
```

Code after:
  override fun apply(base: Statement, description: Description): Statement {
    return TestDescriptionHolder.wrap(object : Statement() {
      override fun evaluate() {
        try {
          base.evaluate()
          // If the test fails, evaluate() will throw and we won't run the analysis (which is good).
          LeakAssertions.assertNoLeaks(tag)
        } finally {
          // Otherwise upstream test failures will be reported as leaks.
          // https://github.com/square/leakcanary/issues/2297
          AppWatcher.objectWatcher.clearWatchedObjects()
        }
      }
    }, description)
  }

  companion object {
    /**
     * A helper function to trigger leak detection twice during test tear down, before and after
     * the tear down of a set of wrapped rule chains. For example, this can be useful to detect
     * leaks both right before and right after the activity under test is destroyed. Before means
     * we can detect detached fragment leaks that go away when the activity is destroyed. After
     * means we can detect activity leaks.
     *
     * ```kotlin
     * RuleChain.outerRule(LoginRule())
     *   .detectLeaksAfterTestSuccessWrapping("ActivitiesDestroyed") {
     *     around(ActivityListenerRule)
     *   }
     *   .around(LoadingScreenRule)
     * ```
     */
    fun RuleChain.detectLeaksAfterTestSuccessWrapping(
      tag: String,
      wrapped: RuleChain.() -> RuleChain
    ): RuleChain {
      return around(DetectLeaksAfterTestSuccess("After$tag")).wrapped()
        .around(DetectLeaksAfterTestSuccess("Before$tag"))
    }
  }
}