@Test public void testParseCommandCopy() { String cmd = "copy file1 file2"; ITool resultTool = shell.parse(cmd); assertNotNull(resultTool); assertEquals("CopyTool", resultTool.getClass().getSimpleName()); }
@Test public void testParseCommandDelete() { String cmd = "delete file1"; ITool resultTool = shell.parse(cmd); assertNotNull(resultTool); assertEquals("DeleteTool", resultTool.getClass().getSimpleName()); }
@Test public void testParseCommandCat() { String cmd = "cat path/to/file1"; ITool resultTool = shell.parse(cmd); assertNotNull(resultTool); assertEquals("CatTool", resultTool.getClass().getSimpleName()); }
@Test public void testParseCommandCd() { String cmd = "cd path/new"; ITool result = shell.parse(cmd); assertNotNull(result); assertEquals("CdTool", result.getClass().getSimpleName()); }
@Test public void testParseCommandWc() { String cmd = "wc testfile.txt"; ITool resultTool = shell.parse(cmd); assertNotNull(resultTool); assertEquals("WcTool", resultTool.getClass().getSimpleName()); }
@Test public void testParseCommandWithInconsistentSpace() { String cmd = " eChO sOme wOrd "; ITool resultTool = shell.parse(cmd); assertNotNull(resultTool); assertEquals("EchoTool", resultTool.getClass().getSimpleName()); }
@Test public void testParseCommandPiping() { String cmd = "cmd-1 | cmd-2"; ITool resultTool = shell.parse(cmd); assertNotNull(resultTool); assertEquals("PipingTool", resultTool.getClass().getSimpleName()); }
@Test public void testParseCommandMove() { String cmd = "move path1 path2"; ITool resultTool = shell.parse(cmd); assertNotNull(resultTool); assertEquals("MoveTool", resultTool.getClass().getSimpleName()); }
@Test public void testParseCommandLs() { String cmd = "ls"; ITool resultTool = shell.parse(cmd); assertNotNull(resultTool); assertEquals("LsTool", resultTool.getClass().getSimpleName()); }
@Test public void testParseCommandEcho() { String cmd = "echo some word"; ITool resultTool = shell.parse(cmd); assertNotNull(resultTool); assertEquals("EchoTool", resultTool.getClass().getSimpleName()); }
@Test public void testParseCommandWithEscapedPipe2() throws IllegalArgumentException, IllegalAccessException { String cmd = "cat testCase_3.txt | grep [a|b]"; ITool resultTool = shell.parse(cmd); String[] expected = {"cat testCase_3.txt", "grep [a", "b]"}; Field fields[] = resultTool.getClass().getSuperclass().getDeclaredFields(); for (Field field : fields) { if (field.getName().equals("args")) { field.setAccessible(true); List<String> args = Arrays.asList((String[]) field.get(resultTool)); for (int i = 0; i < expected.length; i++) { assertEquals(expected[i], args.get(i)); } } } }
@Test public void testParseCommandWithQuotes() throws IllegalArgumentException, IllegalAccessException { String cmd = "ls \"normal\" 'test's"; String[] expected = {"normal", "tests"}; ITool resultTool = shell.parse(cmd); assertNotNull(resultTool); Field fields[] = resultTool.getClass().getSuperclass().getDeclaredFields(); for (Field field : fields) { if (field.getName().equals("args")) { field.setAccessible(true); List<String> args = Arrays.asList((String[]) field.get(resultTool)); for (int i = 0; i < expected.length; i++) { assertEquals(expected[i], args.get(i)); } } } }
@Test public void testParseCommandWithMixedQuotesandEscapedSpaceandDuplicateMatch() throws IllegalArgumentException, IllegalAccessException { String cmd = "echo \"normal\" as\"as'\\ r\\$'w\\ e\"d's d's dd\\ sa\\$"; String[] expected = {"normal", "asas'\\ r$'w\\ eds ds", "dd sa$"}; ITool resultTool = shell.parse(cmd); assertNotNull(resultTool); Field fields[] = resultTool.getClass().getSuperclass().getDeclaredFields(); for (Field field : fields) { if (field.getName().equals("args")) { field.setAccessible(true); List<String> args = Arrays.asList((String[]) field.get(resultTool)); for (int i = 0; i < expected.length; i++) { assertEquals(expected[i], args.get(i)); } } } }