<!DOCTYPE html>
<html>
<head>
<title>Yote Template Tests</title>
<style>
.fail { color:red }
.pass { color:green }
body { background-color:wheat }
.hide { display:none }
</style>
<script src="/yote/js/jquery-2.1.0.js"></script>
<script src="/yote/js/jquery.base64.min.js"></script>
<script src="/yote/js/jquery.cookie.js"></script>
<script src="/yote/js/json2.js"></script>
<script src="/yote/js/yote.js"></script>
<script src="/yote/js/yote.templates.js"></script>
<script>
<!--
var tests = 0;
var failed = 0;
var calls_made = 0;
$.yote.old_message = $.yote.message;
$.yote.message = function( params ) {
calls_made++;
return $.yote.old_message( params );
}
function fail( test ) {
return function(d) {
++failed;
++tests;
$('#tests').append( "<span class=fail>FAILED : </span>" + test + '<BR>' );
}
}
function pass( test ) {
return function(d) {
++tests;
$('#tests').append( "<span class=pass>PASSED : </span>" + test + '<BR>' );
}
}
function msg( txt ) {
$('#tests').append( txt + '<BR>' );
}
function is( result, expected, msg ) {
if( result === expected ) {
pass( msg )();
}
else {
fail( msg + "( expected '" + expected + "' and got '" + result +"')" )();
}
}
function ok( result, msg ) {
if( result === true ) {
pass( msg )();
}
else {
fail( msg )();
}
}
function wrap_up() {
$( '#results' ).append( "<BR>Calls made " + calls_made + "<BR>" );
if( failed > 0 ) {
$('#results').append( "Failed " + failed + " tests out of " + tests );
} else {
$('#results').append( "Passed all " + tests + " tests" );
}
var d = new Date();
var t2 = d.getTime();
var d2 = new Date();
$('#tests').append( "<br>tests completed in " + Math.round(d2.getTime() - t2) + " ms" );
} //wrap_up
var step_count = 0;
function step( msg ) {
console.log( "step " + step_count + ':' + msg );
step_count++;
}
$().ready(function(){
/*
Test the following template parts
<# comments go here #>
<??? function( ctx ) ...
- test ability to set values in the context
- test ability to return other filled in templates
<$$$ set varname value $$$>
- value can be text or a context variable
<$$$ set varname function( ctx ) ...
<$$$ control controlname html
<?? function( ctx ) ....
<$$ template
<@ template list max_rows @>
<% template list max_rows %>
<$ var $>
<$ _index_ $>
<$ _hashkey_ $>
<? function( ctx ) ... ( after render )
*/
$.yote.templates.import_templates( '/templates/default_templates.html' );
function do_tests() {
// init yote
$.yote.templates.debug = true;
$.yote.init('Yote::Test::Template');
$.yote.logout();
$.yote.reinit();
$.yote.debug = true;
try {
var app = $.yote.fetch_default_app();
app.reset();
var sandbox = app.get_sandbox();
sandbox.set( 'name', 'sandy' );
$.yote.templates.init();
$.yote.templates.refresh();
is( $.yote.templates.fill_template( 'Comments' ).trim(), 'This template has comments', '$.yote.templates.fill_template works' );
is( $( '#comments' ).html().trim(), 'This template has comments', 'automatic filling of template variables' );
is( $( '#comments' ).html().trim(), 'This template has comments', '<# comment template #>' );
is( $.yote.templates.fill_template( 'TripleQuest' ).trim().replace( /[\n\s]+/g, ' '), 'A very simple test This template has comments Number 4 WAZ ZBorgs around', '< ??? function ??\?> effect on building templates, effect on using $$$ to create variables and effect of setting values directly in the context' );
is( $.yote.templates.fill_template( 'SimpleTemplate' ).trim().replace( /[\n\s]+/g, ' '), 'A very simple test This template has comments', '<$$ templatename $$>' );
is( $.yote.templates.fill_template( 'VarTest' ).trim().replace( /[\n\s]+/g, ' '),
'Give me arguments alpha beta zappa wack Tootwack',
'<$$ templatename args $$>, <$$$ set varname "value" $$$> and <$$$ set varname function $$$>' );
is( $.yote.templates.fill_template( 'ArrayTest' ).trim().replace( /[\n\s]+/g, ' '), 'ARRAY 0 : A 1 : C 2 : B Apple : butter Daikon : radish Lemon : yellow YARRA', 'tests <$$$ set varname function(args) { that returns a list } $$$>, <@ template list maxrows @>, <$ _index_ $> <$ _hashkey_ $> and <$ _ $> ' );
is( $.yote.templates.fill_template( 'SandBox' ).trim().replace( /[\n\s]+/g, ' '), 'sandy sandy sandy', '<$ app.sandbox.name $>' );
is( $.yote.templates.fill_template( 'Vars' ).trim().replace( /[\n\s]+/g, ' '), 'My name is unknown', 'default value for get' );
is( $( '#after_render' ).text().trim().replace( /[\n\s]+/g, ' '), 'TEST', 'after render function' );
// is( $.yote.templates.fill_template( '' ).trim().replace( /[\n\s]+/g, ' '), '', '' );
}
catch( err ) {
fail( err )();
wrap_up();
}
wrap_up();
} //do_tests
do_tests();
} ); //ready
-->
</script>
</head>
<templates>
<script type="text/template" class="yote_template_definition" template_name="AfterRender">
<$$$ control ar_div <div></div>$$$>
<? function( ctx ) { $( ctx.controls.ar_div ).append( "TEST" ) } ?>
</script>
<script type="text/template" class="yote_template_definition" template_name="Vars">
My name is <$ name unknown $>
</script>
<script type="text/template" class="yote_template_definition" template_name="SandBox">
<$$$ set app function( ctx ) {
return $.yote.fetch_default_app();
} $$$>
<$$$ set sandbox function( ctx ) {
return $.yote.fetch_default_app().get_sandbox();
} $$$>
<$ app.sandbox.name $>
<$ _app_.sandbox.name $>
<$ sandbox.name $>
</script>
<script type="text/template" class="yote_template_definition" template_name="ArrayRow">
<$ _index_ $> : <$ _ $>
</script>
<script type="text/template" class="yote_template_definition" template_name="HashRow">
<$ _hashkey_ $> : <$ _ $>
</script>
<script type="text/template" class="yote_template_definition" template_name="ArrayTest">
<$$$ set array function( ctx ) { return [ "A", "C", "B", "D", "E" ]; } $$$>
<$$$ set hash function( ctx ) { return { Apple : 'butter', Spinach : 'wild', Daikon : 'radish', Lemon : 'yellow', Pea : 'green'}; } $$$>
ARRAY
<@ ArrayRow @array 3 @>
<% HashRow %hash 3 %>
YARRA
</script>
<script type="text/template" class="yote_template_definition" template_name="VarTestPartTwo">
<$$$ set e x $$$>
<?? function( ctx ) { return ctx.args.join(" ").toLowerCase() + ' ' + ctx.get( 'x' ) + ' ' + ctx.get( 'y' ) + ctx.get('e'); } ??>
</script>
<script type="text/template" class="yote_template_definition" template_name="VarTest">
<$$$ set x "wack" $$$>
<$$$ set y function( args ) { return "Toot"; } $$$>
Give me arguments <$$ VarTestPartTwo 'Alpha' 'Beta' 'Zappa' $$>
</script>
<script type="text/template" class="yote_template_definition" template_name="TripleQuest">
<??? function( ctx ) { ctx.set( 'foo', 1 + 3 ); return '<$$$ set fool "Borgs around"$$$>' + "<$$$ set bar 'WAZ Z' $$$><$$ SimpleTemplate $$>" } ???>
<?? function( ctx ) { return "Number " + ctx.get( 'foo' ) + ' ' + ctx.get( 'bar' ) + ctx.get( 'fool' ); } ??>
</script>
<script type="text/template" class="yote_template_definition" template_name="SimpleTemplate">
A very simple test <$$ Comments $$>
</script>
<script type="text/template" class="yote_template_definition" template_name="Comments">
This template<# doesn't #> has comments <# I'd say #>
</script>
<script type="text/template" class="yote_template_definition" template_name="Ed">
Edit field <$$ edit _app_.sandbox 'wistle' $$> UM
</script>
</templates>
<body>
<h1>Yote Template Tests</h1>
<div id="comments" class="yote_template hide" template="Comments"></div>
<div id="after_render" class="yote_template hide" template="AfterRender"></div>
<div id="eddy" class="yote_template" template="Ed"></div>
<div id="tests"></div>
<div id="results"></div>
</body>
</html>