Skip to content

Commit

Permalink
Added annotation NotBlank for String parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Feb 19, 2021
1 parent d213628 commit 2ad693c
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 3 deletions.
8 changes: 6 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
- 1.5.0

- `@NotBlank` for Strings

- Remove deprecated Arbitrary.just()

- Generate documentation

- Release


- 1.5.1

Expand Down
22 changes: 22 additions & 0 deletions api/src/main/java/net/jqwik/api/constraints/NotBlank.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package net.jqwik.api.constraints;

import java.lang.annotation.*;

import org.apiguardian.api.*;

import static org.apiguardian.api.API.Status.*;

/**
* Constrain a string to never be blank, i.e. not empty and not just whitespace.
*
* Applies to String parameters which are also annotated with {@code @ForAll}.
*
* @see net.jqwik.api.ForAll
* @see NotEmpty
*/
@Target({ ElementType.ANNOTATION_TYPE, ElementType.PARAMETER, ElementType.TYPE_USE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@API(status = EXPERIMENTAL, since = "1.5.0")
public @interface NotBlank {
}
4 changes: 3 additions & 1 deletion docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ title: jqwik Release Notes

#### New and Enhanced Features

- Added constraint annotation `@NotBlank` for String parameters

- Generated email addresses get a few more edge cases

#### Breaking Changes
Expand All @@ -44,7 +46,7 @@ title: jqwik Release Notes
- Some arbitrary types, e.g. `Arbitraries.lazyOf()` could not be used in sampling.
Now all should work.

- Bounded shrinking could result in an `OutsideJqwikException`.
- Bounded shrinking could previously result in an `OutsideJqwikException`.


## 1.4.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ depending on the requested parameter type.
- [`@NotEmpty`](/docs/${docsVersion}/javadoc/net/jqwik/api/constraints/NotEmpty.html):
Set minimum length to `1`.

#### String not Blank

- [`@NotBlank`](/docs/${docsVersion}/javadoc/net/jqwik/api/constraints/NotBlank.html):
Strings must not be empty or only contain whitespace.


#### Character Sets

When generating chars any unicode character might be generated.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package net.jqwik.engine.properties.configurators;

import net.jqwik.api.*;
import net.jqwik.api.configurators.*;
import net.jqwik.api.constraints.*;
import net.jqwik.api.providers.*;

public class NotBlankConfigurator extends ArbitraryConfiguratorBase {

@Override
protected boolean acceptTargetType(TypeUsage targetType) {
return targetType.isOfType(String.class);
}

public Arbitrary<String> configure(Arbitrary<String> arbitrary, NotBlank notBlank) {
return arbitrary.filter(s -> s != null && !s.trim().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ net.jqwik.engine.properties.configurators.BigDecimalRangeConfigurator
net.jqwik.engine.properties.configurators.PositiveConfigurator
net.jqwik.engine.properties.configurators.NegativeConfigurator
net.jqwik.engine.properties.configurators.WhitespaceConfigurator
net.jqwik.engine.properties.configurators.NotBlankConfigurator
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.jqwik.api.constraints;

import net.jqwik.api.*;

class NotBlankProperties {

@Property
boolean aStringIsNeverBlank(@ForAll @NotBlank String aString) {
return isNotBlank(aString);
}

@Property
boolean aProvidedStringIsNeverBlank(@ForAll("provided") @NotBlank String aString) {
return isNotBlank(aString);
}

@Provide
Arbitrary<String> provided() {
return Arbitraries.strings().ofMaxLength(5);
}

@Property
void doesNotInfluenceNonStringArbitrary(@ForAll @NotBlank long aLong) {
}

private boolean isNotBlank(String aString) {
return aString != null && !aString.trim().isEmpty();
}

}

0 comments on commit 2ad693c

Please sign in to comment.