diff --git a/.gitignore b/.gitignore index 7b65c95..91c1a8d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,17 @@ +###################### +# Maven +###################### /target + +###################### +# Intellij +###################### .idea/ *.iml + +###################### +# Eclipse +###################### +.project +.settings +.classpath diff --git a/src/main/java/com/mifmif/common/regex/Generex.java b/src/main/java/com/mifmif/common/regex/Generex.java index f9dbd8d..3d9a6f8 100644 --- a/src/main/java/com/mifmif/common/regex/Generex.java +++ b/src/main/java/com/mifmif/common/regex/Generex.java @@ -425,4 +425,20 @@ private static String requote(String regex) { return sb.toString(); } + /** + * Get the length of the longest string that can be generated. + * @return + */ + public int generateMaxLength() { + return automaton.getNumberOfStates() - 1; + } + + /** + * Get the length of the smallest string that can be generated. + * @return + */ + public int generateMinLength() { + return automaton.getShortestExample(true).length(); + } + } diff --git a/src/test/java/com/mifmif/common/regex/GenerexTest.java b/src/test/java/com/mifmif/common/regex/GenerexTest.java index 34c3777..cc3d69b 100644 --- a/src/test/java/com/mifmif/common/regex/GenerexTest.java +++ b/src/test/java/com/mifmif/common/regex/GenerexTest.java @@ -20,21 +20,28 @@ public class GenerexTest { private String pattern; private Generex generex; - private int expectedMatchedStringsSize; + private int expectedMatchedStringsSize; + private int expectedStringsMinLength; + private int expectedStringsMaxLength; @Parameters(name = "Test get match: {0}") public static Collection data() { - return Arrays.asList(new Object[][] { { "Sample multicharacter expression", "[A-B]{5,9}", 992 }, { "Sample expression", "[0-3]([a-c]|[e-g]{1,2})", 60 }, - { "Number format", "\\d{3,4}", 11000 }, - // {"Any non-number","\\D{3,4}"}, - { "Any word", "\\w{1,2}", 4032 }, { "Empty string", "", 1 }, - // {"Any non-word","\\W{1,2}"} - }); + return Arrays.asList(new Object[][] { // + {"Sample multicharacter expression", "[A-B]{5,9}", 992, 5, 9}, // + {"Sample expression", "[0-3]([a-c]|[e-g]{1,2})", 60, 2, 3}, // + {"Number format", "\\d{3,4}", 11000, 3, 4}, // + // {"Any non-number","\\D{3,4}", ???, 3, 4}, // + {"Any word", "\\w{1,2}", 4032, 1, 2}, // + {"Empty string", "", 1, 0, 0}, // + // {"Any non-word","\\W{1,2}", ???, 1, 2} // + }); } - public GenerexTest(String description, String patternValue, int numberOfStrings) { + public GenerexTest(String description, String patternValue, int numberOfStrings, int minLength, int maxLength) { this.pattern = patternValue; - this.expectedMatchedStringsSize = numberOfStrings; + this.expectedMatchedStringsSize = numberOfStrings; + this.expectedStringsMinLength = minLength; + this.expectedStringsMaxLength = maxLength; } @Before @@ -54,6 +61,22 @@ public void testMatchedStringsSizeShouldReturnExpectedValues() { expectedMatchedStringsSize == size); } + @Test + public void testMinLengthMatchedStringsShouldReturnExpectedValues() { + long size = generex.generateMinLength(); + Assert.assertTrue( + String.format("The minimum lengh matched strings '%s' doesn't match the value '%s'", size, expectedStringsMinLength), + expectedStringsMinLength == size); + } + + @Test + public void testMaxLengthMatchedStringsShouldReturnExpectedValues() { + long size = generex.generateMaxLength(); + Assert.assertTrue( + String.format("The maximum lengh matched strings '%s' doesn't match the value '%s'", size, expectedStringsMaxLength), + expectedStringsMaxLength == size); + } + @Test public void testGetMatchedFirstMatchShouldBeTheSameAsMatchWithZeroIndex() { String firstMatch = generex.getFirstMatch();