Skip to content

Commit

Permalink
Throw parseError instead of emit parseError
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerlong committed Sep 10, 2017
1 parent bb53422 commit 167368d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 76 deletions.
58 changes: 20 additions & 38 deletions src/mermaid.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,6 @@ import mermaid from './mermaid'

global.mermaid = mermaid

const validateDefinition = (text, valid) => {
const foo = {
onError: () => {
}
}
spyOn(foo, 'onError')
mermaid.eventEmitter.on('parseError', (err, hash) => {
foo.onError(err)
})
var res = mermaid.parse(text)

if (valid) {
expect(res).toBe(true)
expect(foo.onError).not.toHaveBeenCalled()
} else {
expect(res).toBe(false)
expect(foo.onError).toHaveBeenCalled()
}
}

describe('when using mermaid and ', function () {
describe('when detecting chart type ', function () {
it('should not start rendering with mermaid_config.startOnLoad set to false', function () {
Expand Down Expand Up @@ -219,19 +199,19 @@ describe('when using mermaid and ', function () {
})

describe('checking validity of input ', function () {
it('it should return false for an invalid definiton', function () {
validateDefinition('this is not a mermaid diagram definition', false)
it('it should throw for an invalid definiton', function () {
expect(() => mermaid.parse('this is not a mermaid diagram definition')).toThrow()
})

it('it should return true for a valid flow definition', function () {
validateDefinition('graph TD;A--x|text including URL space|B;', true)
it('it should not throw for a valid flow definition', function () {
expect(() => mermaid.parse('graph TD;A--x|text including URL space|B;')).not.toThrow()
})
it('it should return false for an invalid flow definition', function () {
validateDefinition('graph TQ;A--x|text including URL space|B;', false)
it('it should throw for an invalid flow definition', function () {
expect(() => mermaid.parse('graph TQ;A--x|text including URL space|B;')).toThrow()
})

it('it should return true for a valid sequenceDiagram definition', function () {
var str = 'sequenceDiagram\n' +
it('it should not throw for a valid sequenceDiagram definition', function () {
var text = 'sequenceDiagram\n' +
'Alice->Bob: Hello Bob, how are you?\n\n' +
'%% Comment\n' +
'Note right of Bob: Bob thinks\n' +
Expand All @@ -240,11 +220,11 @@ describe('when using mermaid and ', function () {
'else isSick\n' +
'Bob-->Alice: Feel sick...\n' +
'end'
validateDefinition(str, true)
expect(() => mermaid.parse(text)).not.toThrow()
})

it('it should return false for an invalid sequenceDiagram definition', function () {
var str = 'sequenceDiagram\n' +
it('it should throw for an invalid sequenceDiagram definition', function () {
var text = 'sequenceDiagram\n' +
'Alice:->Bob: Hello Bob, how are you?\n\n' +
'%% Comment\n' +
'Note right of Bob: Bob thinks\n' +
Expand All @@ -253,23 +233,25 @@ describe('when using mermaid and ', function () {
'else isSick\n' +
'Bob-->Alice: Feel sick...\n' +
'end'
validateDefinition(str, false)
expect(() => mermaid.parse(text)).toThrow()
})

it('it should return true for a valid dot definition', function () {
validateDefinition('digraph\n' +
it('it should not throw for a valid dot definition', function () {
const text = 'digraph\n' +
'{\n' +
' a -> b -> c -- d -> e;\n' +
' a -- e;\n' +
'}', true)
'}'
expect(() => mermaid.parse(text)).not.toThrow()
})

it('it should return false for an invalid dot definition', function () {
validateDefinition('digraph\n' +
it('it should throw for an invalid dot definition', function () {
const text = 'digraph\n' +
'{\n' +
'a -:> b -> c -- d -> e;\n' +
'a -- e;\n' +
'}', false)
'}'
expect(() => mermaid.parse(text)).toThrow()
})
})
})
18 changes: 4 additions & 14 deletions src/mermaidAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,6 @@ var config = {

Logger.setLogLevel(config.logLevel)

/**
* ## parse
* Function that parses a mermaid diagram definition. If parsing fails the parseError callback is called and an error is
* thrown and
* @param text
*/
var parse = function (text) {
var graphType = utils.detectType(text)
var parser
Expand Down Expand Up @@ -287,16 +281,12 @@ var parse = function (text) {
break
}

parser.parser.yy.parseError = (err, hash) => {
module.exports.eventEmitter.emit('parseError', err, hash)
parser.parser.yy.parseError = (str, hash) => {
const error = { str, hash }
throw error
}

try {
parser.parse(text)
return true
} catch (err) {
return false
}
parser.parse(text)
}
module.exports.parse = parse

Expand Down
28 changes: 4 additions & 24 deletions src/mermaidAPI.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,6 @@
*/
var api = require('./mermaidAPI.js')

const validateDefinition = (text, valid) => {
const foo = {
onError: () => {
}
}
spyOn(foo, 'onError')
global.mermaidAPI.eventEmitter.on('parseError', (err, hash) => {
foo.onError(err)
})
var res = api.parse(text)

if (valid) {
expect(res).toBe(true)
expect(foo.onError).not.toHaveBeenCalled()
} else {
expect(res).toBe(false)
expect(foo.onError).toHaveBeenCalled()
}
}

describe('when using mermaidAPI and ', function () {
describe('doing initialize ', function () {
beforeEach(function () {
Expand Down Expand Up @@ -63,11 +43,11 @@ describe('when using mermaidAPI and ', function () {
})
})
describe('checking validity of input ', function () {
it('it should return false for an invalid definiton', function () {
validateDefinition('this is not a mermaid diagram definition', false)
it('it should throw for an invalid definiton', function () {
expect(() => global.mermaidAPI.parse('this is not a mermaid diagram definition')).toThrow()
})
it('it should return true for a valid definiton', function () {
validateDefinition('graph TD;A--x|text including URL space|B;', true)
it('it should not throw for a valid definiton', function () {
expect(() => global.mermaidAPI.parse('graph TD;A--x|text including URL space|B;')).not.toThrow()
})
})
})

0 comments on commit 167368d

Please sign in to comment.