gantt_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package parser
  2. import (
  3. "mermaid-go/pkg/ast"
  4. "mermaid-go/pkg/renderer"
  5. "strings"
  6. "testing"
  7. )
  8. func TestGanttParser_BasicFunctionality(t *testing.T) {
  9. tests := []struct {
  10. name string
  11. input string
  12. expected string
  13. }{
  14. {
  15. name: "Basic Gantt Chart",
  16. input: `gantt
  17. title A Gantt Diagram
  18. dateFormat YYYY-MM-DD
  19. section Section
  20. A task :a1, 2014-01-01, 30d
  21. Another task :after a1, 45d`,
  22. expected: `gantt
  23. title A Gantt Diagram
  24. dateFormat YYYY-MM-DD
  25. section Section
  26. A task : a1 2014-01-01 30d
  27. Another task : after a1 45d
  28. `,
  29. },
  30. {
  31. name: "Multiple Sections",
  32. input: `gantt
  33. title Project Timeline
  34. section Design
  35. UI Design :design, 2024-01-01, 14d
  36. API Design :api, after design, 10d
  37. section Development
  38. Frontend :frontend, after api, 21d
  39. Backend :backend, after api, 21d`,
  40. expected: `gantt
  41. title Project Timeline
  42. dateFormat YYYY-MM-DD
  43. section Design
  44. UI Design : design 2024-01-01 14d
  45. API Design : api after design 10d
  46. section Development
  47. Frontend : frontend after api 21d
  48. Backend : backend after api 21d
  49. `,
  50. },
  51. {
  52. name: "Task Status",
  53. input: `gantt
  54. title Task Status Example
  55. section Completed Tasks
  56. Task 1 :done, 2024-01-01, 7d
  57. Task 2 :active, 2024-01-08, 5d
  58. Task 3 :crit, 2024-01-13, 3d`,
  59. expected: `gantt
  60. title Task Status Example
  61. dateFormat YYYY-MM-DD
  62. section Completed Tasks
  63. Task 1 : done 2024-01-01 7d
  64. Task 2 : 2024-01-08 5d
  65. Task 3 : crit 2024-01-13 3d
  66. `,
  67. },
  68. }
  69. for _, tt := range tests {
  70. t.Run(tt.name, func(t *testing.T) {
  71. parser := NewGanttParser()
  72. diagram, err := parser.Parse(tt.input)
  73. if err != nil {
  74. t.Fatalf("Failed to parse: %v", err)
  75. }
  76. // Test rendering
  77. renderer := renderer.NewGanttRenderer()
  78. output, err := renderer.Render(diagram)
  79. if err != nil {
  80. t.Fatalf("Failed to render: %v", err)
  81. }
  82. // Normalize whitespace for comparison
  83. expected := strings.TrimSpace(tt.expected)
  84. actual := strings.TrimSpace(output)
  85. if actual != expected {
  86. t.Errorf("Expected:\n%s\nGot:\n%s", expected, actual)
  87. }
  88. })
  89. }
  90. }
  91. func TestGanttParser_DateFormatParsing(t *testing.T) {
  92. input := `gantt
  93. title Date Format Test
  94. dateFormat DD-MM-YYYY
  95. section Test
  96. Task : task1, 01-01-2024, 7d`
  97. parser := NewGanttParser()
  98. diagram, err := parser.Parse(input)
  99. if err != nil {
  100. t.Fatalf("Failed to parse: %v", err)
  101. }
  102. if diagram.DateFormat != "DD-MM-YYYY" {
  103. t.Errorf("Expected dateFormat 'DD-MM-YYYY', got '%s'", diagram.DateFormat)
  104. }
  105. if diagram.Title == nil || *diagram.Title != "Date Format Test" {
  106. t.Errorf("Expected title 'Date Format Test', got '%v'", diagram.Title)
  107. }
  108. }
  109. func TestGanttParser_TaskParsing(t *testing.T) {
  110. input := `gantt
  111. title Task Parsing Test
  112. section Tasks
  113. Simple Task : task1, 2024-01-01, 7d
  114. Task with Status :done, task2, 2024-01-08, 5d
  115. Task with Dependencies :after task1, task3, 2024-01-13, 3d`
  116. parser := NewGanttParser()
  117. diagram, err := parser.Parse(input)
  118. if err != nil {
  119. t.Fatalf("Failed to parse: %v", err)
  120. }
  121. if len(diagram.Sections) != 1 {
  122. t.Fatalf("Expected 1 section, got %d", len(diagram.Sections))
  123. }
  124. section := diagram.Sections[0]
  125. if len(section.Tasks) != 3 {
  126. t.Fatalf("Expected 3 tasks, got %d", len(section.Tasks))
  127. }
  128. // Check first task
  129. task1 := section.Tasks[0]
  130. if task1.Name != "Simple Task" {
  131. t.Errorf("Task 1: expected name 'Simple Task', got '%s'", task1.Name)
  132. }
  133. if task1.Status != ast.GanttStatusActive {
  134. t.Errorf("Task 1: expected status 'active', got '%s'", task1.Status)
  135. }
  136. // Check second task
  137. task2 := section.Tasks[1]
  138. if task2.Name != "Task with Status" {
  139. t.Errorf("Task 2: expected name 'Task with Status', got '%s'", task2.Name)
  140. }
  141. if task2.Status != ast.GanttStatusDone {
  142. t.Errorf("Task 2: expected status 'done', got '%s'", task2.Status)
  143. }
  144. // Check third task
  145. task3 := section.Tasks[2]
  146. if task3.Name != "Task with Dependencies" {
  147. t.Errorf("Task 3: expected name 'Task with Dependencies', got '%s'", task3.Name)
  148. }
  149. }