Adds CLI tests

This commit is contained in:
fardog 2014-12-20 17:40:58 -08:00
parent c703c9a0d3
commit f349ddd796
7 changed files with 228 additions and 1 deletions

View File

@ -45,6 +45,8 @@ gulp.task('jasmine',['jison','lint'], function () {
.pipe(jasmine({includeStackTrace:true}));
});
gulp.task('tape', shell.task(['./node_modules/.bin/tape ./test/cli_test-*.js']));
gulp.task('coverage', function (cb) {
gulp.src(['src/**/*.js', '!src/**/*.spec.js'])
.pipe(istanbul()) // Covering files

View File

@ -25,7 +25,9 @@
"which": "^1.0.8"
},
"devDependencies": {
"async": "^0.9.0",
"browserify": "~6.2.0",
"clone": "^0.2.0",
"codeclimate-test-reporter": "0.0.4",
"d3": "~3.4.13",
"dagre-d3": "~0.3.2",
@ -62,6 +64,8 @@
"mock-browser": "^0.90.27",
"path": "^0.4.9",
"phantomjs": "^1.9.12",
"rewire": "^2.1.3"
"rewire": "^2.1.3",
"rimraf": "^2.2.8",
"tape": "^3.0.3"
}
}

101
test/cli_test-output.js Normal file
View File

@ -0,0 +1,101 @@
var fs = require('fs')
, path = require('path')
var test = require('tape')
, async = require('async')
, clone = require('clone')
, rimraf = require('rimraf')
var mermaid = require('../lib')
var singleFile = {
files: ['test/fixtures/test.mermaid']
, outputDir: 'test/tmp/'
, phantomPath: './node_modules/.bin/phantomjs'
}
, multiFile = {
files: ['test/fixtures/test.mermaid', 'test/fixtures/test2.mermaid']
, outputDir: 'test/tmp/'
, phantomPath: './node_modules/.bin/phantomjs'
}
test('output of single png', function(t) {
t.plan(3)
var expected = ['test.mermaid.png']
opt = clone(singleFile)
opt.png = true
mermaid.process(opt.files, opt, function(code) {
t.equal(code, 0, 'has clean exit code')
verifyFiles(expected, opt.outputDir, t)
})
})
test('output of multiple png', function(t) {
t.plan(3)
var expected = ['test.mermaid.png', 'test2.mermaid.png']
opt = clone(multiFile)
opt.png = true
mermaid.process(opt.files, opt, function(code) {
t.equal(code, 0, 'has clean exit code')
verifyFiles(expected, opt.outputDir, t)
})
})
test('output of single svg', function(t) {
t.plan(3)
var expected = ['test.mermaid.svg']
opt = clone(singleFile)
opt.svg = true
mermaid.process(opt.files, opt, function(code) {
t.equal(code, 0, 'has clean exit code')
verifyFiles(expected, opt.outputDir, t)
})
})
test('output of multiple svg', function(t) {
t.plan(3)
var expected = ['test.mermaid.svg', 'test2.mermaid.svg']
opt = clone(multiFile)
opt.svg = true
mermaid.process(opt.files, opt, function(code) {
t.equal(code, 0, 'has clean exit code')
verifyFiles(expected, opt.outputDir, t)
})
})
function verifyFiles(expected, dir, t) {
async.each(
expected
, function(file, cb) {
filename = path.join(dir, path.basename(file))
fs.stat(filename, function(err, stat) {
cb(err)
})
}
, function(err) {
t.notOk(err, 'all files passed')
rimraf(dir, function(rmerr) {
t.notOk(rmerr, 'cleaned up')
t.end()
})
}
)
}

100
test/cli_test-parser.js Normal file
View File

@ -0,0 +1,100 @@
var test = require('tape')
, cliPath = '../lib/cli'
test('parses multiple files', function(t) {
t.plan(2)
var cli = require(cliPath)
, argv = ['example/file1.mermaid', 'file2.mermaid', 'file3.mermaid']
, expect = ['example/file1.mermaid', 'file2.mermaid', 'file3.mermaid']
cli.parse(argv, function(err, msg, opt) {
t.equal(opt.files.length, 3, 'should have 3 parameters')
t.deepEqual(opt.files, expect, 'should match expected values')
t.end()
})
})
test('defaults to png', function(t) {
t.plan(2)
var cli = require(cliPath)
, argv = ['example/file1.mermaid']
cli.parse(argv, function(err, msg, opt) {
t.ok(opt.png, 'png is set by default')
t.notOk(opt.svg, 'svg is not set by default')
t.end()
})
})
test('setting svg unsets png', function(t) {
t.plan(2)
var cli = require(cliPath)
, argv = ['example/file1.mermaid', '-s']
cli.parse(argv, function(err, msg, opt) {
t.ok(opt.svg, 'svg is set when requested')
t.notOk(opt.png, 'png is unset when svg is set')
t.end()
})
})
test('setting png and svg is allowed', function(t) {
t.plan(2)
var cli = require(cliPath)
, argv = ['example/file1.mermaid', '-s', '-p']
cli.parse(argv, function(err, msg, opt) {
t.ok(opt.png, 'png is set when requested')
t.ok(opt.svg, 'svg is set when requested')
t.end()
})
})
test('setting an output directory succeeds', function(t) {
t.plan(1)
var cli = require(cliPath)
, argv = ['-o', 'example/']
cli.parse(argv, function(err, msg, opt) {
t.equal(opt.outputDir, 'example/', 'output directory is set')
t.end()
})
})
test('setting an output directory incorrectly causes an error', function(t) {
t.plan(1)
var cli = require(cliPath)
, argv = ['-o']
cli.parse(argv, function(err) {
t.ok(err, 'an error is raised')
t.end()
})
})
test('a callback function is called after parsing', function(t) {
t.plan(2)
var cli = require(cliPath)
, argv = ['example/test.mermaid']
, expects = ['example/test.mermaid']
cli.parse(argv, function(err, msg, opts) {
t.ok(true, 'callback was called')
t.deepEqual(argv, opts.files, 'options are as expected')
t.end()
})
})

8
test/fixtures/sequence.mermaid vendored Normal file
View File

@ -0,0 +1,8 @@
sequenceDiagram
Alice->Bob: Hello Bob, how are you?
Note right of Bob: Bob thinks
Bob-->Alice: I am good thanks!
Bob-->John the Long: How about you John?
Bob-->Alice: Checking with John...
Alice->John the Long: Yes... John, how are you?
John the Long-->Alice: Better than you!

5
test/fixtures/test.mermaid vendored Normal file
View File

@ -0,0 +1,5 @@
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;

7
test/fixtures/test2.mermaid vendored Normal file
View File

@ -0,0 +1,7 @@
graph LR;
A[Hard edge]-->|Link text|B(Round edge);
B-->C{Decision};
C-->|One|D[Result one];
C-->|Two|E[Result two];
classDef pink fill:#f9f,stroke:#333,stroke-width:4px;
class C pink;