|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package executor |
| 4 | + |
| 5 | +import ( |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/mendixlabs/mxcli/mdl/ast" |
| 11 | + "github.com/mendixlabs/mxcli/mdl/visitor" |
| 12 | +) |
| 13 | + |
| 14 | +// TestValidateMicroflow_XPathBooleanCall covers `true()` / `false()` in a |
| 15 | +// microflow expression. That is XPath's spelling of the Boolean constants — it is |
| 16 | +// valid inside a retrieve's `where [...]`, and CE0117 "Error(s) in expression" |
| 17 | +// anywhere a microflow expression is stored (measured with mx check 11.12.2 on a |
| 18 | +// filter condition and on a declare). Before the fix `mxcli check` with no project |
| 19 | +// passed it, and with -p reported E014 with advice about `empty`, which sent the |
| 20 | +// reader looking for a keyword they had not written. |
| 21 | +// |
| 22 | +// The filter/find condition is covered explicitly: list operations were not |
| 23 | +// among the expression sites MDL044 walked at all. |
| 24 | +func TestValidateMicroflow_XPathBooleanCall(t *testing.T) { |
| 25 | + cases := []struct { |
| 26 | + name string |
| 27 | + body string |
| 28 | + wantMDL bool |
| 29 | + }{ |
| 30 | + {"true() in filter", "$R = filter($L, $currentObject/Flag = true());", true}, |
| 31 | + {"false() in find", "$R = find($L, $currentObject/Flag = false());", true}, |
| 32 | + {"bare true() as filter condition", "$R = filter($L, true());", true}, |
| 33 | + {"true() in declare", "declare $b Boolean = true();", true}, |
| 34 | + {"true() inside and", "$R = filter($L, $currentObject/Flag = true() and $currentObject != $G);", true}, |
| 35 | + // Controls: the literal spelling is valid and stays silent. |
| 36 | + {"true literal in filter", "$R = filter($L, $currentObject/Flag = true);", false}, |
| 37 | + {"object compare in filter", "$R = filter($L, $currentObject != $G);", false}, |
| 38 | + {"true literal in declare", "declare $b Boolean = true;", false}, |
| 39 | + // An unknown call inside a filter condition is caught too, now that the |
| 40 | + // condition is walked. |
| 41 | + {"unknown func in filter", "$R = filter($L, isBlank($currentObject/Name));", true}, |
| 42 | + } |
| 43 | + for _, tc := range cases { |
| 44 | + t.Run(tc.name, func(t *testing.T) { |
| 45 | + src := "create microflow M.F ($L: list of M.E, $G: M.E)\nbegin\n " + tc.body + "\nend;" |
| 46 | + ids := mdlIDs(buildMF(t, src)) |
| 47 | + msg, got := ids["MDL044"] |
| 48 | + if got != tc.wantMDL { |
| 49 | + t.Fatalf("MDL044 fired=%v, want %v (body %q) all=%v", got, tc.wantMDL, tc.body, ids) |
| 50 | + } |
| 51 | + if got && strings.Contains(tc.body, "true()") && !strings.Contains(msg, "write `true`") { |
| 52 | + t.Errorf("suggestion should name the literal spelling, got %q", msg) |
| 53 | + } |
| 54 | + }) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// TestFilterPredicateParsingTerminates guards the report that one `filter(..., |
| 59 | +// true…)` variant hung `mxcli check`. Every malformed shape of the statement |
| 60 | +// must come back — with errors or without — well inside the deadline, through |
| 61 | +// both the MDL visitor and the validator. A parser must never hang. |
| 62 | +func TestFilterPredicateParsingTerminates(t *testing.T) { |
| 63 | + bodies := []string{ |
| 64 | + "$R = filter($L, true;", |
| 65 | + "$R = filter($L, true));", |
| 66 | + "$R = filter($L, true true);", |
| 67 | + "$R = filter($L, );", |
| 68 | + "$R = filter($L, = true);", |
| 69 | + "$R = filter($L, $currentObject != );", |
| 70 | + "$R = filter($L, $currentObject/Flag = true()", |
| 71 | + "$R = filter($L, true) and true;", |
| 72 | + "$R = filter($L, true or);", |
| 73 | + "$R = filter($L, (true);", |
| 74 | + "$R = filter($L, true() = true());", |
| 75 | + "$R = filter($L, true()()());", |
| 76 | + "$R = filter($L, true(true));", |
| 77 | + "$R = filter($L, if true() then true() else false());", |
| 78 | + "$R = filter($L, [Flag = true()]);", |
| 79 | + "$R = filter($L, true", |
| 80 | + "$R = filter($L, true(", |
| 81 | + } |
| 82 | + for _, b := range bodies { |
| 83 | + src := "create microflow M.F ($L: list of M.E, $G: M.E)\nbegin\n " + b + "\nend;" |
| 84 | + done := make(chan struct{}) |
| 85 | + go func() { |
| 86 | + defer close(done) |
| 87 | + prog, _ := visitor.Build(src) |
| 88 | + if prog == nil { |
| 89 | + return |
| 90 | + } |
| 91 | + for _, s := range prog.Statements { |
| 92 | + if mf, ok := s.(*ast.CreateMicroflowStmt); ok { |
| 93 | + _ = ValidateMicroflow(mf) |
| 94 | + } |
| 95 | + } |
| 96 | + }() |
| 97 | + select { |
| 98 | + case <-done: |
| 99 | + case <-time.After(5 * time.Second): |
| 100 | + t.Fatalf("parsing/validating did not terminate within 5s: %q", b) |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments