-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added annotation NotBlank for String parameters
- Loading branch information
Showing
7 changed files
with
86 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
engine/src/main/java/net/jqwik/engine/properties/configurators/NotBlankConfigurator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
engine/src/test/java/net/jqwik/api/constraints/NotBlankProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |