Skip to content

Commit 14db8f5

Browse files
committed
debugging and refactoring
1 parent dc2b38f commit 14db8f5

File tree

4 files changed

+108
-51
lines changed

4 files changed

+108
-51
lines changed

src/main/java/TestCodePropertyAnalyzer.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class TestCodePropertyAnalyzer {
4242
private static int NumFunCall = 0;
4343
private static int MaxFunCall = 0;
4444
private static float AveFunCall = 0;
45-
private static int NumTriggerTest;
45+
private static int NumTriggerInTest;
4646
private static int NumObjCreate;
4747

4848
public static void main(String[] args) throws Exception {
@@ -116,21 +116,22 @@ private static void analyseJSTestFile(String canonicalPath) throws Exception {
116116
NumTests += codeAnalyzer.getNumTests();
117117
NumAsyncTests += codeAnalyzer.getNumAsyncTests();
118118
NumAssertions += codeAnalyzer.getNumAssertions();
119-
NumFunCall += codeAnalyzer.getNumFunCall();
120-
if (codeAnalyzer.getNumFunCall() > MaxFunCall)
121-
MaxFunCall = codeAnalyzer.getNumFunCall();
122-
AveFunCall = (float)NumFunCall/(float)NumTests;
123-
NumTriggerTest += codeAnalyzer.getNumTriggerTest();
119+
NumFunCall += codeAnalyzer.getNumFunCallTest();
120+
if (codeAnalyzer.getMaxFunCallTest() > MaxFunCall)
121+
MaxFunCall = codeAnalyzer.getMaxFunCallTest();
122+
AveFunCall = (float)NumFunCall/(float)NumTests;
123+
NumTriggerInTest += codeAnalyzer.getNumTriggerTest();
124124
NumObjCreate += codeAnalyzer.getNumObjCreate();
125125

126126

127127
System.out.println("==========================");
128128
System.out.println("++++ NumTests: " + NumTests);
129129
System.out.println("++++ NumAsyncTests: " + NumAsyncTests);
130130
System.out.println("++++ NumAssertions: " + NumAssertions);
131+
System.out.println("++++ NumFunCall: " + NumFunCall);
131132
System.out.println("++++ MaxFunCall: " + MaxFunCall);
132133
System.out.println("++++ AveFunCall: " + AveFunCall);
133-
System.out.println("++++ NumTriggerTest: " + NumTriggerTest);
134+
System.out.println("++++ NumTriggerInTest: " + NumTriggerInTest);
134135
System.out.println("++++ NumObjCreate: " + NumObjCreate);
135136
System.out.println("==========================");
136137

src/main/java/core/JSAnalyzer.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class JSAnalyzer {
2727
private static int NumAsyncTests = 0;
2828
private static int NumAssertions = 0;
2929
private static int NumFunCall = 0;
30+
private static int NumFunCallTest = 0; // store number of function calls in a test code
31+
private static int MaxNumFunCallTest = 0; // store max number of function calls in a test case
3032
private static int NumTriggerTest = 0;
3133
private static int NumObjCreate = 0;
3234

@@ -475,16 +477,18 @@ public void analyzeTestCodeProperties() throws Exception {
475477
NumTests = astVisitor.getTestCounter();
476478
NumAsyncTests = astVisitor.getAsynchTestCounter();
477479
NumAssertions = astVisitor.getAssertionCounter();
478-
NumFunCall = astVisitor.getFunCallCounter();
480+
NumFunCallTest = astVisitor.getFunCallCounterInTest();
481+
MaxNumFunCallTest = astVisitor.getMaxFunctionCallsInTests();
479482
NumTriggerTest = astVisitor.getTriggerCounetr();
480483
NumObjCreate = astVisitor.getNewExpressionCounter();
481484

482-
System.out.println("testCounter: " + astVisitor.getTestCounter());
483-
System.out.println("asynchTestCounter: " + astVisitor.getAsynchTestCounter());
484-
System.out.println("assertionCounter: " + astVisitor.getAssertionCounter());
485-
System.out.println("funCallCounter: " + astVisitor.getFunCallCounter());
486-
System.out.println("newExpressionCounter: " + astVisitor.getNewExpressionCounter());
487-
System.out.println("trieggerCounter: " + astVisitor.getTriggerCounetr());
485+
System.out.println("NumTests: " + NumTests);
486+
System.out.println("NumAsyncTests: " + NumAsyncTests);
487+
System.out.println("NumAssertions: " + NumAssertions);
488+
System.out.println("NumFunCallTest: " + NumFunCallTest);
489+
System.out.println("MaxNumFunCallTest: " + MaxNumFunCallTest);
490+
System.out.println("NumTriggerTest: " + NumTriggerTest);
491+
System.out.println("NumObjCreate: " + NumObjCreate);
488492

489493
/* clean up */
490494
Context.exit();
@@ -513,6 +517,14 @@ public int getNumAssertions() {
513517
public int getNumFunCall() {
514518
return NumFunCall;
515519
}
520+
521+
public int getNumFunCallTest() {
522+
return NumFunCallTest;
523+
}
524+
525+
public int getMaxFunCallTest() {
526+
return MaxNumFunCallTest;
527+
}
516528

517529
public int getNumTriggerTest() {
518530
return NumTriggerTest;

src/main/java/instrumentor/JSASTInstrumentor.java

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/main/java/instrumentor/TestCaseInfo.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public class TestCaseInfo {
99
private int numAssertions = 0;
1010
private int numObjCreation = 0;
1111
private int numTriggers = 0;
12+
private int beginLineNum = 0;
13+
private int endLineNum = 0;
1214

1315
public TestCaseInfo(int testNumber, String type){
1416
this.testNumber = testNumber;
@@ -27,6 +29,19 @@ public void addFunctionCall(String fc) {
2729
}
2830

2931

32+
public void setBeginEndLines(int begin, int end){
33+
this.beginLineNum = begin;
34+
this.endLineNum = end;
35+
}
36+
37+
public boolean containsLine(int lineNum){
38+
if (lineNum >= beginLineNum && lineNum <= endLineNum)
39+
return true;
40+
System.out.println("Line " + lineNum + " is out of test function [" + beginLineNum + "-" + endLineNum + "]");
41+
return false;
42+
}
43+
44+
3045
public int getTestNumber() {
3146
return testNumber;
3247
}

0 commit comments

Comments
 (0)