All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.episode6.mxo2.api.MockspressoPluginApi.kt Maven / Gradle / Ivy

Go to download

Public api of Mockspresso2, a testing tool designed to reduce friction, boiler-plate and brittleness in unit tests.

There is a newer version: 2.0.1
Show newest version
package com.episode6.mxo2.api

import com.episode6.mxo2.reflect.DependencyKey

/**
 * An accessor into the mockspresso dependency map that is passed to real and dynamic object makers which enables
 * them to inject dependencies into the objects they create.
 */
interface Dependencies {

  /**
   * Returns a dependency from mockspresso
   */
  fun  get(key: DependencyKey): T
}

/**
 * A function that makes real objects (usually via reflection). Should always return a valid value or throw withh
 * a descriptive reason.
 */
fun interface RealObjectMaker {
  /**
   * Return a new object matching the type represented in the [key]. Any dependencies needed can be pulled
   * from the [dependencies] object
   */
  fun makeRealObject(key: DependencyKey<*>, dependencies: Dependencies): Any?
}

/**
 * A function that gets a chance to create any unregistered dependency needed in a given test.
 */
fun interface DynamicObjectMaker {

  /**
   * Response type for [DynamicObjectMaker].
   */
  sealed interface Answer {

    /**
     * Answer to return when [DynamicObjectMaker] wants to create the object. The included [value] will be cached and
     * used for the provided dependencyKey
     */
    data class Yes(val value: Any?) : Answer

    /**
     * Answer to return when [DynamicObjectMaker] doesn't know how to create the given object.
     */
    object No : Answer
  }

  /**
   * Return an [Answer], if [Answer.Yes], the value must be castable as type represented in [key]
   */
  fun canMakeObject(key: DependencyKey<*>, dependencies: Dependencies): Answer
}

/**
 * A function that creates any dependencies needed by but not explicitly registered in a given test.
 * Usually implemented by returning mocks.
 */
interface FallbackObjectMaker {
  fun  makeObject(key: DependencyKey): T
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy