# HG changeset patch # User Michael Van De Vanter # Date 1449521492 28800 # Node ID 825d0d0301c9a923065d5c52ecb4dd17e649ddab # Parent 56a37a3ee60b22bfe64514b7520ddd279852bab0 Truffle/Source: add tests for LineLocations and a recent Source change diff -r 56a37a3ee60b -r 825d0d0301c9 truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/source/LineLocationTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/source/LineLocationTest.java Mon Dec 07 12:51:32 2015 -0800 @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2015, 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.source; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; + +import org.junit.Test; + +public class LineLocationTest { + + @Test + public void lineLocationLiteralTest() { + final Source source = Source.fromText("line 1\nline2\n", "lineLocationTest"); + final LineLocation line1 = source.createLineLocation(1); + final LineLocation line2 = source.createLineLocation(2); + assertEquals(line1.getLineNumber(), 1); + assertNotEquals(line1, line2); + assertEquals(line1, source.createLineLocation(1)); + assertEquals(line1.compareTo(line2), -1); + assertEquals(line1.compareTo(source.createLineLocation(1)), 0); + } + + @Test + public void lineLocationPathTest() throws IOException { + final Source s1 = createSourceFile("Hello1 line 1\nline2\n", "LineLocation1"); + final Source s2 = createSourceFile("Hello2 line 1\nline2\n", "LineLocation2"); + assertNotNull(s1); + assertNotNull(s2); + final LineLocation s1l1 = s1.createLineLocation(1); + final LineLocation s1l2 = s1.createLineLocation(2); + final LineLocation s2l1 = s2.createLineLocation(1); + assertEquals(s1l1.compareTo(s1l2), -1); + assertEquals(s1l1.compareTo(s2l1), -1); + assertEquals(s1l2.compareTo(s2l1), -1); + } + + @Test + public void lineLocationMixedTest() throws IOException { + final Source s1 = createSourceFile("same contents", "Testfile"); + final Source s2 = Source.fromText("same contents", "literal test source"); + final LineLocation s1l1 = s1.createLineLocation(1); + final LineLocation s2l1 = s2.createLineLocation(1); + assertEquals(s1l1.compareTo(s2l1), 0); + assertEquals(s2l1.compareTo(s1l1), 0); + } + + private static Source createSourceFile(String contents, String name) throws IOException { + final File file = File.createTempFile(name, ".txt"); + try (FileWriter w = new FileWriter(file)) { + w.write(contents); + } + file.deleteOnExit(); + return Source.fromFileName(file.getAbsolutePath()); + } +} diff -r 56a37a3ee60b -r 825d0d0301c9 truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/source/SourceTest.java --- a/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/source/SourceTest.java Mon Dec 07 12:49:17 2015 -0800 +++ b/truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/source/SourceTest.java Mon Dec 07 12:51:32 2015 -0800 @@ -138,4 +138,19 @@ assertNotEquals("But different type", s1.getMimeType(), s2.getMimeType()); assertNotEquals("So they are different", s1, s2); } + + @Test + public void literalSources() throws IOException { + final String code = "test code"; + final String description = "test description"; + final Source literal = Source.fromText(code, description); + assertEquals(literal.getName(), description); + assertEquals(literal.getShortName(), description); + assertEquals(literal.getCode(), code); + assertNull(literal.getPath()); + assertNull(literal.getURL()); + final char[] buffer = new char[code.length()]; + assertEquals(literal.getReader().read(buffer), code.length()); + assertEquals(new String(buffer), code); + } }