Search in sources :

Example 96 with Source

use of com.oracle.truffle.api.source.Source in project graal by oracle.

the class SourceBuilderTest method clientManagedSourceChangeAbsolute.

@Test
public void clientManagedSourceChangeAbsolute() {
    final String path = new File("test.input").getAbsolutePath();
    final String code1 = "test\ntest";
    final String code2 = "test\ntest\nlonger\ntest";
    final Source source1 = Source.newBuilder(new File(path)).content(code1).mimeType("x-application/input").build();
    assertEquals(source1.getCharacters(), code1);
    assertEquals(source1.getLineNumber(code1.length() - 1), 2);
    final Source source2 = Source.newBuilder(new File(path)).content(code2).mimeType("x-application/input").build();
    assertEquals(source2.getCharacters(), code2);
    assertEquals(source2.getLineNumber(code2.length() - 1), 4);
    assertEquals("File URI", new File("test.input").getAbsoluteFile().toURI(), source1.getURI());
    assertEquals("File sources with different content have the same URI", source1.getURI(), source2.getURI());
}
Also used : File(java.io.File) Source(com.oracle.truffle.api.source.Source) Test(org.junit.Test)

Example 97 with Source

use of com.oracle.truffle.api.source.Source in project graal by oracle.

the class SourceBuilderTest method assignMimeTypeAndIdentityForFile.

@Test
public void assignMimeTypeAndIdentityForFile() throws IOException {
    File file = File.createTempFile("Hello", ".java").getCanonicalFile();
    file.deleteOnExit();
    String text;
    try (FileWriter w = new FileWriter(file)) {
        text = "// Hello";
        w.write(text);
    }
    // JDK8 default fails on OS X: https://bugs.openjdk.java.net/browse/JDK-8129632
    String nonCannonical = file.getParent() + File.separatorChar + ".." + File.separatorChar + file.getParentFile().getName() + File.separatorChar + file.getName();
    final File nonCannonicalFile = new File(nonCannonical);
    assertTrue("Exists, as it is the same file", nonCannonicalFile.exists());
    Source.Builder<IOException, RuntimeException, RuntimeException> builder = Source.newBuilder(nonCannonicalFile);
    Source s1 = builder.build();
    assertEquals("Path is cannonicalized", file.getPath(), s1.getPath());
    assertEquals("Name is short", file.getName(), s1.getName());
    assertEquals("Recognized as Java", "text/x-java", s1.getMimeType());
    Source s2 = builder.mimeType("text/x-c").build();
    assertEquals("They have the same content", s1.getCharacters(), s2.getCharacters());
    assertEquals("// Hello", s1.getCharacters());
    assertNotEquals("But different type", s1.getMimeType(), s2.getMimeType());
    assertNotEquals("So they are different", s1, s2);
    assertEquals("File URI is used from cannonicalized form", file.toURI(), s1.getURI());
    assertEquals("Sources with different MIME type has the same URI", s1.getURI(), s2.getURI());
}
Also used : FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) Source(com.oracle.truffle.api.source.Source) Test(org.junit.Test)

Example 98 with Source

use of com.oracle.truffle.api.source.Source in project graal by oracle.

the class SourceBuilderTest method subSourceHashAndEquals.

public void subSourceHashAndEquals() {
    Source src = Source.newBuilder("One Two Three").name("counting.en").mimeType("content/unknown").build();
    Source one = src.subSource(0, 3);
    Source two = src.subSource(4, 3);
    Source three = src.subSource(8, src.getLength() - 8);
    Source oneSnd = src.subSource(0, 3);
    Source twoSnd = src.subSource(4, 3);
    Source threeSnd = src.subSource(8, src.getLength() - 8);
    assertNotEquals("One: " + one.getCharacters() + " two: " + two.getCharacters(), one, two);
    assertNotEquals(three, two);
    assertNotEquals(one, three);
    assertNotEquals(oneSnd, twoSnd);
    assertEquals(one, oneSnd);
    assertEquals(two, twoSnd);
    assertEquals(three, threeSnd);
    assertEquals(one.hashCode(), oneSnd.hashCode());
    assertEquals(two.hashCode(), twoSnd.hashCode());
    assertEquals(three.hashCode(), threeSnd.hashCode());
    assertEquals(src.getURI(), one.getURI());
    assertEquals(src.getURI(), two.getURI());
    assertEquals(src.getURI(), three.getURI());
}
Also used : Source(com.oracle.truffle.api.source.Source)

