@@ -28,6 +28,118 @@ describe("Executor", () => {
2828 expect ( result ) . toContain ( '"a"' ) ;
2929 expect ( result ) . toContain ( '"b"' ) ;
3030 } ) ;
31+
32+ test ( "transforms {{key}} += value to compound assignment" , ( ) => {
33+ const result = transformCode ( "{{x}} += 2" ) ;
34+ expect ( result ) . toBe ( '__set(r, "x", __get(r, "x") + 2)' ) ;
35+ } ) ;
36+
37+ test ( "transforms {{key}} -= value to compound assignment" , ( ) => {
38+ const result = transformCode ( "{{x}} -= 1" ) ;
39+ expect ( result ) . toBe ( '__set(r, "x", __get(r, "x") - 1)' ) ;
40+ } ) ;
41+
42+ test ( "transforms {{key}} *= value to compound assignment" , ( ) => {
43+ const result = transformCode ( "{{x}} *= 3" ) ;
44+ expect ( result ) . toBe ( '__set(r, "x", __get(r, "x") * 3)' ) ;
45+ } ) ;
46+
47+ test ( "transforms {{key}} /= value to compound assignment" , ( ) => {
48+ const result = transformCode ( "{{x}} /= 2" ) ;
49+ expect ( result ) . toBe ( '__set(r, "x", __get(r, "x") / 2)' ) ;
50+ } ) ;
51+
52+ test ( "transforms {{key}} **= value to compound assignment" , ( ) => {
53+ const result = transformCode ( "{{x}} **= 2" ) ;
54+ expect ( result ) . toBe ( '__set(r, "x", __get(r, "x") ** 2)' ) ;
55+ } ) ;
56+
57+ test ( "transforms {{key}} ||= value to compound assignment" , ( ) => {
58+ const result = transformCode ( "{{x}} ||= 5" ) ;
59+ expect ( result ) . toBe ( '__set(r, "x", __get(r, "x") || 5)' ) ;
60+ } ) ;
61+
62+ test ( "transforms {{key}} &&= value to compound assignment" , ( ) => {
63+ const result = transformCode ( "{{x}} &&= 5" ) ;
64+ expect ( result ) . toBe ( '__set(r, "x", __get(r, "x") && 5)' ) ;
65+ } ) ;
66+
67+ test ( "transforms {{key}} ??= value to compound assignment" , ( ) => {
68+ const result = transformCode ( "{{x}} ??= 5" ) ;
69+ expect ( result ) . toBe ( '__set(r, "x", __get(r, "x") ?? 5)' ) ;
70+ } ) ;
71+
72+ test ( "transforms {{key}} >>= value to compound assignment" , ( ) => {
73+ const result = transformCode ( "{{x}} >>= 1" ) ;
74+ expect ( result ) . toBe ( '__set(r, "x", __get(r, "x") >> 1)' ) ;
75+ } ) ;
76+
77+ test ( "transforms {{key}} >>>= value to compound assignment" , ( ) => {
78+ const result = transformCode ( "{{x}} >>>= 1" ) ;
79+ expect ( result ) . toBe ( '__set(r, "x", __get(r, "x") >>> 1)' ) ;
80+ } ) ;
81+
82+ test ( "transforms {{key}} <<= value to compound assignment" , ( ) => {
83+ const result = transformCode ( "{{x}} <<= 1" ) ;
84+ expect ( result ) . toBe ( '__set(r, "x", __get(r, "x") << 1)' ) ;
85+ } ) ;
86+
87+ test ( "transforms {{key}} |= value to compound assignment" , ( ) => {
88+ const result = transformCode ( "{{x}} |= 3" ) ;
89+ expect ( result ) . toBe ( '__set(r, "x", __get(r, "x") | 3)' ) ;
90+ } ) ;
91+
92+ test ( "transforms {{key}} &= value to compound assignment" , ( ) => {
93+ const result = transformCode ( "{{x}} &= 3" ) ;
94+ expect ( result ) . toBe ( '__set(r, "x", __get(r, "x") & 3)' ) ;
95+ } ) ;
96+
97+ test ( "transforms {{key}} ^= value to compound assignment" , ( ) => {
98+ const result = transformCode ( "{{x}} ^= 3" ) ;
99+ expect ( result ) . toBe ( '__set(r, "x", __get(r, "x") ^ 3)' ) ;
100+ } ) ;
101+
102+ test ( "transforms {{key}} //= value to compound assignment" , ( ) => {
103+ const result = transformCode ( "{{x}} //= 10" ) ;
104+ expect ( result ) . toBe ( '__set(r, "x", __get(r, "x") // 10)' ) ;
105+ } ) ;
106+
107+ test ( "transforms {{key}} %= value to compound assignment" , ( ) => {
108+ const result = transformCode ( "{{x}} %= 3" ) ;
109+ expect ( result ) . toBe ( '__set(r, "x", __get(r, "x") % 3)' ) ;
110+ } ) ;
111+
112+ test ( "compound assignment with nested keyspec" , ( ) => {
113+ const result = transformCode ( "{{a/b}} += 1" ) ;
114+ expect ( result ) . toBe ( '__set(r, "a/b", __get(r, "a/b") + 1)' ) ;
115+ } ) ;
116+
117+ test ( "uses custom recordVar" , ( ) => {
118+ const result = transformCode ( "{{x}} += 1" , "$r" ) ;
119+ expect ( result ) . toBe ( '__set($r, "x", __get($r, "x") + 1)' ) ;
120+ } ) ;
121+
122+ test ( "uses custom recordVar for simple assign" , ( ) => {
123+ const result = transformCode ( "{{x}} = 5" , "$r" ) ;
124+ expect ( result ) . toBe ( '__set($r, "x", 5)' ) ;
125+ } ) ;
126+
127+ test ( "uses custom recordVar for read" , ( ) => {
128+ const result = transformCode ( "{{x}}" , "$r" ) ;
129+ expect ( result ) . toBe ( '__get($r, "x")' ) ;
130+ } ) ;
131+
132+ test ( "does not match == as assignment" , ( ) => {
133+ const result = transformCode ( "{{x}} == 5" ) ;
134+ expect ( result ) . not . toContain ( "__set" ) ;
135+ expect ( result ) . toContain ( "__get" ) ;
136+ } ) ;
137+
138+ test ( "does not match === as assignment" , ( ) => {
139+ const result = transformCode ( "{{x}} === 5" ) ;
140+ expect ( result ) . not . toContain ( "__set" ) ;
141+ expect ( result ) . toContain ( "__get" ) ;
142+ } ) ;
31143 } ) ;
32144
33145 describe ( "executeCode" , ( ) => {
@@ -62,6 +174,34 @@ describe("Executor", () => {
62174 expect ( data [ "new_field" ] ) . toBe ( "created" ) ;
63175 } ) ;
64176
177+ test ( "evaluates {{}} compound assignment +=" , ( ) => {
178+ const executor = new Executor ( "{{x}} += 10" ) ;
179+ const record = new Record ( { x : 5 } ) ;
180+ executor . executeCode ( record ) ;
181+ expect ( record . dataRef ( ) [ "x" ] ) . toBe ( 15 ) ;
182+ } ) ;
183+
184+ test ( "evaluates {{}} compound assignment -=" , ( ) => {
185+ const executor = new Executor ( "{{x}} -= 3" ) ;
186+ const record = new Record ( { x : 10 } ) ;
187+ executor . executeCode ( record ) ;
188+ expect ( record . dataRef ( ) [ "x" ] ) . toBe ( 7 ) ;
189+ } ) ;
190+
191+ test ( "evaluates {{}} compound assignment *=" , ( ) => {
192+ const executor = new Executor ( "{{x}} *= 4" ) ;
193+ const record = new Record ( { x : 3 } ) ;
194+ executor . executeCode ( record ) ;
195+ expect ( record . dataRef ( ) [ "x" ] ) . toBe ( 12 ) ;
196+ } ) ;
197+
198+ test ( "evaluates {{}} compound assignment **=" , ( ) => {
199+ const executor = new Executor ( "{{x}} **= 3" ) ;
200+ const record = new Record ( { x : 2 } ) ;
201+ executor . executeCode ( record ) ;
202+ expect ( record . dataRef ( ) [ "x" ] ) . toBe ( 8 ) ;
203+ } ) ;
204+
65205 test ( "provides $line counter" , ( ) => {
66206 const executor = new Executor ( "return $line" ) ;
67207 const r = new Record ( { } ) ;
0 commit comments