| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- package tests
- import (
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "mermaid-go/pkg/ast"
- "mermaid-go/pkg/parser"
- )
- func TestParser_ParseBasicGraph(t *testing.T) {
- input := `graph TD
- A --> B`
- p := parser.NewParser()
- diagram, err := p.Parse(input)
- require.NoError(t, err)
- require.NotNil(t, diagram)
- flowchart, ok := diagram.(*ast.Flowchart)
- require.True(t, ok, "Expected flowchart diagram")
- assert.Equal(t, "TD", flowchart.Direction)
- assert.Len(t, flowchart.Edges, 1)
- assert.Len(t, flowchart.Vertices, 2)
- // Check edge
- edge := flowchart.Edges[0]
- assert.Equal(t, "A", edge.Start)
- assert.Equal(t, "B", edge.End)
- assert.Equal(t, "", edge.Text)
- }
- func TestParser_ParseGraphWithShapes(t *testing.T) {
- input := `graph TD
- A[Rectangle] --> B(Round)
- B --> C{Diamond}`
- p := parser.NewParser()
- diagram, err := p.Parse(input)
- require.NoError(t, err)
- flowchart := diagram.(*ast.Flowchart)
- // Check vertices
- require.Contains(t, flowchart.Vertices, "A")
- require.Contains(t, flowchart.Vertices, "B")
- require.Contains(t, flowchart.Vertices, "C")
- // Check vertex A
- vertexA := flowchart.Vertices["A"]
- assert.Equal(t, "A", vertexA.ID)
- require.NotNil(t, vertexA.Text)
- assert.Equal(t, "Rectangle", *vertexA.Text)
- require.NotNil(t, vertexA.Type)
- assert.Equal(t, ast.VertexTypeRect, *vertexA.Type)
- // Check vertex B
- vertexB := flowchart.Vertices["B"]
- assert.Equal(t, "B", vertexB.ID)
- require.NotNil(t, vertexB.Text)
- assert.Equal(t, "Round", *vertexB.Text)
- require.NotNil(t, vertexB.Type)
- assert.Equal(t, ast.VertexTypeRound, *vertexB.Type)
- // Check vertex C
- vertexC := flowchart.Vertices["C"]
- assert.Equal(t, "C", vertexC.ID)
- require.NotNil(t, vertexC.Text)
- assert.Equal(t, "Diamond", *vertexC.Text)
- require.NotNil(t, vertexC.Type)
- assert.Equal(t, ast.VertexTypeDiamond, *vertexC.Type)
- // Check edges
- assert.Len(t, flowchart.Edges, 2)
- }
- func TestParser_ParseGraphWithLabels(t *testing.T) {
- input := `graph TD
- A -->|Get money| B
- B -->|Go shopping| C`
- p := parser.NewParser()
- diagram, err := p.Parse(input)
- require.NoError(t, err)
- flowchart := diagram.(*ast.Flowchart)
- require.Len(t, flowchart.Edges, 2)
- // Check first edge
- edge1 := flowchart.Edges[0]
- assert.Equal(t, "A", edge1.Start)
- assert.Equal(t, "B", edge1.End)
- assert.Equal(t, "Get money", edge1.Text)
- // Check second edge
- edge2 := flowchart.Edges[1]
- assert.Equal(t, "B", edge2.Start)
- assert.Equal(t, "C", edge2.End)
- assert.Equal(t, "Go shopping", edge2.Text)
- }
- func TestParser_ParseGraphWithDifferentArrows(t *testing.T) {
- input := `graph TD
- A --> B
- B -.-> C
- C ==> D
- D --x E`
- p := parser.NewParser()
- diagram, err := p.Parse(input)
- require.NoError(t, err)
- flowchart := diagram.(*ast.Flowchart)
- require.Len(t, flowchart.Edges, 4)
- // Check arrow types and strokes
- edges := flowchart.Edges
- // A --> B (solid arrow)
- assert.Equal(t, ast.StrokeNormal, *edges[0].Stroke)
- assert.Equal(t, "arrow_point", *edges[0].Type)
- // B -.-> C (dotted arrow)
- assert.Equal(t, ast.StrokeDotted, *edges[1].Stroke)
- assert.Equal(t, "arrow_point", *edges[1].Type)
- // C ==> D (thick arrow)
- assert.Equal(t, ast.StrokeThick, *edges[2].Stroke)
- assert.Equal(t, "arrow_point", *edges[2].Type)
- // D --x E (cross arrow)
- assert.Equal(t, ast.StrokeNormal, *edges[3].Stroke)
- assert.Equal(t, "arrow_cross", *edges[3].Type)
- }
- func TestParser_ValidationError(t *testing.T) {
- input := `graph TD
- A --> NonExistentVertex`
- p := parser.NewParser()
- diagram, err := p.Parse(input)
- require.NoError(t, err) // Parsing should succeed
- // But validation should fail if we manually create an invalid diagram
- flowchart := diagram.(*ast.Flowchart)
- // Remove the auto-created vertex to simulate invalid state
- delete(flowchart.Vertices, "NonExistentVertex")
- err = flowchart.Validate()
- assert.Error(t, err)
- assert.Contains(t, err.Error(), "non-existent")
- }
|