diff graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/SourceSectionTest.java @ 16967:c5db657d93c1

Truffle-DSL: added test for source sections in @CreateCast methods.
author Christian Humer <christian.humer@gmail.com>
date Wed, 27 Aug 2014 11:35:59 +0200
parents f5541b01f374
children a665483c3881
line wrap: on
line diff
--- a/graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/SourceSectionTest.java	Wed Aug 27 09:14:19 2014 +0200
+++ b/graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/SourceSectionTest.java	Wed Aug 27 11:35:59 2014 +0200
@@ -26,12 +26,16 @@
 import static org.hamcrest.CoreMatchers.*;
 import static org.junit.Assert.*;
 
+import org.junit.*;
 import org.junit.experimental.theories.*;
 import org.junit.runner.*;
 
 import com.oracle.truffle.api.dsl.*;
 import com.oracle.truffle.api.dsl.test.SourceSectionTestFactory.SourceSection0Factory;
-import com.oracle.truffle.api.dsl.test.TypeSystemTest.*;
+import com.oracle.truffle.api.dsl.test.SourceSectionTestFactory.SourceSection1Factory;
+import com.oracle.truffle.api.dsl.test.TypeSystemTest.ArgumentNode;
+import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode;
+import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode;
 import com.oracle.truffle.api.nodes.*;
 import com.oracle.truffle.api.source.*;
 
@@ -54,7 +58,7 @@
         expectSourceSection(root.getNode(), section);
     }
 
-    private void expectSourceSection(Node root, SourceSection section) {
+    private static void expectSourceSection(Node root, SourceSection section) {
         assertThat(root.getSourceSection(), is(sameInstance(section)));
         for (Node child : root.getChildren()) {
             if (child instanceof ArgumentNode) {
@@ -101,4 +105,33 @@
             return a; // the generic answer to all questions
         }
     }
+
+    @Test
+    public void testCreateCast() {
+        SourceSection section = new NullSourceSection("a", "b");
+        TestRootNode<SourceSection1> root = createRootPrefix(SourceSection1Factory.getInstance(), true, section);
+        expectSourceSection(root.getNode(), section);
+        assertThat((int) executeWith(root, 1), is(1));
+        expectSourceSection(root.getNode(), section);
+    }
+
+    @NodeChild("a")
+    static class SourceSection1 extends ValueNode {
+
+        public SourceSection1(SourceSection section) {
+            super(section);
+        }
+
+        @CreateCast("a")
+        public ValueNode cast(ValueNode node) {
+            assert getSourceSection() != null;
+            return node;
+        }
+
+        @Specialization
+        int do0(int a) {
+            return a;
+        }
+
+    }
 }