mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-02-04 07:13:25 +08:00
Throw parseError instead of emit parseError
This commit is contained in:
parent
bb534225b3
commit
167368d508
@ -9,26 +9,6 @@ import mermaid from './mermaid'
|
|||||||
|
|
||||||
global.mermaid = 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 using mermaid and ', function () {
|
||||||
describe('when detecting chart type ', function () {
|
describe('when detecting chart type ', function () {
|
||||||
it('should not start rendering with mermaid_config.startOnLoad set to false', function () {
|
it('should not start rendering with mermaid_config.startOnLoad set to false', function () {
|
||||||
@ -219,19 +199,19 @@ describe('when using mermaid and ', function () {
|
|||||||
})
|
})
|
||||||
|
|
||||||
describe('checking validity of input ', function () {
|
describe('checking validity of input ', function () {
|
||||||
it('it should return false for an invalid definiton', function () {
|
it('it should throw for an invalid definiton', function () {
|
||||||
validateDefinition('this is not a mermaid diagram definition', false)
|
expect(() => mermaid.parse('this is not a mermaid diagram definition')).toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('it should return true for a valid flow definition', function () {
|
it('it should not throw for a valid flow definition', function () {
|
||||||
validateDefinition('graph TD;A--x|text including URL space|B;', true)
|
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 () {
|
it('it should throw for an invalid flow definition', function () {
|
||||||
validateDefinition('graph TQ;A--x|text including URL space|B;', false)
|
expect(() => mermaid.parse('graph TQ;A--x|text including URL space|B;')).toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('it should return true for a valid sequenceDiagram definition', function () {
|
it('it should not throw for a valid sequenceDiagram definition', function () {
|
||||||
var str = 'sequenceDiagram\n' +
|
var text = 'sequenceDiagram\n' +
|
||||||
'Alice->Bob: Hello Bob, how are you?\n\n' +
|
'Alice->Bob: Hello Bob, how are you?\n\n' +
|
||||||
'%% Comment\n' +
|
'%% Comment\n' +
|
||||||
'Note right of Bob: Bob thinks\n' +
|
'Note right of Bob: Bob thinks\n' +
|
||||||
@ -240,11 +220,11 @@ describe('when using mermaid and ', function () {
|
|||||||
'else isSick\n' +
|
'else isSick\n' +
|
||||||
'Bob-->Alice: Feel sick...\n' +
|
'Bob-->Alice: Feel sick...\n' +
|
||||||
'end'
|
'end'
|
||||||
validateDefinition(str, true)
|
expect(() => mermaid.parse(text)).not.toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('it should return false for an invalid sequenceDiagram definition', function () {
|
it('it should throw for an invalid sequenceDiagram definition', function () {
|
||||||
var str = 'sequenceDiagram\n' +
|
var text = 'sequenceDiagram\n' +
|
||||||
'Alice:->Bob: Hello Bob, how are you?\n\n' +
|
'Alice:->Bob: Hello Bob, how are you?\n\n' +
|
||||||
'%% Comment\n' +
|
'%% Comment\n' +
|
||||||
'Note right of Bob: Bob thinks\n' +
|
'Note right of Bob: Bob thinks\n' +
|
||||||
@ -253,23 +233,25 @@ describe('when using mermaid and ', function () {
|
|||||||
'else isSick\n' +
|
'else isSick\n' +
|
||||||
'Bob-->Alice: Feel sick...\n' +
|
'Bob-->Alice: Feel sick...\n' +
|
||||||
'end'
|
'end'
|
||||||
validateDefinition(str, false)
|
expect(() => mermaid.parse(text)).toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('it should return true for a valid dot definition', function () {
|
it('it should not throw for a valid dot definition', function () {
|
||||||
validateDefinition('digraph\n' +
|
const text = 'digraph\n' +
|
||||||
'{\n' +
|
'{\n' +
|
||||||
' a -> b -> c -- d -> e;\n' +
|
' a -> b -> c -- d -> e;\n' +
|
||||||
' a -- e;\n' +
|
' a -- e;\n' +
|
||||||
'}', true)
|
'}'
|
||||||
|
expect(() => mermaid.parse(text)).not.toThrow()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('it should return false for an invalid dot definition', function () {
|
it('it should throw for an invalid dot definition', function () {
|
||||||
validateDefinition('digraph\n' +
|
const text = 'digraph\n' +
|
||||||
'{\n' +
|
'{\n' +
|
||||||
'a -:> b -> c -- d -> e;\n' +
|
'a -:> b -> c -- d -> e;\n' +
|
||||||
'a -- e;\n' +
|
'a -- e;\n' +
|
||||||
'}', false)
|
'}'
|
||||||
|
expect(() => mermaid.parse(text)).toThrow()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
@ -246,12 +246,6 @@ var config = {
|
|||||||
|
|
||||||
Logger.setLogLevel(config.logLevel)
|
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 parse = function (text) {
|
||||||
var graphType = utils.detectType(text)
|
var graphType = utils.detectType(text)
|
||||||
var parser
|
var parser
|
||||||
@ -287,16 +281,12 @@ var parse = function (text) {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
parser.parser.yy.parseError = (err, hash) => {
|
parser.parser.yy.parseError = (str, hash) => {
|
||||||
module.exports.eventEmitter.emit('parseError', err, hash)
|
const error = { str, hash }
|
||||||
|
throw error
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
parser.parse(text)
|
||||||
parser.parse(text)
|
|
||||||
return true
|
|
||||||
} catch (err) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
module.exports.parse = parse
|
module.exports.parse = parse
|
||||||
|
|
||||||
|
@ -7,26 +7,6 @@
|
|||||||
*/
|
*/
|
||||||
var api = require('./mermaidAPI.js')
|
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('when using mermaidAPI and ', function () {
|
||||||
describe('doing initialize ', function () {
|
describe('doing initialize ', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
@ -63,11 +43,11 @@ describe('when using mermaidAPI and ', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
describe('checking validity of input ', function () {
|
describe('checking validity of input ', function () {
|
||||||
it('it should return false for an invalid definiton', function () {
|
it('it should throw for an invalid definiton', function () {
|
||||||
validateDefinition('this is not a mermaid diagram definition', false)
|
expect(() => global.mermaidAPI.parse('this is not a mermaid diagram definition')).toThrow()
|
||||||
})
|
})
|
||||||
it('it should return true for a valid definiton', function () {
|
it('it should not throw for a valid definiton', function () {
|
||||||
validateDefinition('graph TD;A--x|text including URL space|B;', true)
|
expect(() => global.mermaidAPI.parse('graph TD;A--x|text including URL space|B;')).not.toThrow()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user