Example 99 with Source

use of com.oracle.truffle.api.source.Source in project graal by oracle.

the class SourceBuilderTest method subSourceFromTwoFiles.

@Test
public void subSourceFromTwoFiles() throws Exception {
    File f1 = File.createTempFile("subSource", ".js").getCanonicalFile();
    File f2 = File.createTempFile("subSource", ".js").getCanonicalFile();
    try (FileWriter w = new FileWriter(f1)) {
        w.write("function test() {\n" + "  return 1;\n" + "}\n");
    }
    try (FileWriter w = new FileWriter(f2)) {
        w.write("function test() {\n" + "  return 1;\n" + "}\n");
    }
    Source s1 = Source.newBuilder(f1).build();
    Source s2 = Source.newBuilder(f2).build();
    assertNotEquals("Different sources", s1, s2);
    assertEquals("But same content", s1.getCharacters(), s2.getCharacters());
    Source sub1 = s1.subSource(0, 8);
    Source sub2 = s2.subSource(0, 8);
    assertNotEquals("Different sub sources", sub1, sub2);
    assertEquals("with the same content", sub1.getCharacters(), sub2.getCharacters());
    assertNotEquals("and different hash", sub1.hashCode(), sub2.hashCode());
    assertEquals(f1.toURI(), s1.getURI());
    assertEquals(s1.getURI(), sub1.getURI());
    assertEquals(f2.toURI(), s2.getURI());
    assertEquals(s2.getURI(), sub2.getURI());
}
Also used : FileWriter(java.io.FileWriter) File(java.io.File) Source(com.oracle.truffle.api.source.Source) Test(org.junit.Test)

Example 100 with Source

use of com.oracle.truffle.api.source.Source in project graal by oracle.

the class SourceBuilderTest method whatAreTheDefaultValuesOfNewFromReader.

@Test
public void whatAreTheDefaultValuesOfNewFromReader() throws Exception {
    StringReader r = new StringReader("Hi!");
    Source source = Source.newBuilder(r).name("almostEmpty").mimeType("text/plain").build();
    assertEquals("Hi!", source.getCharacters());
    assertEquals("almostEmpty", source.getName());
    assertNull(source.getPath());
    assertNotNull(source.getURI());
    assertTrue("URI ends with the name", source.getURI().toString().endsWith("almostEmpty"));
    assertEquals("truffle", source.getURI().getScheme());
    assertNull(source.getURL());
    assertEquals("text/plain", source.getMimeType());
}
Also used : StringReader(java.io.StringReader) Source(com.oracle.truffle.api.source.Source) Test(org.junit.Test)

Aggregations

Source (com.oracle.truffle.api.source.Source)113 Test (org.junit.Test)65 RootNode (com.oracle.truffle.api.nodes.RootNode)23 File (java.io.File)20 InstrumentableNode (com.oracle.truffle.api.instrumentation.InstrumentableNode)16 Node (com.oracle.truffle.api.nodes.Node)16 SourceSection (com.oracle.truffle.api.source.SourceSection)16 ProbeNode (com.oracle.truffle.api.instrumentation.ProbeNode)15 TruffleObject (com.oracle.truffle.api.interop.TruffleObject)11 SourceSectionFilter (com.oracle.truffle.api.instrumentation.SourceSectionFilter)8 IOException (java.io.IOException)8 ArrayList (java.util.ArrayList)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 CallTarget (com.oracle.truffle.api.CallTarget)5 FileWriter (java.io.FileWriter)5 RootCallTarget (com.oracle.truffle.api.RootCallTarget)4 TruffleContext (com.oracle.truffle.api.TruffleContext)3 Params (com.oracle.truffle.tools.chromeinspector.commands.Params)3 CommandProcessException (com.oracle.truffle.tools.chromeinspector.server.CommandProcessException)3 Script (com.oracle.truffle.tools.chromeinspector.types.Script)3