parser_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. package tests
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. "mermaid-go/pkg/ast"
  7. "mermaid-go/pkg/parser"
  8. )
  9. func TestParser_ParseBasicGraph(t *testing.T) {
  10. input := `graph TD
  11. A --> B`
  12. p := parser.NewParser()
  13. diagram, err := p.Parse(input)
  14. require.NoError(t, err)
  15. require.NotNil(t, diagram)
  16. flowchart, ok := diagram.(*ast.Flowchart)
  17. require.True(t, ok, "Expected flowchart diagram")
  18. assert.Equal(t, "TD", flowchart.Direction)
  19. assert.Len(t, flowchart.Edges, 1)
  20. assert.Len(t, flowchart.Vertices, 2)
  21. // Check edge
  22. edge := flowchart.Edges[0]
  23. assert.Equal(t, "A", edge.Start)
  24. assert.Equal(t, "B", edge.End)
  25. assert.Equal(t, "", edge.Text)
  26. }
  27. func TestParser_ParseGraphWithShapes(t *testing.T) {
  28. input := `graph TD
  29. A[Rectangle] --> B(Round)
  30. B --> C{Diamond}`
  31. p := parser.NewParser()
  32. diagram, err := p.Parse(input)
  33. require.NoError(t, err)
  34. flowchart := diagram.(*ast.Flowchart)
  35. // Check vertices
  36. require.Contains(t, flowchart.Vertices, "A")
  37. require.Contains(t, flowchart.Vertices, "B")
  38. require.Contains(t, flowchart.Vertices, "C")
  39. // Check vertex A
  40. vertexA := flowchart.Vertices["A"]
  41. assert.Equal(t, "A", vertexA.ID)
  42. require.NotNil(t, vertexA.Text)
  43. assert.Equal(t, "Rectangle", *vertexA.Text)
  44. require.NotNil(t, vertexA.Type)
  45. assert.Equal(t, ast.VertexTypeRect, *vertexA.Type)
  46. // Check vertex B
  47. vertexB := flowchart.Vertices["B"]
  48. assert.Equal(t, "B", vertexB.ID)
  49. require.NotNil(t, vertexB.Text)
  50. assert.Equal(t, "Round", *vertexB.Text)
  51. require.NotNil(t, vertexB.Type)
  52. assert.Equal(t, ast.VertexTypeRound, *vertexB.Type)
  53. // Check vertex C
  54. vertexC := flowchart.Vertices["C"]
  55. assert.Equal(t, "C", vertexC.ID)
  56. require.NotNil(t, vertexC.Text)
  57. assert.Equal(t, "Diamond", *vertexC.Text)
  58. require.NotNil(t, vertexC.Type)
  59. assert.Equal(t, ast.VertexTypeDiamond, *vertexC.Type)
  60. // Check edges
  61. assert.Len(t, flowchart.Edges, 2)
  62. }
  63. func TestParser_ParseGraphWithLabels(t *testing.T) {
  64. input := `graph TD
  65. A -->|Get money| B
  66. B -->|Go shopping| C`
  67. p := parser.NewParser()
  68. diagram, err := p.Parse(input)
  69. require.NoError(t, err)
  70. flowchart := diagram.(*ast.Flowchart)
  71. require.Len(t, flowchart.Edges, 2)
  72. // Check first edge
  73. edge1 := flowchart.Edges[0]
  74. assert.Equal(t, "A", edge1.Start)
  75. assert.Equal(t, "B", edge1.End)
  76. assert.Equal(t, "Get money", edge1.Text)
  77. // Check second edge
  78. edge2 := flowchart.Edges[1]
  79. assert.Equal(t, "B", edge2.Start)
  80. assert.Equal(t, "C", edge2.End)
  81. assert.Equal(t, "Go shopping", edge2.Text)
  82. }
  83. func TestParser_ParseGraphWithDifferentArrows(t *testing.T) {
  84. input := `graph TD
  85. A --> B
  86. B -.-> C
  87. C ==> D
  88. D --x E`
  89. p := parser.NewParser()
  90. diagram, err := p.Parse(input)
  91. require.NoError(t, err)
  92. flowchart := diagram.(*ast.Flowchart)
  93. require.Len(t, flowchart.Edges, 4)
  94. // Check arrow types and strokes
  95. edges := flowchart.Edges
  96. // A --> B (solid arrow)
  97. assert.Equal(t, ast.StrokeNormal, *edges[0].Stroke)
  98. assert.Equal(t, "arrow_point", *edges[0].Type)
  99. // B -.-> C (dotted arrow)
  100. assert.Equal(t, ast.StrokeDotted, *edges[1].Stroke)
  101. assert.Equal(t, "arrow_point", *edges[1].Type)
  102. // C ==> D (thick arrow)
  103. assert.Equal(t, ast.StrokeThick, *edges[2].Stroke)
  104. assert.Equal(t, "arrow_point", *edges[2].Type)
  105. // D --x E (cross arrow)
  106. assert.Equal(t, ast.StrokeNormal, *edges[3].Stroke)
  107. assert.Equal(t, "arrow_cross", *edges[3].Type)
  108. }
  109. func TestParser_ValidationError(t *testing.T) {
  110. input := `graph TD
  111. A --> NonExistentVertex`
  112. p := parser.NewParser()
  113. diagram, err := p.Parse(input)
  114. require.NoError(t, err) // Parsing should succeed
  115. // But validation should fail if we manually create an invalid diagram
  116. flowchart := diagram.(*ast.Flowchart)
  117. // Remove the auto-created vertex to simulate invalid state
  118. delete(flowchart.Vertices, "NonExistentVertex")
  119. err = flowchart.Validate()
  120. assert.Error(t, err)
  121. assert.Contains(t, err.Error(), "non-existent")
  122. }