| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | describe("basic switch tests", () => { | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     test("string case does not match number target", () => { | 
					
						
							|  |  |  |         switch (1 + 2) { | 
					
						
							|  |  |  |             case "3": | 
					
						
							|  |  |  |                 expect().fail(); | 
					
						
							|  |  |  |             case 3: | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             case 5: | 
					
						
							|  |  |  |             case 1: | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |         expect().fail(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     test("string concatenation in target", () => { | 
					
						
							|  |  |  |         var a = "foo"; | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |         switch (a + "bar") { | 
					
						
							|  |  |  |             case 1: | 
					
						
							|  |  |  |                 expect().fail(); | 
					
						
							|  |  |  |             case "foobar": | 
					
						
							|  |  |  |             case 2: | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  |         expect().fail(); | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-07-05 09:27:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |     test("default", () => { | 
					
						
							|  |  |  |         switch (100) { | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-07-05 09:27:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-06 07:37:45 -07:00
										 |  |  |         expect().fail(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-10-18 17:44:55 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     test("return from switch statement", () => { | 
					
						
							|  |  |  |         function foo(value) { | 
					
						
							|  |  |  |             switch (value) { | 
					
						
							|  |  |  |                 case 42: | 
					
						
							|  |  |  |                     return "return from 'case 42'"; | 
					
						
							|  |  |  |                 default: | 
					
						
							|  |  |  |                     return "return from 'default'"; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         expect(foo(42)).toBe("return from 'case 42'"); | 
					
						
							|  |  |  |         expect(foo(43)).toBe("return from 'default'"); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-10-18 18:01:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     test("continue from switch statement", () => { | 
					
						
							|  |  |  |         let i = 0; | 
					
						
							|  |  |  |         for (; i < 5; ++i) { | 
					
						
							|  |  |  |             switch (i) { | 
					
						
							|  |  |  |                 case 0: | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                     expect().fail(); | 
					
						
							|  |  |  |                 case 0: | 
					
						
							|  |  |  |                     expect().fail(); | 
					
						
							|  |  |  |                 default: | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                     expect().fail(); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             expect().fail(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         expect(i).toBe(5); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-07-03 14:39:25 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-10-18 23:12:27 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe("errors", () => { | 
					
						
							|  |  |  |     test("syntax errors", () => { | 
					
						
							|  |  |  |         expect("switch () {}").not.toEval(); | 
					
						
							|  |  |  |         expect("switch (foo) { bar }").not.toEval(); | 
					
						
							|  |  |  |         expect("switch (foo) { default: default: }").not.toEval(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | }); |