readImports-test.js (3638B)
1 var vows = require("vows"), 2 assert = require("assert"), 3 smash = require("../"); 4 5 var suite = vows.describe("smash.readImports"); 6 7 suite.addBatch({ 8 "readImports": { 9 "on a file with no imports": { 10 topic: function() { 11 smash.readImports("test/data/foo.js", this.callback); 12 }, 13 "returns the empty array": function(imports) { 14 assert.deepEqual(imports, []); 15 } 16 }, 17 "on a file with imports with trailing comments": { 18 topic: function() { 19 smash.readImports("test/data/trailing-comment-import.js", this.callback); 20 }, 21 "returns the empty array": function(imports) { 22 assert.deepEqual(imports, ["test/data/foo.js", "test/data/bar.js"]); 23 } 24 }, 25 "on a file with invalid import syntax": { 26 topic: function() { 27 var callback = this.callback; 28 smash.readImports("test/data/invalid-import-syntax.js", function(error) { 29 callback(null, error); 30 }); 31 }, 32 "throws an error with the expected message": function(error) { 33 assert.deepEqual(error.message, "invalid import: test/data/invalid-import-syntax.js:0: import foo;"); 34 } 35 }, 36 "on a file with that imports a file that does not exist": { 37 topic: function() { 38 smash.readImports("test/data/imports-not-found.js", this.callback); 39 }, 40 "returns the expected import": function(imports) { 41 assert.deepEqual(imports, ["test/data/not-found.js"]); 42 } 43 }, 44 "on a file with a commented-out import": { 45 topic: function() { 46 smash.readImports("test/data/commented-import.js", this.callback); 47 }, 48 "ignores the commented-out input": function(imports) { 49 assert.deepEqual(imports, []); 50 } 51 }, 52 "on a file with a not-commented-out import": { 53 topic: function() { 54 smash.readImports("test/data/not-commented-import.js", this.callback); 55 }, 56 "does not ignore the not-commented-out import": function(imports) { 57 assert.deepEqual(imports, ["test/data/foo.js"]); 58 } 59 }, 60 "on a file with one import": { 61 topic: function() { 62 smash.readImports("test/data/imports-foo.js", this.callback); 63 }, 64 "returns the expected import": function(imports) { 65 assert.deepEqual(imports, ["test/data/foo.js"]); 66 } 67 }, 68 "on a file with multiple imports": { 69 topic: function() { 70 smash.readImports("test/data/imports-foo-bar-baz.js", this.callback); 71 }, 72 "returns the expected imports, in order": function(imports) { 73 assert.deepEqual(imports, ["test/data/foo.js", "test/data/bar.js", "test/data/baz.js"]); 74 } 75 }, 76 "on a file with multiple redundant imports": { 77 topic: function() { 78 smash.readImports("test/data/imports-foo-foo-bar-foo.js", this.callback); 79 }, 80 "returns all imports, in order": function(imports) { 81 assert.deepEqual(imports, ["test/data/foo.js", "test/data/foo.js", "test/data/bar.js", "test/data/foo.js"]); 82 } 83 }, 84 "on a file with nested imports": { 85 topic: function() { 86 smash.readImports("test/data/imports-imports-foo.js", this.callback); 87 }, 88 "returns the expected imports, in order": function(imports) { 89 assert.deepEqual(imports, ["test/data/imports-foo.js"]); 90 } 91 }, 92 "on a file that imports itself": { 93 topic: function() { 94 smash.readImports("test/data/imports-self.js", this.callback); 95 }, 96 "returns the expected import": function(imports) { 97 assert.deepEqual(imports, ["test/data/imports-self.js"]); 98 } 99 } 100 } 101 }); 102 103 suite.export(module);