An open API service indexing awesome lists of open source software.

https://github.com/valid8j/valid8j

A value checking library for Java (DbC, validations, and test assertioins)
https://github.com/valid8j/valid8j

assertions-library dbc preconditions testing

Last synced: 5 months ago
JSON representation

A value checking library for Java (DbC, validations, and test assertioins)

Awesome Lists containing this project

README

          

:toc:

= `valid8j`: DbC, Validation, and Test Assertion Library

`valid8j` is a library that provides consistent programming experiences across DbC<>, Value-checking, and Test assertions.
It is named after `valid4j`<>, but it does more.

It also provides readability both in codes and messages on failures.
Easy to write.

[%nowrap, java]
.`IntroductionExample` class
----
public class IntroductionExample {
public String examplePublicMethod(String name, int basePrice) {
// Use `Expectations.requireXyz` method to check values in production.
requireArguments(
that(name).satisfies()
.notNull(),
that(basePrice).satisfies()
.greaterThanOrEqualTo(0)
.lessThan(10_000));
return examplePrivateMethod(name, basePrice);
}

private String examplePrivateMethod(String name, int basePrice) {
// Use `assert` statement with`Expectations.` {`precondition`,`invariant`,`postcondition`} methods
// and their plural for Design by Contract programming.
assert preconditions(
// `value(var)` and `that(var)` are synonyms. Use the one you like.
// `toBe(var)` and `satisfies(var)` are synonyms. Use the one you like.
value(name).toBe()
.notNull(),
value(basePrice).toBe()
.greaterThanOrEqualTo(0)
.lessThan(10_000));
int price = (int) (basePrice * 1.08);
return String.format("%s:%s", name, price);
}

@TestMethodExpectation(FAILURE)
@Test
public void exampleMethod() {
String message = examplePublicMethod("Kirin Ichiban", 100);
// Use `Expectations.assertAll` for test assertions.
assertAll(
that(message)
.substringAfter(":")
.parseInt()
.satisfies()
.equalTo(110),
that(message)
.satisfies()
.startingWith("Kirin Ichiban"));
}
}
----

This gives the following output as your IDE's window.:

.Output of `IntroductionExample.java`
|===
|Expectation |Actual

a|
[%nowrap]
----
"Kirin Ichiban:108"->WHEN:transform ->true
-> substringAfter[:] ->"108"
"108" -> parseInt ->108
[0] 108 -> THEN:=[110] ->true
"Kirin Ichiban:108"->WHEN:startsWith[Kirin Ichiban]->true

.Detail of failure [0]
---
=[110]
---
----
a|
[%nowrap]
----
"Kirin Ichiban:108"->WHEN:transform ->false
-> substringAfter[:] ->"108"
"108" -> parseInt ->108
[0] 108 -> THEN:=[110] ->false
"Kirin Ichiban:108"->WHEN:startsWith[Kirin Ichiban]->true

.Detail of failure [0]
---
108
---

----
|===

== Installation

Have a following maven dependency in your `pom.xml`.

[source,xml]
[subs="verbatim,attributes"]
----

com.github.dakusui
valid8j
{valid8j}

----

Visit https://oss.sonatype.org/#nexus-search;quick~valid8j[oss.sonatype.org] to figure out the most recent version of `valid8j`.
Check https://valid8j.github.io/valid8j/[valid8j]'s documentation site for more detail.

[bibliography]
== References

- [[[DbC, 1]]] Wikipedia article on Design by Contract, https://en.wikipedia.org/wiki/Design_by_contract
- [[[v4j, 2]]] Valid4j, http://www.valid4j.org