2
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
6
* The contents of this file are subject to the terms of either the GNU
7
* General Public License Version 2 only ("GPL") or the Common
8
* Development and Distribution License("CDDL") (collectively, the
9
* "License"). You may not use this file except in compliance with the
10
* License. You can obtain a copy of the License at
11
* http://www.netbeans.org/cddl-gplv2.html
12
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
* specific language governing permissions and limitations under the
14
* License. When distributing the software, include this License Header
15
* Notice in each file and include the License file at
16
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
17
* particular file as subject to the "Classpath" exception as provided
18
* by Sun in the GPL Version 2 section of the License file that
19
* accompanied this code. If applicable, add the following below the
20
* License Header, with the fields enclosed by brackets [] replaced by
21
* your own identifying information:
22
* "Portions Copyrighted [year] [name of copyright owner]"
26
* The Original Software is NetBeans. The Initial Developer of the Original
27
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
28
* Microsystems, Inc. All Rights Reserved.
30
* If you wish your version of this file to be governed by only the CDDL
31
* or only the GPL Version 2, indicate your decision by adding
32
* "[Contributor] elects to include this software in this distribution
33
* under the [CDDL or GPL Version 2] license." If you do not indicate a
34
* single choice of license, a recipient has the option to distribute
35
* your version of this file under either the CDDL, the GPL Version 2 or
36
* to extend the choice of license to its licensees as provided above.
37
* However, if you add GPL Version 2 code and therefore, elected the GPL
38
* Version 2 license, then the option applies only if the new code is
39
* made subject to such option by the copyright holder.
41
package org.netbeans.modules.php.model;
43
import java.util.List;
50
public class ForEachTest extends BaseCase {
52
public void testForEach() throws Exception {
53
PhpModel model = getModel();
57
List<ForEachStatement> children = model.getStatements(
58
ForEachStatement.class );
59
assert children.size() > 0 : "Expected at least one foreach statement";
60
ForEachStatement forEach = children.get( 0 );
62
assert !forEach.isAlternative() : "Foreach recognized as alternative, " +
65
assert forEach.getForEach()!= null: "Expected not null for each expression";
66
assert forEach.getStatements().size() == 1 :"Expected exactly one statement " +
67
" body for foreach, found :" +forEach.getStatements().size();
68
Statement statement = forEach.getStatements().get( 0 );
69
assert statement instanceof Block : "Expected Block as " +
70
"body statement in foreach";
72
assert statement.getElementType().equals( Block.class );
79
public void testForEachBody() throws Exception {
80
PhpModel model = getModel();
84
List<ForEachStatement> children = model.getStatements(
85
ForEachStatement.class );
86
ForEachStatement forEach = children.get( 0 );
87
Statement statement = forEach.getStatements().get( 0 );
88
Block block = (Block) statement;
89
List<Statement> list = block.getStatements();
91
assert list.size() > 0 : "Expected at least one statement in " +
92
" foreach body block";
94
assert list.get( 0 ) instanceof ExpressionStatement : "Expected expression " +
95
"statement, but found : " + list.get( 0 );
96
assert list.get( 0 ).getElementType().equals( ExpressionStatement.class );
98
ExpressionStatement stat = (ExpressionStatement)list.get( 0 );
99
Expression expression = stat.getExpression();
100
assert expression != null;
101
assert expression.getElementType().equals( BinaryExpression.class ) :
102
"Expected to find binary expression in foreach body, but found :"+
103
expression.getElementType();
105
BinaryExpression binary = (BinaryExpression) expression ;
106
Expression left = binary.getLeftOperand();
107
Expression right = binary.getRightOperand();
110
assert right != null;
117
public void testForEachHead() throws Exception {
118
PhpModel model = getModel();
122
List<ForEachStatement> children = model.getStatements(
123
ForEachStatement.class );
124
ForEachStatement forEachStatement = children.get( 0 );
125
ForEach forEach = forEachStatement.getForEach();
127
assert forEach != null ;
129
assert forEach.getElementType().equals( ForEach.class ) :
130
"Expected to find for each type , but found :" +forEach.getElementType();
132
VariableDeclaration index = forEach.getIndexVariable();
133
VariableDeclaration value = forEach.getValueVariable();
135
assert index == null: "Expected absence of index , but found index :" +index;
137
assert value != null;
138
assert value.getElementType().equals( VariableDeclaration.class ) :
139
"Expected variable declaration class , but found :" + value.getElementType();
141
Expression expression = forEach.getExpression();
143
assert expression != null;
144
assert expression.getElementType().equals( Variable.class ) :
145
"Expected to find variable as expression in for each but found :"
146
+expression.getElementType();