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.test.*;
|
|
15
|
|
16 /**
|
|
17 * Test that methods can be redefined.
|
|
18 */
|
|
19 public class RedefinitionTests extends RubyTests {
|
|
20
|
|
21 /**
|
|
22 * Test that a method on Fixnum can be redefined and that we will use the new definition. The
|
|
23 * call that expects to find the redefinition needs to have already been specialized, which is
|
|
24 * why do it through a method.
|
|
25 */
|
|
26 @Test
|
|
27 public void testSimpleOperatorRedefinition() {
|
|
28 assertPrints("3\n-1\n", //
|
|
29 "def add(x, y)\n" + //
|
|
30 " x + y\n" + //
|
|
31 "end\n" + //
|
|
32 "puts add(1, 2)\n" + //
|
|
33 "class Fixnum\n" + //
|
|
34 " def +(other)\n" + //
|
|
35 " self - other\n" + //
|
|
36 " end\n" + //
|
|
37 "end\n" + //
|
|
38 "puts add(1, 2)\n");
|
|
39 }
|
|
40
|
|
41 /**
|
|
42 * This is quite a subtle test. We redefine Float#+. We have a method which calls +. It is
|
|
43 * initially specialized to Fixnum, but then used with Float. The tricky bit is that Float has
|
|
44 * not been redefined since the operator has been specialized. The operator thinks it is up to
|
|
45 * date. When we emit a + node, we need to check if any class that that node could specialize to
|
|
46 * has been redefined.
|
|
47 */
|
|
48 @Test
|
|
49 public void testSpecialisationConsidersRedefinitionInOtherClasses() {
|
|
50 assertPrints("16\n12.25\n", //
|
|
51 "def add(a, b)\n" + //
|
|
52 " a + b\n" + //
|
|
53 "end\n" + //
|
|
54 "class Float\n" + //
|
|
55 " def +(other)\n" + //
|
|
56 " self - other\n" + //
|
|
57 " end\n" + //
|
|
58 "end\n" + //
|
|
59 "puts add(14, 2)\n" + //
|
|
60 "puts add(14.5, 2.25)");
|
|
61 }
|
|
62
|
|
63 }
|