在RhinoUnit中设置JSLint JavaScript代码使用的属性

时间:2011-07-23 02:02:06

标签: javascript ant rhino jslint rhino-unit

我正在尝试使用RhinoUnit对基于XUL的独立JavaScript应用进行单元测试。我已经成功地改变了给定build.xml中的很多路径 - 主要是改变RhinoUnit脚本的路径,我更喜欢将其放在另一个目录而不是默认目录中(也就是说,我将RhinoUnit文件放在chrome/content/rhino中和chrome/content/lint中的JSLint文件。但是,在某个时刻,我遇到了这个错误:

/<project-path>/build.xml:52: javax.script.ScriptException: 
sun.org.mozilla.javascript.internal.WrappedException: Wrapped 
java.io.FileNotFoundException: /<project-path>/jslint/fulljslint.js
(No such file or directory) (<Unknown source>#31) in <Unknown source>
at line number 31

jslint/fulljslint.js中没有build.xml的引用,但我在jslintant.js找到了此代码:

var jsLintPath = "jslint/fulljslint.js";
if (attributes.get("jslintpath")) {
    jsLintPath = attributes.get("jslintpath");
}

在我看来,此代码为变量设置了默认值,然后尝试使用某个attributes对象中的值。我假设可以在脚本之外设置这些attributes,例如一些<atttribute />标记int build.xml或一些配置文件。

我的问题是:如何更改对象的值?那可能吗?或者我应该从脚本中更改硬编码字符串吗?

1 个答案:

答案 0 :(得分:1)

这是一种考虑的可能性。

如何弄清楚发生了什么: 如果你在jslintant.js文件中插入它,就在jsLintPath赋值之前你提到:

echo = project.createTask( "echo" );
echo.setMessage( attributes );
echo.perform( );

然后运行RhinoUnit构建,您应该看到类似的内容:

run-js-lint:
     [echo] {options={eqeqeq : false, white: true, plusplus : false, bitwise :  ... }}

如何做你想做的事情 '选项'被定义为jslintant scriptdef的属性。要传播jslintpath的值,需要在scriptdef中将其添加为属性,然后在使用如此定义的任务时设置它。例如:

<scriptdef name="jslintant"
           src="jslint/jslintant.js"
           language="javascript">
    <attribute name="options" />
    <attribute name="jslintpath" /> <!-- This line added. -->
    <element name="fileset" type="fileset" />
</scriptdef>

然后使用任务:

<jslintant options="{eqeqeq : false, ... }"
           jslintpath="your_path_here/fulljslint.js" />

如果您重新运行构建,则应该看到:

run-js-lint:
     [echo] {jslintpath=your_path_here/fulljslint.js, options={eqeqeq : false, ... }}

您选择的路径将用于查找fulljslint.js