Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1685 from Ro4052/fix/playground-demo
Browse files Browse the repository at this point in the history
Fix playground demo bug (#1646)
  • Loading branch information
tjohnson-scottlogic authored Jul 14, 2020
2 parents 33e6b5b + ba7c05e commit da3646a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.scottlogic.datahelix.generator.common.RandomNumberGenerator;
import com.scottlogic.datahelix.generator.common.util.OrderedRandom;

import java.lang.reflect.InvocationTargetException;
import java.util.stream.Stream;

public class FakerGenerator implements StringGenerator {
Expand Down Expand Up @@ -71,6 +72,30 @@ public Stream<String> generateRandomValues(RandomNumberGenerator randomNumberGen
}

private String getFakerValue(Faker faker) {
return faker.expression("#{" + this.fakerSpec + "}");
try {
return faker.expression("#{" + this.fakerSpec + "}");
} catch (Exception e) {
return getFakerValueWorkaround(faker);
}
}

/*
* This method is a temporary workaround for the issue highlighted here:
* https://github.com/DiUS/java-faker/issues/474
*
* Once the underlying issue has been fixed, this method can be removed.
*/
private String getFakerValueWorkaround(Faker faker) {
String elements[] = this.fakerSpec.split("\\.");
Object obj = faker;
for (String element : elements) {
try {
obj = obj.getClass().getMethod(element).invoke(obj);
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
}

return (String) obj;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -74,4 +75,21 @@ void generateRandomValuesRestrictedMax() {

assertTrue(results.allMatch(str -> str.length() <= length));
}

@Test
void generateWithCompositeExpression() {
String jobTitleRegex = "^[A-Za-z]+ [A-Za-z]+$";
StringRestrictions restrictions = StringRestrictionsFactory.forStringMatching(
Pattern.compile(jobTitleRegex), false
);
RegexStringGenerator regex = (RegexStringGenerator) restrictions.createGenerator();
FakerGenerator generator = new FakerGenerator(regex, "job.title");

final int size = 10;

Stream<String> results = generator.generateRandomValues(new JavaUtilRandomNumberGenerator())
.limit(size);

assertTrue(results.allMatch(str -> str.matches(jobTitleRegex)));
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ CUCUMBER_VERSION=4.0.0
CUCUMBER_EXPRESSIONS_VERSION=6.0.1
CUCUMBER_PICOCONTAINER_VERSION=1.2.5

FAKER_VERSION=1.0.1
FAKER_VERSION=1.0.2

0 comments on commit da3646a

Please sign in to comment.