| 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 | |
| 6 | gTestfile = 'expression-001.js'; |
| 7 | |
| 8 | /** |
| 9 | File Name: expression-001.js |
| 10 | Corresponds to: ecma/Expressions/11.12-2-n.js |
| 11 | ECMA Section: 11.12 |
| 12 | Description: |
| 13 | |
| 14 | The grammar for a ConditionalExpression in ECMAScript is a little bit |
| 15 | different from that in C and Java, which each allow the second |
| 16 | subexpression to be an Expression but restrict the third expression to |
| 17 | be a ConditionalExpression. The motivation for this difference in |
| 18 | ECMAScript is to allow an assignment expression to be governed by either |
| 19 | arm of a conditional and to eliminate the confusing and fairly useless |
| 20 | case of a comma expression as the center expression. |
| 21 | |
| 22 | Author: [email protected] |
| 23 | Date: 09 september 1998 |
| 24 | */ |
| 25 | var SECTION = "expression-001"; |
| 26 | var VERSION = "JS1_4"; |
| 27 | var TITLE = "Conditional operator ( ? : )" |
| 28 | startTest(); |
| 29 | writeHeaderToLog( SECTION + " " + TITLE ); |
| 30 | |
| 31 | // the following expression should be an error in JS. |
| 32 | |
| 33 | var result = "Failed" |
| 34 | var exception = "No exception was thrown"; |
| 35 | |
| 36 | try { |
| 37 | eval("var MY_VAR = true ? \"EXPR1\", \"EXPR2\" : \"EXPR3\""); |
| 38 | } catch ( e ) { |
| 39 | result = "Passed"; |
| 40 | exception = e.toString(); |
| 41 | } |
| 42 | |
| 43 | new TestCase( |
| 44 | SECTION, |
| 45 | "comma expression in a conditional statement "+ |
| 46 | "(threw "+ exception +")", |
| 47 | "Passed", |
| 48 | result ); |
| 49 | |
| 50 | |
| 51 | test(); |