comtest
Class ComTest.ComTestScanner.CommentCodeHandler

java.lang.Object
  extended by comtest.ComTest.ComTestScanner.CommentCodeHandler
Enclosing class:
ComTest.ComTestScanner

public class ComTest.ComTestScanner.CommentCodeHandler
extends java.lang.Object

Handles to ComTest-comment and returns the position of last handled line. The main method is readCode and all others are just helper methods to handle the lines. Precondition: inside JavaDoc comment Postcondition: still inside JavaDoc comment


Field Summary
private  java.lang.String baseIndent
           
private  Strings endOfLine
           
private  boolean everyLineInits
           
private  java.lang.String indent
           
private  Strings initCmds
           
private  int initLine
           
private static java.lang.String javaShortComment
           
private  int lineNr
           
private  Strings methodFooter
           
private  Strings methodHeader
           
private  boolean methodHeaderPrinted
           
private  int methodIndex
           
private  java.lang.String methodName
           
private  java.lang.String methodThrows
           
private  boolean oneTablePrinted
           
private  boolean rawMode
           
private  java.lang.String sMethodIndex
           
private  Strings startOfLine
           
private  boolean tablePrinted
           
private  Strings template
           
private  Strings values
           
private  Strings vars
           
 
Constructor Summary
ComTest.ComTestScanner.CommentCodeHandler()
           
 
Method Summary
private  void addInit(Strings commands, int realLineNr)
          Add sentences to init section.
private  java.lang.String checkMethodThrows(java.lang.String s)
          Check if line is THROWS line.
private  void checkSemiColon(Strings sentences, int i, Strings commands)
          Check if there is coming a ; then nothing is done.
private  java.lang.String checkThrows(java.lang.String s)
          Check if line is THROWS line.
private  void createHeaderAndFooter(java.lang.String s)
          Creates a header and footer for new ComTest test method.
private  void deleteLastNewLine(Strings cmds)
          Deletes \n from the last line of cmds
 Strings getInitCmnds()
          This is just for testing purposes to return the initCmds
 java.lang.String handleAnnotationInComments(java.lang.String s)
          Annotations in comments must be prefixed with something.
 void handleSentences(java.lang.String line)
          Handles normal lines and table lines from ComTest lines.
private  void handleTableLine(java.lang.String line)
          Handles lines like " $s | $what | $result" or " "aab" | "a" | "ab" " If there is a $ on the line, it is kept as a header line.
private  java.lang.String isPseudoComment(java.lang.String line)
          Check if line is pseudo comment /.* or *
 boolean isTableLine(java.lang.String s)
          Check if the line is a table line, so containing either " | " or | at the beginning or end.
private  boolean jUnitAnnotate(java.lang.String s)
          Check if s is one of the JUnit annotation.
private  void outFooter(int realLineNr)
          Copies method footer to outlines if method header is already moved.
private  void outLines(Strings commands, int realLineNr)
          Moves commands to outlines.
 void outNewHeader()
          Moves header lines to outlines.
private  void outWithHeader(Strings list, java.lang.String target, java.lang.String replacement)
          Moves list to outlines.
 int readCode(int start)
          Reads the code from contents starting from line start and returns the line last handled.
private  java.lang.String saveStartAndEndOfLine(java.lang.String s)
          Find comments and put possible try catch to start and end of the the current line's start and end lists.
 java.lang.String tryToFindMethodName(Strings contents, int lineNr)
          Try to find name for next method.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

javaShortComment

private static final java.lang.String javaShortComment
See Also:
Constant Field Values

lineNr

private int lineNr

methodName

private java.lang.String methodName

template

private final Strings template

initCmds

private final Strings initCmds

methodHeader

private final Strings methodHeader

methodFooter

private final Strings methodFooter

methodHeaderPrinted

private boolean methodHeaderPrinted

methodIndex

private int methodIndex

sMethodIndex

private java.lang.String sMethodIndex

vars

private Strings vars

values

private Strings values

initLine

private int initLine

everyLineInits

private boolean everyLineInits

tablePrinted

private boolean tablePrinted

oneTablePrinted

