13514
|
1 /*
|
|
2 * Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved. This
|
|
3 * code is released under a tri EPL/GPL/LGPL license. You can use it,
|
|
4 * redistribute it and/or modify it under the terms of the:
|
|
5 *
|
|
6 * Eclipse Public License version 1.0
|
|
7 * GNU General Public License version 2
|
|
8 * GNU Lesser General Public License version 2.1
|
|
9 */
|
|
10 package com.oracle.truffle.ruby.test.language;
|
|
11
|
|
12 import org.junit.*;
|
|
13
|
|
14 import com.oracle.truffle.ruby.runtime.control.*;
|
|
15 import com.oracle.truffle.ruby.test.*;
|
|
16
|
|
17 /**
|
|
18 * Test method definitions and calls.
|
|
19 */
|
|
20 public class MethodTests extends RubyTests {
|
|
21
|
|
22 @Test
|
|
23 public void testDefineCallNoArguments() {
|
|
24 assertPrints("1\n", "def foo; puts 1; end; foo()");
|
|
25 assertPrints("1\n", "def foo; puts 1; end; foo");
|
|
26 }
|
|
27
|
|
28 @Test
|
|
29 public void testDefineCallOnePreArgument() {
|
|
30 assertPrints("1\n", "def foo(a); puts a; end; foo(1)");
|
|
31 assertPrints("1\n", "def foo(a); puts a; end; foo 1");
|
|
32 }
|
|
33
|
|
34 @Test
|
|
35 public void testDefineCallTwoPreArguments() {
|
|
36 assertPrints("1\n2\n", "def foo(a, b); puts a; puts b; end; foo(1, 2)");
|
|
37 assertPrints("1\n2\n", "def foo(a, b); puts a; puts b; end; foo 1, 2");
|
|
38 }
|
|
39
|
|
40 @Test
|
|
41 public void testSingleReturn() {
|
|
42 assertPrints("1\n", "def foo; return 1; end; puts foo");
|
|
43 assertPrints("1\n", "def foo(n); return n; end; puts foo(1)");
|
|
44 }
|
|
45
|
|
46 @Test
|
|
47 public void testImplicitReturn() {
|
|
48 assertPrints("1\n", "def foo; 1; end; puts foo");
|
|
49 assertPrints("3\n", "def foo; 1+2; end; puts foo");
|
|
50 assertPrints("14\n", "def foo; x=14; end; puts foo");
|
|
51 }
|
|
52
|
|
53 @Test
|
|
54 public void testNestedCall() {
|
|
55 assertPrints("1\n", "def foo(n); return n; end; def bar(n); return foo(n); end; puts bar(1)");
|
|
56 assertPrints("1\n1\n1\n", "def foo(n); puts n; return n; end; def bar(n); puts n; return foo(n); end; puts bar(1)");
|
|
57 assertPrints("1\n1\n", "def foo(a, b); puts a; puts b; end; def bar(n); foo(n, n); end; bar(1)");
|
|
58 }
|
|
59
|
|
60 @Test
|
|
61 public void testNestedOperatorCall() {
|
|
62 assertPrints("3\n", "def foo(a, b); puts a + b; end; foo(1, 2)");
|
|
63 }
|
|
64
|
|
65 /**
|
|
66 * Tests that arguments are evaluated before method dispatch takes place, by putting an action
|
|
67 * in one of the arguments that modifies the method that we should find in dispatch.
|
|
68 */
|
|
69 @Test
|
|
70 public void testArgumentsExecutedBeforeDispatch() {
|
|
71 /*
|
|
72 * We have to use Object#send instead of calling define_method directly because that method
|
|
73 * is private.
|
|
74 */
|
|
75
|
|
76 assertPrints("12\n", "def foo\n" + //
|
|
77 " Fixnum.send(:define_method, :+) do |other|\n" + //
|
|
78 " self - other\n" + //
|
|
79 " end \n" + //
|
|
80 " 2\n" + //
|
|
81 "end\n" + //
|
|
82 "puts 14 + foo");
|
|
83 }
|
|
84
|
|
85 @Test(expected = RaiseException.class)
|
|
86 public void testTooFewArguments1() {
|
|
87 assertPrints("", "def foo(a); end; foo()");
|
|
88 }
|
|
89
|
|
90 @Test(expected = RaiseException.class)
|
|
91 public void testTooFewArguments2() {
|
|
92 assertPrints("", "def foo(a, b); end; foo(1)");
|
|
93 }
|
|
94
|
|
95 @Test(expected = RaiseException.class)
|
|
96 public void testTooFewArguments3() {
|
|
97 assertPrints("", "def foo(a, b, c); end; foo(1, 2)");
|
|
98 }
|
|
99
|
|
100 @Test(expected = RaiseException.class)
|
|
101 public void testTooManyArguments1() {
|
|
102 assertPrints("", "def foo(); end; foo(1)");
|
|
103 }
|
|
104
|
|
105 @Test(expected = RaiseException.class)
|
|
106 public void testTooManyArguments2() {
|
|
107 assertPrints("", "def foo(a); end; foo(1, 2)");
|
|
108 }
|
|
109
|
|
110 @Test(expected = RaiseException.class)
|
|
111 public void testTooManyArguments3() {
|
|
112 assertPrints("", "def foo(a, b); end; foo(1, 2, 3)");
|
|
113 }
|
|
114
|
|
115 @Test
|
|
116 public void testPolymophicMethod() {
|
|
117 assertPrints("A\nB\n", "class A\n" + //
|
|
118 " def foo\n" + //
|
|
119 " puts \"A\"\n" + //
|
|
120 " end\n" + //
|
|
121 "end\n" + //
|
|
122 "\n" + //
|
|
123 "class B\n" + //
|
|
124 " def foo\n" + //
|
|
125 " puts \"B\"\n" + //
|
|
126 " end\n" + //
|
|
127 "end\n" + //
|
|
128 "\n" + //
|
|
129 "def bar(x)\n" + //
|
|
130 " x.foo\n" + //
|
|
131 "end\n" + //
|
|
132 "\n" + //
|
|
133 "a = A.new\n" + //
|
|
134 "b = B.new\n" + //
|
|
135 "\n" + //
|
|
136 "bar(a)\n" + //
|
|
137 "bar(b)\n");
|
|
138 }
|
|
139
|
|
140 @Test
|
|
141 public void testOneDefaultValue() {
|
|
142 assertPrints("1\n2\n", "def foo(a=1); puts a; end; foo; foo(2)");
|
|
143 }
|
|
144
|
|
145 @Test
|
|
146 public void testTwoDefaultValues() {
|
|
147 assertPrints("1\n2\n3\n2\n3\n4\n", "def foo(a=1,b=2); puts a; puts b; end; foo; foo(3); foo(3, 4)");
|
|
148 }
|
|
149
|
|
150 @Test
|
|
151 public void testOneDefaultValueAfterNonDefault() {
|
|
152 assertPrints("2\n1\n2\n3\n", "def foo(a, b=1); puts a; puts b; end; foo(2); foo(2, 3)");
|
|
153 }
|
|
154
|
|
155 @Test
|
|
156 public void testOneDefaultValueBeforeNonDefault() {
|
|
157 assertPrints("1\n2\n2\n3\n", "def foo(a=1, b); puts a; puts b; end; foo(2); foo(2, 3)");
|
|
158 }
|
|
159
|
|
160 @Test
|
|
161 public void testBlockArgument() {
|
|
162 assertPrints("1\n2\n3\n", "def foo(&block); block.call(14); end; puts 1; foo { |n| puts 2 }; puts 3");
|
|
163 }
|
|
164
|
|
165 @Test
|
|
166 public void testBlockArgumentWithOthers() {
|
|
167 assertPrints("1\n2\n3\n4\n5\n", "def foo(a, b, &block); puts a; block.call(14); puts b; end; puts 1; foo(2, 4) { |n| puts 3 }; puts 5");
|
|
168 }
|
|
169
|
|
170 @Test
|
|
171 public void testBlockPass() {
|
|
172 assertPrints("1\n2\n3\n", "def bar; yield; end; def foo(&block); bar(&block); end; puts 1; foo { puts 2 }; puts 3");
|
|
173 }
|
|
174
|
|
175 @Test
|
|
176 public void testBlockPassWithOthers() {
|
|
177 assertPrints("1\n2\n3\n4\n5\n", "def bar(a, b); puts a; yield; puts b; end; def foo(a, b, &block); bar(a, b, &block); end; puts 1; foo(2, 4) { puts 3 }; puts 5");
|
|
178 }
|
|
179
|
|
180 @Test
|
|
181 public void testSplatWhole() {
|
|
182 assertPrints("1\n2\n3\n", "def foo(a, b, c); puts a; puts b; puts c; end; d = [1, 2, 3]; foo(*d)");
|
|
183 }
|
|
184
|
|
185 @Test
|
|
186 public void testSplatSome() {
|
|
187 assertPrints("1\n2\n3\n", "def foo(a, b, c); puts a; puts b; puts c; end; d = [2, 3]; foo(1, *d)");
|
|
188 }
|
|
189
|
|
190 @Test
|
|
191 public void testSplatParam() {
|
|
192 assertPrints("[1, 2, 3]\n", "def foo(*bar); puts bar.to_s; end; foo(1, 2, 3)");
|
|
193 }
|
|
194
|
|
195 @Test
|
|
196 public void testSplatParamWithOther() {
|
|
197 assertPrints("1\n[2]\n", "def foo(a, *b); puts a.to_s; puts b.to_s; end; foo(1, 2)");
|
|
198 }
|
|
199
|
|
200 @Test
|
|
201 public void testSplatParamWithBlockPass() {
|
|
202 assertPrints("[1, 2, 3]\n", "def foo(*bar, &block); block.call(bar); end; foo(1, 2, 3) { |x| puts x.to_s }");
|
|
203 }
|
|
204
|
|
205 @Test
|
|
206 public void testSingletonMethod() {
|
|
207 assertPrints("1\n", "foo = Object.new; def foo.bar; puts 1; end; foo.bar");
|
|
208 }
|
|
209
|
|
210 @Test
|
|
211 public void testAlias() {
|
|
212 assertPrints("1\n", "def foo; puts 1; end; alias bar foo; bar");
|
|
213 assertPrints("1\n", "class Foo; def foo; puts 1; end; alias bar foo; end; Foo.new.bar");
|
|
214 }
|
|
215
|
|
216 @Test
|
|
217 public void testAliasMethod() {
|
|
218 assertPrints("1\n", "class Foo; def foo; puts 1; end; alias_method :bar, :foo; end; Foo.new.bar");
|
|
219 }
|
|
220
|
|
221 @Test
|
|
222 public void testSymbolAsBlock() {
|
|
223 assertPrints("6\n", "puts [1, 2, 3].inject(&:+)");
|
|
224 }
|
|
225
|
|
226 @Test
|
|
227 public void testSuper() {
|
|
228 assertPrints("1\n2\n3\n", "class Foo; def foo; puts 2; end; end; class Bar < Foo; def foo; puts 1; super; puts 3; end; end; Bar.new.foo");
|
|
229 }
|
|
230
|
|
231 @Test
|
|
232 public void testSuperWithArgs() {
|
|
233 assertPrints("1\n2\n3\n", "class Foo; def foo(n); puts n; end; end; class Bar < Foo; def foo(n); puts 1; super(n); puts 3; end; end; Bar.new.foo(2)");
|
|
234 }
|
|
235
|
|
236 @Test
|
|
237 public void testPushSplat() {
|
|
238 assertPrints("[1, 0, 4]\n", "a = [1, 2, 3, 4]; b = [1, 2]; a[*b] = 0; puts a.to_s");
|
|
239 }
|
|
240
|
|
241 @Test
|
|
242 public void testBlocksPassedIntoBlocks() {
|
|
243 assertPrints("14\n", "def foo; 1.times do; yield; end; end; foo do; puts 14; end");
|
|
244 }
|
|
245
|
|
246 @Test
|
|
247 public void testBlocksNotPassedIntoFullMethods() {
|
|
248 assertPrints("no block\n", "def foo(&block); if block; puts 'block'; else; puts 'no block'; end; end; def bar; foo; end; bar do; end");
|
|
249 }
|
|
250
|
|
251 }
|