Skip to content

Instantly share code, notes, and snippets.

@rapha
Created May 10, 2009 01:23
Show Gist options
  • Save rapha/109463 to your computer and use it in GitHub Desktop.
Save rapha/109463 to your computer and use it in GitHub Desktop.
JUnit tests in Javascript, using this ant include file which provides the `compile-jsjunit-tests` task
<project name='jsjunit'>
<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
<property name='slash' value='${file.separator}'/>
<path id='classpath'>
<pathelement path="${junit.jar}"/>
<pathelement path="${js.jar}"/>
<pathelement path="${basedir}"/>
</path>
<macrodef name='build-interface-file'>
<attribute name='jspath'/>
<attribute name='interface'/>
<attribute name='package'/>
<attribute name='location'/>
<sequential>
<var name='interface.file' value='@{location}${slash}@{interface}.java' />
<mkdir dir='@{location}'/>
<echo file='${interface.file}' append='false'>package @{package};</echo>
<echo file='${interface.file}' append='true' >public interface @{interface} {</echo>
<concat destfile='${interface.file}' append='true'>
<path>
<pathelement location='@{jspath}'/>
</path>
<filterchain>
<linecontainsregexp>
<regexp pattern='(\btest\w+ = function)|(function test\w+)|(\bsetUp = function)|(function setUp\b)|(\btearDown= function)|(function tearDown\b)'/>
</linecontainsregexp>
<replaceregex pattern='.*\b(test\w*).*' replace='public void \1();'/>
<replaceregex pattern='.*\b(setUp).*' replace='public void setUp();'/>
<replaceregex pattern='.*\b(tearDown).*' replace='public void tearDown();'/>
</filterchain>
</concat>
<echo file='${interface.file}' append='true' >}</echo>
<javac srcdir='@{location}'/>
<delete file='${interface.file}'/>
</sequential>
</macrodef>
<macrodef name='compile-jsjunit-test'>
<attribute name='jspath'/>
<attribute name='interface'/>
<attribute name='package'/>
<attribute name='dir'/>
<sequential>
<java classname='org.mozilla.javascript.tools.jsc.Main'>
<classpath>
<path refid='classpath'/>
<pathelement location='@{dir}'/>
</classpath>
<arg value='-extends'/><arg value='junit.framework.TestCase'/>
<arg value='-implements'/><arg value='@{package}.@{interface}'/>
<arg value='-package'/><arg value='@{package}'/>
<arg value='-d'/><arg value='@{dir}'/>
<arg value='@{jspath}'/>
</java>
</sequential>
</macrodef>
<macrodef name='compile-jsjunit-tests'>
<element name='includes' implicit='true' optional='true'/>
<attribute name='srcdir'/>
<attribute name='destdir'/>
<sequential>
<ac:for param='jspath' xmlns:ac="antlib:net.sf.antcontrib">
<path>
<fileset dir='@{srcdir}'>
<includes/>
</fileset>
</path>
<sequential>
<propertyregex property='jsfile' input='@{jspath}' regexp='.*${slash}(.*)' select='\1' override='true'/>
<propertyregex property='name' input='${jsfile}' regexp='(\w+)\..*' select='\1' override='true'/>
<propertyregex property='subdir' input='@{jspath}' regexp='${basedir}${slash}@{srcdir}${slash}(.*)${slash}${jsfile}' select='\1' override='true'/>
<propertyregex property='package' input='${subdir}' regexp='${slash}' replace='.' override='true' defaultValue='${subdir}'/>
<build-interface-file jspath='@{jspath}' interface='${name}Interface' package='${package}' location='@{destdir}${slash}${subdir}' />
<compile-jsjunit-test jspath='@{jspath}' interface='${name}Interface' package='${package}' dir='@{destdir}' />
</sequential>
</ac:for>
</sequential>
</macrodef>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment