@@ -39,7 +39,6 @@ public class JSASTInstrumentor implements NodeVisitor{
3939 private int testCounter = 0 ;
4040 private int asyncTestCounter = 0 ;
4141 private int assertionCounter = 0 ;
42- private int funCallCounter = 0 ;
4342 private int newExpressionCounter = 0 ;
4443 private int triggerCounetr = 0 ;
4544
@@ -97,17 +96,35 @@ public ArrayList<Integer> getMissedFunctionLines() {
9796 public ArrayList <String > getFunctionCallsInTests () {
9897 ArrayList <String > funCalls = new ArrayList <String >();
9998 for (TestCaseInfo tc : testCaseInfoList ){
100- funCalls .addAll (tc .getFunctionCalls ());
99+ for (String fc : tc .getFunctionCalls ())
100+ // should also consider repeated fun calls so removed if(!funCalls.contains(fc))
101+ funCalls .add (fc );
101102 }
103+ // calc max, ave, all
104+
102105 return funCalls ;
103106 }
104107
108+
109+ public int getFunCallCounterInTest () {
110+ return getFunctionCallsInTests ().size ();
111+ }
112+
113+ public int getMaxFunctionCallsInTests () {
114+ int max = 0 ;
115+ for (TestCaseInfo tc : testCaseInfoList )
116+ if (tc .getFunctionCalls ().size () > max )
117+ max = tc .getFunctionCalls ().size ();
118+ return max ;
119+ }
120+
121+
105122 public ArrayList <String > getFunctionCalls () {
106123 return functionCalls ;
107124 }
108-
109-
110-
125+
126+
127+
111128 private int functionCounter = 0 ;
112129
113130 private String visitOnly = "" ;
@@ -620,10 +637,22 @@ private void analyzeTestCodeFunctionCallNode(AstNode node) {
620637 functionName = targetNode .toSource ().substring (targetNode .toSource ().lastIndexOf ("." )+1 );
621638
622639 if (testsFramework .equals ("qunit" )){
623- if (targetNode .toSource ().equals ("QUnit.test" ) || targetNode .toSource ().equals ("test" )){
640+ if (targetNode .toSource ().equals ("QUnit.test" ) || targetNode .toSource ().equals ("test" )){
624641 testCounter ++;
625642 // add a new TestCaseInfo object
626643 TestCaseInfo t = new TestCaseInfo (testCounter , "sync" );
644+
645+ // find begin and end line of code for the test anonym function
646+ for (AstNode arg : fcall .getArguments ()){
647+ if (arg instanceof FunctionNode ){
648+ FunctionNode f = (FunctionNode ) arg ;
649+ //System.out.println("f.getLineno(): " + (f.getLineno()+1));
650+ //System.out.println("f.getEndLineno(): " + (f.getEndLineno()+1));
651+ t .setBeginEndLines (f .getLineno ()+1 , f .getEndLineno ()+1 );
652+ //System.out.println(t.containsLine(f.getLineno()+1));
653+ //System.out.println(t.containsLine(f.getLineno()));
654+ }
655+ }
627656 testCaseInfoList .add (t );
628657 }
629658 if (targetNode .toSource ().equals ("QUnit.asyncTest" ) || targetNode .toSource ().equals ("asyncTest" )){
@@ -656,12 +685,11 @@ private void analyzeTestCodeFunctionCallNode(AstNode node) {
656685 tufi .setNumAssertions (tufi .getNumAssertions ()+1 );
657686 System .out .println ("Test utility function " + tufi .getFuncName () + " has " + tufi .getNumAssertions () + " assertions!" );
658687 //System.out.println("An assertion found out of a test case");
659-
660-
688+
689+
661690 // add to num of assertions
662-
663-
664-
691+
692+
665693 break ;
666694 }
667695 }
@@ -674,8 +702,17 @@ private void analyzeTestCodeFunctionCallNode(AstNode node) {
674702 return ;
675703 }else {
676704 System .out .println ("Counting the called function: " + functionName + " with enclosingFunction: " + enclosingFunction );
677- if (testCaseInfoList .size ()!=0 ){
678- TestCaseInfo t = testCaseInfoList .get (testCaseInfoList .size ()-1 );
705+ if (testCaseInfoList .size ()!=0 ){
706+ // retrieving the last testCaseInfo
707+ TestCaseInfo t = testCaseInfoList .get (testCaseInfoList .size ()-1 );
708+
709+ // check if funCall is inside test function body
710+ if (testsFramework .equals ("qunit" )){
711+ // find begin and end line of code for the test anonym function
712+ if (!t .containsLine (fcall .getLineno ()+1 ))
713+ return ;
714+
715+
679716 int currentNumFunCalls = t .getNumFunCall ();
680717 // search for a test utility function with the same name as the functionName
681718 boolean testUtilFunCall = false ;
@@ -697,23 +734,25 @@ private void analyzeTestCodeFunctionCallNode(AstNode node) {
697734 else {
698735 t .setNumFunCall (currentNumFunCalls ); // do not add the call to the test utility function
699736 }
700-
737+
701738 System .out .println ("Test case " + t .getTestNumber () + " has " + t .getNumFunCall () + " function calls!" );
702- funCallCounter ++;
703- }else {
704- // search for a test utility function with the same name as the enclosingFunction
705- for (TestUtilityFunctionInfo tufi : testUtilityFunctionInfoList ){
706- if (tufi .getFuncName ().equals (enclosingFunction )){
707- tufi .setNumFunCall (tufi .getNumFunCall ()+1 );
708- System .out .println ("Test utility function " + tufi .getFuncName () + " has " + tufi .getNumFunCall () + " function calls!" );
709- //System.out.println("A function call found out of a test case");
710- tufi .addFunctionCall (functionName );
711- System .out .println ("Adding to the list of function calls for the test utility function: " + tufi .getFunctionCalls ());
712- break ;
713- }
739+
740+ }
741+
742+ }else {
743+ // search for a test utility function with the same name as the enclosingFunction
744+ for (TestUtilityFunctionInfo tufi : testUtilityFunctionInfoList ){
745+ if (tufi .getFuncName ().equals (enclosingFunction )){
746+ tufi .setNumFunCall (tufi .getNumFunCall ()+1 );
747+ System .out .println ("Test utility function " + tufi .getFuncName () + " has " + tufi .getNumFunCall () + " function calls!" );
748+ //System.out.println("A function call found out of a test case");
749+ tufi .addFunctionCall (functionName );
750+ System .out .println ("Adding to the list of function calls for the test utility function: " + tufi .getFunctionCalls ());
751+ break ;
714752 }
715- //TestUtilityFunctionInfo tufi = testUtilityFunctionInfoList.get(testUtilityFunctionInfoList.size()-1);
716753 }
754+ //TestUtilityFunctionInfo tufi = testUtilityFunctionInfoList.get(testUtilityFunctionInfoList.size()-1);
755+ }
717756 }
718757
719758 }
@@ -1113,15 +1152,5 @@ public ArrayList<TestUtilityFunctionInfo> getTestUtilityFunctions() {
11131152 return testUtilityFunctionInfoList ;
11141153 }
11151154
1116-
1117-
1118-
1119- public void setFunCallCounter (int funCallCounter ) {
1120- this .funCallCounter = funCallCounter ;
1121- }
1122- public int getFunCallCounter () {
1123- return funCallCounter ;
1124- }
1125-
11261155}
11271156
0 commit comments