|
6 | 6 | "strings" |
7 | 7 | "testing" |
8 | 8 |
|
| 9 | + "github.com/mendixlabs/mxcli/mdl/ast" |
9 | 10 | "github.com/mendixlabs/mxcli/model" |
10 | 11 | "github.com/mendixlabs/mxcli/sdk/microflows" |
11 | 12 | ) |
@@ -108,6 +109,115 @@ func TestTraverseFlow_EnumSplitPreservesExplicitCaseOrder(t *testing.T) { |
108 | 109 | } |
109 | 110 | } |
110 | 111 |
|
| 112 | +func TestEnumSplitRoundtripPreservesEmptyCaseBeforeNamedCases(t *testing.T) { |
| 113 | + out := describeBuiltEnumSplitBody(t, []ast.MicroflowStatement{ |
| 114 | + &ast.EnumSplitStmt{ |
| 115 | + Variable: "ImageKind", |
| 116 | + Cases: []ast.EnumSplitCase{ |
| 117 | + { |
| 118 | + Value: "(empty)", |
| 119 | + Body: []ast.MicroflowStatement{ |
| 120 | + &ast.LogStmt{ |
| 121 | + Level: ast.LogError, |
| 122 | + Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "missing kind"}, |
| 123 | + Annotations: &ast.ActivityAnnotations{Position: &ast.Position{X: -300, Y: -200}}, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + { |
| 128 | + Value: "Cover", |
| 129 | + Body: []ast.MicroflowStatement{ |
| 130 | + &ast.LogStmt{ |
| 131 | + Level: ast.LogInfo, |
| 132 | + Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "cover"}, |
| 133 | + Annotations: &ast.ActivityAnnotations{Position: &ast.Position{X: -300, Y: 100}}, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + { |
| 138 | + Value: "Logo", |
| 139 | + Body: []ast.MicroflowStatement{ |
| 140 | + &ast.LogStmt{ |
| 141 | + Level: ast.LogInfo, |
| 142 | + Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "logo"}, |
| 143 | + Annotations: &ast.ActivityAnnotations{Position: &ast.Position{X: -300, Y: -40}}, |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + }) |
| 150 | + |
| 151 | + assertOrder(t, out, "case (empty)", "case Cover", "case Logo") |
| 152 | +} |
| 153 | + |
| 154 | +func TestEnumSplitRoundtripPreservesEmptyGroupedCaseBeforeTerminalCase(t *testing.T) { |
| 155 | + out := describeBuiltEnumSplitBody(t, []ast.MicroflowStatement{ |
| 156 | + &ast.EnumSplitStmt{ |
| 157 | + Variable: "Event/Type", |
| 158 | + Cases: []ast.EnumSplitCase{ |
| 159 | + { |
| 160 | + Values: []string{"CREATE", "DELETE"}, |
| 161 | + }, |
| 162 | + { |
| 163 | + Value: "UPDATE", |
| 164 | + Body: []ast.MicroflowStatement{ |
| 165 | + &ast.LogStmt{Level: ast.LogInfo, Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "update"}}, |
| 166 | + &ast.ReturnStmt{}, |
| 167 | + }, |
| 168 | + }, |
| 169 | + { |
| 170 | + Value: "(empty)", |
| 171 | + Body: []ast.MicroflowStatement{ |
| 172 | + &ast.LogStmt{ |
| 173 | + Level: ast.LogInfo, |
| 174 | + Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "empty"}, |
| 175 | + Annotations: &ast.ActivityAnnotations{ |
| 176 | + Anchor: &ast.FlowAnchors{From: ast.AnchorSideBottom, To: ast.AnchorSideTop}, |
| 177 | + }, |
| 178 | + }, |
| 179 | + &ast.ReturnStmt{ |
| 180 | + Annotations: &ast.ActivityAnnotations{ |
| 181 | + Anchor: &ast.FlowAnchors{To: ast.AnchorSideTop}, |
| 182 | + }, |
| 183 | + }, |
| 184 | + }, |
| 185 | + }, |
| 186 | + }, |
| 187 | + }, |
| 188 | + &ast.LogStmt{Level: ast.LogInfo, Message: &ast.LiteralExpr{Kind: ast.LiteralString, Value: "shared tail"}}, |
| 189 | + }) |
| 190 | + |
| 191 | + assertOrder(t, out, "case CREATE, DELETE", "case UPDATE", "case (empty)") |
| 192 | +} |
| 193 | + |
| 194 | +func describeBuiltEnumSplitBody(t *testing.T, body []ast.MicroflowStatement) string { |
| 195 | + t.Helper() |
| 196 | + |
| 197 | + fb := &flowBuilder{posX: 100, posY: 100, spacing: HorizontalSpacing, measurer: &layoutMeasurer{}} |
| 198 | + oc := fb.buildFlowGraph(body, nil) |
| 199 | + mf := µflows.Microflow{ObjectCollection: oc} |
| 200 | + e := newTestExecutor() |
| 201 | + |
| 202 | + return strings.Join(formatMicroflowActivities(e.newExecContext(t.Context()), mf, nil, nil), "\n") |
| 203 | +} |
| 204 | + |
| 205 | +func assertOrder(t *testing.T, out string, items ...string) { |
| 206 | + t.Helper() |
| 207 | + |
| 208 | + lastIdx := -1 |
| 209 | + for _, item := range items { |
| 210 | + idx := strings.Index(out, item) |
| 211 | + if idx == -1 { |
| 212 | + t.Fatalf("missing %q in output:\n%s", item, out) |
| 213 | + } |
| 214 | + if idx < lastIdx { |
| 215 | + t.Fatalf("expected %q after previous item in output:\n%s", item, out) |
| 216 | + } |
| 217 | + lastIdx = idx |
| 218 | + } |
| 219 | +} |
| 220 | + |
111 | 221 | func assertContainsAny(t *testing.T, lines []string, want string) { |
112 | 222 | t.Helper() |
113 | 223 | for _, line := range lines { |
|
0 commit comments