| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package parser
- import (
- "mermaid-go/pkg/ast"
- "mermaid-go/pkg/renderer"
- "strings"
- "testing"
- )
- func TestGanttParser_BasicFunctionality(t *testing.T) {
- tests := []struct {
- name string
- input string
- expected string
- }{
- {
- name: "Basic Gantt Chart",
- input: `gantt
- title A Gantt Diagram
- dateFormat YYYY-MM-DD
- section Section
- A task :a1, 2014-01-01, 30d
- Another task :after a1, 45d`,
- expected: `gantt
- title A Gantt Diagram
- dateFormat YYYY-MM-DD
- section Section
- A task : a1 2014-01-01 30d
- Another task : after a1 45d
- `,
- },
- {
- name: "Multiple Sections",
- input: `gantt
- title Project Timeline
- section Design
- UI Design :design, 2024-01-01, 14d
- API Design :api, after design, 10d
- section Development
- Frontend :frontend, after api, 21d
- Backend :backend, after api, 21d`,
- expected: `gantt
- title Project Timeline
- dateFormat YYYY-MM-DD
- section Design
- UI Design : design 2024-01-01 14d
- API Design : api after design 10d
- section Development
- Frontend : frontend after api 21d
- Backend : backend after api 21d
- `,
- },
- {
- name: "Task Status",
- input: `gantt
- title Task Status Example
- section Completed Tasks
- Task 1 :done, 2024-01-01, 7d
- Task 2 :active, 2024-01-08, 5d
- Task 3 :crit, 2024-01-13, 3d`,
- expected: `gantt
- title Task Status Example
- dateFormat YYYY-MM-DD
- section Completed Tasks
- Task 1 : done 2024-01-01 7d
- Task 2 : 2024-01-08 5d
- Task 3 : crit 2024-01-13 3d
- `,
- },
- }
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- parser := NewGanttParser()
- diagram, err := parser.Parse(tt.input)
- if err != nil {
- t.Fatalf("Failed to parse: %v", err)
- }
- // Test rendering
- renderer := renderer.NewGanttRenderer()
- output, err := renderer.Render(diagram)
- if err != nil {
- t.Fatalf("Failed to render: %v", err)
- }
- // Normalize whitespace for comparison
- expected := strings.TrimSpace(tt.expected)
- actual := strings.TrimSpace(output)
- if actual != expected {
- t.Errorf("Expected:\n%s\nGot:\n%s", expected, actual)
- }
- })
- }
- }
- func TestGanttParser_DateFormatParsing(t *testing.T) {
- input := `gantt
- title Date Format Test
- dateFormat DD-MM-YYYY
- section Test
- Task : task1, 01-01-2024, 7d`
- parser := NewGanttParser()
- diagram, err := parser.Parse(input)
- if err != nil {
- t.Fatalf("Failed to parse: %v", err)
- }
- if diagram.DateFormat != "DD-MM-YYYY" {
- t.Errorf("Expected dateFormat 'DD-MM-YYYY', got '%s'", diagram.DateFormat)
- }
- if diagram.Title == nil || *diagram.Title != "Date Format Test" {
- t.Errorf("Expected title 'Date Format Test', got '%v'", diagram.Title)
- }
- }
- func TestGanttParser_TaskParsing(t *testing.T) {
- input := `gantt
- title Task Parsing Test
- section Tasks
- Simple Task : task1, 2024-01-01, 7d
- Task with Status :done, task2, 2024-01-08, 5d
- Task with Dependencies :after task1, task3, 2024-01-13, 3d`
- parser := NewGanttParser()
- diagram, err := parser.Parse(input)
- if err != nil {
- t.Fatalf("Failed to parse: %v", err)
- }
- if len(diagram.Sections) != 1 {
- t.Fatalf("Expected 1 section, got %d", len(diagram.Sections))
- }
- section := diagram.Sections[0]
- if len(section.Tasks) != 3 {
- t.Fatalf("Expected 3 tasks, got %d", len(section.Tasks))
- }
- // Check first task
- task1 := section.Tasks[0]
- if task1.Name != "Simple Task" {
- t.Errorf("Task 1: expected name 'Simple Task', got '%s'", task1.Name)
- }
- if task1.Status != ast.GanttStatusActive {
- t.Errorf("Task 1: expected status 'active', got '%s'", task1.Status)
- }
- // Check second task
- task2 := section.Tasks[1]
- if task2.Name != "Task with Status" {
- t.Errorf("Task 2: expected name 'Task with Status', got '%s'", task2.Name)
- }
- if task2.Status != ast.GanttStatusDone {
- t.Errorf("Task 2: expected status 'done', got '%s'", task2.Status)
- }
- // Check third task
- task3 := section.Tasks[2]
- if task3.Name != "Task with Dependencies" {
- t.Errorf("Task 3: expected name 'Task with Dependencies', got '%s'", task3.Name)
- }
- }
|