|
| 1 | +package sequence |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/AlexanderGrooff/mermaid-ascii/pkg/diagram" |
| 8 | +) |
| 9 | + |
| 10 | +func firstFragment(sd *SequenceDiagram) *Fragment { |
| 11 | + for _, ev := range sd.Events { |
| 12 | + if ev.Kind == EventFragmentStart { |
| 13 | + return ev.Fragment |
| 14 | + } |
| 15 | + } |
| 16 | + return nil |
| 17 | +} |
| 18 | + |
| 19 | +// TestParseParCriticalBreakRect covers the fragment types mermaid tests: par |
| 20 | +// (with `and`), critical (with/without `option`), break, and rect. |
| 21 | +func TestParseParCriticalBreakRect(t *testing.T) { |
| 22 | + tests := []struct { |
| 23 | + name string |
| 24 | + input string |
| 25 | + wantType FragmentType |
| 26 | + wantDividers []string |
| 27 | + wantLabel string // opener label (after keyword) |
| 28 | + }{ |
| 29 | + {"par with and", "sequenceDiagram\n par a\n A->>B: 1\n and b\n A->>C: 2\n end", FragmentPar, []string{"b"}, "a"}, |
| 30 | + {"par multiple and", "sequenceDiagram\n par a\n A->>B: 1\n and b\n A->>B: 2\n and c\n A->>B: 3\n end", FragmentPar, []string{"b", "c"}, "a"}, |
| 31 | + {"critical with option", "sequenceDiagram\n critical conn\n A->>B: 1\n option down\n A->>B: 2\n end", FragmentCritical, []string{"down"}, "conn"}, |
| 32 | + {"critical without option", "sequenceDiagram\n critical conn\n A->>B: 1\n end", FragmentCritical, nil, "conn"}, |
| 33 | + {"break", "sequenceDiagram\n break oops\n A->>B: 1\n end", FragmentBreak, nil, "oops"}, |
| 34 | + {"rect strips rgb", "sequenceDiagram\n rect rgb(0,255,0)\n A->>B: 1\n end", FragmentRect, nil, ""}, |
| 35 | + {"rect strips rgba", "sequenceDiagram\n rect rgba(0,0,0,0.1)\n A->>B: 1\n end", FragmentRect, nil, ""}, |
| 36 | + {"rect keeps non-color text", "sequenceDiagram\n rect highlight\n A->>B: 1\n end", FragmentRect, nil, "highlight"}, |
| 37 | + } |
| 38 | + for _, tt := range tests { |
| 39 | + t.Run(tt.name, func(t *testing.T) { |
| 40 | + sd, err := Parse(tt.input) |
| 41 | + if err != nil { |
| 42 | + t.Fatalf("parse: %v", err) |
| 43 | + } |
| 44 | + f := firstFragment(sd) |
| 45 | + if f == nil { |
| 46 | + t.Fatal("no fragment start") |
| 47 | + } |
| 48 | + if f.Type != tt.wantType { |
| 49 | + t.Errorf("type = %v, want %v", f.Type, tt.wantType) |
| 50 | + } |
| 51 | + if f.Label != tt.wantLabel { |
| 52 | + t.Errorf("label = %q, want %q", f.Label, tt.wantLabel) |
| 53 | + } |
| 54 | + got := altDividers(sd) // reused: collects EventFragmentDivider labels |
| 55 | + if len(got) != len(tt.wantDividers) { |
| 56 | + t.Fatalf("dividers = %v, want %v", got, tt.wantDividers) |
| 57 | + } |
| 58 | + for i, w := range tt.wantDividers { |
| 59 | + if got[i] != w { |
| 60 | + t.Errorf("divider %d = %q, want %q", i, got[i], w) |
| 61 | + } |
| 62 | + } |
| 63 | + }) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +// TestParseDividerMismatch covers dividers used with the wrong fragment type. |
| 68 | +func TestParseDividerMismatch(t *testing.T) { |
| 69 | + tests := []struct{ name, input, wantErr string }{ |
| 70 | + {"and outside par", "sequenceDiagram\n A->>B: x\n and\n", "outside a matching par"}, |
| 71 | + {"option outside critical", "sequenceDiagram\n A->>B: x\n option\n", "outside a matching critical"}, |
| 72 | + {"and inside alt", "sequenceDiagram\n alt x\n A->>B: y\n and z\n end", "outside a matching par"}, |
| 73 | + {"option inside par", "sequenceDiagram\n par x\n A->>B: y\n option z\n end", "outside a matching critical"}, |
| 74 | + {"else inside par", "sequenceDiagram\n par x\n A->>B: y\n else z\n end", "outside a matching alt"}, |
| 75 | + {"unclosed par", "sequenceDiagram\n par x\n A->>B: y", "unclosed"}, |
| 76 | + } |
| 77 | + for _, tt := range tests { |
| 78 | + t.Run(tt.name, func(t *testing.T) { |
| 79 | + if _, err := Parse(tt.input); err == nil || !strings.Contains(err.Error(), tt.wantErr) { |
| 80 | + t.Errorf("err = %v, want containing %q", err, tt.wantErr) |
| 81 | + } |
| 82 | + }) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// TestRenderParCriticalBreakRectSmoke renders each in both charsets. |
| 87 | +func TestRenderParCriticalBreakRectSmoke(t *testing.T) { |
| 88 | + inputs := map[string]string{ |
| 89 | + "par": "sequenceDiagram\n par a\n A->>B: x\n and b\n A->>B: y\n end", |
| 90 | + "critical": "sequenceDiagram\n critical c\n A->>B: x\n option o\n A->>B: y\n end", |
| 91 | + "break": "sequenceDiagram\n break oops\n A->>B: x\n end", |
| 92 | + "rect": "sequenceDiagram\n rect rgb(0,255,0)\n A->>B: x\n end", |
| 93 | + } |
| 94 | + for name, in := range inputs { |
| 95 | + for _, ascii := range []bool{false, true} { |
| 96 | + sd, err := Parse(in) |
| 97 | + if err != nil { |
| 98 | + t.Fatalf("%s parse: %v", name, err) |
| 99 | + } |
| 100 | + out, err := Render(sd, diagram.NewTestConfig(ascii, "cli")) |
| 101 | + if err != nil { |
| 102 | + t.Fatalf("%s render ascii=%v: %v", name, ascii, err) |
| 103 | + } |
| 104 | + if !strings.Contains(out, "["+name) { |
| 105 | + t.Errorf("%s ascii=%v: missing [%s ...] label:\n%s", name, ascii, name, out) |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments