| 1 |
package xpath |
| 2 |
|
| 3 |
import ( |
| 4 |
"math" |
| 5 |
"testing" |
| 6 |
) |
| 7 |
|
| 8 |
// Synthetic ledger-style document for numeric operand context isolation. |
| 9 |
// Two Credit siblings (Amounts 20 and 5) so one compiled expression can be |
| 10 |
// evaluated across different record contexts. |
| 11 |
func createCreditDoc() *TNode { |
| 12 |
// <Root> |
| 13 |
// <Credit><Entry><Amount>20</Amount></Entry></Credit> |
| 14 |
// <Credit><Entry><Amount>5</Amount></Entry></Credit> |
| 15 |
// </Root> |
| 16 |
doc := createNode("", RootNode) |
| 17 |
root := doc.createChildNode("Root", ElementNode) |
| 18 |
for _, amount := range []string{"20", "5"} { |
| 19 |
ev := root.createChildNode("Credit", ElementNode) |
| 20 |
entry := ev.createChildNode("Entry", ElementNode) |
| 21 |
amt := entry.createChildNode("Amount", ElementNode) |
| 22 |
amt.createChildNode(amount, TextNode) |
| 23 |
} |
| 24 |
return doc |
| 25 |
} |
| 26 |
|
| 27 |
func selectCredit(doc *TNode) *TNode { |
| 28 |
return selectNode(doc, "//Credit") |
| 29 |
} |
| 30 |
|
| 31 |
func selectCredits(doc *TNode) []*TNode { |
| 32 |
return selectNodes(doc, "//Credit") |
| 33 |
} |
| 34 |
|
| 35 |
func evalNumberAt(t *testing.T, context *TNode, expr string) float64 { |
| 36 |
t.Helper() |
| 37 |
exp, err := Compile(expr) |
| 38 |
if err != nil { |
| 39 |
t.Fatalf("Compile(%q): %v", expr, err) |
| 40 |
} |
| 41 |
nav := createNavigator(context) |
| 42 |
v := exp.Evaluate(nav) |
| 43 |
f, ok := v.(float64) |
| 44 |
if !ok { |
| 45 |
t.Fatalf("Evaluate(%q) type %T want float64 (value=%v)", expr, v, v) |
| 46 |
} |
| 47 |
return f |
| 48 |
} |
| 49 |
|
| 50 |
func assertNear(t *testing.T, got, want float64, expr string) { |
| 51 |
t.Helper() |
| 52 |
if math.IsNaN(got) || math.IsNaN(want) { |
| 53 |
if !(math.IsNaN(got) && math.IsNaN(want)) { |
| 54 |
t.Fatalf("%s: got %v want %v", expr, got, want) |
| 55 |
} |
| 56 |
return |
| 57 |
} |
| 58 |
if got != want { |
| 59 |
t.Fatalf("%s: got %v want %v", expr, got, want) |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// TestNumericOperatorContextIsolation covers the predicated-sum × context-sensitive |
| 64 |
// factor class: predicates move the shared navigator; the right operand must still |
| 65 |
// observe the operator's original context. |
| 66 |
func TestNumericOperatorContextIsolation(t *testing.T) { |
| 67 |
doc := createCreditDoc() |
| 68 |
ctx := selectCredit(doc) |
| 69 |
if ctx == nil { |
| 70 |
t.Fatal("Credit context not found") |
| 71 |
} |
| 72 |
|
| 73 |
const ( |
| 74 |
predSumLit = `sum(.//Entry[not(@excluded='true')]/Amount) * -1` |
| 75 |
plainSumCS = `sum(.//Amount) * (1 - 2*count(self::Credit))` |
| 76 |
predSumCS = `sum(.//Entry[not(@excluded='true')]/Amount) * (1 - 2*count(self::Credit))` |
| 77 |
factorFirst = `(1 - 2*count(self::Credit)) * sum(.//Entry[not(@excluded='true')]/Amount)` |
| 78 |
// Magnitude > 1 non-identity factor |
| 79 |
predSumMag = `sum(.//Entry[not(@excluded='true')]/Amount) * (3 * count(self::Credit))` |
| 80 |
// Div and minus neighbors sharing numericQuery |
| 81 |
predSumDiv = `sum(.//Entry[not(@excluded='true')]/Amount) div (count(self::Credit) + 1)` |
| 82 |
predSumSub = `sum(.//Entry[not(@excluded='true')]/Amount) - count(self::Credit)` |
| 83 |
) |
| 84 |
|
| 85 |
// Controls and the defect row (must all be correct after the fix). |
| 86 |
cases := []struct { |
| 87 |
expr string |
| 88 |
want float64 |
| 89 |
}{ |
| 90 |
{predSumLit, -20}, |
| 91 |
{plainSumCS, -20}, |
| 92 |
{predSumCS, -20}, |
| 93 |
{factorFirst, -20}, |
| 94 |
{predSumMag, 60}, |
| 95 |
{predSumDiv, 10}, |
| 96 |
{predSumSub, 19}, |
| 97 |
} |
| 98 |
for _, tc := range cases { |
| 99 |
got := evalNumberAt(t, ctx, tc.expr) |
| 100 |
assertNear(t, got, tc.want, tc.expr) |
| 101 |
} |
| 102 |
|
| 103 |
// Original context navigator must be unchanged after full evaluation. |
| 104 |
nav := createNavigator(ctx) |
| 105 |
beforeName := nav.LocalName() |
| 106 |
exp := MustCompile(predSumCS) |
| 107 |
_ = exp.Evaluate(nav) |
| 108 |
if nav.LocalName() != beforeName { |
| 109 |
t.Fatalf("context navigator moved after Evaluate: got %q want %q", nav.LocalName(), beforeName) |
| 110 |
} |
| 111 |
|
| 112 |
// Same compiled expression: same-context repeat (fresh navigator each time). |
| 113 |
exp2 := MustCompile(predSumCS) |
| 114 |
gSame1 := exp2.Evaluate(createNavigator(ctx)).(float64) |
| 115 |
gSame2 := exp2.Evaluate(createNavigator(ctx)).(float64) |
| 116 |
if gSame1 != -20 || gSame2 != -20 { |
| 117 |
t.Fatalf("same-context repeat Evaluate: g1=%v g2=%v want both -20", gSame1, gSame2) |
| 118 |
} |
| 119 |
|
| 120 |
// Different record contexts must not contaminate query/navigator state. |
| 121 |
// One compiled expression, contexts A (Amount=20) → B (Amount=5) → A. |
| 122 |
credits := selectCredits(doc) |
| 123 |
if len(credits) != 2 { |
| 124 |
t.Fatalf("expected 2 Credit contexts, got %d", len(credits)) |
| 125 |
} |
| 126 |
ctxA, ctxB := credits[0], credits[1] |
| 127 |
expAB := MustCompile(predSumCS) |
| 128 |
vA1 := expAB.Evaluate(createNavigator(ctxA)) |
| 129 |
vB := expAB.Evaluate(createNavigator(ctxB)) |
| 130 |
vA2 := expAB.Evaluate(createNavigator(ctxA)) |
| 131 |
gotA1, okA1 := vA1.(float64) |
| 132 |
gotB, okB := vB.(float64) |
| 133 |
gotA2, okA2 := vA2.(float64) |
| 134 |
if !okA1 || !okB || !okA2 { |
| 135 |
t.Fatalf("cross-context Evaluate types: A1=%T B=%T A2=%T", vA1, vB, vA2) |
| 136 |
} |
| 137 |
if gotA1 != -20 || gotB != -5 || gotA2 != -20 { |
| 138 |
t.Fatalf("cross-context Evaluate contamination: A1=%v B=%v A2=%v want -20, -5, -20", gotA1, gotB, gotA2) |
| 139 |
} |
| 140 |
|
| 141 |
// Empty node-set: sum empty = 0; context-sensitive factor still non-identity. |
| 142 |
emptyExpr := `sum(.//Entry[@missing='yes']/Amount) * (1 - 2*count(self::Credit))` |
| 143 |
gotEmpty := evalNumberAt(t, ctx, emptyExpr) |
| 144 |
assertNear(t, gotEmpty, 0, emptyExpr) // 0 * -1 = 0 |
| 145 |
} |
| 146 |
|
| 147 |
// TestNumericOperatorNodeSetCoercion ensures a bare node-set left operand still |
| 148 |
// converts correctly when the right operand is context-sensitive (eager asNumber |
| 149 |
// path after the isolation fix). |
| 150 |
func TestNumericOperatorNodeSetCoercion(t *testing.T) { |
| 151 |
doc := createCreditDoc() |
| 152 |
ctx := selectCredit(doc) |
| 153 |
// First Amount node value is 20; times context-sensitive -1. |
| 154 |
expr := `.//Amount * (1 - 2*count(self::Credit))` |
| 155 |
got := evalNumberAt(t, ctx, expr) |
| 156 |
assertNear(t, got, -20, expr) |
| 157 |
} |