Tests of parser and graph db implementation used when parsing the graph script file.

This commit is contained in:
knsv 2014-11-04 18:55:30 +01:00
parent f35e43c512
commit e54a0fcb0f
6 changed files with 229 additions and 67 deletions

49
src/parser/graph.js Normal file
View File

@ -0,0 +1,49 @@
/**
* Created by knut on 14-11-03.
*/
define('parser/graph',function() {
var vertices = {};
var edges = [];
var graph = {
addVertex: function (id, text, type, style) {
if(typeof vertices[id] === 'undefined'){
vertices[id]={id:id, styles:[]};
}
if(typeof text !== undefined){
vertices[id].text = text;
}
if(typeof type !== undefined){
vertices[id].type = type;
}
if(typeof style !== 'undefined'){
if(style !== null){
//console.log('Adding style: '+style[0]);
style.forEach(function(s){
vertices[id].styles.push(s);
});
}
}
//console.log('Got node ' + id + ' ' + type + ' ' + text + ' styles: ' + JSON.stringify(style));
},
getVertices:function(){
return vertices;
},
addLink: function (start, end, type, linktext) {
var edge = {start:start, end:end, type:type.type, text:''};
if(typeof linktext !== 'undefined'){
edge.text = linktext;
}
edges.push(edge);
//console.log('Got link from ' + start + ' to ' + end + ' type:' + type.type + ' linktext:' + linktext);
},
getEdges: function () {
return edges;
},
clear:function(){
vertices = {};
edges = [];
}
};
return graph;
});

View File

@ -103,7 +103,7 @@ styles:
styledef
{$$ = [$1];}
| styles COMMA styledef
{console.log('in styles:'+JSON.stringify($1));$1.push($3);$$ = $1;}
{$1.push($3);$$ = $1;}
;
styledef: BKG COLON colordef
{$$={"background":$3}}
@ -129,5 +129,5 @@ borderStyle: BORDER_STYLE
%%
define('parser/mermaid',function(){
return mermaid;
return parser;
});

View File

