Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
John McEleney
/
casperjs
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit
dd321f7e
...
dd321f7e76897bb7d00d75ea24776d11deac2e11
authored
2011-12-12 23:41:41 +0100
by
Nicolas Perriault
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
new tester can handle coffeescript test files as well
1 parent
7c2f9593
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
97 additions
and
85 deletions
lib/tester.js
lib/utils.js
tests/run.coffee → tests/suites/coffee.coffee
tests/suites/injector.js
tests/suites/tester.js
tests/suites/xunit.js
lib/tester.js
View file @
dd321f7
...
...
@@ -265,10 +265,14 @@
* @param String file Absolute path to some js/coffee file
*/
this
.
exec
=
function
(
file
)
{
if
(
!
fs
.
isFile
(
file
)
||
!
this
.
isJsFile
(
file
))
{
if
(
!
fs
.
isFile
(
file
)
||
!
isJsFile
(
file
))
{
throw
"Can only exec() files with .js or .coffee extensions"
;
}
eval
(
fs
.
read
(
file
));
if
(
fileExt
(
file
)
===
"coffee"
)
{
phantom
.
injectJs
(
file
);
}
else
{
eval
(
fs
.
read
(
file
));
}
};
/**
...
...
@@ -301,7 +305,7 @@
}
});
return
entries
.
filter
(
function
(
entry
)
{
return
self
.
isJsFile
(
fs
.
absolute
(
pathJoin
(
dir
,
entry
)));
return
isJsFile
(
fs
.
absolute
(
pathJoin
(
dir
,
entry
)));
});
};
...
...
@@ -329,20 +333,6 @@
};
/**
* Checks if a file is apparently javascript compatible (.js or .coffee).
*
* @param String file Path to the file to test
* @return Boolean
*/
this
.
isJsFile
=
function
(
file
)
{
var
ext
;
try
{
ext
=
file
.
split
(
'.'
).
pop
().
toLowerCase
();
}
catch
(
e
)
{}
return
isType
(
ext
,
"string"
)
&&
[
'js'
,
'coffee'
].
indexOf
(
ext
)
!==
-
1
;
};
/**
* Adds a successful test entry to the stack.
*
* @param String message
...
...
lib/utils.js
View file @
dd321f7
...
...
@@ -151,6 +151,20 @@ function dump(value) {
}
/**
* Returns the file extension in lower case.
*
* @param String file File path
* @return string
*/
function
fileExt
(
file
)
{
try
{
return
file
.
split
(
'.'
).
pop
().
toLowerCase
();
}
catch
(
e
)
{
return
''
;
}
}
/**
* Takes a string and append blank until the pad value is reached.
*
* @param String text
...
...
@@ -166,6 +180,17 @@ function fillBlanks(text, pad) {
}
/**
* Checks if a file is apparently javascript compatible (.js or .coffee).
*
* @param String file Path to the file to test
* @return Boolean
*/
function
isJsFile
(
file
)
{
var
ext
=
fileExt
(
file
);
return
isType
(
ext
,
"string"
)
&&
[
'js'
,
'coffee'
].
indexOf
(
ext
)
!==
-
1
;
}
/**
* Shorthands for checking if a value is of the given type. Can check for
* arrays.
*
...
...
tests/
run
.coffee
→
tests/
suites/coffee
.coffee
View file @
dd321f7
# A small subset of the run.js written in coffeescript
phantom
.
injectJs
"casper.js"
do
(
casper
)
->
steps
=
0
casper
=
new
phantom
.
Casper
faultTolerant
:
false
verbose
:
true
onStepComplete
:
->
@
test
.
comment
"step completed"
casper
.
options
.
onStepComplete
=
->
steps
++
casper
.
start
"tests/site/index.html"
,
->
@
test
.
assertTitle
"CasperJS test index"
,
"Casper.start() casper can start itself an open an url"
@
test
.
comment
"fetching"
@
test
.
assertEquals
@
fetchText
(
"ul li"
),
"onetwothree"
,
"Casper.fetchText() can retrieves text contents"
@
test
.
comment
"clicking"
@
click
"a[href=
\"
test.html
\"
]"
casper
.
start
"tests/site/index.html"
,
->
@
test
.
assertTitle
"CasperJS test index"
,
"Casper.start() casper can start itself an open an url"
@
test
.
assertEquals
@
fetchText
(
"ul li"
),
"onetwothree"
,
"Casper.fetchText() can retrieves text contents"
@
click
"a[href=
\"
test.html
\"
]"
casper
.
test
.
comment
"then"
casper
.
then
->
@
test
.
assertTitle
"CasperJS test target"
,
"Casper.click() casper can click on a text link"
@
click
"a[href=
\"
form.html
\"
]"
casper
.
then
->
@
test
.
assertTitle
"CasperJS test target"
,
"Casper.click() casper can click on a text link"
@
click
"a[href=
\"
form.html
\"
]"
casper
.
run
()
\ No newline at end of file
casper
.
run
->
@
test
.
assertEquals
steps
,
3
,
"Casper.options.onStepComplete() is called on step complete"
@
options
.
onStepComplete
=
null
@
test
.
done
()
\ No newline at end of file
...
...
tests/suites/injector.js
View file @
dd321f7
// phantom.Casper.FunctionArgsInjector
var
t
=
casper
.
test
;
(
function
(
t
)
{
function
createInjector
(
fn
,
values
)
{
return
new
phantom
.
Casper
.
FunctionArgsInjector
(
fn
,
values
);
}
function
createInjector
(
fn
,
values
)
{
return
new
phantom
.
Casper
.
FunctionArgsInjector
(
fn
,
values
);
}
var
testFn
=
function
(
a
,
b
)
{
return
a
+
b
;
};
var
injector
=
createInjector
(
testFn
);
var
extract
=
injector
.
extract
(
testFn
);
var
testFn
=
function
(
a
,
b
)
{
return
a
+
b
;
};
var
injector
=
createInjector
(
testFn
);
var
extract
=
injector
.
extract
(
testFn
);
t
.
comment
(
'FunctionArgsInjector.extract()'
);
t
.
assertType
(
extract
,
"object"
,
'FunctionArgsInjector.extract() returns an object'
);
t
.
assertEquals
(
extract
.
name
,
null
,
'FunctionArgsInjector.extract() process function name as expected'
);
t
.
assertEquals
(
extract
.
body
,
'return a + b;'
,
'FunctionArgsInjector.extract() process function body as expected'
);
t
.
assertEquals
(
extract
.
args
,
[
'a'
,
'b'
],
'FunctionArgsInjector.extract() process function args as expected'
);
t
.
comment
(
'FunctionArgsInjector.extract()'
);
t
.
assertType
(
extract
,
"object"
,
'FunctionArgsInjector.extract() returns an object'
);
t
.
assertEquals
(
extract
.
name
,
null
,
'FunctionArgsInjector.extract() process function name as expected'
);
t
.
assertEquals
(
extract
.
body
,
'return a + b;'
,
'FunctionArgsInjector.extract() process function body as expected'
);
t
.
assertEquals
(
extract
.
args
,
[
'a'
,
'b'
],
'FunctionArgsInjector.extract() process function args as expected'
);
var
processed
;
eval
(
'processed = '
+
injector
.
process
({
a
:
1
,
b
:
2
}));
var
processed
;
eval
(
'processed = '
+
injector
.
process
({
a
:
1
,
b
:
2
}));
t
.
assertEquals
(
processed
(),
3
,
'FunctionArgsInjector.process() proccessed the function correctly'
);
t
.
assertEquals
(
processed
(),
3
,
'FunctionArgsInjector.process() proccessed the function correctly'
);
t
.
done
();
\ No newline at end of file
t
.
done
();
})(
casper
.
test
);
...
...
tests/suites/tester.js
View file @
dd321f7
var
t
=
casper
.
test
;
(
function
(
t
)
{
t
.
comment
(
'Tester.testEquals()'
);
t
.
assert
(
casper
.
test
.
testEquals
(
null
,
null
),
'Tester.testEquals() null equality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
(
null
,
undefined
),
'Tester.testEquals() null vs. undefined inequality'
);
t
.
assert
(
casper
.
test
.
testEquals
(
"hi"
,
"hi"
),
'Tester.testEquals() string equality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
(
"hi"
,
"ih"
),
'Tester.testEquals() string inequality'
);
t
.
assert
(
casper
.
test
.
testEquals
(
5
,
5
),
'Tester.testEquals() number equality'
);
t
.
assert
(
casper
.
test
.
testEquals
(
5
,
5.0
),
'Tester.testEquals() cast number equality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
(
5
,
10
),
'Tester.testEquals() number inequality'
);
t
.
assert
(
casper
.
test
.
testEquals
([],
[]),
'Tester.testEquals() empty array equality'
);
t
.
assert
(
casper
.
test
.
testEquals
([
1
,
2
],
[
1
,
2
]),
'Tester.testEquals() array equality'
);
t
.
assert
(
casper
.
test
.
testEquals
([
1
,
2
,[
1
,
2
,
function
(){}]],
[
1
,
2
,[
1
,
2
,
function
(){}]]),
'Tester.testEquals() complex array equality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
([
1
,
2
,[
1
,
2
,
function
(
a
){}]],
[
1
,
2
,[
1
,
2
,
function
(
b
){}]]),
'Tester.testEquals() complex array inequality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
([
1
,
2
],
[
2
,
1
]),
'Tester.testEquals() shuffled array inequality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
([
1
,
2
],
[
1
,
2
,
3
]),
'Tester.testEquals() array length inequality'
);
t
.
assert
(
casper
.
test
.
testEquals
({},
{}),
'Tester.testEquals() empty object equality'
);
t
.
assert
(
casper
.
test
.
testEquals
({
a
:
1
,
b
:
2
},
{
a
:
1
,
b
:
2
}),
'Tester.testEquals() object length equality'
);
t
.
assert
(
casper
.
test
.
testEquals
({
a
:
1
,
b
:
2
},
{
b
:
2
,
a
:
1
}),
'Tester.testEquals() shuffled object keys equality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
({
a
:
1
,
b
:
2
},
{
a
:
1
,
b
:
3
}),
'Tester.testEquals() object inequality'
);
t
.
assert
(
casper
.
test
.
testEquals
({
1
:{
name
:
"bob"
,
age
:
28
},
2
:{
name
:
"john"
,
age
:
26
}},
{
1
:{
name
:
"bob"
,
age
:
28
},
2
:{
name
:
"john"
,
age
:
26
}}),
'Tester.testEquals() complex object equality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
({
1
:{
name
:
"bob"
,
age
:
28
},
2
:{
name
:
"john"
,
age
:
26
}},
{
1
:{
name
:
"bob"
,
age
:
28
},
2
:{
name
:
"john"
,
age
:
27
}}),
'Tester.testEquals() complex object inequality'
);
t
.
assert
(
casper
.
test
.
testEquals
(
function
(
x
){
return
x
;},
function
(
x
){
return
x
;}),
'Tester.testEquals() function equality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
(
function
(
x
){
return
x
;},
function
(
y
){
return
y
+
2
;}),
'Tester.testEquals() function inequality'
);
t
.
comment
(
'Tester.testEquals()'
);
t
.
assert
(
casper
.
test
.
testEquals
(
null
,
null
),
'Tester.testEquals() null equality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
(
null
,
undefined
),
'Tester.testEquals() null vs. undefined inequality'
);
t
.
assert
(
casper
.
test
.
testEquals
(
"hi"
,
"hi"
),
'Tester.testEquals() string equality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
(
"hi"
,
"ih"
),
'Tester.testEquals() string inequality'
);
t
.
assert
(
casper
.
test
.
testEquals
(
5
,
5
),
'Tester.testEquals() number equality'
);
t
.
assert
(
casper
.
test
.
testEquals
(
5
,
5.0
),
'Tester.testEquals() cast number equality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
(
5
,
10
),
'Tester.testEquals() number inequality'
);
t
.
assert
(
casper
.
test
.
testEquals
([],
[]),
'Tester.testEquals() empty array equality'
);
t
.
assert
(
casper
.
test
.
testEquals
([
1
,
2
],
[
1
,
2
]),
'Tester.testEquals() array equality'
);
t
.
assert
(
casper
.
test
.
testEquals
([
1
,
2
,[
1
,
2
,
function
(){}]],
[
1
,
2
,[
1
,
2
,
function
(){}]]),
'Tester.testEquals() complex array equality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
([
1
,
2
,[
1
,
2
,
function
(
a
){}]],
[
1
,
2
,[
1
,
2
,
function
(
b
){}]]),
'Tester.testEquals() complex array inequality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
([
1
,
2
],
[
2
,
1
]),
'Tester.testEquals() shuffled array inequality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
([
1
,
2
],
[
1
,
2
,
3
]),
'Tester.testEquals() array length inequality'
);
t
.
assert
(
casper
.
test
.
testEquals
({},
{}),
'Tester.testEquals() empty object equality'
);
t
.
assert
(
casper
.
test
.
testEquals
({
a
:
1
,
b
:
2
},
{
a
:
1
,
b
:
2
}),
'Tester.testEquals() object length equality'
);
t
.
assert
(
casper
.
test
.
testEquals
({
a
:
1
,
b
:
2
},
{
b
:
2
,
a
:
1
}),
'Tester.testEquals() shuffled object keys equality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
({
a
:
1
,
b
:
2
},
{
a
:
1
,
b
:
3
}),
'Tester.testEquals() object inequality'
);
t
.
assert
(
casper
.
test
.
testEquals
({
1
:{
name
:
"bob"
,
age
:
28
},
2
:{
name
:
"john"
,
age
:
26
}},
{
1
:{
name
:
"bob"
,
age
:
28
},
2
:{
name
:
"john"
,
age
:
26
}}),
'Tester.testEquals() complex object equality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
({
1
:{
name
:
"bob"
,
age
:
28
},
2
:{
name
:
"john"
,
age
:
26
}},
{
1
:{
name
:
"bob"
,
age
:
28
},
2
:{
name
:
"john"
,
age
:
27
}}),
'Tester.testEquals() complex object inequality'
);
t
.
assert
(
casper
.
test
.
testEquals
(
function
(
x
){
return
x
;},
function
(
x
){
return
x
;}),
'Tester.testEquals() function equality'
);
t
.
assertNot
(
casper
.
test
.
testEquals
(
function
(
x
){
return
x
;},
function
(
y
){
return
y
+
2
;}),
'Tester.testEquals() function inequality'
);
t
.
done
();
\ No newline at end of file
t
.
done
();
})(
casper
.
test
);
...
...
tests/suites/xunit.js
View file @
dd321f7
var
t
=
casper
.
test
;
(
function
(
t
)
{
t
.
comment
(
'phantom.Casper.XUnitExporter'
);
t
.
comment
(
'phantom.Casper.XUnitExporter'
);
xunit
=
new
phantom
.
Casper
.
XUnitExporter
();
xunit
.
addSuccess
(
'foo'
,
'bar'
);
t
.
assertMatch
(
xunit
.
getXML
(),
/<testcase classname="foo" name="bar"/
,
'XUnitExporter.addSuccess() adds a successful testcase'
);
xunit
.
addFailure
(
'bar'
,
'baz'
,
'wrong'
,
'chucknorriz'
);
t
.
assertMatch
(
xunit
.
getXML
(),
/<testcase classname="bar" name="baz"><failure type="chucknorriz">wrong/
,
'XUnitExporter.addFailure() adds a failed testcase'
);
xunit
=
new
phantom
.
Casper
.
XUnitExporter
();
xunit
.
addSuccess
(
'foo'
,
'bar'
);
t
.
assertMatch
(
xunit
.
getXML
(),
/<testcase classname="foo" name="bar"/
,
'XUnitExporter.addSuccess() adds a successful testcase'
);
xunit
.
addFailure
(
'bar'
,
'baz'
,
'wrong'
,
'chucknorriz'
);
t
.
assertMatch
(
xunit
.
getXML
(),
/<testcase classname="bar" name="baz"><failure type="chucknorriz">wrong/
,
'XUnitExporter.addFailure() adds a failed testcase'
);
t
.
done
();
\ No newline at end of file
t
.
done
();
})(
casper
.
test
);
...
...
Please
register
or
sign in
to post a comment