|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package executor |
| 4 | + |
| 5 | +import ( |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/mendixlabs/mxcli/mdl/ast" |
| 9 | + "github.com/mendixlabs/mxcli/sdk/microflows" |
| 10 | +) |
| 11 | + |
| 12 | +// TestNestedIfPreservesCaptions is a regression test for the bug where |
| 13 | +// the outer IF's @caption would be overwritten by the inner IF's @caption |
| 14 | +// because pendingAnnotations is shared mutable state across recursive |
| 15 | +// addStatement calls. |
| 16 | +// |
| 17 | +// Before the fix: |
| 18 | +// - outer ExclusiveSplit received caption "Right format?" (from inner IF) |
| 19 | +// - inner ExclusiveSplit kept its condition expression as caption |
| 20 | +// - inner IF's @annotation got attached to the outer split |
| 21 | +// |
| 22 | +// After the fix: |
| 23 | +// - addIfStatement consumes its own pendingAnnotations right after |
| 24 | +// creating its split, so outer and inner captions stay bound to the |
| 25 | +// correct splits. |
| 26 | +func TestNestedIfPreservesCaptions(t *testing.T) { |
| 27 | + // Build an AST equivalent to: |
| 28 | + // if $S != empty @caption 'String not empty?' |
| 29 | + // if isMatch($S, 'x') @caption 'Right format?' |
| 30 | + // return true |
| 31 | + // else |
| 32 | + // return false |
| 33 | + // else |
| 34 | + // return false |
| 35 | + innerIf := &ast.IfStmt{ |
| 36 | + Condition: &ast.FunctionCallExpr{ |
| 37 | + Name: "isMatch", |
| 38 | + Arguments: []ast.Expression{&ast.VariableExpr{Name: "S"}, &ast.LiteralExpr{Value: "x", Kind: ast.LiteralString}}, |
| 39 | + }, |
| 40 | + ThenBody: []ast.MicroflowStatement{ |
| 41 | + &ast.ReturnStmt{Value: &ast.LiteralExpr{Value: true, Kind: ast.LiteralBoolean}}, |
| 42 | + }, |
| 43 | + ElseBody: []ast.MicroflowStatement{ |
| 44 | + &ast.ReturnStmt{Value: &ast.LiteralExpr{Value: false, Kind: ast.LiteralBoolean}}, |
| 45 | + }, |
| 46 | + Annotations: &ast.ActivityAnnotations{Caption: "Right format?"}, |
| 47 | + } |
| 48 | + outerIf := &ast.IfStmt{ |
| 49 | + Condition: &ast.BinaryExpr{ |
| 50 | + Left: &ast.VariableExpr{Name: "S"}, |
| 51 | + Operator: "!=", |
| 52 | + Right: &ast.LiteralExpr{Value: nil, Kind: ast.LiteralNull}, |
| 53 | + }, |
| 54 | + ThenBody: []ast.MicroflowStatement{innerIf}, |
| 55 | + ElseBody: []ast.MicroflowStatement{ |
| 56 | + &ast.ReturnStmt{Value: &ast.LiteralExpr{Value: false, Kind: ast.LiteralBoolean}}, |
| 57 | + }, |
| 58 | + Annotations: &ast.ActivityAnnotations{Caption: "String not empty?"}, |
| 59 | + } |
| 60 | + |
| 61 | + fb := &flowBuilder{ |
| 62 | + posX: 100, |
| 63 | + posY: 100, |
| 64 | + spacing: HorizontalSpacing, |
| 65 | + varTypes: map[string]string{"S": "String"}, |
| 66 | + declaredVars: map[string]string{"S": "String"}, |
| 67 | + } |
| 68 | + fb.buildFlowGraph([]ast.MicroflowStatement{outerIf}, nil) |
| 69 | + |
| 70 | + // Collect ExclusiveSplits with their captions. The outer split is created |
| 71 | + // first, so objects[1] is the outer split (objects[0] is the StartEvent). |
| 72 | + var splits []*microflows.ExclusiveSplit |
| 73 | + for _, obj := range fb.objects { |
| 74 | + if sp, ok := obj.(*microflows.ExclusiveSplit); ok { |
| 75 | + splits = append(splits, sp) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if len(splits) != 2 { |
| 80 | + t.Fatalf("expected 2 ExclusiveSplits, got %d", len(splits)) |
| 81 | + } |
| 82 | + |
| 83 | + // Splits are appended in creation order: outer first (from outerIf), |
| 84 | + // then inner (when recursion into ThenBody creates the nested IF's split). |
| 85 | + outerSplit, innerSplit := splits[0], splits[1] |
| 86 | + |
| 87 | + if outerSplit.Caption != "String not empty?" { |
| 88 | + t.Errorf("outer split caption: got %q, want %q", outerSplit.Caption, "String not empty?") |
| 89 | + } |
| 90 | + if innerSplit.Caption != "Right format?" { |
| 91 | + t.Errorf("inner split caption: got %q, want %q", innerSplit.Caption, "Right format?") |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// TestIfCaptionWithoutNesting confirms a single IF with @caption still gets |
| 96 | +// the right caption after the fix (baseline sanity check). |
| 97 | +func TestIfCaptionWithoutNesting(t *testing.T) { |
| 98 | + ifStmt := &ast.IfStmt{ |
| 99 | + Condition: &ast.BinaryExpr{ |
| 100 | + Left: &ast.VariableExpr{Name: "S"}, |
| 101 | + Operator: "!=", |
| 102 | + Right: &ast.LiteralExpr{Value: nil, Kind: ast.LiteralNull}, |
| 103 | + }, |
| 104 | + ThenBody: []ast.MicroflowStatement{ |
| 105 | + &ast.ReturnStmt{Value: &ast.LiteralExpr{Value: true, Kind: ast.LiteralBoolean}}, |
| 106 | + }, |
| 107 | + ElseBody: []ast.MicroflowStatement{ |
| 108 | + &ast.ReturnStmt{Value: &ast.LiteralExpr{Value: false, Kind: ast.LiteralBoolean}}, |
| 109 | + }, |
| 110 | + Annotations: &ast.ActivityAnnotations{Caption: "String not empty?"}, |
| 111 | + } |
| 112 | + |
| 113 | + fb := &flowBuilder{ |
| 114 | + posX: 100, |
| 115 | + posY: 100, |
| 116 | + spacing: HorizontalSpacing, |
| 117 | + varTypes: map[string]string{"S": "String"}, |
| 118 | + declaredVars: map[string]string{"S": "String"}, |
| 119 | + } |
| 120 | + fb.buildFlowGraph([]ast.MicroflowStatement{ifStmt}, nil) |
| 121 | + |
| 122 | + for _, obj := range fb.objects { |
| 123 | + if sp, ok := obj.(*microflows.ExclusiveSplit); ok { |
| 124 | + if sp.Caption != "String not empty?" { |
| 125 | + t.Errorf("split caption: got %q, want %q", sp.Caption, "String not empty?") |
| 126 | + } |
| 127 | + return |
| 128 | + } |
| 129 | + } |
| 130 | + t.Fatal("no ExclusiveSplit found") |
| 131 | +} |
| 132 | + |
| 133 | +// TestIfAnnotationStaysWithCorrectSplit confirms @annotation on a nested IF |
| 134 | +// attaches to that IF's split, not to the outer one. |
| 135 | +func TestIfAnnotationStaysWithCorrectSplit(t *testing.T) { |
| 136 | + innerIf := &ast.IfStmt{ |
| 137 | + Condition: &ast.FunctionCallExpr{ |
| 138 | + Name: "isMatch", |
| 139 | + Arguments: []ast.Expression{&ast.VariableExpr{Name: "S"}, &ast.LiteralExpr{Value: "x", Kind: ast.LiteralString}}, |
| 140 | + }, |
| 141 | + ThenBody: []ast.MicroflowStatement{ |
| 142 | + &ast.ReturnStmt{Value: &ast.LiteralExpr{Value: true, Kind: ast.LiteralBoolean}}, |
| 143 | + }, |
| 144 | + ElseBody: []ast.MicroflowStatement{ |
| 145 | + &ast.ReturnStmt{Value: &ast.LiteralExpr{Value: false, Kind: ast.LiteralBoolean}}, |
| 146 | + }, |
| 147 | + Annotations: &ast.ActivityAnnotations{ |
| 148 | + Caption: "Right format?", |
| 149 | + AnnotationText: "Inner IF note", |
| 150 | + }, |
| 151 | + } |
| 152 | + outerIf := &ast.IfStmt{ |
| 153 | + Condition: &ast.BinaryExpr{ |
| 154 | + Left: &ast.VariableExpr{Name: "S"}, |
| 155 | + Operator: "!=", |
| 156 | + Right: &ast.LiteralExpr{Value: nil, Kind: ast.LiteralNull}, |
| 157 | + }, |
| 158 | + ThenBody: []ast.MicroflowStatement{innerIf}, |
| 159 | + ElseBody: []ast.MicroflowStatement{ |
| 160 | + &ast.ReturnStmt{Value: &ast.LiteralExpr{Value: false, Kind: ast.LiteralBoolean}}, |
| 161 | + }, |
| 162 | + Annotations: &ast.ActivityAnnotations{ |
| 163 | + Caption: "String not empty?", |
| 164 | + AnnotationText: "Outer IF note", |
| 165 | + }, |
| 166 | + } |
| 167 | + |
| 168 | + fb := &flowBuilder{ |
| 169 | + posX: 100, |
| 170 | + posY: 100, |
| 171 | + spacing: HorizontalSpacing, |
| 172 | + varTypes: map[string]string{"S": "String"}, |
| 173 | + declaredVars: map[string]string{"S": "String"}, |
| 174 | + } |
| 175 | + fb.buildFlowGraph([]ast.MicroflowStatement{outerIf}, nil) |
| 176 | + |
| 177 | + var splits []*microflows.ExclusiveSplit |
| 178 | + var annotations []*microflows.Annotation |
| 179 | + for _, obj := range fb.objects { |
| 180 | + switch o := obj.(type) { |
| 181 | + case *microflows.ExclusiveSplit: |
| 182 | + splits = append(splits, o) |
| 183 | + case *microflows.Annotation: |
| 184 | + annotations = append(annotations, o) |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + if len(splits) != 2 { |
| 189 | + t.Fatalf("expected 2 splits, got %d", len(splits)) |
| 190 | + } |
| 191 | + if len(annotations) != 2 { |
| 192 | + t.Fatalf("expected 2 annotations, got %d", len(annotations)) |
| 193 | + } |
| 194 | + |
| 195 | + outerSplit, innerSplit := splits[0], splits[1] |
| 196 | + |
| 197 | + // AnnotationFlow links Annotation -> activity. Verify each flow points |
| 198 | + // from the annotation with the expected text to the expected split. |
| 199 | + var outerNoteDestID, innerNoteDestID string |
| 200 | + for _, af := range fb.annotationFlows { |
| 201 | + // Find the Annotation referenced by OriginID |
| 202 | + for _, ann := range annotations { |
| 203 | + if ann.ID != af.OriginID { |
| 204 | + continue |
| 205 | + } |
| 206 | + switch ann.Caption { |
| 207 | + case "Outer IF note": |
| 208 | + outerNoteDestID = string(af.DestinationID) |
| 209 | + case "Inner IF note": |
| 210 | + innerNoteDestID = string(af.DestinationID) |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + if outerNoteDestID != string(outerSplit.ID) { |
| 216 | + t.Errorf("outer note destination: got %q, want %q (outer split)", outerNoteDestID, outerSplit.ID) |
| 217 | + } |
| 218 | + if innerNoteDestID != string(innerSplit.ID) { |
| 219 | + t.Errorf("inner note destination: got %q, want %q (inner split)", innerNoteDestID, innerSplit.ID) |
| 220 | + } |
| 221 | +} |
0 commit comments