| 
									
										
										
										
											2021-03-15 21:42:44 +01:00
										 |  |  | test("basic eval() functionality", () => { | 
					
						
							|  |  |  |     expect(eval("1 + 2")).toBe(3); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function foo(a) { | 
					
						
							|  |  |  |         var x = 5; | 
					
						
							|  |  |  |         eval("x += a"); | 
					
						
							|  |  |  |         return x; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     expect(foo(7)).toBe(12); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-03-15 22:08:28 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-16 09:12:34 +01:00
										 |  |  | test("returns value of last value-producing statement", () => { | 
					
						
							|  |  |  |     // See https://tc39.es/ecma262/#sec-block-runtime-semantics-evaluation
 | 
					
						
							| 
									
										
										
										
											2021-04-25 22:52:19 +02:00
										 |  |  |     expect(eval("")).toBeUndefined(); | 
					
						
							| 
									
										
										
										
											2021-03-16 09:12:34 +01:00
										 |  |  |     expect(eval("1;;;;;")).toBe(1); | 
					
						
							|  |  |  |     expect(eval("1;{}")).toBe(1); | 
					
						
							|  |  |  |     expect(eval("1;var a;")).toBe(1); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-15 22:08:28 +01:00
										 |  |  | test("syntax error", () => { | 
					
						
							|  |  |  |     expect(() => { | 
					
						
							|  |  |  |         eval("{"); | 
					
						
							|  |  |  |     }).toThrowWithMessage( | 
					
						
							|  |  |  |         SyntaxError, | 
					
						
							|  |  |  |         "Unexpected token Eof. Expected CurlyClose (line: 1, column: 2)" | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-03-17 20:57:29 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | test("returns 1st argument unless 1st argument is a string", () => { | 
					
						
							| 
									
										
										
										
											2021-03-17 21:08:54 +01:00
										 |  |  |     var stringObject = new String("1 + 2"); | 
					
						
							|  |  |  |     expect(eval(stringObject)).toBe(stringObject); | 
					
						
							| 
									
										
										
										
											2021-03-17 20:57:29 +01:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2021-06-19 20:13:53 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | // These eval scope tests use function expressions due to bug #8198
 | 
					
						
							|  |  |  | var testValue = "outer"; | 
					
						
							|  |  |  | test("eval only touches locals if direct use", function () { | 
					
						
							|  |  |  |     var testValue = "inner"; | 
					
						
							|  |  |  |     expect(globalThis.eval("testValue")).toEqual("outer"); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("alias to eval works as a global eval", function () { | 
					
						
							|  |  |  |     var testValue = "inner"; | 
					
						
							|  |  |  |     var eval1 = globalThis.eval; | 
					
						
							|  |  |  |     expect(eval1("testValue")).toEqual("outer"); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("eval evaluates all args", function () { | 
					
						
							|  |  |  |     var i = 0; | 
					
						
							|  |  |  |     expect(eval("testValue", i++, i++, i++)).toEqual("outer"); | 
					
						
							|  |  |  |     expect(i).toEqual(3); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("eval tests for exceptions", function () { | 
					
						
							|  |  |  |     var i = 0; | 
					
						
							|  |  |  |     expect(function () { | 
					
						
							|  |  |  |         eval("testValue", i++, i++, j, i++); | 
					
						
							|  |  |  |     }).toThrowWithMessage(ReferenceError, "'j' is not defined"); | 
					
						
							|  |  |  |     expect(i).toEqual(2); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("direct eval inherits non-strict evaluation", function () { | 
					
						
							|  |  |  |     expect(eval("01")).toEqual(1); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("direct eval inherits strict evaluation", function () { | 
					
						
							|  |  |  |     "use strict"; | 
					
						
							|  |  |  |     expect(() => { | 
					
						
							|  |  |  |         eval("01"); | 
					
						
							|  |  |  |     }).toThrowWithMessage(SyntaxError, "Unprefixed octal number not allowed in strict mode"); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | test("global eval evaluates as non-strict", function () { | 
					
						
							|  |  |  |     "use strict"; | 
					
						
							|  |  |  |     expect(globalThis.eval("01")); | 
					
						
							|  |  |  | }); |