Skip to content

Commit

Permalink
#36. Generate Not Matching String
Browse files Browse the repository at this point in the history
Fixing CompleteTests
  • Loading branch information
curious-odd-man committed Aug 25, 2020
1 parent 175973a commit b1a1fad
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public String toString() {
return "SymbolSet{" + Arrays.toString(aSymbols) + '}';
}

public boolean isAnyChar() {
return Arrays.deepEquals(aSymbols, ALL_SYMBOLS);
public boolean isEmpty() {
return aSymbols.length == 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ public NotMatchingGenerationVisitor(Random random) {
@Override
public void visit(SymbolSet node) {
// There is only one way to generate not matching for any character - is to not generate anything
if (!node.isAnyChar()) {
String pattern = node.getPattern();
super.visit(new SymbolSet("[^" + pattern.substring(1), node.getSymbols(), SymbolSet.TYPE.NEGATIVE));
String pattern = node.getPattern();
SymbolSet symbolSet = new SymbolSet("[^" + pattern.substring(1), node.getSymbols(), SymbolSet.TYPE.NEGATIVE);
if (!symbolSet.isEmpty()) {
super.visit(symbolSet);
}
}

Expand All @@ -49,10 +50,10 @@ public void visit(Choice node) {
// We need to add existing group values, so that we could later use it in matching pattern
StringBuilder groupsBuilder = new StringBuilder();
StringBuilder valuePrefixBuilder = new StringBuilder();
for (int i = 0; i < aGroupValues.size(); i++) {
for (int i = 1; i < aGroupValues.size(); i++) {
String s = aGroupValues.get(i);
groupsBuilder.append('(')
.append(s)
.append(Pattern.quote(s))
.append(')');
valuePrefixBuilder.append(s);
}
Expand Down
35 changes: 26 additions & 9 deletions src/test/java/com/github/curiousoddman/rgxgen/CompleteTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.Random;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

@RunWith(Parameterized.class)
public class CompleteTests {
@Parameterized.Parameters(name = "{0}")
@Parameterized.Parameters(name = "{2}: {0}")
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][]{
return Arrays.stream(new Object[][]{
{"Card number", "[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}"},
{"IP v4", "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9])\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9])\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9])\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9])"},
{"IP v6", "(([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,6})|(([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5})|" +
Expand All @@ -39,7 +43,10 @@ public static Collection<Object[]> data() {
{"Email after @ - dns name", "[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\."},
{"Email after @", "(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"},
{"Email pattern", "(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])"},
});
})
.flatMap(arr -> IntStream.range(0, 100)
.mapToObj(index -> new Object[]{arr[0], arr[1], index}))
.collect(Collectors.toList());
}

@Parameterized.Parameter
Expand All @@ -48,14 +55,24 @@ public static Collection<Object[]> data() {
@Parameterized.Parameter(1)
public String aRegex;

@Parameterized.Parameter(2)
public int aSeed;

@Test
public void generateTest() {
RgxGen rgxGen = new RgxGen(aRegex);
for (int i = 0; i < 100; i++) {
String s = rgxGen.generate();
assertTrue("Text: '" + s + "'does not match pattern " + aRegex, Pattern.compile(aRegex)
.matcher(s)
.matches());
}
String s = rgxGen.generate(new Random(aSeed));
assertTrue("Text: '" + s + "'does not match pattern " + aRegex, Pattern.compile(aRegex)
.matcher(s)
.matches());
}

@Test
public void generateNotMatchingTest() {
RgxGen rgxGen = new RgxGen(aRegex);
String s = rgxGen.generateNotMatching(new Random(aSeed));
assertFalse("Text: '" + s + "'does not match pattern " + aRegex, Pattern.compile(aRegex)
.matcher(s)
.matches());
}
}

0 comments on commit b1a1fad

Please sign in to comment.