private boolean oneTablePrinted

rawMode

private boolean rawMode

baseIndent

private java.lang.String baseIndent

indent

private java.lang.String indent

startOfLine

private final Strings startOfLine

endOfLine

private final Strings endOfLine

methodThrows

private java.lang.String methodThrows
Constructor Detail

ComTest.ComTestScanner.CommentCodeHandler

public ComTest.ComTestScanner.CommentCodeHandler()
Method Detail

getInitCmnds

public Strings getInitCmnds()
This is just for testing purposes to return the initCmds

Returns:
initCmnds

tryToFindMethodName

public java.lang.String tryToFindMethodName(Strings contents,
                                            int lineNr)
Try to find name for next method. If no name is found, then a sequence number is used. This is very unfinished and works only for nice code where the method name and type is on the same line.

Parameters:
contents - where to find the name
lineNr - where to start
Returns:
test method name

checkThrows

private java.lang.String checkThrows(java.lang.String s)
Check if line is THROWS line. If it is, correct the start and end lists as needed.

Parameters:
s - line to look.
Returns:
line without THROWS sentence

checkMethodThrows

private java.lang.String checkMethodThrows(java.lang.String s)
Check if line is THROWS line. If it is, correct the start and end lists as needed.

Parameters:
s - line to look.
Returns:
line without THROWS sentence

saveStartAndEndOfLine

private java.lang.String saveStartAndEndOfLine(java.lang.String s)
Find comments and put possible try catch to start and end of the the current line's start and end lists.

Parameters:
s - line to handle
Returns:
the line where comments and THROWS-sentence is removed.

outFooter

private void outFooter(int realLineNr)
Copies method footer to outlines if method header is already moved. Nothing is done if method header is not copied.

Parameters:
realLineNr - current line number

outNewHeader

public void outNewHeader()
Moves header lines to outlines. Possible method index markers are replaced by method index.


outWithHeader

private void outWithHeader(Strings list,
                           java.lang.String target,
                           java.lang.String replacement)
Moves list to outlines. If header is not moved, then move it now. Possible target strings are replaced by replacement strings while moving.

Parameters:
list - what to move
target - what is replaced
replacement - by what to replace

addInit

private void addInit(Strings commands,
                     int realLineNr)
Add sentences to init section. Possible line number markers are replaced by current line number.

Parameters:
commands - sentences to add
realLineNr - real line number for current line

outLines

private void outLines(Strings commands,
                      int realLineNr)
Moves commands to outlines. If header is not moved, then move it now. Possible line number markers are replaced by current line number.

Parameters:
commands - what to move
realLineNr - real line number for current line

jUnitAnnotate

private boolean jUnitAnnotate(java.lang.String s)
Check if s is one of the JUnit annotation.

Parameters:
s - line to look
Returns:
true if annotation, false otherwise.

handleAnnotationInComments

public java.lang.String handleAnnotationInComments(java.lang.String s)
Annotations in comments must be prefixed with something. This takes the prefixing away.

Parameters:
s - where to look annotation
Returns:
s if no annotation, otherwise annotation with prefix info removed.
Example:
   TestHelpper t = new TestHelpper();
   t.codeHandler.handleAnnotationInComments("a") === "a";    
   t.codeHandler.handleAnnotationInComments("* #@Test") === "@Test";    
   t.codeHandler.handleAnnotationInComments("* #@example") === "* #@example";    
 
 

deleteLastNewLine

private void deleteLastNewLine(Strings cmds)
Deletes \n from the last line of cmds

Parameters:
cmds - where to delete

isTableLine

public boolean isTableLine(java.lang.String s)
Check if the line is a table line, so containing either " | " or | at the beginning or end.

Parameters:
s - line to check
Returns:
true if was a table line.
Example:
   TestHelpper t = new TestHelpper();
   t.codeHandler.isTableLine("") === false;    
   t.codeHandler.isTableLine("a") === false;    
   t.codeHandler.isTableLine("a|") === true;    
   t.codeHandler.isTableLine("|a") === true;    
   t.codeHandler.isTableLine(" a|b ") === false;    
   t.codeHandler.isTableLine(" a | b ") === true;    
 

