Skip to content

Commit

Permalink
test drive the peg grunt task functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
dvberkel committed Nov 18, 2012
1 parent e0298b4 commit 0c57dc4
Show file tree
Hide file tree
Showing 3 changed files with 259 additions and 12 deletions.
15 changes: 11 additions & 4 deletions tasks/peg.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* Licensed under the MIT license.
*/

var PEG = require('pegjs');

module.exports = function(grunt) {

// Please see the grunt documentation for more information regarding task and
Expand All @@ -15,16 +17,21 @@ module.exports = function(grunt) {
// TASKS
// ==========================================================================

grunt.registerTask('peg', 'Your task description goes here.', function() {
grunt.log.write(grunt.helper('peg'));
grunt.registerMultiTask('peg', 'Generate a parser from a PEG grammar', function() {
grunt.log.write(grunt.template.process("Generating parser from <%= grammar %>", this.data));
var grammar = grunt.file.read(this.data.grammar);
grunt.file.write(this.data.outputFile, grunt.helper('peg', grammar, this.data.exportVar));
});

// ==========================================================================
// HELPERS
// ==========================================================================

grunt.registerHelper('peg', function() {
return 'peg!!!';
grunt.registerHelper('peg', function(grammar, exportVar) {
exportVar = exportVar || "module.exports";
var parser = PEG.buildParser(grammar);

return exportVar + " = " + parser.toSource() + ";";
});

};
15 changes: 7 additions & 8 deletions test/peg_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ var grunt = require('grunt');
*/

exports['peg'] = {
setUp: function(done) {
// setup here
done();
},
// setUp: function(done) {
// grunt.file.write('test_data/a.js', grunt.helper('peg', "start = 'a'"));
// done();
// },
'helper': function(test) {
test.expect(1);
// tests here
test.equal(grunt.helper('peg'), 'peg!!!', 'should return the correct value.');
var expected = grunt.file.read('test_data/a.js')
test.equal(grunt.helper('peg', "start = 'a'"), expected, 'should return a proper grammar');
test.done();
}
},
};
241 changes: 241 additions & 0 deletions test_data/a.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0c57dc4

Please sign in to comment.