From crosseyedpenguin at cox.net Fri Apr 1 08:15:42 2005
From: crosseyedpenguin at cox.net (Roger)
Date: Fri Apr 1 11:21:57 2005
Subject: [Jsconsole] Getting Started
Message-ID: <424D659E.2050906@cox.net>
Hi,
I have read the docs, installed the JSConsole package, and have run a
few succesful tests from the Quick Start. But I need an example or two
of how this tool is intended to be used.
My first thought was to load my test web page that loads a .js file with
several new functions in need of testing. I expected to click on the
FireFox Tools-JSConsole menu and have JSConsole come up knowing about my
web page and the loaded Javascript files. This doesn't seem to happen.
JSConsole seems to want me to load the .js file, add some unit test code
at the end, and then right-click, and choose run, and then look for
error/success results in the Unit Test tab. However, I do not understand
how my Javascript functions can access the HTML form text fields, radio
buttons, etc. embedded in my web page. Also, how can I change the values
of a web-page text field within my unit test.
A useful example for me would be to test a text field to verify a user
entered an integer. So if my web-page had something like:
Obviously I could test by passing an integer or non-integer strings and
get results. But Javascript functions are often more complicated and
access multiple form inputs. So is the idea to refactor Javascript code
and test without accessing form inputs, and then write "simple" wrapper
functions to gather form data and/or update form data? So I would write
a wrapper for checkInt that looked like:
function callCheckInt(this) {return checkInt(this.value);}
and then rewrite the onBlur to
onBlur="return callCheckInt(this)"
Am I correct in that JSConsole knows nothing about the web page loaded
at the time it is started?
Finally, is there a way to write and leave the test cases in the .js
file? Something like:
function a.....
function b.....
function c....
testcase1...
testcase2...
testcase3...
Or do you just comment the test cases out?
Roger Haase
From zdev at noos.fr Mon Apr 4 17:39:41 2005
From: zdev at noos.fr (Daniel Fournier)
Date: Mon Apr 4 10:48:28 2005
Subject: [Jsconsole] Getting Started
Message-ID: <425151AD.8010401@noos.fr>
Hi,
Unfortunately, JS Console doesn't run JS embedded in HTML pages. It has
been developped to run standalone JS files.
You have to consider that input from your web page(s) are only input
data. So just define a set of JS variables corresponding to samples of
data that your web page user can input and test that set of variables
with your validating functions.
In my opinion it's a good practice to decouple your JS logic and your
your JS data input from HTML forms, using an intermediate function. That
way, when you change something in the user interface (HTML forms), you
just have to make corresponding adjustment in the intermediate
functions, the main logic beeing as generic as possible.
>
> Am I correct in that JSConsole knows nothing about the web page loaded
> at the time it is started?
Quite true.
>
> Finally, is there a way to write and leave the test cases in the .js
> file? Something like:
>
It depends whether you want to take advantage of the unit testing
framework or not.
If you do, then just define a boolean global variable that you set to
true when you want to have the unit tests run, or false otherwise. Any
ASSERT function takes an argument to decide whether the unit test have
to be run (see Unit testing doc), then passing your global variable as
this argument, you can either run every ASSERT function you have
inserted in your code, or bypass all (you can also combine this global
variable with a specific boolean variable that would enable or not the
asserting function).
But if you don't want to use the testing framework, then just surround
your test cases with a conditional construct to run or not run them. For
instance:
if (true) //change to false to skip all tests
{
testcase1(arg0, arg1);
testcase2(arg0, arg1);
}
You can also have a more fine grain testing, isolating one or several
test cases within conditional constructs.
There's a third way, my prefered. Just isolate all your testing
functions in a specific file and import your "real" functions within it,
using the __IMPORT__ function. That way, everything is clean and easy to
modify and when your code is ready, you don't have to load the testing
file and you avoid a (slight) processing overhead for the JS parsing
engine.
Daniel Fournier
From crosseyedpenguin at cox.net Mon Apr 4 11:11:09 2005
From: crosseyedpenguin at cox.net (Roger)
Date: Mon Apr 4 13:18:24 2005
Subject: [Jsconsole] Getting Started
In-Reply-To: <425151AD.8010401@noos.fr>
References: <425151AD.8010401@noos.fr>
Message-ID: <4251752D.9030207@cox.net>
Daniel Fournier wrote:
>
> There's a third way, my prefered. Just isolate all your testing
> functions in a specific file and import your "real" functions within
> it, using the __IMPORT__ function. That way, everything is clean and
> easy to modify and when your code is ready, you don't have to load the
> testing file and you avoid a (slight) processing overhead for the JS
> parsing engine.
>
>
>
Perfect. That will be a considerable improvement over what I have been
doing. I am looking forward to writing more Javascript code. I am a
Python bigot and have really hated dealing with the curly braces and
semi-colons of Javascript.
Roger Haase
From crosseyedpenguin at cox.net Sun Apr 10 09:56:21 2005
From: crosseyedpenguin at cox.net (Roger)
Date: Sun Apr 10 12:06:16 2005
Subject: [Jsconsole] Getting Started
In-Reply-To: <42521E11.6060102@noos.fr>
References: <425151AD.8010401@noos.fr> <4251752D.9030207@cox.net>
<42521E11.6060102@noos.fr>
Message-ID: <42594CA5.1090505@cox.net>
Daniel Fournier wrote:
>
> I understand your point of view. Although I never really wrote any
> program using Python, I extensively learned it. I like very much
> Python, which is, IMHO, the best scripting language of our days.
>
> As a matter of fact, I recently tried to apply Python coding style to
> JavaScript, in a library of functions I developped, aiming to use JS
> in a functional programming way. Here is an example of this style:
>
> -------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> String.prototype.getLastToFirst = function
> //StringpublicReverse string characters
> ()
> { if (this.isEmpty())
> { return this.getDefault(); //trying
> to set an empty String yields nothing
> } var string = '';
> /*String*/
> for (var i=this.length-1, m=0; i>=m; i--)
> { string += this.charAt(i);
> } return string;
> };
> //getLastToFirst
> -------------------------------------------------------------------------------------------------------------------------------------------------------------
>
>
> I'd like to know whether this style is "acceptable" by a Python
> programmer.
>
> This is an extract of a library extending JS built-in types (String,
> Number, Array...) with useful methods (about 200) with a functional
> programming horizon. Let me know whether you are interested by such a
> library: I'll send you a copy of it.
> Of course, it has been developped using extensively JS Console, and
> each type package (it's a Python vocable, isn't it?) has its own test
> file.
>
>
Converting from a Python coding style to Javascript is not something
that comes natural. I have been influenced by Javascript Lint (the
add-on to HTML-Kit). Javascript Lint does not like lines that end with
")" and demands a ";" at the end of every statement. Per the Python
coding style guidlines by Guido van Rossum, Python code is pretty
dense. I find the use of the closing curly brace on a line by itself
very annoying, and I stick them on the end of the preceeding line.
So taking a random routine from Paul Johnston's SHA-1 as an example,
here is the before:
------------------------------------
/*
* Calculate the HMAC-SHA1 of a key and some data
*/
function core_hmac_sha1(key, data)
{
var bkey = str2binb(key);
if(bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz);
var ipad = Array(16), opad = Array(16);
for(var i = 0; i < 16; i++)
{
ipad[i] = bkey[i] ^ 0x36363636;
opad[i] = bkey[i] ^ 0x5C5C5C5C;
}
var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length *
chrsz);
return core_sha1(opad.concat(hash), 512 + 160);
}
------------------------------
And my after which passes Javascript Lint. "i++" replaced with "i+=1"
and an extra pair of braces in the if statement.
--------------------------------
// Calculate the HMAC-SHA1 of a key and some data
function core_hmac_sha1(key, data) {
var bkey = str2binb(key);
if(bkey.length > 16) {
bkey = core_sha1(bkey, key.length * chrsz); }
var ipad = Array(16), opad = Array(16);
for(var i = 0; i < 16; i+=1) {
ipad[i] = bkey[i] ^ 0x36363636;
opad[i] = bkey[i] ^ 0x5C5C5C5C; }
var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length
* chrsz);
return core_sha1(opad.concat(hash), 512 + 160); }
----------------------------------
Roger Haase