Skip to content

Commit

Permalink
basic tests, more fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
khrome committed Dec 7, 2012
1 parent e91c4be commit 708fe47
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 39 deletions.
6 changes: 6 additions & 0 deletions Panels/foreach.panel.tpl
@@ -0,0 +1,6 @@
<h2>{$test}</h2>
<ul>
{foreach from="$list" item="value" key="name"}
<li><a>{$name}</a><b>{$value}</b></li>
{/foreach}
</ul>
6 changes: 6 additions & 0 deletions Panels/if.panel.tpl
@@ -0,0 +1,6 @@
<h2>{$test}</h2>
<ul>
{foreach from="$list" item="value" key="name"}
<li><a>{$name}</a><b>{$value}</b></li>
{/foreach}
</ul>
File renamed without changes.
29 changes: 16 additions & 13 deletions protolus-templates.js
Expand Up @@ -40,8 +40,11 @@ array.combine = function(thisArray, thatArray){ //parallel
});
return result;
};
array.contains = function(haystack, needle){ //parallel
return haystack.indexOf(needle) != -1;
};
prime.keys = function(object){
result = [];
var result = [];
for(var key in object) result.push(key);
return result;
}
Expand Down Expand Up @@ -217,12 +220,12 @@ Templates.TagParser = new Class({ //my ultra-slim tag parser
//console.log('close opening tag');
this.open(tag);
tagStack.push(tag);
literalMode = this.literalTags.contains(tag.name);
literalMode = array.contains(this.literalTags, tag.name);
if(literalMode) literal = tag.name;
if(
currentTag[currentTag.length-1] == this.closeEscape ||
this.unaryTags.contains(tag.name) ||
(this.unrecognized == 'unary' && !recognizedTags.contains(tag.name))
array.contains(this.unaryTags, tag.name) ||
(this.unrecognized == 'unary' && !array.contains(recognizedTags, tag.name))
){
this.close(tag);
var lastTag = tagStack.pop();
Expand Down Expand Up @@ -277,7 +280,7 @@ Templates.TagParser = new Class({ //my ultra-slim tag parser
continue;
}
}else{
if(this.attributeDelimiters.contains(ch)){
if(array.contains(this.attributeDelimiters, ch)){
inQuote = ch;
continue;
}
Expand All @@ -294,7 +297,7 @@ Templates.TagParser = new Class({ //my ultra-slim tag parser
currentValue = '';
}else currentValue += ch;
}
this.attributeDelimiters.contains(ch)
array.contains(this.attributeDelimiters, ch)
}else{
if(ch == ' '){
tagName = currentValue;
Expand Down Expand Up @@ -431,15 +434,15 @@ Templates.Template.Smarty = new Class({
if(!key) key = 'key';
if(from.substring(0,1) == '$') from = from.substring(1);
from = this.getVariable(from);
var func = function(value, index){
var func = fn.bind(function(value, index){
this.set(key, index);
this.set(item, value);
node.children.each(function(child){
array.forEach(node.children, fn.bind(function(child){
res += this.renderNode(child);
}.bind(this));
}.bind(this);
if(typeOf(from) == 'object') Object.each(from, func);
else from.each(func);
}, this));
}, this);
if(type(from) == 'object') prime.each(from, func);
else array.forEach(from, func);
return res;
break;
case 'page':
Expand Down Expand Up @@ -743,7 +746,7 @@ Templates.Panel = new Class({
}
},
fetch : function(file, callback, error){
Templates.load('./'+file, function(err, data){
Templates.load('.'+file, function(err, data){
if(err){
if(error) error(err);
else throw(err);
Expand Down
68 changes: 42 additions & 26 deletions test.js
@@ -1,37 +1,53 @@
var should = require("should");
var request = require('request');
var http = require('http');
var port = 221;
var resource = require('./protolus-resource');

describe('ProtolusResource', function(){
describe('Simple \'test-component\' tests', function(){
var server;
var running = false;
before(function(done){
try{
server = http.createServer(function(req, res) {
resource.handleResourceCalls(req, res, function(){
//serve a page
});
}).listen(port);
server.on("listening", function() {
running = true;
describe('Panel tests', function(){
var Templates = require('./protolus-templates');
before(function(){
Templates({ templateDirectory : '/Panels' });
});

it('Basic render', function(done){
new Templates.Panel('simple', function(panel){
panel.render({} , function(html){
html.indexOf('OMG').should.not.equal(-1);
done();
});
}catch(ex){
should.not.exist(ex);
}
});
});

it('Server Runs', function(){
should.equal(running, true);
it('Basic if', function(done){
/*new Templates.Panel('foreach', function(panel){
var data = {
test : 'blah'
};
panel.render(data , function(html){
html.indexOf('YES').should.not.equal(-1);
done();
});
});*/
});

it('Should perform simple, macroless render');

after(function(done) {
server.close();
done();
it('Basic foreach', function(done){
new Templates.Panel('foreach', function(panel){
var data = {
test : 'blah',
list : [
'foo',
'bar',
'baz'
]
};
panel.render(data , function(html){
html.indexOf('<h2>'+data.test+'</h2>').should.not.equal(-1);
data.list.forEach(function(value, key){
html.indexOf('<a>'+key+'</a>').should.not.equal(-1);
html.indexOf('<b>'+value+'</b>').should.not.equal(-1);
});
done();
});
});
});

});
});

0 comments on commit 708fe47

Please sign in to comment.