@ -141,7 +141,7 @@ case 20:
this.$ = [$$[$0]];
break;
case 21:
console.log('in styles:'+JSON.stringify($$[$0-2]));$$[$0-2].push($$[$0]);this.$ = $$[$0-2];
$$[$0-2].push($$[$0]);this.$ = $$[$0-2];
break;
case 22:
this.$={"background":$$[$0]}
@ -307,9 +307,7 @@ parse: function parse(input) {
return true;
}};
define('parser/mermaid.js',function(){
//var parserSource = generator.generate({moduleName: "mermaid"});
//return marserSource;
define('parser/mermaid',function(){
return parser;
});/* generated by jison-lex 0.3.4 */
var lexer = (function(){

View File

@ -1,27 +1,190 @@
/**
* Created by knut on 14-11-03.
*/
define('parser/mermaid.spec',['parser/scope','parser/mermaid.js'],function(scope, p){
console.log('here:'+p);
define('parser/mermaid.spec',['parser/graph','parser/mermaid'],function(graph, p){
describe('when parsing ',function(){
beforeEach(function(){
console.log('here');
p.yy = scope;
graph.clear();
p.yy = graph;
});
it('should',function(){
it('should handle a nodes and edges',function(){
var res = p.parse('A-->B;');
expect(p.yy.getLinks()).toBe('apa');
})
});
var vert = p.yy.getVertices();
var edges = p.yy.getEdges();
describe('just checking', function() {
it('works for app', function () {
expect(2).toBe(2);
expect(vert['A'].id).toBe('A');
expect(vert['B'].id).toBe('B');
expect(edges.length).toBe(1);
expect(edges[0].start).toBe('A');
expect(edges[0].end).toBe('B');
expect(edges[0].type).toBe('arrow');
expect(edges[0].text).toBe('');
});
it('should handle open ended edges',function(){
var res = p.parse('A---B;');
var vert = p.yy.getVertices();
var edges = p.yy.getEdges();
expect(edges[0].type).toBe('arrow_open');
});
it('should handle cross ended edges',function(){
var res = p.parse('A--xB;');
var vert = p.yy.getVertices();
var edges = p.yy.getEdges();
expect(edges[0].type).toBe('arrow_cross');
});
it('should handle open ended edges',function(){
var res = p.parse('A--oB;');
var vert = p.yy.getVertices();
var edges = p.yy.getEdges();
expect(edges[0].type).toBe('arrow_circle');
});
it('should handle text on edges without space',function(){
var res = p.parse('A--xB|textNoSpace;');
var vert = p.yy.getVertices();
var edges = p.yy.getEdges();
expect(edges[0].type).toBe('arrow_cross');
});
it('should handle text on edges with space',function(){
var res = p.parse('A--xB|text including space;');
var vert = p.yy.getVertices();
var edges = p.yy.getEdges();
expect(edges[0].type).toBe('arrow_cross');
});
it('should handle multi-line text',function(){
var res = p.parse('A--oB|text space;\nB-->C|more text with space;');
var vert = p.yy.getVertices();
var edges = p.yy.getEdges();
expect(edges[0].type).toBe('arrow_circle');
expect(edges[1].type).toBe('arrow');
expect(vert['A'].id).toBe('A');
expect(vert['B'].id).toBe('B');
expect(vert['C'].id).toBe('C');
expect(edges.length).toBe(2);
expect(edges[0].start).toBe('A');
expect(edges[0].end).toBe('B');
expect(edges[0].text).toBe('text space');
expect(edges[1].start).toBe('B');
expect(edges[1].end).toBe('C');
expect(edges[1].text).toBe('more text with space');
});
it('should handle text in vertices with space',function(){
var res = p.parse('A[chimpansen hoppar]-->C;');
var vert = p.yy.getVertices();
var edges = p.yy.getEdges();
expect(vert['A'].type).toBe('square');
expect(vert['A'].text).toBe('chimpansen hoppar');
});
it('should handle text in vertices with space',function(){
var res = p.parse('A(chimpansen hoppar)-->C;');
var vert = p.yy.getVertices();
var edges = p.yy.getEdges();
expect(vert['A'].type).toBe('round');
expect(vert['A'].text).toBe('chimpansen hoppar');
});
it('should handle text in vertices with space',function(){
var res = p.parse('A{chimpansen hoppar}-->C;');
var vert = p.yy.getVertices();
var edges = p.yy.getEdges();
expect(vert['A'].type).toBe('diamond');
expect(vert['A'].text).toBe('chimpansen hoppar');
});
it('should handle a single node',function(){
// Silly but syntactically correct
var res = p.parse('A;');
var vert = p.yy.getVertices();
var edges = p.yy.getEdges();
expect(edges.length).toBe(0);
expect(vert['A'].styles.length).toBe(0);
});
//console.log(p.parse('style Q background:#fff;'));
it('should handle styles for vertices',function(){
var res = p.parse('style Q background:#fff;');
var vert = p.yy.getVertices();
var edges = p.yy.getEdges();
var style = vert['Q'].styles[0];
expect(vert['Q'].styles.length).toBe(1);
expect(vert['Q'].styles[0].background).toBe('#fff');
});
it('should handle multiple styles for a vortex',function(){
var res = p.parse('style R background:#fff,border:1px solid red;');
var vert = p.yy.getVertices();
var edges = p.yy.getEdges();
expect(vert['R'].styles.length).toBe(2);
expect(vert['R'].styles[0].background).toBe('#fff');
expect(vert['R'].styles[1].border).toBe('1px solid red');
});
it('should handle multiple styles in a graph',function(){
var res = p.parse('style S background:#aaa;\nstyle T background:#bbb,border:1px solid red;');
var vert = p.yy.getVertices();
var edges = p.yy.getEdges();
expect(vert['S'].styles.length).toBe(1);
expect(vert['T'].styles.length).toBe(2);
expect(vert['S'].styles[0].background).toBe('#aaa');
expect(vert['T'].styles[0].background).toBe('#bbb');
expect(vert['T'].styles[1].border).toBe('1px solid red');
});
it('should handle styles and graph definitons in a graph',function(){
var res = p.parse('S-->T;\nstyle S background:#aaa;\nstyle T background:#bbb,border:1px solid red;');
var vert = p.yy.getVertices();
var edges = p.yy.getEdges();
expect(vert['S'].styles.length).toBe(1);
expect(vert['T'].styles.length).toBe(2);
expect(vert['S'].styles[0].background).toBe('#aaa');
expect(vert['T'].styles[0].background).toBe('#bbb');
expect(vert['T'].styles[1].border).toBe('1px solid red');
});
//console.log(p.parse('style S background:#aaa;\nstyle T background:#bbb,border:1px solid red;'));
});
});

View File

@ -1,18 +0,0 @@
/**
* Created by knut on 14-11-03.
*/
define('parser/scope',function() {
var scope = {
addVertex: function (id, text, type, style) {
console.log('Got node ' + id + ' ' + type + ' ' + text + ' styles: ' + JSON.stringify(style));
},
addLink: function (start, end, type, linktext) {
console.log('Got link from ' + start + ' to ' + end + ' type:' + type.type + ' linktext:' + linktext);
},
getLinks: function () {
return 'apa';
}
};
return scope;
});

View File

@ -1,30 +0,0 @@
//console.log(p.parse('#fcfcfc'));
//console.log(p.parse('background: #fcfcfc'));
//console.log(p.parse('background: black'));
//
/*
var p = require('./parser/mermaid.js');
p.parser.yy = require('./parser/scope');
console.log(p.parse('A-->B;'));
console.log(p.parse('A---B;'));
console.log(p.parse('A--xB;'));
console.log(p.parse('A--oB|apan;'));
console.log(p.parse('A--oB|apan hoppar;\nB-->C|apan hoppar vidare;'));
console.log(p.parse('A-->B|apan hoppar;\nB-->C;'));
console.log(p.parse('A-->B|apan hoppar;'));
console.log(p.parse('A[chimpansen hoppar]-->C;'));
console.log(p.parse('A(chimpansen hoppar)-->C;'));
//console.log(p.parse('A{chimpansen hoppar}-->C;'));
console.log(p.parse('style Q background:#fff;'));
console.log(p.parse('style R background:#fff,border:1px solid red;'));
console.log(p.parse('style S background:#aaa;\nstyle T background:#bbb,border:1px solid red;'));
//console.log(p.parse('A;'));
*/