literate-jasmine
Advanced tools
Comparing version 0.0.23 to 0.0.25
{ | ||
"name": "literate-jasmine", | ||
"description": "write jasmine tests in markdown", | ||
"version": "0.0.23", | ||
"version": "0.0.25", | ||
"bin": { | ||
@@ -34,4 +34,5 @@ "literate-jasmine": "./bin/literate-jasmine" | ||
"readme": "./bin/literate-jasmine README.md", | ||
"integration": "node spec/integration_it.js && node spec/integration_describe.js && node spec/integration_root.js" | ||
"integration": "node spec/integration_spec.js", | ||
"suite": "npm test && npm run integration" | ||
} | ||
} |
@@ -15,4 +15,2 @@ # literate-jasmine [![Build Status](https://travis-ci.org/cymen/literate-jasmine.png?branch=master)](https://travis-ci.org/cymen/literate-jasmine) | ||
console.log(a, b, a + b); | ||
expect(a + b).toBe(3); | ||
@@ -19,0 +17,0 @@ |
@@ -1,2 +0,2 @@ | ||
var parser = require('../src/parser'), | ||
var Parser = require('../src/parser'), | ||
fs = require('fs'), | ||
@@ -6,2 +6,6 @@ markdown = require('markdown').markdown; | ||
describe('parser', function() { | ||
beforeEach(function() { | ||
parser = new Parser("fileName"); | ||
}); | ||
describe('code block(s)', function() { | ||
@@ -80,3 +84,3 @@ it('parses out the code block', function() { | ||
it('parses out the name', function() { | ||
var parsedIt = parser.parseIt(tree, 1); | ||
var parsedIt = parser.parseSpec(tree, 1); | ||
@@ -87,3 +91,3 @@ expect(parsedIt.name).toBe('it 1'); | ||
it('parses out the code', function() { | ||
var parsedIt = parser.parseIt(tree, 1); | ||
var parsedIt = parser.parseSpec(tree, 1); | ||
@@ -95,3 +99,3 @@ expect(parsedIt.code).toBe('var x = 10;\nconsole.log("x", x);\nexpect(x).toBe(10);'); | ||
spyOn(console, 'log'); | ||
var parsedIt = parser.parseIt(tree, 1); | ||
var parsedIt = parser.parseSpec(tree, 1); | ||
@@ -137,3 +141,3 @@ parsedIt.fn(); | ||
expect(parsedDescribe.it.length).toBe(1); | ||
expect(parsedDescribe.spec.length).toBe(1); | ||
}); | ||
@@ -140,0 +144,0 @@ |
@@ -5,67 +5,49 @@ require('jasmine-node'); | ||
markdown = require('markdown').markdown, | ||
stackTraceParser = require('stack-trace-parser'), | ||
errorReporter = require('./error-reporter'), | ||
ROOT_LEVEL = 1, | ||
DESCRIBE_LEVEL = 2, | ||
IT_LEVEL = 3, | ||
parser; | ||
SPEC_LEVEL = 3; | ||
var require = require('./proxy-require')({ | ||
path: 'node_modules' | ||
}); | ||
var runsDone = new RegExp(/\sdone()/); | ||
var isHeader = function(node) { | ||
function isAsync(code) { | ||
return runsDone.test(code); | ||
} | ||
function isHeader(node) { | ||
return node[0] === 'header'; | ||
}; | ||
var isCodeBlock = function(node) { | ||
function isCodeBlock(node) { | ||
return node[0] === 'code_block'; | ||
}; | ||
var getName = function(node) { | ||
function getName(node) { | ||
return node[2]; | ||
}; | ||
var getLevel = function(node) { | ||
function getLevel(node) { | ||
return node[1].level; | ||
}; | ||
var makeFileNameRelative = function(fileName) { | ||
return fileName.replace(process.cwd(), '.'); | ||
}; | ||
var runExample = function(name, code, done) { | ||
function runExample(fileName, name, code, done) { | ||
try { | ||
return eval(code); | ||
} catch (exception) { | ||
parser.displayEvalException(exception, name, code); | ||
exception.message += ' (' + makeFileNameRelative(parser.fileName) + ')'; | ||
throw exception; | ||
throw errorReporter.display(fileName, exception, name, code); | ||
} | ||
} | ||
parser = { | ||
displayEvalException: function(exception, name, code) { | ||
var parsedStackTrace = stackTraceParser.parse(exception); | ||
if (parsedStackTrace[0].isEval) { | ||
console.log('\n'); | ||
console.log(exception.toString().red, 'thrown from', name.red, 'in', makeFileNameRelative(parser.fileName).red + ':'); | ||
console.log('. . . . .'); | ||
var errorOnLineNumber = parsedStackTrace[0].evalLineNumber - 1; | ||
code.split('\n').forEach(function(line, index) { | ||
if (index == errorOnLineNumber) { | ||
line = line.red; | ||
} | ||
console.log(line); | ||
}); | ||
console.log('. . . . .\n'); | ||
} | ||
}, | ||
function validNode(node, type, level) { | ||
return node[0] === type && node[1].level === level; | ||
}; | ||
run: function(name, code) { | ||
if (runsDone.test(code)) { | ||
return function(done) { runExample(name, code, done); } | ||
} else { | ||
return function() { runExample(name, code); } | ||
} | ||
}, | ||
var Parser = function(fileName) { | ||
this.fileName = fileName; | ||
parse: function(text, fileName) { | ||
this.parse = function(text) { | ||
var tree = markdown.parse(text), | ||
@@ -78,15 +60,13 @@ root = tree[1], | ||
this.fileName = fileName; | ||
if (!parser.validNode(root, 'header', ROOT_LEVEL)) { | ||
if (!validNode(root, 'header', ROOT_LEVEL)) { | ||
return; | ||
} | ||
complete.global = parser.parseCodeBlocks(tree, 1); | ||
complete.globalFn = parser.run(complete.name, complete.global); | ||
complete.global = this.parseCodeBlocks(tree, 1); | ||
complete.globalFn = this.run(complete.name, complete.global); | ||
for (var i=2; i < tree.length; i++) { | ||
for (var i = 2; i < tree.length; i++) { | ||
var node = tree[i]; | ||
if (parser.validNode(node, 'header', DESCRIBE_LEVEL)) { | ||
complete.describes.push(parser.parseDescribe(tree, i)); | ||
if (validNode(node, 'header', DESCRIBE_LEVEL)) { | ||
complete.describes.push(this.parseDescribe(tree, i)); | ||
} | ||
@@ -106,13 +86,22 @@ } | ||
return complete; | ||
}, | ||
}; | ||
parseDescribe: function(tree, offset) { | ||
this.run = function(name, code) { | ||
var fileName = this.fileName; | ||
if (isAsync(code)) { | ||
return function(done) { runExample(fileName, name, code, done); } | ||
} else { | ||
return function() { runExample(fileName, name, code); } | ||
} | ||
}; | ||
this.parseDescribe = function(tree, offset) { | ||
var node = tree[offset], | ||
parsedDescribe = { | ||
name: getName(node), | ||
it: [] | ||
spec: [] | ||
}; | ||
parsedDescribe.beforeEach = parser.parseCodeBlocks(tree, offset); | ||
parsedDescribe.beforeEachFn = parser.run(parsedDescribe.name, parsedDescribe.beforeEach); | ||
parsedDescribe.beforeEach = this.parseCodeBlocks(tree, offset); | ||
parsedDescribe.beforeEachFn = this.run(parsedDescribe.name, parsedDescribe.beforeEach); | ||
@@ -123,16 +112,16 @@ while (true) { | ||
if (!child || isHeader(child) && getLevel(child) < IT_LEVEL) { | ||
if (!child || isHeader(child) && getLevel(child) < SPEC_LEVEL) { | ||
break; | ||
} | ||
if (parser.validNode(child, 'header', IT_LEVEL)) { | ||
parsedDescribe.it.push(parser.parseIt(tree, offset)); | ||
if (validNode(child, 'header', SPEC_LEVEL)) { | ||
parsedDescribe.spec.push(this.parseSpec(tree, offset)); | ||
} | ||
} | ||
parsedDescribe.fn = function(itFn) { | ||
itFn = itFn || it; | ||
parsedDescribe.fn = function(specFn) { | ||
specFn = specFn || it; | ||
beforeEach(parsedDescribe.beforeEachFn); | ||
parsedDescribe.it.forEach(function(parsedIt) { | ||
itFn(parsedIt.name, parsedIt.fn); | ||
parsedDescribe.spec.forEach(function(parsedSpec) { | ||
specFn(parsedSpec.name, parsedSpec.fn); | ||
}); | ||
@@ -142,18 +131,18 @@ }; | ||
return parsedDescribe; | ||
}, | ||
}; | ||
parseIt: function(tree, offset) { | ||
this.parseSpec = function(tree, offset) { | ||
var node = tree[offset], | ||
it = { | ||
spec = { | ||
name: getName(node) | ||
}; | ||
it.code = parser.parseCodeBlocks(tree, offset); | ||
it.fn = parser.run(it.name, it.code); | ||
spec.code = this.parseCodeBlocks(tree, offset); | ||
spec.fn = this.run(spec.name, spec.code); | ||
return it; | ||
}, | ||
return spec; | ||
}; | ||
parseCodeBlocks: function(tree, offset) { | ||
var code_blocks = []; | ||
this.parseCodeBlocks = function(tree, offset) { | ||
var codeBlocks = []; | ||
@@ -169,14 +158,10 @@ while (true) { | ||
if (isCodeBlock(child)) { | ||
code_blocks.push(child[1]); | ||
codeBlocks.push(child[1]); | ||
} | ||
} | ||
return code_blocks.join('\n'); | ||
}, | ||
validNode: function(node, type, level) { | ||
return node[0] === type && node[1].level === level; | ||
} | ||
return codeBlocks.join('\n'); | ||
}; | ||
}; | ||
module.exports = parser; | ||
module.exports = Parser; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Shell access
Supply chain riskThis module accesses the system shell. Accessing the system shell increases the risk of executing arbitrary code.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
100687
26
538
77
4
2