Skip to content

Commit

Permalink
Merge 10bdaaa into 33f96e1
Browse files Browse the repository at this point in the history
  • Loading branch information
TatsianaLuchonak committed Aug 21, 2019
2 parents 33f96e1 + 10bdaaa commit ac23884
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
Expand Up @@ -22,7 +22,8 @@
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 +346,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
@@ -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)));
}
}
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 ac23884

Please sign in to comment.