Debian Codegraph @

test/com/google/javascript/rhino/IRTest.java

raw symbols SCIP
1 /*
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is Rhino code, released
17 * May 6, 1999.
18 *
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 1997-1999
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 * John Lenz
26 *
27 * Alternatively, the contents of this file may be used under the terms of
28 * the GNU General Public License Version 2 or later (the "GPL"), in which
29 * case the provisions of the GPL are applicable instead of those above. If
30 * you wish to allow use of your version of this file only under the terms of
31 * the GPL and not to allow others to use your version of this file under the
32 * MPL, indicate your decision by deleting the provisions above and replacing
33 * them with the notice and other provisions required by the GPL. If you do
34 * not delete the provisions above, a recipient may use your version of this
35 * file under either the MPL or the GPL.
36 *
37 * ***** END LICENSE BLOCK ***** */
38
39 package com.google.javascript.rhino;
40
41 import com.google.common.collect.Lists;
42
43 import junit.framework.TestCase;
44
45 /**
46 * @author [email protected] (John Lenz)
47 */
48 public class IRTest extends TestCase {
49
50 public void testEmpty() {
51 testIR(IR.empty(), "EMPTY\n");
52 }
53
54 public void testFunction() {
55 testIR(IR.function(IR.name("hi"), IR.paramList(), IR.block()),
56 "FUNCTION hi\n" +
57 " NAME hi\n" +
58 " PARAM_LIST\n" +
59 " BLOCK\n");
60 }
61
62 public void testParamList() {
63 testIR(IR.paramList(),
64 "PARAM_LIST\n");
65
66 testIR(IR.paramList(IR.name("a"), IR.name("b")),
67 "PARAM_LIST\n" +
68 " NAME a\n" +
69 " NAME b\n");
70
71 testIR(IR.paramList(Lists.newArrayList(IR.name("a"), IR.name("b"))),
72 "PARAM_LIST\n" +
73 " NAME a\n" +
74 " NAME b\n");
75 }
76
77 public void testBlock() {
78 testIR(IR.block(),
79 "BLOCK\n");
80
81 testIR(IR.block(IR.empty(), IR.empty()),
82 "BLOCK\n" +
83 " EMPTY\n" +
84 " EMPTY\n");
85
86 testIR(IR.block(Lists.newArrayList(IR.empty(), IR.empty())),
87 "BLOCK\n" +
88 " EMPTY\n" +
89 " EMPTY\n");
90 }
91
92 public void testScript() {
93 testIR(IR.script(),
94 "SCRIPT\n");
95
96 testIR(IR.script(IR.empty(), IR.empty()),
97 "SCRIPT\n" +
98 " EMPTY\n" +
99 " EMPTY\n");
100
101 testIR(IR.script(Lists.newArrayList(IR.empty(), IR.empty())),
102 "SCRIPT\n" +
103 " EMPTY\n" +
104 " EMPTY\n");
105 }
106
107 public void testScriptThrows() {
108 boolean caught = false;
109 try {
110 IR.script(IR.returnNode());
111 } catch(IllegalStateException e) {
112 caught = true;
113 }
114 assertTrue("expected exception was not seen", caught);
115 }
116
117 public void testVar() {
118 testIR(IR.var(IR.name("a")),
119 "VAR\n" +
120 " NAME a\n");
121
122 testIR(IR.var(IR.name("a"), IR.trueNode()),
123 "VAR\n" +
124 " NAME a\n" +
125 " TRUE\n");
126 }
127
128 public void testReturn() {
129 testIR(IR.returnNode(),
130 "RETURN\n");
131
132 testIR(IR.returnNode(IR.name("a")),
133 "RETURN\n" +
134 " NAME a\n");
135 }
136
137 public void testThrow() {
138 testIR(IR.throwNode(IR.name("a")),
139 "THROW\n" +
140 " NAME a\n");
141 }
142
143 public void testExprResult() {
144 testIR(IR.exprResult(IR.name("a")),
145 "EXPR_RESULT\n" +
146 " NAME a\n");
147 }
148
149 public void testIf() {
150 testIR(IR.ifNode(IR.name("a"), IR.block()),
151 "IF\n" +
152 " NAME a\n" +
153 " BLOCK\n");
154
155 testIR(IR.ifNode(IR.name("a"), IR.block(), IR.block()),
156 "IF\n" +
157 " NAME a\n" +
158 " BLOCK\n" +
159 " BLOCK\n");
160 }
161
162 public void testIssue727_1() {
163 testIR(
164 IR.tryFinally(
165 IR.block(),
166 IR.block()),
167 "TRY\n" +
168 " BLOCK\n" +
169 " BLOCK\n" +
170 " BLOCK\n");
171 }
172
173 public void testIssue727_2() {
174 testIR(
175 IR.tryCatch(
176 IR.block(),
177 IR.catchNode(
178 IR.name("e"),
179 IR.block())),
180 "TRY\n" +
181 " BLOCK\n" +
182 " BLOCK\n" +
183 " CATCH\n" +
184 " NAME e\n" +
185 " BLOCK\n");
186 }
187
188 public void testIssue727_3() {
189 testIR(
190 IR.tryCatchFinally(
191 IR.block(),
192 IR.catchNode(IR.name("e"), IR.block()),
193 IR.block()),
194 "TRY\n" +
195 " BLOCK\n" +
196 " BLOCK\n" +
197 " CATCH\n" +
198 " NAME e\n" +
199 " BLOCK\n" +
200 " BLOCK\n");
201 }
202
203 public void testAdd() {
204 testIR(
205 IR.add(
206 IR.cast(IR.number(1)),
207 IR.number(2)),
208 "ADD\n" +
209 " CAST\n" +
210 " NUMBER 1.0\n" +
211 " NUMBER 2.0\n");
212
213 }
214
215 private void testIR(Node node, String expectedStructure) {
216 assertEquals(expectedStructure, node.toStringTree());
217 }
218 }