Unit Testing JScript.Net functions
Let's
say you have a JScript function that you want to unit test that cleans your
host data for TTS. The "&" ampersand is an illegal character for TTS and will cause your speech application to
crash on the speech server. So you
create a nifty little static function called CleanTTS inside a Global JScript class
that will make sure your Speech Synthesis Markup Language (SSML) is compliant. W3C
SSML Specification.
Now you want to test this function just to make sure it works. Normally you would have to include this
script in your speech application and then run it and make sure that it works. Well, I have found a way to unit test this
Jscript Function. Here is how to do this:
Create
a file called UnitTestedJScriptFunctions.js in your speech web project.
public class Global
{
public static function CleanTTS(string1: String): String
{
if ( (string1 == null) || (string1 == undefined) || (string1 == "undefined"))
{
return string1;
}
return string1.Replace("&", " AND ");
}
}
To
add this file to your speech application you can have the following line in
your .aspx page inside the body tag:
<script language="javascript" id="jscript1" src="UnitTestedJScriptFunctions.js"/>
Or,
you can put it in your code behind in the Page_Load function:
Page.RegisterStartupScript("UnitTestedJScriptFunctions.js",
"<script language=\"javascript\"
id=\"_jscript1\" src=\"UnitTestedJScriptFunctions.js\"/>"));
through the use of the Jscript.Net complier (jsc.exe).
There is an option in the Jscript compiler
that will create a .Net assembly .dll.
You can also import libraries, specifically the nunit.framework.
So, to create the unit tests do the
following:
Create
a separate new project that is a class library and call it UnitTests.
In
the UnitTests project create a new Jscript .js file called:
JScriptFunctions.js.
The
file should look like this:
import System;
import NUnit.Framework;
TestFixture public class MyTests
{
SetUp public function Init(): void
{
}
Test public function CleanTTSTest1(): void
{
var input:String = "Texas A&M";
var expected:String = "Texas A AND M";
var actual:String = Global.CleanTTS(input);
Assert.AreEqual(expected, actual, "Global.CleanTTS() did not return expected results.");
}
Test public function CleanTTSTest2(): void
{
var input:String = "A T&T";
var expected:String = "A T AND T";
var actual:String = Global.CleanTTS(input);
Assert.AreEqual(expected, actual, "Global.CleanTTS() did not return expected results.");
}
Test public function CleanTTSTest3(): void
{
var input:String = "ACME Corp.";
var expected:String = "ACME Corp.";
var actual:String = Global.CleanTTS(input);
Assert.AreEqual(expected, actual, "Global.CleanTTS() did not return expected results.");
}
Test public function CleanTTSTest4(): void
{
var input:String;
var expected:String;
var actual:String = Global.CleanTTS(input);
Assert.AreEqual(expected, actual, "Global.CleanTTS() did not return expected results.");
}
Test public function CleanTTSTest5(): void
{
var input:String = null;
var expected:String = null;
var actual:String = Global.CleanTTS(input);
Assert.AreEqual(expected, actual, "Global.CleanTTS() did not return expected results.");
}
}
Notice
the typical NUnit attributes: TestFixture, SetUp, and Test
Create
a batch file that will compile your Jscript code into a .Net assembly dll.
You will need the latest version of NUnit (for .net 1.1) for the next step. You can download it here.
Then
call nunit-console.exe on that .dll to run the unit tests.
JScriptUnitTests.bat:
SET
FrameworkLocation=c:\windows\Microsoft.NET\Framework\v1.1.4322
SET
NUnitLocation=D:\My NUnit\bin
ECHO.
ECHO
Compiling JScript Unit Tests: JScriptFunctionsTests.js to create
JScriptFunctionsTests.dll
ECHO.
"%FrameworkLocation%\jsc"
/nologo /out:JScriptFunctionsTests.dll /w:0 /warnaserror+ /r:nunit.framework
/lib:"%NUnitLocation%" JScriptFunctionsTests.js
..\speechweb\JScript\UnitTestedJScriptFunctions.js
ECHO.
ECHO
Running NUnit on JScript Unit Tests: JScriptFunctionsTests.dll
ECHO.
"%NUnitLocation%\nunit-console.exe"
/nologo JScriptFunctionsTests.dll
Add
this JScriptUnitTests.bat file to the UnitTests project postbuild event and
the output should look like this:
Tests
run: 5, Failures: 0, Not run: 0, Time: 0.030 seconds
And
there you go! 5 Unit tests passed on
your JScript.