Konsist
GitHubSlack (kotlinlang)Twitter
  • 🚀GETTING STARTED
    • What is Konsist?
    • Getting Started
      • Add Konsist Dependency
      • Create First Konsist Test - Declaration Check
      • Create Secound Konsist Test - Architectural Check
    • Articles & Videos
  • ✅WRITING TESTS
    • Create The Scope
    • Declaration Filtering
    • Declaration Assertion
    • Architecture Assertion
    • Suppress Konsist Test
  • ✏️VERYFYING CODEBASE
    • Verify Classes
    • Verify Interfaces
    • Verify Functions
    • Verify Properties
    • Verify Generics
    • Verify Source Declarations
  • 📗FEATURES
    • Add Konsist Existing To Project (Baseline)
    • Debug Konsist Test
    • Declaration
    • Declaration Vs Property
    • Compiler Type Inference
    • Package Wildcard
    • Declaration References
    • Indirect Parents
    • Kotest Support
  • 🔍INSPIRATION
    • Starter Projects
    • Snippets
      • General Snippets
      • Android Snippets
      • Spring Snippets
      • Test Snippets
      • JUnit Snippets
      • Kotest Snippets
      • Architecture Snippets
      • Clean Architecture Snippets
      • Kotlin Serialization Snippets
      • Library Snippets
      • Generic Types Snippets
  • 🎓ADVANCED
    • Isolate Konsist Tests
    • Enable Full Command Line Logging
    • Dynamic Konsist Tests
      • Explicit Test Names
    • When Konsist API Is Not Enough
    • Additional JUnit5 Setup
    • Why There Are No Pre-defined Rules?
    • Konsist Snapshots
  • ❓HELP
    • Getting Help
    • Known Issues
      • java.lang.OutOfMemoryError: Java heap space
    • Compatibility
  • ℹ️OTHER
    • Changelog
    • Project Status
    • Contributing
    • Contributors
    • Assets And Logos
    • Open Source Licenses
    • Sponsor Konsist
Powered by GitBook
On this page
  • Verify Property Source Declaration
  • Verify Function Return Type Source Declaration
  • Verify Class Has Interface Source Declaration
Edit on GitHub
Export as PDF
  1. VERYFYING CODEBASE

Verify Source Declarations

The source declaration (sourceDeclaration property) holds the reference to actual type declaration such as class or interface.

Konsist API allows for verify properties of such type e.g.:

  • Check if property type implements certain interface

  • Check if function return type name ends with Repository

  • Check if parent class is annotated with given annotation

Every declaration that is using another type such as property, function, parent exposes sourceDeclaration property.

Let's look at few examples:

Verify Property Source Declaration

Check if type of current property is has a type which is a class declaration heaving internal modifier:

// Code Snippet
internal class Engine
val current: Engine? = null

// Konsist test
Konsist
   .scopeFromProject()
   .properties()
   .assertTrue {
      it
      .type
      ?.sourceDeclaration
      ?.asClassDeclaration()
      ?.hasInternalModifier // true
   }

Note that explicit casting (asXDeclaration) has to be used to access specific properties of the declaration.

Verify Function Return Type Source Declaration

Check if function return type is a basic Kotlin type:

// Code Snippet
internal class Engine {
   fun start(): Boolean
}

// Konsist test
Konsist
   .scopeFromProject()
   .classes()
   .functions()
   .assertTrue {
      it.returnType?
      .sourceDeclaration
      ?.isKotlinBasicType
   }

Verify Class Has Interface Source Declaration

// Code Snippet
internal class Engine {
   fun start(): Boolean
}

// Konsist test
Konsist
   .scopeFromProject()
   .classes()
   .parents()
   .assertTrue {
      it
      .sourceDeclaration
      ?.isInterface
   }

PreviousVerify GenericsNextAdd Konsist Existing To Project (Baseline)

Last updated 5 months ago

✏️