l0bsterssg

node.js static responsive blog post generator
Log | Files | Refs | README

smash-test.js (3426B)


      1 var vows = require("vows"),
      2     assert = require("assert"),
      3     fs = require("fs"),
      4     stream = require("stream"),
      5     smash = require("../");
      6 
      7 var suite = vows.describe("smash");
      8 
      9 suite.addBatch({
     10   "smash": {
     11     "on a file with no imports": testCase(["test/data/foo.js"], "test/data/foo.js"),
     12     "on a file with imports with trailing comments": testCase(["test/data/trailing-comment-import.js"], "test/data/trailing-comment-import-expected.js"),
     13     "on a file with invalid import syntax": testFailureCase(["test/data/invalid-import-syntax.js"], "invalid import: test/data/invalid-import-syntax.js:0: import foo;"),
     14     "on a file with that imports a file that does not exist": testFailureCase(["test/data/imports-not-found.js"], "ENOENT, open 'test/data/not-found.js'"),
     15     "on a file with a commented-out import": testCase(["test/data/commented-import.js"], "test/data/commented-import.js"),
     16     "on a file with a not-commented-out import": testCase(["test/data/not-commented-import.js"], "test/data/not-commented-import-expected.js"),
     17     "on a file with one import": testCase(["test/data/imports-foo.js"], "test/data/imports-foo-expected.js"),
     18     "on a file with multiple imports": testCase(["test/data/imports-foo-bar-baz.js"], "test/data/imports-foo-bar-baz-expected.js"),
     19     "on a file with nested imports": testCase(["test/data/imports-imports-foo.js"], "test/data/imports-imports-foo-expected.js"),
     20     "on a file with empty lines": testCase(["test/data/empty-lines.js"], "test/data/empty-lines.js"),
     21     "on a file which imports a file with empty lines": testCase(["test/data/import-empty-lines.js"], "test/data/empty-lines.js"),
     22     "on multiple input files": testCase(["test/data/foo.js", "test/data/bar.js", "test/data/baz.js"], "test/data/imports-foo-bar-baz-expected.js"),
     23     "with redundant input files": testCase(["test/data/foo.js", "test/data/foo.js"], "test/data/foo.js"),
     24     "on a file with multiple redundant imports": testCase(["test/data/imports-foo-foo-bar-foo.js"], "test/data/imports-foo-foo-bar-foo-expected.js"),
     25     "when a file imports itself": testCase(["test/data/imports-self.js"], "test/data/foo.js"),
     26     "when circular imports are encountered": testCase(["test/data/imports-circular-foo.js"], "test/data/imports-circular-foo-expected.js"),
     27     "when the input is a directory": testCase(["test/data/"], "test/data/index.js"),
     28     "when the input is missing a file extension": testCase(["test/data/imports-index"], "test/data/index.js")
     29   }
     30 });
     31 
     32 suite.export(module);
     33 
     34 function testCase(inputs, expected) {
     35   return {
     36     topic: function() {
     37       smash(inputs).pipe(testStream(this.callback));
     38     },
     39     "produces the expected output": function(actual) {
     40       assert.deepEqual(actual, fs.readFileSync(expected, "utf8"));
     41     }
     42   };
     43 }
     44 
     45 function testFailureCase(inputs, expected) {
     46   return {
     47     topic: function() {
     48       var callback = this.callback;
     49       smash(inputs).on("error", function(error) {
     50         callback(null, error);
     51       });
     52     },
     53     "produces the expected error message": function(error) {
     54       assert.deepEqual(error.message, expected);
     55     }
     56   };
     57 }
     58 
     59 function testStream(callback) {
     60   var s = new stream.Writable, chunks = [];
     61 
     62   s._write = function(chunk, encoding, callback) {
     63     chunks.push(chunk);
     64     callback();
     65   };
     66 
     67   s.on("error", callback);
     68   s.on("finish", function() { callback(null, Buffer.concat(chunks).toString("utf8")); });
     69   return s;
     70 }