CommandsParser { public List<ParsingError> getParsingErrors() { List<ParsingError> result = new ArrayList<>( this.parsingErrors ); if( this.context.getCommandFile() != null && ! this.context.getCommandFile().exists()) result.add( 0, new ParsingError( ErrorCode.CMD_INEXISTING_COMMAND_FILE, this.context.getCommandFile(), 1 )); else if( this.instructions.isEmpty()) result.add( 0, new ParsingError( ErrorCode.CMD_NO_INSTRUCTION, this.context.getCommandFile(), 1 )); return result; } CommandsParser( AbstractApplication app, File commandsFile );  CommandsParser( AbstractApplication app, String instructionsText ); List<ParsingError> getParsingErrors(); List<AbstractCommandInstruction> getInstructions(); static String injectContextVariables( String line, Map<String,String> context );  }
@Test public void testValidate_1() throws Exception { File f = TestUtils.findTestFile( "/commands/single-line-commands.txt" ); CommandsParser parser = new CommandsParser( this.app, f ); Assert.assertEquals( 0, parser.getParsingErrors().size()); Assert.assertEquals( 3, parser.instructions.size()); }
@Test public void testValidate_2() throws Exception { File f = TestUtils.findTestFile( "/commands/multi-line-commands.txt" ); CommandsParser parser = new CommandsParser( this.app, f ); Assert.assertEquals( 0, parser.getParsingErrors().size()); Assert.assertEquals( 3, parser.instructions.size()); }
@Test public void testValidate_3() throws Exception { File f = TestUtils.findTestFile( "/commands/commands-in-error.txt" ); CommandsParser parser = new CommandsParser( this.app, f ); Assert.assertEquals( 3, parser.instructions.size()); List<ParsingError> errors = parser.getParsingErrors(); Assert.assertEquals( 4, errors.size()); Assert.assertEquals( ErrorCode.CMD_UNRECOGNIZED_INSTRUCTION, errors.get( 0 ).getErrorCode()); Assert.assertEquals( 2, errors.get( 0 ).getLine()); Assert.assertEquals( ErrorCode.CMD_UNRESOLVED_VARIABLE, errors.get( 1 ).getErrorCode()); Assert.assertEquals( 3, errors.get( 1 ).getLine()); Assert.assertEquals( ErrorCode.CMD_UNRESOLVED_VARIABLE, errors.get( 1 ).getErrorCode()); Assert.assertEquals( 4, errors.get( 2 ).getLine()); Assert.assertEquals( ErrorCode.CMD_UNRESOLVED_VARIABLE, errors.get( 2 ).getErrorCode()); Assert.assertEquals( 7, errors.get( 3 ).getLine()); }
@Test public void testInexistingFile() { CommandsParser parser = new CommandsParser( this.app, new File( "inexisting" )); List<ParsingError> errors = parser.getParsingErrors(); Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.CMD_INEXISTING_COMMAND_FILE, errors.get( 0 ).getErrorCode()); }
@Test public void testEmptyFile() throws Exception { CommandsParser parser = new CommandsParser( this.app, this.folder.newFile()); List<ParsingError> errors = parser.getParsingErrors(); Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.CMD_NO_INSTRUCTION, errors.get( 0 ).getErrorCode()); }
@Test public void testAllCommands() throws Exception { this.app.setDirectory( this.folder.newFolder()); File cmdDir = new File( this.app.getDirectory(), Constants.PROJECT_DIR_COMMANDS ); Assert.assertTrue( cmdDir.mkdirs()); File f = TestUtils.findTestFile( "/commands/single-line-commands.txt" ); Utils.copyStream( f, new File( cmdDir, "single-line-commands" + Constants.FILE_EXT_COMMANDS )); f = TestUtils.findTestFile( "/commands/all-commands.txt" ); CommandsParser parser = new CommandsParser( this.app, f ); List<ParsingError> errors = parser.getParsingErrors(); Assert.assertEquals( 0, errors.size()); }
@Test public void testConstructorWithNullString() throws Exception { CommandsParser parser = new CommandsParser( this.app, (String) null ); List<ParsingError> errors = parser.getParsingErrors(); Assert.assertEquals( 1, errors.size()); Assert.assertEquals( ErrorCode.CMD_NO_INSTRUCTION, errors.get( 0 ).getErrorCode()); }