Skip to content

Commit

Permalink
spec runner modified to run all the test with render, stream and pipe.
Browse files Browse the repository at this point in the history
  • Loading branch information
jairodemorais committed Jun 28, 2012
1 parent 9d133f6 commit 8c0e05c
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 12 deletions.
30 changes: 26 additions & 4 deletions dist/dust-core-0.6.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,17 @@ Stream.prototype.on = function(type, callback) {
return this;
};

Stream.prototype.pipe = function(stream) {
this.on("data", function(data) {
stream.write(data, "utf8");
}).on("end", function() {
stream.end();
}).on("error", function(err) {
stream.error(err);
});
return this;
};

function Chunk(root, next, taps) {
this.root = root;
this.next = next;
Expand Down Expand Up @@ -541,6 +552,16 @@ if (typeof exports !== "undefined") {
}
(function(dust){

/* make a safe version of console if it is not available
* currently supporting:
* _console.log
* */
var _console = (typeof console !== 'undefined')? console: {
log: function(){
/* a noop*/
}
};

function isSelect(context) {
var value = context.current();
return typeof value === "object" && value.isSelect === true;
Expand Down Expand Up @@ -600,7 +621,10 @@ var helpers = {
idx: function(chunk, context, bodies) {
return bodies.block(chunk, context.push(context.stack.index));
},

contextDump: function(chunk, context, bodies) {
_console.log(JSON.stringify(context.stack));
return chunk;
},
"if": function( chunk, context, bodies, params ){
if( params && params.cond ){
var cond = params.cond;
Expand All @@ -626,9 +650,7 @@ var helpers = {
}
// no condition
else {
if( typeof window !== 'undefined' && window.console ){
window.console.log( "No expression given!" );
}
_console.log( "No expression given!" );
}
return chunk;
},
Expand Down
30 changes: 26 additions & 4 deletions dist/dust-full-0.6.0.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,17 @@ Stream.prototype.on = function(type, callback) {
return this;
};

Stream.prototype.pipe = function(stream) {
this.on("data", function(data) {
stream.write(data, "utf8");
}).on("end", function() {
stream.end();
}).on("error", function(err) {
stream.error(err);
});
return this;
};

function Chunk(root, next, taps) {
this.root = root;
this.next = next;
Expand Down Expand Up @@ -541,6 +552,16 @@ if (typeof exports !== "undefined") {
}
(function(dust){

/* make a safe version of console if it is not available
* currently supporting:
* _console.log
* */
var _console = (typeof console !== 'undefined')? console: {
log: function(){
/* a noop*/
}
};

function isSelect(context) {
var value = context.current();
return typeof value === "object" && value.isSelect === true;
Expand Down Expand Up @@ -600,7 +621,10 @@ var helpers = {
idx: function(chunk, context, bodies) {
return bodies.block(chunk, context.push(context.stack.index));
},

contextDump: function(chunk, context, bodies) {
_console.log(JSON.stringify(context.stack));
return chunk;
},
"if": function( chunk, context, bodies, params ){
if( params && params.cond ){
var cond = params.cond;
Expand All @@ -626,9 +650,7 @@ var helpers = {
}
// no condition
else {
if( typeof window !== 'undefined' && window.console ){
window.console.log( "No expression given!" );
}
_console.log( "No expression given!" );
}
return chunk;
},
Expand Down
2 changes: 2 additions & 0 deletions lib/dust.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ Stream.prototype.pipe = function(stream) {
stream.write(data, "utf8");
}).on("end", function() {
stream.end();
}).on("error", function(err) {
stream.error(err);
});
return this;
};
Expand Down
2 changes: 1 addition & 1 deletion test/jasmine-test/client/specRunner.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<script src="../spec/renderTestSpec.js" type="text/javascript" charset="utf-8"></script>

<!-- include source files here... -->
<script src="../../../dist/dust-full-0.4.0.js" type="text/javascript" charset="utf-8"></script>
<script src="../../../dist/dust-full-0.6.0.js" type="text/javascript" charset="utf-8"></script>

<script type="text/javascript">
(function() {
Expand Down
86 changes: 83 additions & 3 deletions test/jasmine-test/spec/renderTestSpec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
describe ("Test the basic functionality of dust", function() {
for (var index = 0; index < dustExamples.length; index++) {
var test = dustExamples[index];
it (test.message, render(test));
it ("RENDER: " + test.message, render(test));
it ("STREAM: " + test.message, stream(test));
it ("PIPE: " + test.message, pipe(test));
};
});

function render(test) {
function render(test) {
return function() {
try {
dust.loadSource(dust.compile(test.source, test.name));
Expand All @@ -18,4 +20,82 @@ function render(test) {
expect(test.error || {} ).toEqual(error.message);
}
};
}
}

function stream(test) {
return function() {
var output ="", flag;
runs(function(){
flag = false;
output = "";
try {
dust.loadSource(dust.compile(test.source, test.name));
dust.stream(test.name, test.context)
.on("data", function(data) {
output += data;
})
.on("end", function() {
flag = true;
})
.on("error", function(err) {
output = err.message;
});
} catch(error) {
output = error.message;
flag= true;
}
});

waitsFor(function(){
return flag;
}, "the output", 500);

runs(function(){
if (test.error) {
expect(test.error || {} ).toEqual(output);
} else {
expect(test.expected).toEqual(output);
}
});
}
};

function pipe(test) {
return function() {
var output, flag;
runs(function(){
flag = false;
output = "";
try {
dust.loadSource(dust.compile(test.source, test.name));
dust.stream(test.name, test.context).pipe({
write: function (data) {
output += data;
},
end: function () {
flag = true;
},
error: function (err) {
flag = true;
output = err.message;
}
});
} catch(error) {
output = error.message;
flag= true;
}
});

waitsFor(function(){
return flag;
}, "the output", 500);

runs(function(){
if (test.error) {
expect(test.error || {} ).toEqual(output);
} else {
expect(test.expected).toEqual(output);
}
});
}
};

0 comments on commit 8c0e05c

Please sign in to comment.