Facilitates comprehensive testing of `equals()`, `hashCode()`, `compareTo`, and `toString()` methods. Features advanced checks for equality groups, collections, and `Comparable` implementations.
Automatic tester for equals(), hashCode(), compareTo(), and toString() methods of a class.
Similar to Guava's EqualsTester, but designed for Kotlin Multiplatform with enhanced validation features.
Example
testEquality {
group(User("alex"), User("alex"), User("alex"))
group(User("sergey"), User("sergey"))
// User("page") is instantiated multiple times (default is 2).// Can be customized via defaultGroupSize.
group { User("page") }
}
Each group must contain objects that are equal to each other, but not equal to objects in any other group.
Adding more than one group is optional, but highly recommended.
There is no limit on the number of groups or objects. If the total number of test iterations is below maxIterations, all combinations are tested exhaustively. Otherwise, randomized testing is used to keep execution time reasonable.
Objects within the same group are equal to each other.
Objects from different groups are not equal.
Equal objects have the same hash code.
Multiple calls to hashCode() return consistent results.
testEquality {
// Whether to require that no two objects in the same group are identical (===). Default: false
requireNonIdentical = true// Maximum number of test iterations before switching to random sampling. Default: 100_000
maxIterations = 1000// Default number of instances generated by factory functions. Default: 2
defaultGroupSize = 3// Whether to check toString() consistency with equals(). Default: false
checkToString = true// Whether to check Collection contract compliance (List, Map, Set). Default: true
checkCollectionContracts = true// Whether to check Comparable contract compliance. Default: true
checkComparable = true// Group with predefined equal objects
group(User("alex", 25), User("alex", 25))
// Group using factory function (creates defaultGroupSize instances)
group { index -> User("user", 30) }
// Group from list
group(listOf(User("bob", 35), User("bob", 35)))
// Group with explicit size
groupOfSize(5) { index -> User("user", 30) }
// Named groups (names are used in error messages)
namedGroup("test group", User("alex", 25), User("alex", 25))
namedGroup("test group", listOf(User("charlie", 45), User("charlie", 45)))
namedGroup("special users") { index -> User("special", 40) }
namedGroup("admin users", size = 2) { index -> User("admin", 50) }
}
turbine
★ 2.8k
cashapp
Small testing library for coroutines Flow. Offers awaitItem(), awaitComplete(), and awaitError() to test emitted items, completion, and errors. Allows standalone usage and custom timeouts, ensuring all events are consumed.