vis.js is a dynamic, browser-based visualization library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
5.6 KiB

  1. var assert = require('assert'),
  2. fs = require('fs'),
  3. dot = require('../lib/network/dotparser.js');
  4. describe('dotparser', function () {
  5. it('should parse a DOT file into JSON', function (done) {
  6. fs.readFile('test/dot.txt', function (err, data) {
  7. data = String(data);
  8. var graph = dot.parseDOT(data);
  9. assert.deepEqual(graph, {
  10. "type": "digraph",
  11. "id": "test_graph",
  12. "rankdir": "LR",
  13. "size": "8,5",
  14. "font": "arial",
  15. "nodes": [
  16. {
  17. "id": "node1",
  18. "attr": {
  19. "shape": "doublecircle"
  20. }
  21. },
  22. {
  23. "id": "node2",
  24. "attr": {
  25. "shape": "doublecircle"
  26. }
  27. },
  28. {
  29. "id": "node3",
  30. "attr": {
  31. "shape": "doublecircle"
  32. }
  33. },
  34. {
  35. "id": "node4",
  36. "attr": {
  37. "shape": "diamond",
  38. "color": "red"
  39. }
  40. },
  41. {
  42. "id": "node5",
  43. "attr": {
  44. "shape": "square",
  45. "color": "blue",
  46. "width": 3
  47. }
  48. },
  49. {
  50. "id": 6,
  51. "attr": {
  52. "shape": "circle"
  53. }
  54. },
  55. {
  56. "id": "A",
  57. "attr": {
  58. "shape": "circle"
  59. }
  60. },
  61. {
  62. "id": "B",
  63. "attr": {
  64. "shape": "circle"
  65. }
  66. },
  67. {
  68. "id": "C",
  69. "attr": {
  70. "shape": "circle"
  71. }
  72. }
  73. ],
  74. "edges": [
  75. {
  76. "from": "node1",
  77. "to": "node1",
  78. "type": "->",
  79. "attr": {
  80. "length": 170,
  81. "fontSize": 12,
  82. "label": "a"
  83. }
  84. },
  85. {
  86. "from": "node2",
  87. "to": "node3",
  88. "type": "->",
  89. "attr": {
  90. "length": 170,
  91. "fontSize": 12,
  92. "label": "b"
  93. }
  94. },
  95. {
  96. "from": "node1",
  97. "to": "node4",
  98. "type": "--",
  99. "attr": {
  100. "length": 170,
  101. "fontSize": 12,
  102. "label": "c"
  103. }
  104. },
  105. {
  106. "from": "node3",
  107. "to": "node4",
  108. "type": "->",
  109. "attr": {
  110. "length": 170,
  111. "fontSize": 12,
  112. "label": "d"
  113. }
  114. },
  115. {
  116. "from": "node4",
  117. "to": "node5",
  118. "type": "->",
  119. "attr": {
  120. "length": 170,
  121. "fontSize": 12
  122. }
  123. },
  124. {
  125. "from": "node5",
  126. "to": 6,
  127. "type": "->",
  128. "attr": {
  129. "length": 170,
  130. "fontSize": 12
  131. }
  132. },
  133. {
  134. "from": "A",
  135. "to": {
  136. "nodes": [
  137. {
  138. "id": "B",
  139. "attr": {
  140. "shape": "circle"
  141. }
  142. },
  143. {
  144. "id": "C",
  145. "attr": {
  146. "shape": "circle"
  147. }
  148. }
  149. ]
  150. },
  151. "type": "->",
  152. "attr": {
  153. "length": 170,
  154. "fontSize": 12
  155. }
  156. }
  157. ],
  158. "subgraphs": [
  159. {
  160. "nodes": [
  161. {
  162. "id": "B",
  163. "attr": {
  164. "shape": "circle"
  165. }
  166. },
  167. {
  168. "id": "C",
  169. "attr": {
  170. "shape": "circle"
  171. }
  172. }
  173. ]
  174. }
  175. ]
  176. });
  177. done();
  178. });
  179. });
  180. /**
  181. * DOT-format examples taken from #3015
  182. */
  183. it('properly handles newline escape sequences in strings', function (done) {
  184. var data = 'dinetwork {1 [label="new\\nline"];}';
  185. data = String(data);
  186. var graph = dot.parseDOT(data);
  187. assert.deepEqual(graph, {
  188. "id": "dinetwork",
  189. "nodes": [
  190. {
  191. "id": 1,
  192. "attr": {
  193. "label": "new\nline", // And not "new\\nline"
  194. }
  195. }
  196. ]
  197. });
  198. // Note the double backslashes
  199. var data2 = 'digraph {' + "\n" +
  200. ' 3 [color="#0d2b7c", label="query:1230:add_q\\n0.005283\\n6.83%\\n(0.0001)\\n(0.13%)\\n17×"];' + "\n" +
  201. ' 3 -> 7 [color="#0d2a7b", fontcolor="#0d2a7b", label="0.005128\\n6.63%\\n17×"];' + "\n" +
  202. ' 5 [color="#0d1976", label="urlresolvers:537:reverse\\n0.00219\\n2.83%\\n(0.000193)\\n(0.25%)\\n29×"];' + "\n" +
  203. "}"
  204. data2 = String(data2);
  205. var graph2 = dot.parseDOT(data2);
  206. //console.log(JSON.stringify(graph, null, 2));
  207. assert.deepEqual(graph2, {
  208. "type": "digraph",
  209. "nodes": [
  210. {
  211. "id": 3,
  212. "attr": {
  213. "color": "#0d2b7c",
  214. "label": "query:1230:add_q\n0.005283\n6.83%\n(0.0001)\n(0.13%)\n17×"
  215. }
  216. },
  217. {
  218. "id": 7
  219. },
  220. {
  221. "id": 5,
  222. "attr": {
  223. "color": "#0d1976",
  224. "label": "urlresolvers:537:reverse\n0.00219\n2.83%\n(0.000193)\n(0.25%)\n29×"
  225. }
  226. }
  227. ],
  228. "edges": [
  229. {
  230. "from": 3,
  231. "to": 7,
  232. "type": "->",
  233. "attr": {
  234. "color": "#0d2a7b",
  235. "fontcolor": "#0d2a7b",
  236. "label": "0.005128\n6.63%\n17×"
  237. }
  238. }
  239. ]
  240. });
  241. done();
  242. });
  243. });