~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to scripting/php/srcmodel/test/unit/src/org/netbeans/modules/php/model/ForEachTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
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]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
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.
 
29
 *
 
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.
 
40
 */
 
41
package org.netbeans.modules.php.model;
 
42
 
 
43
import java.util.List;
 
44
 
 
45
 
 
46
/**
 
47
 * @author ads
 
48
 *
 
49
 */
 
50
public class ForEachTest extends BaseCase {
 
51
 
 
52
    public void testForEach() throws Exception {
 
53
        PhpModel model = getModel();
 
54
        model.sync();
 
55
        model.readLock();
 
56
        try {
 
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 );
 
61
            
 
62
            assert !forEach.isAlternative() : "Foreach recognized as alternative, " +
 
63
                    "but shouldn't";
 
64
            
 
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";
 
71
            
 
72
            assert statement.getElementType().equals( Block.class ); 
 
73
        }
 
74
        finally {
 
75
            model.readUnlock();
 
76
        }
 
77
    }
 
78
    
 
79
    public void testForEachBody() throws Exception {
 
80
        PhpModel model = getModel();
 
81
        model.sync();
 
82
        model.readLock();
 
83
        try {
 
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();
 
90
            
 
91
            assert list.size() > 0 : "Expected at least one statement in " +
 
92
                    " foreach body block";
 
93
            
 
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 );
 
97
            
 
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();
 
104
            
 
105
            BinaryExpression binary = (BinaryExpression) expression ;
 
106
            Expression left = binary.getLeftOperand();
 
107
            Expression right = binary.getRightOperand();
 
108
            
 
109
            assert left != null;
 
110
            assert right != null;
 
111
        }
 
112
        finally {
 
113
            model.readUnlock();
 
114
        }
 
115
    }
 
116
    
 
117
    public void testForEachHead() throws Exception {
 
118
        PhpModel model = getModel();
 
119
        model.sync();
 
120
        model.readLock();
 
121
        try {
 
122
            List<ForEachStatement> children = model.getStatements( 
 
123
                    ForEachStatement.class );
 
124
            ForEachStatement forEachStatement = children.get( 0 );
 
125
            ForEach forEach = forEachStatement.getForEach();
 
126
            
 
127
            assert forEach != null ;
 
128
            
 
129
            assert forEach.getElementType().equals( ForEach.class ) :
 
130
                "Expected to find for each type , but found :" +forEach.getElementType();
 
131
            
 
132
            VariableDeclaration index = forEach.getIndexVariable();
 
133
            VariableDeclaration value = forEach.getValueVariable();
 
134
 
 
135
            assert index == null: "Expected absence of index , but found index :" +index;
 
136
 
 
137
            assert value != null;
 
138
            assert value.getElementType().equals( VariableDeclaration.class ) :
 
139
                "Expected variable declaration class , but found :" + value.getElementType();
 
140
            
 
141
            Expression expression = forEach.getExpression();
 
142
 
 
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();
 
147
            
 
148
        }
 
149
        finally {
 
150
            model.readUnlock();
 
151
        }
 
152
    }
 
153
}