-
Notifications
You must be signed in to change notification settings - Fork 101
Add StringCombinableArbitrary for easy efficient customisation #1179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
seongahjo
wants to merge
2
commits into
main
Choose a base branch
from
sa/string-combinable-arbitrary
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
106 changes: 106 additions & 0 deletions
106
...pi/src/main/java/com/navercorp/fixturemonkey/api/arbitrary/StringCombinableArbitrary.java
This file contains hidden or 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,106 @@ | ||
/* | ||
* Fixture Monkey | ||
* | ||
* Copyright (c) 2021-present NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.navercorp.fixturemonkey.api.arbitrary; | ||
|
||
import java.util.function.Predicate; | ||
|
||
public interface StringCombinableArbitrary extends CombinableArbitrary<String> { | ||
int STRING_DEFAULT_MIN_LENGTH = 0; | ||
int STRING_DEFAULT_MAX_LENGTH = 100; | ||
|
||
@Override | ||
String combined(); | ||
|
||
@Override | ||
String rawValue(); | ||
|
||
StringCombinableArbitrary withLength(int min, int max); | ||
|
||
/** | ||
* Generates a StringCombinableArbitrary which contains only alphabetic characters. | ||
* It conflicts with {@link #ascii()} and {@link #numeric()} and {@link #korean()}. | ||
* | ||
* @return the StringCombinableArbitrary which contains only alphabetic characters | ||
*/ | ||
StringCombinableArbitrary alphabetic(); | ||
|
||
/** | ||
* Generates a StringCombinableArbitrary which contains only ASCII characters. | ||
* It conflicts with {@link #alphabetic()} and {@link #numeric()} and {@link #korean()}. | ||
* | ||
* @return the StringCombinableArbitrary which contains only ASCII characters | ||
*/ | ||
StringCombinableArbitrary ascii(); | ||
|
||
/** | ||
* Generates a StringCombinableArbitrary which contains only numeric characters. | ||
* It conflicts with {@link #alphabetic()} and {@link #ascii()} and {@link #korean()}. | ||
* | ||
* @return the StringCombinableArbitrary which contains only numeric characters | ||
*/ | ||
StringCombinableArbitrary numeric(); | ||
|
||
/** | ||
* Generates a StringCombinableArbitrary which contains only Korean characters. | ||
* It conflicts with {@link #alphabetic()} and {@link #ascii()} and {@link #numeric()}. | ||
* | ||
* @return the StringCombinableArbitrary which contains only Korean characters | ||
*/ | ||
StringCombinableArbitrary korean(); | ||
|
||
default StringCombinableArbitrary withMinLength(int min) { | ||
return this.withLength(min, STRING_DEFAULT_MAX_LENGTH); | ||
} | ||
|
||
default StringCombinableArbitrary withMaxLength(int max) { | ||
return this.withLength(STRING_DEFAULT_MIN_LENGTH, max); | ||
} | ||
|
||
@Override | ||
default StringCombinableArbitrary filter(Predicate<String> predicate) { | ||
return this.filter(DEFAULT_MAX_TRIES, predicate); | ||
} | ||
|
||
@Override | ||
default StringCombinableArbitrary filter(int tries, Predicate<String> predicate) { | ||
return new StringCombinableArbitraryDelegator(CombinableArbitrary.super.filter(tries, predicate)); | ||
} | ||
|
||
default StringCombinableArbitrary filterCharacter(Predicate<Character> predicate) { | ||
return this.filterCharacter(DEFAULT_MAX_TRIES, predicate); | ||
} | ||
|
||
StringCombinableArbitrary filterCharacter(int tries, Predicate<Character> predicate); | ||
|
||
@Override | ||
default StringCombinableArbitrary injectNull(double nullProbability) { | ||
return new StringCombinableArbitraryDelegator(CombinableArbitrary.super.injectNull(nullProbability)); | ||
} | ||
|
||
@Override | ||
default StringCombinableArbitrary unique() { | ||
return new StringCombinableArbitraryDelegator(CombinableArbitrary.super.unique()); | ||
} | ||
|
||
@Override | ||
void clear(); | ||
|
||
@Override | ||
boolean fixed(); | ||
} |
89 changes: 89 additions & 0 deletions
89
...in/java/com/navercorp/fixturemonkey/api/arbitrary/StringCombinableArbitraryDelegator.java
This file contains hidden or 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,89 @@ | ||
/* | ||
* Fixture Monkey | ||
* | ||
* Copyright (c) 2021-present NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.navercorp.fixturemonkey.api.arbitrary; | ||
|
||
import java.util.function.Predicate; | ||
|
||
final class StringCombinableArbitraryDelegator implements StringCombinableArbitrary { | ||
private final CombinableArbitrary<String> delegate; | ||
|
||
public StringCombinableArbitraryDelegator(CombinableArbitrary<String> delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
@Override | ||
public String combined() { | ||
return delegate.combined(); | ||
} | ||
|
||
@Override | ||
public String rawValue() { | ||
return delegate.combined(); | ||
} | ||
|
||
@Override | ||
public StringCombinableArbitrary withLength(int min, int max) { | ||
return new StringCombinableArbitraryDelegator(delegate.filter(it -> min <= it.length() && it.length() <= max)); | ||
} | ||
|
||
@Override | ||
public StringCombinableArbitrary alphabetic() { | ||
return CombinableArbitrary.strings().alphabetic(); | ||
} | ||
|
||
@Override | ||
public StringCombinableArbitrary ascii() { | ||
return CombinableArbitrary.strings().ascii(); | ||
} | ||
|
||
@Override | ||
public StringCombinableArbitrary numeric() { | ||
return CombinableArbitrary.strings().numeric(); | ||
} | ||
|
||
@Override | ||
public StringCombinableArbitrary korean() { | ||
return CombinableArbitrary.strings().korean(); | ||
} | ||
|
||
@Override | ||
public StringCombinableArbitrary filter(int tries, Predicate<String> predicate) { | ||
return new StringCombinableArbitraryDelegator(delegate.filter(tries, predicate)); | ||
} | ||
|
||
@Override | ||
public StringCombinableArbitrary filterCharacter(int tries, Predicate<Character> predicate) { | ||
return this.filter(tries, it -> it.chars().mapToObj(Character.class::cast).allMatch(predicate)); | ||
} | ||
|
||
@Override | ||
public StringCombinableArbitrary unique() { | ||
return new StringCombinableArbitraryDelegator(delegate.unique()); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
delegate.clear(); | ||
} | ||
|
||
@Override | ||
public boolean fixed() { | ||
return delegate.fixed(); | ||
} | ||
} |
This file contains hidden or 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
114 changes: 114 additions & 0 deletions
114
...i/src/main/java/com/navercorp/fixturemonkey/api/jqwik/JqwikStringCombinableArbitrary.java
This file contains hidden or 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,114 @@ | ||
/* | ||
* Fixture Monkey | ||
* | ||
* Copyright (c) 2021-present NAVER Corp. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.navercorp.fixturemonkey.api.jqwik; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import net.jqwik.api.Arbitraries; | ||
import net.jqwik.api.Arbitrary; | ||
import net.jqwik.api.arbitraries.ListArbitrary; | ||
|
||
import com.navercorp.fixturemonkey.api.arbitrary.StringCombinableArbitrary; | ||
|
||
public final class JqwikStringCombinableArbitrary implements StringCombinableArbitrary { | ||
private final Arbitrary<Character> characterArbitrary; | ||
@Nullable | ||
private Integer minSize = null; | ||
@Nullable | ||
private Integer maxSize = null; | ||
|
||
public JqwikStringCombinableArbitrary() { | ||
this.characterArbitrary = Arbitraries.chars(); | ||
} | ||
|
||
private JqwikStringCombinableArbitrary(Arbitrary<Character> characterArbitrary) { | ||
this.characterArbitrary = characterArbitrary; | ||
} | ||
|
||
@Override | ||
public String combined() { | ||
ListArbitrary<Character> characterListArbitrary = characterArbitrary.list(); | ||
if (this.minSize != null) { | ||
characterListArbitrary = characterListArbitrary.ofMinSize(this.minSize); | ||
} | ||
|
||
if (this.maxSize != null) { | ||
characterListArbitrary = characterListArbitrary.ofMaxSize(this.maxSize); | ||
} | ||
|
||
List<Character> characters = characterListArbitrary.sample(); | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
for (Character character : characters) { | ||
stringBuilder.append(character); | ||
} | ||
return stringBuilder.toString(); | ||
} | ||
|
||
@Override | ||
public String rawValue() { | ||
return this.combined(); | ||
} | ||
|
||
@Override | ||
public StringCombinableArbitrary withLength(int minSize, int maxSize) { | ||
this.minSize = minSize; | ||
this.maxSize = maxSize; | ||
return this; | ||
} | ||
|
||
@Override | ||
public StringCombinableArbitrary alphabetic() { | ||
return new JqwikStringCombinableArbitrary(Arbitraries.chars().alpha()); | ||
} | ||
|
||
@Override | ||
public StringCombinableArbitrary ascii() { | ||
return new JqwikStringCombinableArbitrary(Arbitraries.chars().ascii()); | ||
} | ||
|
||
@Override | ||
public StringCombinableArbitrary numeric() { | ||
return new JqwikStringCombinableArbitrary(Arbitraries.chars().numeric()); | ||
} | ||
|
||
@Override | ||
public StringCombinableArbitrary korean() { | ||
return new JqwikStringCombinableArbitrary(Arbitraries.chars().filter( | ||
it -> '가' <= it && it <= '힣' | ||
)); | ||
} | ||
|
||
@Override | ||
public StringCombinableArbitrary filterCharacter(int tries, Predicate<Character> predicate) { | ||
return new JqwikStringCombinableArbitrary(this.characterArbitrary.filter(tries, predicate)); | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
// ignored | ||
} | ||
|
||
@Override | ||
public boolean fixed() { | ||
return false; | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
...ces/META-INF/services/com.navercorp.fixturemonkey.api.arbitrary.StringCombinableArbitrary
This file contains hidden or 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 @@ | ||
com.navercorp.fixturemonkey.api.jqwik.JqwikStringCombinableArbitrary | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems it's already loaded (it's inside the api module). Is that ok?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I test it, SPI does not load the implementation implicitly.
It should specify the implementation in
META-INF/services
.I also did not find the following specification in the document.
https://docs.oracle.com/javase/tutorial/sound/SPI-intro.html