@@ -136,3 +136,43 @@ test("Array property access", () => {
136136 ` ;
137137 expect ( util . transpileAndExecute ( code ) ) . toBe ( "bar123" ) ;
138138} ) ;
139+
140+ test . each ( [ { length : 0 , result : 0 } , { length : 1 , result : 1 } , { length : 7 , result : 3 } ] ) (
141+ "Array length set" ,
142+ ( { length, result } ) => {
143+ const code = `
144+ const arr = [1, 2, 3];
145+ arr.length = ${ length } ;
146+ return arr.length;
147+ ` ;
148+ expect ( util . transpileAndExecute ( code ) ) . toBe ( result ) ;
149+ } ,
150+ ) ;
151+
152+ test . each ( [
153+ { length : 0 , result : "0/0" } ,
154+ { length : 1 , result : "1/1" } ,
155+ { length : 7 , result : "7/3" } ,
156+ ] ) ( "Array length set as expression" , ( { length, result } ) => {
157+ const code = `
158+ const arr = [1, 2, 3];
159+ const l = arr.length = ${ length } ;
160+ return \`\${l}/\${arr.length}\`;
161+ ` ;
162+ expect ( util . transpileAndExecute ( code ) ) . toBe ( result ) ;
163+ } ) ;
164+
165+ test . each ( [
166+ { length : - 1 , result : - 1 } ,
167+ { length : - 7 , result : - 7 } ,
168+ { length : 0.1 , result : 0.1 } ,
169+ { length : "0 / 0" , result : "NaN" } ,
170+ { length : "1 / 0" , result : "Infinity" } ,
171+ { length : "-1 / 0" , result : "-Infinity" } ,
172+ ] ) ( "Invalid array length set" , ( { length, result } ) => {
173+ const code = `
174+ const arr = [1, 2, 3];
175+ arr.length = ${ length } ;
176+ ` ;
177+ expect ( ( ) => util . transpileAndExecute ( code ) ) . toThrowError ( `invalid array length: ${ result } ` ) ;
178+ } ) ;
0 commit comments