Coder Social home page Coder Social logo

cybernetics / klip Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mpetuska/klip

0.0 2.0 0.0 890 KB

Kotlin Multiplatform (pending KSP support) snapshot (klip) manager for tests

Home Page: https://mpetuska.github.io/klip

Kotlin 99.23% Shell 0.77%

klip's Introduction

Gitpod ready-to-code Slack chat Dokka docs Version gradle-plugin-portal Version maven-central

KLIP

Kotlin Multiplatform snapshot ((c|k)lip) manager for tests. Automatically generates and asserts against a persistent Any::toString() representation of the object until you explicitly trigger an update. Powered by kotlin compiler plugin to inject relevant keys and paths.

Support

The plugin only works on targets using new IR kotlin compiler (which is pretty much all of them since kotlin 1.5 except JS that still defaults to legacy compiler).

Versions

The current version was built using the following tooling versions and is guaranteed to work with this setup. Given the experimental nature of kotlin compiler plugin API, the plugin powering this library is likely to stop working on projects using newer/older kotlin versions

  • Kotlin: 1.5.30-RC
  • Gradle: 7.2.0
  • JDK: 11

Targets

Bellow is a list of currently supported targets and planned targets

  • js
  • jvm
  • linuxX64
  • macosX64
  • mingwX64
  • iosArm32
  • iosArm64
  • iosX64
  • watchosX86
  • watchosX64
  • watchosArm64
  • watchosArm32
  • tvosArm64
  • tvosX64
  • macosX64
  • mingwX64
  • mingwX86

There's also a subset of targets that you currently cannot run tests on (and as such making the library redundant). These targets will use a fallback implementation that throws an error on native api access (since those targets will not execute tests) to enable the general library usage in commonMain source set. If you have a valid use-case of the library for these targets, please raise an issue to discuss a real implementation.

  • androidNativeArm32
  • androidNativeArm64
  • linuxArm32Hfp
  • linuxMips32
  • linuxMipsel32
  • linuxArm64
  • mingwX86

Usage

  1. Apply the plugin
plugins {
  kotlin("multiplatform")
  id("dev.petuska.klip") version "<<version>>"

  kotlin {
    sourceSets {
      commonTest {
        dependencies {
          implementation("dev.petuska:klip:<<version>>")
        }
      }
    }
  }
}
  1. (Optional) Configure the plugin extension (shown with default values). For property descriptions. see Gradle Properties
klip {
  enabled = true
  update = false
  klipAnnotations = setOf("dev.petuska.klip.Klippable") // Takes full control of annotations
  klipAnnotation("dev.petuska.klip.Klippable") // Appends the annotation to the default ones
  scopeAnnotations = setOf( // Takes full control of annotations
    "kotlin.Test",
    "org.junit.Test",
    "org.junit.jupiter.api.Test",
    "org.testng.annotations.Test"
  )
  scopeAnnotation("kotlin.Test") // Appends the annotation to the default ones
}
  1. Use provided klip assertions anywhere under one of the scopeAnnotations.
class MyTest {
  data class DomainObject(val name: String, val value: String?)

  @Test
  fun test1() {
    assertMatchesClip(DomainObject("Dick", "Dickens"))
    DomainObject("John", "Doe").assertKlip()
  }

  @Test
  fun test2() {
    doAssertions()
  }

  private fun doAssertions() {
    assertMatchesClip(DomainObject("Joe", "Mama"))
    DomainObject("Ben", "Dover").assertKlip()
  }
}

Gradle Properties

Most of the DSL configuration options can also be set/overridden via gradle properties ./gradlew <some-task> -Pprop.name=propValue, gradle.properties or ~/.gradle/gradle.properties. Environment variables are also supported and take precedence over gradle properties. Bellow is the full list of supported properties:

  • klip.enabled (KLIP_ENABLED) - toggles the compiler processing on/off.
  • klip.update (KLIP_UPDATE) - if true, will override and update all previous klips during test run.
  • klip.klipAnnotations (KLIP_KLIPANNOTATIONS) - comma separated list of fully qualified names of annotations to process. Only useful when writing your own klippable functions.
  • klip.scopeAnnotations (KLIP_SCOPEANNOTATIONS - comma separated list of fully qualified names of test annotations to scope klip keys.

Basic Flow

  1. Run tests as normal and use generated klip assertions such as assertMatchesKlip(myObject) or myObject.assertKlip(). New klips will always be written to file, whereas existing ones (identified by test class scope and given id) will be read and used for assertions.
  2. When the actual value changes, tests will fail since they do not match previous klip. In such cases inspect the differences and if everything is as expected, re-run test(s) with klip updates enabled. This is done by either passing a gradle prop ./gradlew test -Pklip.update or setting an environment variable KLIP_UPDATE=true ./gradlew test.

Modules

  • :library:klip-api - main runtime library
  • :plugin:klip-gradle-plugin - gradle plugin to manage kotlin compiler plugins
  • :plugin:klip-common-plugin - shared code between plugins (should not have any dependencies)
  • :plugin:klip-kotlin-plugin - kotlin compiler plugin for jvm & js that does the actual work
  • :plugin:klip-kotlin-plugin:klip-kotlin-plugin-native - kotlin compiler plugin for native that does the actual work
  • sandbox - a playground to test local changes from consumer end

klip's People

Contributors

dependabot[bot] avatar mpetuska avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.