view truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/vm/ValueTest.java @ 22425:b39b603e2a1e

Organize imports after rename.
author Christian Humer <christian.humer@oracle.com>
date Wed, 02 Dec 2015 15:21:01 +0100
parents fec62e298245
children
line wrap: on
line source

/*
 * 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.vm;

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 java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Executor;

import org.junit.Test;

import com.oracle.truffle.api.source.Source;

public class ValueTest implements Executor {
    private List<Runnable> pending = new LinkedList<>();

    @Test
    public void valueToStringValue() throws Exception {
        PolyglotEngine engine = PolyglotEngine.newBuilder().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.newBuilder().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.newBuilder().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.newBuilder().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();
        }
    }
}