literate-jasmine
Advanced tools
Comparing version 0.0.8 to 0.0.10
{ | ||
"name": "literate-jasmine", | ||
"description": "write tests in markdown that are parsed to specification files to run with jasmine-node", | ||
"version": "0.0.8", | ||
"version": "0.0.10", | ||
"bin": { | ||
@@ -6,0 +6,0 @@ "literate-jasmine": "./bin/literate-jasmine" |
describe("literate-jasmine ", function() { | ||
var PI; | ||
describe("Mathematics", function() { | ||
beforeEach(function() { | ||
PI = 22/7; | ||
}); | ||
it("add can add numbers", function() { | ||
@@ -20,6 +25,8 @@ var a = 1, | ||
it("appending works with +", function() { | ||
var text = "abc"; | ||
it("calculates the circumference of a circle", function() { | ||
var circumference = function(radius) { | ||
return 2 * PI * radius; | ||
}; | ||
expect(text + "d").toBe("abcd"); | ||
expect(circumference(5)).toBe(2 * 22/7 * 5); | ||
}); | ||
@@ -26,0 +33,0 @@ |
# literate-jasmine [![Build Status](https://travis-ci.org/cymen/literate-jasmine.png?branch=master)](https://travis-ci.org/cymen/literate-jasmine) | ||
var PI; | ||
[![NPM](https://nodei.co/npm/literate-jasmine.png?downloads=true&stars=true)](https://npmjs.org/package/literate-jasmine) | ||
@@ -10,64 +12,34 @@ | ||
This README.md has this markdown structure (which includes the main header | ||
above and the other parts below): | ||
This README.md has a markdown structure (which includes the main header | ||
above and the other parts below) that is parsed into a tree: | ||
# literate-jasmine | ||
## Mathematices | ||
### add can add numbers (level 3 header) | ||
### add can add numbers | ||
## Strings | ||
### appending works with + | ||
* literate-jasmine | ||
* Mathematices | ||
* add can add numbers (level 3 header) | ||
* add can add numbers | ||
* calculates the circumference of a circle | ||
* Strings | ||
* appending works with + | ||
Which is parsed into a tree: | ||
Which is then written to disk as `FILENAME_spec.js`. Take a look at `README_spec.js` | ||
as an example -- it is generated using this README as input! | ||
literate-jasmine | ||
Mathematices | ||
add can add numbers (level 3 header) | ||
add can add numbers | ||
Strings | ||
appending works with + | ||
Which is then written to disk as `FILENAME_spec.js` (so `README_spec.js`): | ||
describe('literate-jasmine', function() { | ||
describe('Mathematics', function() { | ||
it('add can add numbers', function() { | ||
// test code | ||
}); | ||
it('can divide numbers', function() { | ||
// test code | ||
}); | ||
}); | ||
describe('Strings', function() { | ||
it('appending works with +', function() { | ||
// test code | ||
}); | ||
}); | ||
}); | ||
The command `literate-jasmine` is used to convert the markdown to JavaScript | ||
(assuming you ran `npm install -g literate-jasmine`): | ||
literate-jasmine README.md | ||
literate-jasmine README.md | ||
(If you're working on this project, run `./bin/literate-jasmine` instead.) | ||
Then run the jasmine tests: | ||
Then run the jasmine tests: `jasmine-node README_spec.js`. | ||
> jasmine-node README_spec.js | ||
1 2 3 | ||
.... | ||
Take a close look at how scope works for globals. In the Mathematics section below, we | ||
reference `PI` to reset it as a `beforeEach` so every test has `PI` reset to the correct | ||
value. The actual declaration of `PI` as a variable happens on the third line of this | ||
README. The root describe treats any code blocks after it as global setup. | ||
Finished in 0.006 seconds | ||
4 tests, 4 assertions, 0 failures, 0 skipped | ||
## Mathematics | ||
## Mathematics | ||
PI = 22/7; | ||
### add can add numbers | ||
@@ -91,2 +63,12 @@ | ||
### calculates the circumference of a circle | ||
Note that we reference the variable PI below that is defined in the code | ||
block at the top of this describe (so right under "Mathematics"). | ||
var circumference = function(radius) { | ||
return 2 * PI * radius; | ||
}; | ||
expect(circumference(5)).toBe(2 * 22/7 * 5); | ||
## Strings | ||
@@ -93,0 +75,0 @@ ### appending works with + |
@@ -5,2 +5,54 @@ var parser = require('../src/parser'), | ||
describe('parser', function() { | ||
describe('code block(s)', function() { | ||
it('parses out the code block', function() { | ||
var text = [ | ||
'', | ||
' console.log("hi!");', | ||
'' | ||
].join('\n'); | ||
var tree = markdown.parse(text); | ||
var code = parser.parseCodeBlocks(tree, 0); | ||
expect(code).toContain('console.log("hi!");'); | ||
}); | ||
it('parses out the comments between code blocks', function() { | ||
var text = [ | ||
'', | ||
' var i = 10;', | ||
'', | ||
'And now set x to 20:', | ||
'', | ||
' var x = 20;', | ||
'' | ||
].join('\n'); | ||
var tree = markdown.parse(text); | ||
var code = parser.parseCodeBlocks(tree, 0); | ||
expect(code).toContain('var i = 10;'); | ||
expect(code).toContain('var x = 20;'); | ||
expect(code).not.toContain('And now set x to 20:'); | ||
}); | ||
it('stops looking for additional code blocks when it hits a header', function() { | ||
var text = [ | ||
'', | ||
' var i = 10;', | ||
'', | ||
'### And now for something different:', | ||
'', | ||
' var answer = 42;' | ||
].join('\n'); | ||
var tree = markdown.parse(text); | ||
var code = parser.parseCodeBlocks(tree, 0); | ||
expect(code).toContain('var i = 10;'); | ||
expect(code).not.toContain('var answer = 42;'); | ||
expect(code).not.toContain('And now for something different'); | ||
}); | ||
}); | ||
describe('it block', function() { | ||
@@ -37,17 +89,2 @@ var text, | ||
}); | ||
it('adds the code into a function for calling', function() { | ||
var it = parser.parseIt(tree, 1); | ||
expect(typeof it.fn).toBe('function'); | ||
}); | ||
it('has a function that we can actually call', function() { | ||
spyOn(console, 'log'); | ||
var it = parser.parseIt(tree, 1); | ||
it.fn(); | ||
expect(console.log).toHaveBeenCalledWith('x', 10); | ||
}); | ||
}); | ||
@@ -62,2 +99,5 @@ | ||
'## can set a variable to a number', | ||
'', | ||
' someGlobal = 42;', | ||
'', | ||
'### it 1', | ||
@@ -87,4 +127,9 @@ 'Set x to 10:', | ||
expect(describe.it.length).toBe(1); | ||
expect(typeof describe.it[0].fn).toBe('function'); | ||
}); | ||
it('parses out any code blocks before the it as a beforeEach', function() { | ||
var describe = parser.parseDescribe(tree, 1); | ||
expect(describe.beforeEach).toContain('someGlobal = 42;'); | ||
}); | ||
}); | ||
@@ -99,2 +144,6 @@ | ||
'# my title here', | ||
'', | ||
' var someGlobal = 21;', | ||
' someOtherRealGlobal = 500;', | ||
'', | ||
'## can set a variable to a number', | ||
@@ -110,3 +159,7 @@ '### it 1', | ||
' expect(x).toBe(10);', | ||
'' | ||
'', | ||
'And we can change the value of global someGlobal:', | ||
'', | ||
' someGlobal = 42;', | ||
'', | ||
].join('\n'); | ||
@@ -126,3 +179,9 @@ }); | ||
}); | ||
it('parses any code blocks before first header/describe as beforeEach', function() { | ||
var result = parser.parse(text); | ||
expect(result.global).toContain('someGlobal = 21;'); | ||
}); | ||
}); | ||
}); |
@@ -34,2 +34,33 @@ var writer = require('../src/writer'), | ||
}); | ||
it('writes the root global out', function() { | ||
var tree = { | ||
name: 'example', | ||
global: 'var someVariable = 42;', | ||
describes: [] | ||
}; | ||
writer('someFileName_spec.js', tree); | ||
expect(fs.writeFileSync.mostRecentCall.args[1]).toContain('var someVariable = 42;'); | ||
}); | ||
it('writes the describe beforeEach out', function() { | ||
var tree = { | ||
name: 'example', | ||
describes: [{ | ||
name: 'the describe name', | ||
beforeEach: 'someVariable = 404;', | ||
it: [{ | ||
name: 'the it name', | ||
code: 'var example = "example";\nexpect(example).toBe("example");\n' | ||
}] | ||
}] | ||
}; | ||
writer('someFileName_spec.js', tree); | ||
expect(fs.writeFileSync.mostRecentCall.args[1]).toContain('beforeEach'); | ||
expect(fs.writeFileSync.mostRecentCall.args[1]).toContain('someVariable = 404;'); | ||
}); | ||
}); |
@@ -21,2 +21,4 @@ var lodash = require('lodash'), | ||
complete.global = parser.parseCodeBlocks(tree, 1); | ||
for (var i=2; i < tree.length; i++) { | ||
@@ -39,2 +41,4 @@ var node = tree[i]; | ||
describe.beforeEach = parser.parseCodeBlocks(tree, offset); | ||
while (true) { | ||
@@ -44,3 +48,3 @@ offset += 1; | ||
if (!child || child[0] === 'header' && child[1] < IT_LEVEL) { | ||
if (!child || child[0] === 'header' && child[1].level < IT_LEVEL) { | ||
break; | ||
@@ -61,5 +65,12 @@ } | ||
name: node[2] | ||
}, | ||
code_blocks = []; | ||
}; | ||
it.code = parser.parseCodeBlocks(tree, offset); | ||
return it; | ||
}, | ||
parseCodeBlocks: function(tree, offset) { | ||
var code_blocks = []; | ||
while (true) { | ||
@@ -78,9 +89,3 @@ offset += 1; | ||
it.code = code_blocks.join('\n'); | ||
it.fn = function() { | ||
eval(it.code); | ||
}; | ||
return it; | ||
return code_blocks.join('\n'); | ||
}, | ||
@@ -87,0 +92,0 @@ |
@@ -23,7 +23,17 @@ var fs = require('fs'); | ||
lines.push('describe("' + parserOutput.name + '", function() {\n'); | ||
lines.push('describe("' + parserOutput.name + '", function() {'); | ||
if (parserOutput.global) { | ||
lines.push(indentCode(parserOutput.global + '\n', 2)); | ||
} | ||
parserOutput.describes.forEach(function(describe) { | ||
lines.push(indentLine('describe("' + describe.name + '", function() {\n', 2)); | ||
if (describe.beforeEach) { | ||
lines.push(indentLine('beforeEach(function() {', 4)); | ||
lines.push(indentCode(describe.beforeEach, 6)); | ||
lines.push(indentLine('});\n', 4)); | ||
} | ||
describe.it.forEach(function(it) { | ||
@@ -30,0 +40,0 @@ lines.push(indentLine('it("' + it.name + '", function() {', 4)); |
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
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
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
15339
344
0
78