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 Name
  • Verify Modifiers
  • Verify Annotations
  • Verify Body Type
  • Verify Parameters
  • Verify Return Type
  • Verify Generic Parameters
  • Verify Generic Type Arguments
  • Verify Top Level
Edit on GitHub
Export as PDF
  1. VERYFYING CODEBASE

Verify Functions

PreviousVerify InterfacesNextVerify Properties

Last updated 5 months ago

Functions can be validated for their signatures, modifiers, naming patterns, return types, and parameter structures.

To verify functions start by querying all functions present in the project:

Konsist
.scopeFromProject()
.functions()
...

In practical scenarios you'll typically want to verify a specific subset of functions - such as those defined inside classes:

Konsist
.scopeFromProject()
.classes()
.functions()
...

Konsist API allows to query local functions:

Konsist
.scopeFromProject()
.classes()
.functions(includeLocal = true)
...

Konsist allows you to verify multiple aspects of a functions. For a complete understanding of the available APIs, refer to the language reference documentation for .

Let's look at few examples.

Verify Name

Function names can be validated to ensure they follow project naming conventions and patterns.

Check if function name starts with get :

...
.assertTrue {
   it.hasNameStartingWith("get")
}

Verify Modifiers

Function modifiers can be validated to ensure proper encapsulation and access control.

Check if function has public or default (also public) modifier:

..
.assertTrue {
   it.hasPublicOrDefaultModifier
}

Verify Annotations

Function-level and member annotations can be verified for presence, correct usage, and required attribute values.

Check if function is annotated with Binding annotation:

...
.assertTrue {
   it.hasAnnotationOf(Binding::class)
}

Verify Body Type

Functions with block bodies (using curly braces) can be validated to ensure compliance with code structure requirements:

...
.assertTrue { 
    it.hasBlockBody 
}

Expression body functions (using single-expression syntax) can be verified to maintain consistent style across the codebase:

...
.assertTrue { 
    it.hasExpressionBody 
}

Verify Parameters

Function parameters can be validated for their types, names, modifiers, and annotations to ensure consistent parameter usage.

Check if function has parameter of type String:

...
.assertTrue { 
    it.hasParameter { parameter  -> parameter.hasTypeOf(String::class) }
}

Verify Return Type

Return types can be checked to ensure functions follow expected return type patterns and contracts.

Check if function has Kotlin collection type:

...
.assertTrue { 
    it.returnType?.sourceDeclaration?.isKotlinCollectionType
}

Verify Generic Parameters

Generic type parameters can be validated to ensure proper generic type usage and constraints.

Check if function has type parameters:

...
.assertTrue { 
    it.hasTypeParameters()
}

Verify Generic Type Arguments

Generic type arguments can be checked for correct usage.

Check if return type has no type arguments:

...
.assertFalse {
    it.returnType?.hasTypeArguments()
}

Verify Top Level

Top-level functions (functions not declared inside a class) can be specifically queried and validated:

...
.assertTrue { 
    it.isTopLevel
}

This helps ensure top-level functions follow project conventions, such as limiting their usage or enforcing specific naming patterns.

✏️
KoFunctionDeclaration