@@ -283,7 +283,12 @@ else if (node instanceof Assignment)
283283 analyzeProductionCodeAssignmentNode (node );
284284 }
285285 }else if (visitType .equals ("AnalyzeTestCode" )){
286-
286+ if (node instanceof NewExpression )
287+ newExpressionCounter ++;
288+ else if (node instanceof FunctionNode )
289+ analyzeTestCodeFunctionNode (node );
290+ else if (node instanceof FunctionCall )
291+ analyzeTestCodeFunctionCallNode (node );
287292
288293 }else {
289294 System .out .println ("visitType is not set!" );
@@ -551,7 +556,63 @@ private void instrumentFunctionNode(AstNode node) {
551556
552557
553558 private void analyzeTestCodeFunctionNode (AstNode node ) {
554- System .out .println ("=== instrumentFunctionCallNode ===" );
559+ System .out .println ("=== analyzeTestCodeFunctionNode ===" );
560+ // getting the enclosing function name
561+ String enclosingFunction = "" ;
562+ if (node .getEnclosingFunction ()!=null ){
563+ enclosingFunction = getFunctionName (node .getEnclosingFunction ());
564+ }
565+ System .out .println ("enclosingFunction: " + enclosingFunction );
566+
567+ if (node .shortName ().equals ("NewExpression" ))
568+ return ;
569+
570+ FunctionCall fcall = (FunctionCall ) node ;
571+ AstNode targetNode = fcall .getTarget (); // node evaluating to the function to call. E.g document.getElemenyById(x)
572+
573+ // avoid instrumenting wrapper function calls!
574+ if (fcall .getParent ().toSource ().contains ("funcionCallWrapper" )){
575+ System .out .println ("Not instrumenting " + fcall .getTarget ().toSource () + ", because of: " + fcall .getParent ().toSource ());
576+ return ;
577+ }
578+
579+ String functionName = targetNode .toSource ().substring (targetNode .toSource ().lastIndexOf ("." )+1 );
580+
581+ if (targetNode .toSource ().equals ("QUnit.module" ) || targetNode .toSource ().equals ("module" ))
582+ currentTest = "TestModule" ;
583+ if (targetNode .toSource ().equals ("QUnit.test" ) || targetNode .toSource ().equals ("test" )){
584+ currentTestNumber ++;
585+ currentTest = "Test" + Integer .toString (currentTestNumber );
586+ setTestCounter (getTestCounter () + 1 );
587+ }
588+ if (targetNode .toSource ().equals ("QUnit.asyncTest()" ) || targetNode .toSource ().equals ("asyncTest()" )){
589+ currentTestNumber ++;
590+ currentTest = "AsynchTest" + Integer .toString (currentTestNumber );
591+ setAsynchTestCounter (getAsynchTestCounter () + 1 );
592+ }
593+
594+ if (targetNode .toSource ().equals ("trigger" ) || targetNode .toSource ().equals ("triggerHandler" ))
595+ setTriggerCounetr (getTriggerCounetr () + 1 );
596+
597+ String [] assertionSkipList = { "assert.expect" , "expect" , "assert.equal" , "equal" , "assert.notEqual" , "notEqual" , "assert.deepEqual" , "deepEqual" ,
598+ "assert.notDeepEqual" , "notDeepEqual" , "assert.strictEqual" , "strictEqual" , "assert.notStrictEqual" , "notStrictEqual" , "QUnit.ok" , "assert.ok" , "ok" , "assert.notOk" , "notOk" ,
599+ "assert.propEqual" , "propEqual" , "assert.notPropEqual" , "notPropEqual" , "assert.push" , "assert.throws" , "throws" , "assert.async" };
600+
601+ String [] otherSkipList = { "QUnit.module" , "module" , "QUnit.test" , "test" , "QUnit.asyncTest" , "asyncTest" , "jQuery" , "$" , "start" , "stop" }; // start/stop for asynchronous control
602+
603+ if (ArrayUtils .contains ( assertionSkipList , targetNode .toSource () ))
604+ setAssertionCounter (getAssertionCounter () + 1 );
605+
606+ if (ArrayUtils .contains ( assertionSkipList , targetNode .toSource () ) || ArrayUtils .contains ( otherSkipList , targetNode .toSource () )) {
607+ System .out .println ("Not instrumenting " + targetNode .toSource ());
608+ return ;
609+ }else
610+ System .out .println ("Instrumenting " + functionName );
611+ }
612+
613+
614+ private void analyzeTestCodeFunctionCallNode (AstNode node ) {
615+ System .out .println ("=== analyzeTestCodeFunctionCallNode ===" );
555616 // getting the enclosing function name
556617 String enclosingFunction = "" ;
557618 if (node .getEnclosingFunction ()!=null ){
@@ -603,6 +664,22 @@ private void analyzeTestCodeFunctionNode(AstNode node) {
603664 return ;
604665 }else
605666 System .out .println ("Instrumenting " + functionName );
667+
668+ // functionName, enclosingFunction, function
669+ List <AstNode > args = new ArrayList <AstNode >(fcall .getArguments ());
670+ String wrapperCode = scopeName .replace (".js" ,"" ).replace ("-" , "_" ) + "_funcionCallWrapper(\" " + functionName +"\" , \" " + scopeName .replace (".js" ,"" ) + "_" + currentTest + "\" , " + fcall .toSource () + ")" ;
671+ System .out .println ("wrapperCode : " + wrapperCode );
672+
673+ AstNode wrapperNode = parse (wrapperCode );
674+ ExpressionStatement es = (ExpressionStatement )((AstNode ) wrapperNode .getFirstChild ());
675+ FunctionCall wrapperFunCall = (FunctionCall ) es .getExpression ();
676+ args .add (wrapperFunCall .getArguments ().get (0 ));
677+ //wrapperFunCall.addArgument(fcall);
678+ System .out .println ("Replacing functionCall: " + fcall .toSource () + " with wrapperFunCall: " + wrapperFunCall .toSource ());
679+ fcall .setTarget (wrapperFunCall .getTarget ());
680+ fcall .setArguments (wrapperFunCall .getArguments ());
681+ System .out .println ("New functionCall: " + fcall .toSource ());
682+
606683 }
607684
608685
0 commit comments