#!/usr/bin/env perl
plan
tests
=> 6;
{
my
$js
= JavaScript::Writer->new();
$js
->var(
'a'
);
is
"$js"
,
"var a;"
,
"variable declarition"
;
}
{
my
$js
= JavaScript::Writer->new();
$js
->var(
a
=> 1);
is
"$js"
,
"var a = 1;"
,
"Scalar assignment"
;
}
{
my
$js
= JavaScript::Writer->new();
$js
->var(
a
=> [ 1, 3, 5, 7, 9 ]);
is
"$js"
,
'var a = [1,3,5,7,9];'
,
"Array assignment"
;
}
{
my
$js
= JavaScript::Writer->new();
$js
->var(
a
=> {
Lorem
=>
'Ipsum'
,
'Foo'
=> 0 });
is
"$js"
,
'var a = {"Foo":0,"Lorem":"Ipsum"};'
,
"Hash assignment"
;
}
{
my
$js
= JavaScript::Writer->new();
$js
->var(
salut
=>
sub
{
$_
[0]->alert(
"Nihao"
) });
is
"$js"
,
'var salut = function(){alert("Nihao");};'
,
"function assigned to a var"
;
}
{
my
$jsf
= JavaScript::Writer::Function->new;
$jsf
->arguments(
qw[foo bar baz]
);
$jsf
->body(
sub
{
$_
[0]->alert(
"Nihao"
) } );
my
$js
= JavaScript::Writer->new();
$js
->var(
salut
=>
$jsf
);
is
"$js"
,
'var salut = function(foo,bar,baz){alert("Nihao");};'
,
"anonymous function with arguments assigned to a var"
;
}