|
| 1 | +package sequence |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/AlexanderGrooff/mermaid-ascii/pkg/diagram" |
| 8 | +) |
| 9 | + |
| 10 | +// TestActorDeclarations mirrors mermaid's actor cases (sequenceDiagram.spec.js |
| 11 | +// apa13): `actor` declares a participant, with or without an `as` alias. |
| 12 | +// mermaid draws actors as stick figures; ASCII renders the same labelled box. |
| 13 | +func TestActorDeclarations(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + input string |
| 17 | + wantID string |
| 18 | + wantLabel string |
| 19 | + }{ |
| 20 | + {"actor with alias", "sequenceDiagram\nactor A as Alice\nactor B as Bob\nA->B: Hello Bob, how are you?\nB-->A: I am good thanks!", "A", "Alice"}, |
| 21 | + {"actor without alias", "sequenceDiagram\nactor Alice\nAlice->>Alice: Hi", "Alice", "Alice"}, |
| 22 | + {"uppercase AS", "sequenceDiagram\nactor A AS Database Server\nA->>A: q", "A", "Database Server"}, |
| 23 | + {"case-insensitive keyword", "sequenceDiagram\nActor A as Alice\nA->>A: hi", "A", "Alice"}, |
| 24 | + {"actor name with spaces", "sequenceDiagram\nactor cron job as Cron\ncron job->>cron job: tick", "cron job", "Cron"}, |
| 25 | + } |
| 26 | + |
| 27 | + for _, tt := range tests { |
| 28 | + t.Run(tt.name, func(t *testing.T) { |
| 29 | + d, err := Parse(tt.input) |
| 30 | + if err != nil { |
| 31 | + t.Fatalf("unexpected error: %v", err) |
| 32 | + } |
| 33 | + if len(d.Participants) == 0 { |
| 34 | + t.Fatal("expected at least one participant") |
| 35 | + } |
| 36 | + p := d.Participants[0] |
| 37 | + if p.ID != tt.wantID || p.Label != tt.wantLabel { |
| 38 | + t.Errorf("got %q/%q, want %q/%q", p.ID, p.Label, tt.wantID, tt.wantLabel) |
| 39 | + } |
| 40 | + output, err := Render(d, diagram.DefaultConfig()) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("render error: %v", err) |
| 43 | + } |
| 44 | + if !strings.Contains(output, tt.wantLabel) { |
| 45 | + t.Errorf("output should contain label %q:\n%s", tt.wantLabel, output) |
| 46 | + } |
| 47 | + }) |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// TestActorParticipantMix mirrors mermaid's apa12: actors and participants |
| 52 | +// coexist, keep declaration order, and undeclared names in messages are still |
| 53 | +// auto-created after them. |
| 54 | +func TestActorParticipantMix(t *testing.T) { |
| 55 | + d, err := Parse("sequenceDiagram\nactor Alice as Alice2\nactor Bob\nparticipant John as John2\nparticipant Mandy\nAlice->>Bob: Hi Bob\nBob->>Alice: Hi Alice\nAlice->>John: Hi John\nJohn->>Mandy: Hi Mandy\nMandy ->>Joan: Hi Joan") |
| 56 | + if err != nil { |
| 57 | + t.Fatal(err) |
| 58 | + } |
| 59 | + wantOrder := []struct{ id, label string }{ |
| 60 | + {"Alice", "Alice2"}, {"Bob", "Bob"}, {"John", "John2"}, {"Mandy", "Mandy"}, {"Joan", "Joan"}, |
| 61 | + } |
| 62 | + if len(d.Participants) != len(wantOrder) { |
| 63 | + t.Fatalf("want %d participants, got %d", len(wantOrder), len(d.Participants)) |
| 64 | + } |
| 65 | + for i, w := range wantOrder { |
| 66 | + if p := d.Participants[i]; p.ID != w.id || p.Label != w.label { |
| 67 | + t.Errorf("participant %d = %q/%q, want %q/%q", i, p.ID, p.Label, w.id, w.label) |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +// TestActorDuplicateRejected: redeclaring a name (whether via actor or |
| 73 | +// participant) is a duplicate, as for participant+participant. |
| 74 | +func TestActorDuplicateRejected(t *testing.T) { |
| 75 | + if _, err := Parse("sequenceDiagram\nactor A as Alice\nparticipant A as Al\nA->>A: hi"); err == nil { |
| 76 | + t.Error("expected duplicate participant error") |
| 77 | + } |
| 78 | +} |
0 commit comments