2013-01-03 23:58:07 +00:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright ( c ) 2008 IBM Corporation .
* All rights reserved . This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1 . 0
* which accompanies this distribution , and is available at
* http : //www.eclipse.org/legal/epl-v10.html
*
* Contributors :
* IBM Corporation - initial API and implementation
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
package com.ibm.wala.cast.js.test ;
import java.io.IOException ;
import java.util.Map ;
import junit.framework.Assert ;
import org.junit.Test ;
2013-01-04 17:35:04 +00:00
import com.ibm.wala.cast.ipa.lexical.LexicalModRef ;
2013-01-03 23:58:07 +00:00
import com.ibm.wala.cast.js.ipa.callgraph.JSCFABuilder ;
import com.ibm.wala.ipa.callgraph.CGNode ;
import com.ibm.wala.ipa.callgraph.CallGraph ;
import com.ibm.wala.util.CancelException ;
2013-04-09 22:47:22 +00:00
import com.ibm.wala.util.WalaException ;
2013-01-03 23:58:07 +00:00
import com.ibm.wala.util.collections.Pair ;
import com.ibm.wala.util.intset.OrdinalSet ;
2013-01-04 17:35:04 +00:00
public abstract class TestLexicalModRef {
2013-01-03 23:58:07 +00:00
@Test
2013-04-09 22:47:22 +00:00
public void testSimpleLexical ( ) throws IOException , IllegalArgumentException , CancelException , WalaException {
2013-01-03 23:58:07 +00:00
JSCFABuilder b = JSCallGraphBuilderUtil . makeScriptCGBuilder ( " tests " , " simple-lexical.js " ) ;
CallGraph CG = b . makeCallGraph ( b . getOptions ( ) ) ;
2013-01-04 17:35:04 +00:00
LexicalModRef lexAccesses = LexicalModRef . make ( CG , b . getPointerAnalysis ( ) ) ;
Map < CGNode , OrdinalSet < Pair < CGNode , String > > > readResult = lexAccesses . computeLexicalRef ( ) ;
Map < CGNode , OrdinalSet < Pair < CGNode , String > > > writeResult = lexAccesses . computeLexicalMod ( ) ;
2013-01-04 00:09:48 +00:00
for ( CGNode n : readResult . keySet ( ) ) {
2013-01-03 23:58:07 +00:00
if ( n . toString ( ) . contains ( " Node: <Code body of function Ltests/simple-lexical.js/outer/inner> " ) ) {
// function "inner" reads exactly x and z
2013-01-04 00:09:48 +00:00
OrdinalSet < Pair < CGNode , String > > readVars = readResult . get ( n ) ;
2013-01-03 23:58:07 +00:00
Assert . assertEquals ( 2 , readVars . size ( ) ) ;
Assert . assertEquals ( " [[Node: <Code body of function Ltests/simple-lexical.js/outer> Context: Everywhere,x], [Node: <Code body of function Ltests/simple-lexical.js/outer> Context: Everywhere,z]] " , readVars . toString ( ) ) ;
2013-01-04 00:09:48 +00:00
// writes x and z as well
OrdinalSet < Pair < CGNode , String > > writtenVars = writeResult . get ( n ) ;
Assert . assertEquals ( 2 , writtenVars . size ( ) ) ;
Assert . assertEquals ( " [[Node: <Code body of function Ltests/simple-lexical.js/outer> Context: Everywhere,x], [Node: <Code body of function Ltests/simple-lexical.js/outer> Context: Everywhere,z]] " , writtenVars . toString ( ) ) ;
2013-01-03 23:58:07 +00:00
}
if ( n . toString ( ) . contains ( " Node: <Code body of function Ltests/simple-lexical.js/outer/inner2> " ) ) {
// function "inner3" reads exactly innerName, inner3, and x and z via callees
2013-01-04 00:09:48 +00:00
OrdinalSet < Pair < CGNode , String > > readVars = readResult . get ( n ) ;
2013-01-03 23:58:07 +00:00
Assert . assertEquals ( 4 , readVars . size ( ) ) ;
2013-04-09 22:47:22 +00:00
for ( Pair < CGNode , String > rv : readVars ) {
Assert . assertTrue ( rv . toString ( ) ,
" [Node: <Code body of function Ltests/simple-lexical.js/outer> Context: Everywhere,x] " . equals ( rv . toString ( ) ) | |
" [Node: <Code body of function Ltests/simple-lexical.js/outer> Context: Everywhere,inner3] " . equals ( rv . toString ( ) ) | |
" [Node: <Code body of function Ltests/simple-lexical.js/outer> Context: Everywhere,innerName] " . equals ( rv . toString ( ) ) | |
" [Node: <Code body of function Ltests/simple-lexical.js/outer> Context: Everywhere,z] " . equals ( rv . toString ( ) ) ) ;
}
2013-01-03 23:58:07 +00:00
}
}
}
}