handleTableLine

private void handleTableLine(java.lang.String line)
Handles lines like " $s | $what | $result" or " "aab" | "a" | "ab" " If there is a $ on the line, it is kept as a header line. Otherwise as a normal values line. From header line the variable names a collected. In normal line the variable values a replaced in the corresponding template line. Sentences with unused variable names are removed.

Parameters:
line - line to handle.

checkSemiColon

private void checkSemiColon(Strings sentences,
                            int i,
                            Strings commands)
Check if there is coming a ; then nothing is done. If there is no ; coming, then one ; is added to commands.

Parameters:
sentences - where to look ;
i - from what index to look ;
commands - where to add ; if needed

isPseudoComment

private java.lang.String isPseudoComment(java.lang.String line)
Check if line is pseudo comment /.* or *./

Returns:
comments handled or s

handleSentences

public void handleSentences(java.lang.String line)
Handles normal lines and table lines from ComTest lines. The line is first divided to "sentences" so that ; { } are each one "sentence". Then ===, ~~ is try to find and in case of success the corresponding assert is generated. If the line has at least on $ sign, all sentences are kept as a template sentences. Also after starting the template, all lines are kept as template lines. If generated assert-line does not include ; , then it is added.

Parameters:
line - line to handle.
Example:
   TestHelpper t = new TestHelpper();
     
   t.codeHandler.handleSentences("int a = 5;");
   t.initCmnds.get(1) === "int a = 5";
   
   t.initCmnds.clear();
   t.codeHandler.handleSentences("int a = 5; int b = 7;");
   t.initCmnds.get(0) === "    ";
   t.initCmnds.get(1) === "int a = 5";
   t.initCmnds.get(2) === "; ";
   t.initCmnds.get(3) === "int b = 7";
   t.initCmnds.get(4) === "; ";
   
   t.initCmnds.clear();
   t.codeHandler.handleSentences("a === 5;");
   t.initCmnds.get(1) === "assertEquals(\"From: test line: 1\", 5, a)";

   t.initCmnds.clear();
   t.codeHandler.handleSentences("get(request,new String[]{\"str\",\"id\"},13,true) === 123;");
   t.initCmnds.get(1) === "assertEquals(\"From: test line: 1\", 123, get(request,new String[]{\"str\",\"id\"},13,true))";
   
   t.initCmnds.clear();
   t.codeHandler.handleSentences("\"cat\" =R= \"c.*t\";"); // "cat" =R= "c.*t";
   t.initCmnds.get(1) === "{ String l=\"cat\",r=\"c.*t\"; if ( !l.matches(r) ) fail(\"From: test line: 1\" + \" does not match: [\"+ l + \"] != [\" + r + \"]\");}";
   
   t.initCmnds.clear();
   t.codeHandler.handleSentences("a ~~~ 6.0;");  
   t.initCmnds.get(1) === "assertEquals(\"From: test line: 1\", 6.0, a, 0.000001)";
   
   t.initCmnds.clear();
   t.codeHandler.handleSentences("\"a$1\""); /// Not a template line  
   t.initCmnds.get(1) === "\"a$1\"";
   
   t.template.size() === 0;
 
,
   String s="$a"; s === "$b"; $
     $a | $b
    ---------
      1 | 1    
   
  $a; "a".matches("^a$") === false;  // does not generate code
 
,
   String s=null; s === null; 
   "a".matches("^a$") === true;  // generate code
 
,
 String s="$a"; s === "$b"; $; 
     $a | $b
    ---------
      $ | $    
   "a".matches("^a$") === true;  // this generates
 

createHeaderAndFooter

private void createHeaderAndFooter(java.lang.String s)
Creates a header and footer for new ComTest test method. If the name is for raw-code, then no method header is done. If there is no name for the test method, then next method is searched and that method name is used as a test method name.

Parameters:
s - line to start the test.

readCode

public int readCode(int start)
Reads the code from contents starting from line start and returns the line last handled. The ComTest block ends to next @-line or to end of pre-tag.

Parameters:
start - first line index to handle
Returns:
last line index handled