Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: dev
Fetching contributors…

Cannot retrieve contributors at this time

973 lines (741 sloc) 19.796 kb
<!doctype html>
<html>
<head>
<style type="text/css">
div {
padding: 3px;
margin: 3px;
float: left;
border: 3px solid black;
background: #333;
color: white;
-webkit-box-shadow: 3px 3px 5px #555;
}
div.done {
background: #1c5;
}
</style>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jsTPL.js"></script>
<script type="text/javascript" src="jsTPL.block.js"></script>
<script type="text/javascript" src="jsTPL.filter.js"></script>
<script type="text/javascript">
//************************************ Settings
var $testBlock = 1;
var $testFilter = 1;
//************************************ Settings - end
try {console.log();}
catch (e) {
window.console = {
log: function (str) {
},
error: function () {
alert(Array.prototype.join.apply(arguments,[""]));
},
info: function (str) {
}
};
}
var start, benchmarks=[], global=0;
function _start() {
start = +new Date;
}
function _end() {
return (+new Date - start);
}
function run(i , result) {
i = i || 0;
if (benchmarks[ i ])
result = benchmarks[ i ]();
else
return done();
$('body').append($('<div class="' + (result ? "done": "not") + '">Test # ' + i + '</div>').attr({title:benchmarks[ i ].toString()}).hide().fadeIn(100,function() {
run(i+1);
}));
//setTimeout(function() { run(i+1);},0);
}
function done() {
$('body').append($('<div class="done">All tests done : ' + global +'</div>').hide().fadeIn(250));
}
benchmarks.push(function () {
try {
console.info("Only string");
_start();
var got = jsTPL("Only string")(),
expected = "Only string";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Renaming");
_start();
jsTPL("wrong",[],"a");
jsTPL("right",[],"a");
var got = jsTPL("a")(),
expected = "right";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Only eval");
_start();
var got = jsTPL("{%= 'Only eval' %}")(),
expected = "Only eval";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Only eval variable");
_start();
var got = jsTPL("{%= a %}",["a"])({a:"Only eval variable"}),
expected = "Only eval variable";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Arguments cache");
_start();
jsTPL("{%= a %}");
var got = jsTPL("{%= a %}",["a"])({a:"Only eval variable"}),
expected = "Only eval variable";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Eval and str");
_start();
var got = jsTPL("Work{%= ' is ' %}hard")(),
expected = "Work is hard";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Eval var and str");
_start();
var got = jsTPL("Work{%= a %}hard",["a"])({a: " is "}),
expected = "Work is hard";
global+=_end();
if ( got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Simple if");
_start();
var got = jsTPL("{%if true%}I am ok{%/if%}")(),
expected = "I am ok";
global+=_end();
if ( got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("If with else");
_start();
var got = jsTPL("{%if false%}I am not ok{%else%}I am ok{%/if%}")(),
expected = "I am ok";
global+=_end();
if ( got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("If with elseif");
_start();
var got = jsTPL("{%if false%}I am not ok{%elseif true%}I am ok{%/if%}")(),
expected = "I am ok";
global+=_end();
if ( got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Functions and eval");
_start();
var got = jsTPL("{%@ test(a) {return a+' ok'} %}{%= test(\"I am\") %}")(),
expected = "I am ok";
global+=_end();
if ( got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Scopes");
_start();
var got = jsTPL("{% var a = 'not in scope'; %}{%~ var a = 'in scope';%}{%= a %}")(),
expected = "not in scope";
global+=_end();
if ( got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Each method");
_start();
var got = jsTPL("{%each a%}{%= this + '-' %}{%/each%}",["a"])({a: [1 , 2 , 3]}),
expected = "1-2-3-";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {console.log(e);}
});
benchmarks.push(function () {
try {
console.info("Catch method");
_start();
var got = jsTPL("{%catch var a%}I am a{%/catch%}{%= a +' char' %}")(),
expected = "I am a char";
global+=_end();
if ( got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Naming templates");
jsTPL("test",[],"test");
_start();
var got = jsTPL("test")(),
expected = "test";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Naming templates with short hand");
jsTPL("test",[],"test");
_start();
var got = jsTPL("{%: test {} %} is hard")(),
expected = "test is hard";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Nested naming and renaming");
_start();
jsTPL("test",[],"test");
jsTPL("{%: test {} %}is hard",[],"kaza");
var got = jsTPL("{%: kaza {} %}so do it")(),
expected ="testis hardso do it";
global+=_end();
if ( got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
console.info("Similar speed test");
var inc_g = "1{%= 2%}3",
inc_e = "123",
g="",
e="";
for (var i=1;i<1000;i++) {
g+=inc_g;
e+=inc_e;
}
if (jsTPL(g)() !== e)
return;
i=50;
_start();
while (i>0) {
jsTPL(g)();
i--;
}
i = _end();
global += i;
console.log("Execution time: ", i ,"ms");
return 1;
});
benchmarks.push(function () {
console.info("Incrementing speed test");
var inc_g = "1{%= 2%}3",
inc_e = "123",
g="",
e="",
got;
var i = 100;
_start();
while (i>0) {
if (( got = jsTPL(g+=inc_g)()) !== (e+=inc_e))
return;
i--;
}
i= _end();
console.log("Execution time: ", i ,"ms");
global += i;
return 1;
});
if ($testBlock) {
benchmarks.push(function () {
try {
console.info("Testing jquery.block.js");
console.info("Simple");
_start();
var got = jsTPL("1{%block 'a'%}2{%/block%}3")(),
expected = "123";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.error(e);
}
});
benchmarks.push(function () {
try {
console.info("Attachment testing");
_start();
jsTPL("1{%block 'a'%}2{%/block%}3",[],"a");
var got = jsTPL("{%extends 'a'%}{%block 'a'%}A{%/block%}")(),
expected = "1A3";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.error(e);
}
});
benchmarks.push(function () {
try {
console.info("Attachment with junk");
_start();
jsTPL("1{%block 'a'%}2{%/block%}3",[],"a");
var got = jsTPL("{%extends 'a'%}asdfgas{%block 'a'%}A{%/block%}asdfasd")(),
expected = "1A3";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.error(e);
}
});
benchmarks.push(function () {
try {
console.info("Three different blocks and single attachment");
_start();
jsTPL("1{%block 'a'%}2{%/block%}3{%block 'b'%}4{%/block%}5{%block 'c'%}6{%/block%}7",[],"a");
var got = jsTPL("{%extends 'a'%}{%block 'a'%}A{%/block%}")(),
expected = "1A34567";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.error(e);
}
});
benchmarks.push(function () {
try {
console.info("Three different attached blocks");
_start();
jsTPL("1{%block 'a'%}2{%/block%}3{%block 'b'%}4{%/block%}5{%block 'c'%}6{%/block%}7",[],"a");
var got = jsTPL("{%extends 'a'%}{%block 'a'%}A{%/block%}{%block 'b'%}B{%/block%}{%block 'c'%}C{%/block%}")(),
expected = "1A3B5C7";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.error(e);
}
});
benchmarks.push(function () {
try {
console.info("Three different attached blocks with junk");
_start();
jsTPL("1{%block 'a'%}2{%/block%}3{%block 'b'%}4{%/block%}5{%block 'c'%}6{%/block%}7",[],"a");
var got = jsTPL("{%extends 'a'%}asdfgasd{%block 'a'%}A{%/block%}asdgs{%block 'b'%}B{%/block%}asgsdgsd{%block 'c'%}C{%/block%}asgasg")(),
expected = "1A3B5C7";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Three repeated blocks");
_start();
jsTPL("1{%block 'a'%}2{%/block%}3{%block 'a'%}4{%/block%}5{%block 'a'%}6{%/block%}7",[],"a");
var got = jsTPL("{%extends 'a'%}")(),
expected = "1234567";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Three repeated blocks with one attachment");
_start();
jsTPL("1{%block 'a'%}2{%/block%}3{%block 'a'%}4{%/block%}5{%block 'a'%}6{%/block%}7",[],"a");
var got = jsTPL("{%extends 'a'%}{%block 'a'%}_{%/block%}")(),
expected = "1_3_5_7";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Three repeated blocks with two attachments");
_start();
jsTPL("1{%block 'a'%}2{%/block%}3{%block 'a'%}4{%/block%}5{%block 'a'%}6{%/block%}7",[],"a");
var got = jsTPL("{%extends 'a'%}{%block 'a'%}_{%/block%}{%block 'a'%}-{%/block%}")(),
expected = "1_3-5-7";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Three repeated blocks with three attachments");
_start();
jsTPL("1{%block 'a'%}2{%/block%}3{%block 'a'%}4{%/block%}5{%block 'a'%}6{%/block%}7",[],"a");
var got = jsTPL("{%extends 'a'%}{%block 'a'%}_{%/block%}{%block 'a'%}-{%/block%}{%block 'a'%}${%/block%}")(),
expected = "1_3-5$7";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
try {
console.info("Three repeated blocks with three attachments and passed vars");
_start();
jsTPL("{%= x%}{%block 'a'%}2{%/block%}3{%block 'a'%}4{%/block%}5{%block 'a'%}6{%/block%}{%= x%}",["x"],"a");
var got = jsTPL("{%extends 'a',{x : '#'}%}{%block 'a'%}_{%/block%}{%block 'a'%}-{%/block%}")(),
expected = "#_3-5-#";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.log(e);
}
});
benchmarks.push(function () {
console.info("Similar speed test");
var inc_g = "1{%block 'a'%}2{%/block%}3",
inc_e = "123",
g="",
e="";
for (var i=1;i<400;i++) {
g+=inc_g;
e+=inc_e;
}
if (jsTPL(g)() !== e)
return;
i=50;
_start();
while (i>0) {
jsTPL(g)();
i--;
}
i = _end();
global += i;
console.log("Execution time: ", i ,"ms");
return 1;
});
benchmarks.push(function () {
console.info("Incrementing speed test");
var inc_g = "1{%block 'a'%}2{%/block%}3",
inc_e = "123",
g="",
e="",
got;
var i = 70;
_start();
while (i>0) {
if (( got = jsTPL(g+=inc_g)()) !== (e+=inc_e))
return;
i--;
}
i= _end();
console.log("Execution time: ", i ,"ms");
global += i;
return 1;
});
benchmarks.push(function () {
console.info("Similar speed test with attachments");
var
inc_e = "123",
g1="",
g2="{%extends 'a'%}",
e="";
for (var i=1;i<100;i++) {
g1+="1{%block 'a"+i+"'%}2{%/block%}3";
g2+="{%block 'a"+i+"'%}2{%/block%}";
e+=inc_e;
}
jsTPL(g1, [], "a");
if (( g1 = jsTPL(g2)()) !== e)
return console.log(g1);
i=70;
_start();
while (i>0) {
jsTPL(g2)();
i--;
}
i = _end();
global += i;
console.log("Execution time: ", i ,"ms");
return 1;
});
}
if ($testFilter) {
benchmarks.push(function () {
try {
console.info("Testing jquery.filter.js");
console.info("Simple");
_start();
var got = jsTPL("0{%filter nop%}123{%/filter%}4")(),
expected = "01234";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.error(e);
}
});
benchmarks.push(function () {
try {
console.info("Escape");
_start();
var got = jsTPL("0{%filter escape%}:{%/filter%}1")(),
expected = "0%3A1";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.error(e);
}
});
benchmarks.push(function () {
try {
console.info("Safe");
_start();
var got = jsTPL("0{%filter safe%}<>&\"'{%/filter%}1")(),
expected = "0&lt;&gt;&amp;&quot;&#039;1";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.error(e);
}
});
benchmarks.push(function () {
try {
console.info("Upper");
_start();
var got = jsTPL("0{%filter upper%}abc{%/filter%}1")(),
expected = "0ABC1";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.error(e);
}
});
benchmarks.push(function () {
try {
console.info("Lower");
_start();
var got = jsTPL("0{%filter lower%}ABC{%/filter%}1")(),
expected = "0abc1";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.error(e);
}
});
benchmarks.push(function () {
try {
console.info("Hierarchical");
_start();
var got = jsTPL("0{%filter escape%}{%filter safe%}<>{%/filter%}{%/filter%}1")(),
expected = "0%26lt%3B%26gt%3B1";
global+=_end();
if (got === expected) {
console.log("ok");
return 1;
} else
console.error("error", "Got:",got,"Expected:",expected);
} catch (e) {
console.error(e);
}
});
}
run(0);
</script>
<title>jQuery.tpl test case</title>
</head>
<body>
</body>
</html>
Jump to Line
Something went wrong with that request. Please try again.