changeset 22282:088b3121f5ae

Giving Value instances toString() useful for debugging purposes.
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 07 Oct 2015 09:26:41 +0200
parents 137edaaad62f
children bc0c838ba705
files truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/ValueTest.java truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java
diffstat 2 files changed, 132 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/vm/ValueTest.java	Wed Oct 07 09:26:41 2015 +0200
@@ -0,0 +1,127 @@
+/*
+ * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.test.vm;
+
+import com.oracle.truffle.api.source.Source;
+import com.oracle.truffle.api.vm.PolyglotEngine;
+import java.io.IOException;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.concurrent.Executor;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import org.junit.Test;
+
+public class ValueTest implements Executor {
+    private List<Runnable> pending = new LinkedList<>();
+
+    @Test
+    public void valueToStringValue() throws Exception {
+        PolyglotEngine engine = PolyglotEngine.buildNew().build();
+        PolyglotEngine.Language language1 = engine.getLanguages().get("application/x-test-import-export-1");
+        PolyglotEngine.Language language2 = engine.getLanguages().get("application/x-test-import-export-2");
+        language2.eval(Source.fromText("explicit.value=42", "define 42"));
+        PolyglotEngine.Value value = language1.eval(Source.fromText("return=value", "42.value"));
+        assertEquals("It's fourtytwo", "42", value.get());
+
+        String textual = value.toString();
+        assertTrue("Contains the value " + textual, textual.contains("value=42"));
+        assertTrue("Is computed " + textual, textual.contains("computed=true"));
+        assertTrue("No error " + textual, textual.contains("exception=null"));
+    }
+
+    @Test
+    public void valueToStringException() throws Exception {
+        PolyglotEngine engine = PolyglotEngine.buildNew().build();
+        PolyglotEngine.Language language1 = engine.getLanguages().get("application/x-test-import-export-1");
+        PolyglotEngine.Value value = null;
+        try {
+            value = language1.eval(Source.fromText("parse=does not work", "error.value"));
+            Object res = value.get();
+            fail("Should throw an exception: " + res);
+        } catch (IOException ex) {
+            assertTrue("Message contains the right text: " + ex.getMessage(), ex.getMessage().contains("does not work"));
+        }
+
+        assertNull("No value returned", value);
+    }
+
+    @Test
+    public void valueToStringValueAsync() throws Exception {
+        PolyglotEngine engine = PolyglotEngine.buildNew().executor(this).build();
+        PolyglotEngine.Language language1 = engine.getLanguages().get("application/x-test-import-export-1");
+        PolyglotEngine.Language language2 = engine.getLanguages().get("application/x-test-import-export-2");
+        language2.eval(Source.fromText("explicit.value=42", "define 42"));
+        flush();
+
+        PolyglotEngine.Value value = language1.eval(Source.fromText("return=value", "42.value"));
+
+        String textual = value.toString();
+        assertFalse("Doesn't contain the value " + textual, textual.contains("value=42"));
+        assertTrue("Is not computed " + textual, textual.contains("computed=false"));
+        assertTrue("No error " + textual, textual.contains("exception=null"));
+        assertTrue("No value yet " + textual, textual.contains("value=null"));
+
+        flush();
+
+        textual = value.toString();
+        assertTrue("Is computed " + textual, textual.contains("computed=true"));
+        assertTrue("No error " + textual, textual.contains("exception=null"));
+        assertTrue("value computed " + textual, textual.contains("value=42"));
+    }
+
+    @Test
+    public void valueToStringExceptionAsync() throws Exception {
+        PolyglotEngine engine = PolyglotEngine.buildNew().executor(this).build();
+        PolyglotEngine.Language language1 = engine.getLanguages().get("application/x-test-import-export-1");
+        PolyglotEngine.Value value = language1.eval(Source.fromText("parse=does not work", "error.value"));
+        assertNotNull("Value returned", value);
+
+        String textual = value.toString();
+        assertTrue("Is not computed " + textual, textual.contains("computed=false"));
+        assertTrue("No error " + textual, textual.contains("exception=null"));
+        assertTrue("No value yet " + textual, textual.contains("value=null"));
+
+        flush();
+
+        textual = value.toString();
+        assertTrue("Is computed " + textual, textual.contains("computed=true"));
+        assertTrue("No value at all" + textual, textual.contains("value=null"));
+        assertTrue("Error " + textual, textual.contains("exception=java.io.IOException: does not work"));
+    }
+
+    @Override
+    public void execute(Runnable command) {
+        pending.add(command);
+    }
+
+    private void flush() {
+        for (Runnable r : pending) {
+            r.run();
+        }
+    }
+}
--- a/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java	Tue Oct 06 20:39:22 2015 +0200
+++ b/truffle/com.oracle.truffle.api.vm/src/com/oracle/truffle/api/vm/PolyglotEngine.java	Wed Oct 07 09:26:41 2015 +0200
@@ -801,6 +801,11 @@
                 throw (InterruptedIOException) new InterruptedIOException(ex.getMessage()).initCause(ex);
             }
         }
+
+        @Override
+        public String toString() {
+            return "PolyglotEngine.Value[value=" + result[0] + ",exception=" + result[1] + ",computed=" + (ready.getCount() == 0) + "]";
+        }
     }
 
     /**