Skip to content

Commit 36962a7

Browse files
committed
test: create akkurate-test artifact
1 parent e3deb78 commit 36962a7

File tree

29 files changed

+119
-30
lines changed

29 files changed

+119
-30
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

99
## [Unreleased]
1010

11+
### Added
12+
13+
- New `akkurate-test` artifact to test custom constraints.
14+
1115
### Fixed
1216

1317
- Add a `or equal to zero` suffix to the default message for `isNegativeOrZero` and `isPositiveOrZero` constraints.

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ Check if 'title' is not empty otherwise write "Missing title".
100100
Take advantage of Kotlin Multiplatform; write your validation code once, use it both for front-end and back-end
101101
usages.
102102

103-
- **Testable out of the box** \
103+
- [**Testable out of the box**](https://akkurate.dev/docs/extend.html#test-your-code) \
104104
Finding how to test your validation code shouldn't be one of your tasks. You will find all the tools you need to write
105105
good tests.
106-
107-
> [!NOTE]
108-
> Features marked with ⏱ are on the roadmap.
109-

akkurate-test/build.gradle.kts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import dev.nesk.akkurate.gradle.configureTargets
2+
3+
plugins {
4+
id("akkurate.kmp-library-conventions")
5+
id("org.jetbrains.dokka")
6+
}
7+
8+
kotlin {
9+
configureTargets()
10+
11+
sourceSets {
12+
commonMain {
13+
dependencies {
14+
api(project(":akkurate-core"))
15+
}
16+
}
17+
}
18+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package dev.nesk.akkurate.test
19+
20+
import dev.nesk.akkurate.Validator
21+
import dev.nesk.akkurate.validatables.Validatable
22+
23+
/**
24+
* Instantiates a [Validatable] with a value, allowing to test custom constraints.
25+
*
26+
* Usage example:
27+
*
28+
* ```
29+
* fun Validatable<String>.hasWordCountGreaterThan(count: Int) =
30+
* constrain { it.split(" ").size > count }
31+
*
32+
* @Test
33+
* fun testWordCount() {
34+
* assertFalse(Validatable("one two").hasWordCountGreaterThan(2).satisfied)
35+
* assertTrue(Validatable("one two three").hasWordCountGreaterThan(2).satisfied)
36+
* }
37+
* ```
38+
*/
39+
public fun <T> Validatable(value: T): Validatable<T> {
40+
lateinit var validatable: Validatable<T>
41+
val validator = Validator<T> {
42+
validatable = this
43+
}
44+
validator(value)
45+
return validatable
46+
}

documentation/images/clock.png

-1.89 KB
Binary file not shown.
-1.89 KB
Binary file not shown.

documentation/topics/extend.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,31 @@ private fun Validatable<String?>.hasWordCountGreaterThan(count: Int) =
8282
"Must contain more than $count words"
8383
}
8484
```
85+
86+
## Test your code
87+
88+
When writing your own constraints, you might want to write unit tests to verify their behaviors.
89+
90+
This is done by installing a new artifact:
91+
92+
<procedure title="Install akkurate-test">
93+
94+
<code-block lang="kotlin">
95+
testImplementation("dev.nesk.akkurate:akkurate-test:%version%")
96+
</code-block>
97+
98+
</procedure>
99+
100+
This artifact provides
101+
[the Validatable function](https://akkurate.dev/api/akkurate-test/dev.nesk.akkurate.test/-validatable.html), which
102+
returns a value ready to be constrained:
103+
104+
```kotlin
105+
val validatable = Validatable("The Lord of the Rings")
106+
107+
val satisfiedConstraint = validatable.hasWordCountGreaterThan(4)
108+
assertTrue(satisfiedConstraint.satisfied)
109+
110+
val unsatisfiedConstraint = validatable.hasWordCountGreaterThan(5)
111+
assertFalse(unsatisfiedConstraint.satisfied)
112+
```

documentation/topics/overview.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,8 @@ Check if 'title' is not empty otherwise write "Missing title".
9090
take care of the integrations for you.
9191
- [**Code once, deploy everywhere.**](getting-started.md#installation) Take advantage of Kotlin Multiplatform; write
9292
your validation code once, use it both for front-end and back-end usages.
93-
- ![Coming soon](clock.png){width="16"} **Testable out of the box.** Finding how to test your validation code shouldn't
94-
be one of your tasks. You will find all the tools you need to write good tests.
95-
96-
> Features marked with ![Coming soon](clock.png){width="16"} are on the roadmap.
93+
- [**Testable out of the box.**](extend.md#test-your-code) Finding how to test your validation code shouldn't be one of
94+
your tasks. You will find all the tools you need to write good tests.
9795

9896
<seealso style="links">
9997
<category ref="external">

library/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ kotlin {
2121
dependencies {
2222
implementation(kotlin("test"))
2323
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.7.1")
24+
implementation(project(":akkurate-test"))
2425
}
2526
}
2627
}

library/src/commonTest/kotlin/dev/nesk/akkurate/PathTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package dev.nesk.akkurate
1919

2020
import dev.nesk.akkurate._test.Validatable
21+
import dev.nesk.akkurate.test.Validatable
2122
import dev.nesk.akkurate.validatables.Validatable
2223
import kotlin.test.Test
2324
import kotlin.test.assertContentEquals

0 commit comments

Comments
 (0)