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
  • Declaration Properties
  • Debugging Declaration Properties
Edit on GitHub
Export as PDF
  1. FEATURES

Declaration

What is declaration?

PreviousDebug Konsist TestNextDeclaration Vs Property

Last updated 6 months ago

The declaration (KoDeclaration) represents a code entity, a piece of Kotlin code. Every parsed Kotlin File (KoFileDeclaration) contains one or more declarations. The declaration can be a package (KoPackageDeclaration), property (KoPropertyDeclaration), annotation (KoAnnotationDeclaration), class (KoClassDeclaration), etc.

Consider this Kotlin code snippet file:

private const val logLevel = "debug"

@Entity
open class Logger(val level: String) {
   fun log(message: String) {
   
   } 
}

The above snippet is represented by the KoFileDeclarationclass. It contains two declarations - property declaration (KoPropertyDeclaration) and class declaration (KoClassDeclaration). The Logger class declaration contains a single function declaration (KoFunctionDeclaration ):

Declarations mimic the Kotlin file structure. Konsts API provides a way to retrieve every element. To get all functions in all classes inside the file using .classes().functions() :

koFile // List<KoFile>
    .classes()  // List<KoClassDeclaration>
    .functions() // List<KoFunctionDeclaration>

To print declaration content use koDeclaration.print() method.

Declaration Properties

Each declaration contains a set of properties to facilitate filtering and verification eg. KoClass declaration has name, modifiers , annotations , declarations (containing KoFunction) etc. Here is how the name of the function can be retrieved.

val name = koFile // List<KoFileDeclaration>
    .classes()  // List<KoClassDeclaration>
    .functions() // List<KoFunctionDeclaration>
    .first() // KoFunctionDeclaration
    .name // String
    
println(name) // prints: log

Although it is possible to retrieve a property of a single declaration usually verification is performed on a collection of declarations matching certain criteria eg. methods annotated with specific annotations or classes residing within a single package. See the Declaration Filtering page.

Debugging Declaration Properties

Each declaration exposes a few additional properties to help with debugging:

  • text - provides declaration text eg. val property role = "Developer"

  • location - provides file path with file name, line, and column e.g. ~\Dev\IdeaProject\SampleApp\src\kotlin\com\sample\Logger:10:5

  • locationWithText - provides location together with the declaration text

📗