-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug with range values was fixed. Tests were added by mkolisnyk
- Loading branch information
Showing
5 changed files
with
244 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
69 changes: 69 additions & 0 deletions
69
src/test/java/com/mifmif/common/regex/GenerexIteratorTest.java
This file contains 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,69 @@ | ||
package com.mifmif.common.regex; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
import com.mifmif.common.regex.util.Iterator; | ||
|
||
/** | ||
* @author Myk Kolisnyk | ||
* | ||
*/ | ||
@RunWith(Parameterized.class) | ||
public class GenerexIteratorTest { | ||
|
||
private String pattern; | ||
private Generex generex; | ||
|
||
@Parameters(name = "Test random: {0}") | ||
public static Collection<Object[]> data() { | ||
return Arrays.asList(new Object[][] { { "Sample multicharacter expression", "[A-B]{5,9}" }, { "Sample expression", "[0-3]([a-c]|[e-g]{1,2})" }, | ||
{ "Number format", "\\d{3,4}" }, | ||
// {"Any non-number","\\D{3,4}"}, | ||
{ "Any word", "\\w{1,2}" }, { "Empty string", "" }, | ||
// {"Any non-word","\\W{1,2}"} | ||
}); | ||
} | ||
|
||
public GenerexIteratorTest(String description, String patternValue) { | ||
this.pattern = patternValue; | ||
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
generex = new Generex(pattern); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testIterateThroughAllGeneratedStrings() { | ||
Iterator iterator = generex.iterator(); | ||
while (iterator.hasNext()) { | ||
String result = iterator.next(); | ||
Assert.assertTrue(String.format("The string '%s' doesn't match the '%s' pattern", result, pattern), result.matches(pattern)); | ||
|
||
} | ||
} | ||
|
||
/* | ||
* @Test public void testIterateShouldReturnTheSameAsGetMatchedStrings() { | ||
* int count = 1; Iterator iterator = generex.iterator(); while | ||
* (iterator.hasNext()) { String matchedResult = | ||
* generex.getMatchedString(count); String result = iterator.next(); | ||
* Assert.assertEquals(String.format("Iteration %d mismatch", count), | ||
* result, matchedResult); count++; } } | ||
*/ | ||
} |
82 changes: 82 additions & 0 deletions
82
src/test/java/com/mifmif/common/regex/GenerexRandomTest.java
This file contains 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,82 @@ | ||
/** | ||
* | ||
*/ | ||
package com.mifmif.common.regex; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
/** | ||
* @author Myk Kolisnyk | ||
* | ||
*/ | ||
@RunWith(Parameterized.class) | ||
public class GenerexRandomTest { | ||
|
||
private String pattern; | ||
private int minLength; | ||
private int maxLength; | ||
|
||
public GenerexRandomTest(String description, | ||
String patternValue, | ||
int minLengthValue, | ||
int maxLengthValue) { | ||
this.pattern = patternValue; | ||
this.minLength = minLengthValue; | ||
this.maxLength = maxLengthValue; | ||
} | ||
|
||
@Parameters(name = "Test random: {0}") | ||
public static Collection<Object[]> data() { | ||
return Arrays.asList(new Object[][] { | ||
{"Sample multicharacter expression","[A-Z]{5,9}", 4 , 8}, | ||
{"Sample expression","[0-3]([a-c]|[e-g]{1,2})", 1 , 3}, | ||
{"E-mail format","([a-z0-9]+)[@]([a-z0-9]+)[.]([a-z0-9]+)", 8 , 24}, | ||
{"Any number","(\\d+)", 4 , 8}, | ||
{"Any non-number","(\\D+)", 4 , 8}, | ||
{"Any word","(\\w+)", 4 , 8}, | ||
{"Any non-word","(\\W+)", 4 , 8}, | ||
{"Any text","(.*)", 4 , 8} | ||
}); | ||
} | ||
|
||
@Test | ||
public void testSimpleRandom() { | ||
Generex generex = new Generex(pattern); | ||
String result = generex.random(); | ||
Assert.assertTrue( | ||
String.format("The string '%s' doesn't match the '%s' pattern", result, pattern), | ||
result.matches(pattern)); | ||
} | ||
@Test | ||
public void testRandomWithMinLength() { | ||
Generex generex = new Generex(pattern); | ||
String result = generex.random(minLength); | ||
Assert.assertTrue( | ||
String.format("The string '%s' doesn't match the '%s' pattern", result, pattern), | ||
result.matches(pattern)); | ||
Assert.assertTrue( | ||
String.format("The string '%s' size doesn't fit the minimal size of %d", result, minLength), | ||
result.length() >= minLength); | ||
} | ||
@Test | ||
public void testRandomWithMaxLength() { | ||
Generex generex = new Generex(pattern); | ||
String result = generex.random(minLength, maxLength); | ||
Assert.assertTrue( | ||
String.format("The string '%s' doesn't match the '%s' pattern", result, pattern), | ||
result.matches(pattern)); | ||
Assert.assertTrue( | ||
String.format("The string '%s' size doesn't fit the minimal size of %d", result, minLength), | ||
result.length() >= minLength); | ||
Assert.assertTrue( | ||
String.format("The string '%s' size doesn't fit the maximal size of %d", result, maxLength), | ||
result.length() <= maxLength); | ||
} | ||
} |
This file contains 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,67 @@ | ||
package com.mifmif.common.regex; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
/** | ||
* @author Myk Kolisnyk | ||
* | ||
*/ | ||
@RunWith(Parameterized.class) | ||
public class GenerexTest { | ||
|
||
private String pattern; | ||
private Generex generex; | ||
|
||
@Parameters(name = "Test get match: {0}") | ||
public static Collection<Object[]> data() { | ||
return Arrays.asList(new Object[][] { { "Sample multicharacter expression", "[A-B]{5,9}" }, { "Sample expression", "[0-3]([a-c]|[e-g]{1,2})" }, | ||
{ "Number format", "\\d{3,4}" }, | ||
// {"Any non-number","\\D{3,4}"}, | ||
{ "Any word", "\\w{1,2}" }, { "Empty string", "" }, | ||
// {"Any non-word","\\W{1,2}"} | ||
}); | ||
} | ||
|
||
public GenerexTest(String description, String patternValue) { | ||
this.pattern = patternValue; | ||
} | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
generex = new Generex(pattern); | ||
} | ||
|
||
@After | ||
public void tearDown() throws Exception { | ||
} | ||
|
||
@Test | ||
public void testGetMatchedFirstMatchShouldBeTheSameAsMatchWithZeroIndex() { | ||
String firstMatch = generex.getFirstMatch(); | ||
String matchStringZeroIndex = generex.getMatchedString(0); | ||
Assert.assertTrue(String.format("The generated string '%s' doesn't match the pattern '%s'", firstMatch, pattern), firstMatch.matches(pattern)); | ||
Assert.assertTrue(String.format("The generated string '%s' doesn't match the pattern '%s'", matchStringZeroIndex, pattern), | ||
matchStringZeroIndex.matches(pattern)); | ||
Assert.assertEquals(firstMatch, matchStringZeroIndex); | ||
} | ||
|
||
@Test | ||
public void testIterateThroughAllMatchesShouldReturnConsistentResults() { | ||
generex.getFirstMatch(); | ||
long total = generex.matchedStringsSize(); | ||
for (int count = 1; count < total; count++) { | ||
String matchStringZeroIndex = generex.getMatchedString(count); | ||
Assert.assertTrue(String.format("The generated string '%s' doesn't match the pattern '%s' at iteration #%d", matchStringZeroIndex, pattern, count), | ||
matchStringZeroIndex.matches(pattern)); | ||
} | ||
} | ||
} |