Skip to content

Commit

Permalink
Merge pull request #422 from TatsianaLuchonak/Expression_fix
Browse files Browse the repository at this point in the history
Update arguments parsing from expressions
  • Loading branch information
codingricky authored Feb 10, 2020
2 parents 1d50d29 + 8fe21b5 commit 28d1ddf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import java.util.regex.Pattern;

public class FakeValuesService {
private static final Pattern EXPRESSION_PATTERN = Pattern.compile("#\\{([a-z0-9A-Z_.]+)\\s?(?:'([^']+)')?(?:,'([^']+)')*\\}");

private static final Pattern EXPRESSION_PATTERN = Pattern.compile("#\\{([a-z0-9A-Z_.]+)\\s?((?:,?'([^']+)')*)\\}");
private static final Pattern EXPRESSION_ARGUMENTS_PATTERN = Pattern.compile("(?:'(.*?)')");

private final Logger log = Logger.getLogger("faker");

Expand Down Expand Up @@ -345,9 +347,11 @@ protected String resolveExpression(String expression, Object current, Faker root
while (matcher.find()) {
final String escapedDirective = matcher.group(0);
final String directive = matcher.group(1);
final String arguments = matcher.group(2);
final Matcher argsMatcher = EXPRESSION_ARGUMENTS_PATTERN.matcher(arguments);
List<String> args = new ArrayList<String>();
for (int i = 2; i < matcher.groupCount() + 1 && matcher.group(i) != null; i++) {
args.add(matcher.group(i));
while (argsMatcher.find()) {
args.add(argsMatcher.group(1));
}

// resolve the expression and reprocess it to handle recursive templates
Expand Down
9 changes: 8 additions & 1 deletion src/test/java/com/github/javafaker/LoremTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.github.javafaker;

import static com.github.javafaker.matchers.MatchesRegularExpression.matchesRegularExpression;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.isEmptyOrNullString;
import static org.hamcrest.Matchers.isEmptyString;
import static org.hamcrest.Matchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import org.hamcrest.Matchers;
import org.junit.Test;

public class LoremTest extends AbstractFakerTest {
Expand Down Expand Up @@ -71,7 +74,7 @@ public void testCharactersMinimumMaximumLength() {

@Test
public void testCharactersMinimumMaximumLengthIncludeUppercase() {
assertThat(faker.lorem().characters(1, 10), matchesRegularExpression("[a-zA-Z\\d]{1,10}"));
assertThat(faker.lorem().characters(1, 10, true), matchesRegularExpression("[a-zA-Z\\d]{1,10}"));
}

@Test
Expand Down Expand Up @@ -100,4 +103,8 @@ public void testSentenceFixedNumberOfWords() {
assertThat(faker.lorem().sentence(10, 0), matchesRegularExpression("(\\w+\\s?){10}\\."));
}

@Test
public void testWords() {
assertThat(faker.lorem().words(), hasSize(greaterThanOrEqualTo(1)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ public void pastDateExpression() throws ParseException {
assertThat( date.getTime(), lessThan( now.getTime() ));
}

@Test
public void expressionWithFourArguments() throws ParseException {

assertThat(fakeValuesService.expression("#{Internet.password '5','8','true','true'}", faker),
matchesRegularExpression("[\\w\\d\\!%#$@_\\^&\\*]{5,8}"));
}

/**
* Two things are important here:
* 1) the message in the exception should be USEFUL
Expand Down

0 comments on commit 28d1ddf

Please sign in to comment.