diff --git a/gulpfile.js b/gulpfile.js index 6d77a3d5e..de50f3bff 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -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 diff --git a/package.json b/package.json index ef27ddd9b..8df079cb0 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test/cli_test-output.js b/test/cli_test-output.js new file mode 100644 index 000000000..5cea818d0 --- /dev/null +++ b/test/cli_test-output.js @@ -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() + }) + } + ) +} diff --git a/test/cli_test-parser.js b/test/cli_test-parser.js new file mode 100644 index 000000000..f21acfd67 --- /dev/null +++ b/test/cli_test-parser.js @@ -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() + }) +}) diff --git a/test/fixtures/sequence.mermaid b/test/fixtures/sequence.mermaid new file mode 100644 index 000000000..e0f8a5b57 --- /dev/null +++ b/test/fixtures/sequence.mermaid @@ -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! diff --git a/test/fixtures/test.mermaid b/test/fixtures/test.mermaid new file mode 100644 index 000000000..d5bf6cb31 --- /dev/null +++ b/test/fixtures/test.mermaid @@ -0,0 +1,5 @@ +graph TD; + A-->B; + A-->C; + B-->D; + C-->D; diff --git a/test/fixtures/test2.mermaid b/test/fixtures/test2.mermaid new file mode 100644 index 000000000..02a2a061b --- /dev/null +++ b/test/fixtures/test2.mermaid @@ -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;