/** 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); } }
@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 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); }
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)); 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)); 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\"}"); assertEquals(1, result.size()); assertEquals("hello \"1,2,3\"", result.get(0));
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)); 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)); 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)}"); assertEquals(1, result.size()); assertEquals("hello (1,2,3)", result.get(0));
/** 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; }