@Test public void testOrder() throws InvalidPatternException { List<String> result; result = newGlobExpander().expand("{a,b}"); assertEquals(result, Arrays.asList("a","b"), "Expansion was "+result); result = newGlobExpander().expand("{{a},b}"); assertEquals(result, Arrays.asList("a","b"), "Expansion was "+result); result = newGlobExpander().expand("{a,b}{1,2}"); assertEquals(result, Arrays.asList("a1","a2","b1","b2"), "Expansion was "+result); result = newGlobExpander().expand("{80{8{1,2},90},8000+}"); assertEquals(result, Arrays.asList("8081","8082","8090","8000+"), "Expansion was "+result); }
@Test public void testQuotesAndParen() throws InvalidPatternException { List<String> result; SpecialistGlobExpander special = new SpecialistGlobExpander(true, PhraseTreatment.INTERIOR_EXPANDABLE, PhraseTreatment.INTERIOR_NOT_EXPANDABLE); result = special.expand("\"{hello,goodbye}{1-2,({3-4}),(\")}\""); assertEquals(8, result.size()); assertTrue(result.contains("\"goodbye2\"")); assertTrue(result.contains("\"hello({3-4})\"")); assertTrue(result.contains("\"goodbye(\")\"")); }
@Test public void testQuotes() throws InvalidPatternException { List<String> result; SpecialistGlobExpander notSpecial = new SpecialistGlobExpander(true, PhraseTreatment.NOT_A_SPECIAL_CHAR, PhraseTreatment.NOT_A_SPECIAL_CHAR); result = notSpecial.expand("hello \"{1-3}\""); assertEquals(3, result.size()); assertEquals("hello \"1\"", result.get(0)); SpecialistGlobExpander expanding = new SpecialistGlobExpander(true, PhraseTreatment.INTERIOR_EXPANDABLE, PhraseTreatment.NOT_A_SPECIAL_CHAR); result = expanding.expand("hello \"{1-3}\""); assertEquals(3, result.size()); assertEquals("hello \"1\"", result.get(0)); result = expanding.expand("hello \"{1,2-3}\""); assertEquals(3, result.size()); assertEquals("hello \"2\"", result.get(1)); SpecialistGlobExpander notExpanding = new SpecialistGlobExpander(true, PhraseTreatment.INTERIOR_NOT_EXPANDABLE, PhraseTreatment.NOT_A_SPECIAL_CHAR); result = notExpanding.expand("hello \"{1,2-3}\""); assertEquals(1, result.size()); assertEquals("hello \"{1,2-3}\"", result.get(0)); result = notSpecial.expand("hello {\"1,2,3\"}"); assertEquals(3, result.size()); assertEquals("hello \"1", result.get(0)); result = expanding.expand("hello {\"1,2,3\"}"); assertEquals(1, result.size()); assertEquals("hello \"1,2,3\"", result.get(0)); result = notExpanding.expand("hello {\"1,2,3\"}");
@Test public void testParen() throws InvalidPatternException { List<String> result; SpecialistGlobExpander notSpecial = new SpecialistGlobExpander(true, PhraseTreatment.NOT_A_SPECIAL_CHAR, PhraseTreatment.NOT_A_SPECIAL_CHAR); result = notSpecial.expand("hello ({1-3})"); assertEquals(3, result.size()); assertEquals("hello (1)", result.get(0)); SpecialistGlobExpander expanding = new SpecialistGlobExpander(true, PhraseTreatment.INTERIOR_NOT_EXPANDABLE, PhraseTreatment.INTERIOR_EXPANDABLE); result = expanding.expand("hello ({1-3})"); assertEquals(3, result.size()); assertEquals("hello (1)", result.get(0)); result = expanding.expand("hello ({1,2-3})"); assertEquals(3, result.size()); assertEquals("hello (2)", result.get(1)); SpecialistGlobExpander notExpanding = new SpecialistGlobExpander(true, PhraseTreatment.INTERIOR_EXPANDABLE, PhraseTreatment.INTERIOR_NOT_EXPANDABLE); result = notExpanding.expand("hello ({1,2-3})"); assertEquals(1, result.size()); assertEquals("hello ({1,2-3})", result.get(0)); result = notSpecial.expand("hello {(1,2,3)}"); assertEquals(3, result.size()); assertEquals("hello (1", result.get(0)); result = expanding.expand("hello {(1,2,3)}"); assertEquals(1, result.size()); assertEquals("hello (1,2,3)", result.get(0)); result = notExpanding.expand("hello {(1,2,3)}");
private SpecialistGlobExpander newGlobExpander() { return new SpecialistGlobExpander(true, PhraseTreatment.NOT_A_SPECIAL_CHAR, PhraseTreatment.NOT_A_SPECIAL_CHAR); }
@Test public void testNumLeadZero() throws InvalidPatternException { assertTrue(newGlobExpander().isGlobMatchedNumeric("a{01-03,05}", "a01")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{ 01 - 03 , 05 }", "a01")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{ 01 - 03 , 05 }", "a02")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{ 01 - 03 , 05 }", "a03")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{ 01 - 03 , 05 }", "a 05 ")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{01-03,05}", "a05")); assertFalse(newGlobExpander().isGlobMatchedNumeric("a{01-03,05}", "a04")); assertFalse(newGlobExpander().isGlobMatchedNumeric("a{01-03,05}", "a3")); }
@Test public void testNum() throws InvalidPatternException { assertTrue(newGlobExpander().isGlobMatchedNumeric("{1-3}", "1")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{1-3}", "a1")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{1-3,5}", "a1")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{1-3,5}", "a3")); assertTrue(newGlobExpander().isGlobMatchedNumeric("a{1-3,5}", "a5")); assertFalse(newGlobExpander().isGlobMatchedNumeric("a{1-3,5}", "a4")); assertFalse(newGlobExpander().isGlobMatchedNumeric("a{1-3,5}", "a01")); }
/** expands glob, including custom syntax for numeric part, but to an array, and re-throwing the checked exception as a runtime exception */ public String[] expandToArrayUnchecked(String glob) { try { return expand(glob).toArray(new String[0]); } catch (InvalidPatternException e) { throw Throwables.propagate(e); } }
/** returns true iff the target matches the given pattern, * under simplified bash rules -- viz permitting * and ? and comma delimited patterns inside curly braces, * as well as things like {1,2,5-10} (and also {01,02,05-10} to keep leading 0) * @throws InvalidPatternException */ public boolean isGlobMatchedNumeric(String globPattern, String targetText) throws InvalidPatternException { List<String> patterns = expand(globPattern); for (String p : patterns) { if (isNoBraceGlobMatched(p, targetText)) return true; } return false; }