# HG changeset patch # User Michael Van De Vanter # Date 1433117944 25200 # Node ID ae601ad0b023513103df5916600d022e634a583d # Parent 838f005f9aec49d68380a0fa086a3eff2d3b177c Truffle/Source: add some tests for SourceSections, including a reported design issue with empty Soruces. diff -r 838f005f9aec -r ae601ad0b023 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/source/SourceSectionTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/source/SourceSectionTest.java Sun May 31 17:19:04 2015 -0700 @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2013, 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.source; + +import static org.junit.Assert.*; + +import org.junit.*; + +import com.oracle.truffle.api.source.*; + +public class SourceSectionTest { + + private final Source emptySource = Source.fromText("", null); + + private final Source emptyLineSource = Source.fromText("\n", null); + + private final Source shortSource = Source.fromText("01", null); + + private final Source longSource = Source.fromText("01234\n67\n9\n", null); + + public void emptySourceTest0() { + SourceSection section = emptySource.createSection("test", 0, 0); + assertNotNull(section); + assertEquals(section.getCode(), ""); + } + + @Test + public void emptyLineTest0() { + SourceSection section = emptyLineSource.createSection("test", 0, 0); + assertNotNull(section); + assertEquals(section.getCode(), ""); + assertEquals(section.getCharIndex(), 0); + assertEquals(section.getCharLength(), 0); + assertEquals(section.getStartLine(), 1); + assertEquals(section.getStartColumn(), 1); + } + + @Ignore + @Test + public void emptyLineTest0a() { + SourceSection section = emptyLineSource.createSection("test", 0, 0); + assertEquals(section.getEndLine(), 1); + assertEquals(section.getEndColumn(), 1); + } + + @Test + public void emptyLineTest1() { + SourceSection section = emptyLineSource.createSection("test", 0, 1); + assertNotNull(section); + assertEquals(section.getCode(), "\n"); + assertEquals(section.getCharIndex(), 0); + assertEquals(section.getCharLength(), 1); + assertEquals(section.getStartLine(), 1); + assertEquals(section.getStartColumn(), 1); + assertEquals(section.getEndLine(), 1); + assertEquals(section.getEndColumn(), 1); + } + + @Ignore + @Test + public void emptyLineTest2() { + SourceSection section = emptyLineSource.createSection("test", 1, 0); + assertNotNull(section); + assertEquals(section.getCode(), ""); + assertEquals(section.getCharIndex(), 1); + assertEquals(section.getCharLength(), 0); + assertEquals(section.getStartLine(), 1); + assertEquals(section.getStartColumn(), 1); + assertEquals(section.getEndLine(), 1); + assertEquals(section.getEndColumn(), 1); + } + + @Test + public void emptySectionTest2() { + SourceSection section = shortSource.createSection("test", 0, 0); + assertNotNull(section); + assertEquals(section.getCode(), ""); + } + + @Test + public void emptySectionTest3() { + SourceSection section = longSource.createSection("test", 0, 0); + assertNotNull(section); + assertEquals(section.getCode(), ""); + } + +} diff -r 838f005f9aec -r ae601ad0b023 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/source/SourceTagTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/source/SourceTagTest.java Sun May 31 17:19:04 2015 -0700 @@ -0,0 +1,162 @@ +/* + * Copyright (c) 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.source; + +import static org.junit.Assert.*; + +import org.junit.*; + +import com.oracle.truffle.api.source.*; + +public class SourceTagTest { + + @Test + public void sourceTagTest() { + + // Private tag + final SourceTag testTag = new SourceTag() { + + public String name() { + return null; + } + + public String getDescription() { + return null; + } + }; + + // No sources exist with the private tag + assertEquals(Source.findSourcesTaggedAs(testTag).size(), 0); + + // Create a new source + final Source source = Source.fromText("test1 source", "test1 source"); + + // Initially has only the default tag + assertEquals(source.getSourceTags().size(), 1); + + assertTrue(source.getSourceTags().contains(Source.Tags.FROM_LITERAL)); + assertTrue(source.isTaggedAs(Source.Tags.FROM_LITERAL)); + assertTrue(Source.findSourcesTaggedAs(Source.Tags.FROM_LITERAL).contains(source)); + + assertFalse(source.isTaggedAs(testTag)); + assertEquals(Source.findSourcesTaggedAs(testTag).size(), 0); + + // Add a private tag + source.tagAs(testTag); + + // Now there are exactly two tags + assertEquals(source.getSourceTags().size(), 2); + + assertTrue(source.getSourceTags().contains(Source.Tags.FROM_LITERAL)); + assertTrue(source.isTaggedAs(Source.Tags.FROM_LITERAL)); + assertTrue(Source.findSourcesTaggedAs(Source.Tags.FROM_LITERAL).contains(source)); + + assertTrue(source.getSourceTags().contains(testTag)); + assertTrue(source.isTaggedAs(testTag)); + assertEquals(Source.findSourcesTaggedAs(testTag).size(), 1); + assertTrue(Source.findSourcesTaggedAs(testTag).contains(source)); + + // Add the private tag again + source.tagAs(testTag); + + // Nothing has changed + assertEquals(source.getSourceTags().size(), 2); + + assertTrue(source.getSourceTags().contains(Source.Tags.FROM_LITERAL)); + assertTrue(source.isTaggedAs(Source.Tags.FROM_LITERAL)); + assertTrue(Source.findSourcesTaggedAs(Source.Tags.FROM_LITERAL).contains(source)); + + assertTrue(source.getSourceTags().contains(testTag)); + assertTrue(source.isTaggedAs(testTag)); + assertEquals(Source.findSourcesTaggedAs(testTag).size(), 1); + assertTrue(Source.findSourcesTaggedAs(testTag).contains(source)); + } + + @Test + public void sourceListenerTest() { + + // Private tag + final SourceTag testTag = new SourceTag() { + + public String name() { + return null; + } + + public String getDescription() { + return null; + } + }; + + final int[] newSourceEvents = {0}; + final Source[] newSource = {null}; + + final int[] newTagEvents = {0}; + final Source[] taggedSource = {null}; + final SourceTag[] newTag = {null}; + + Source.addSourceListener(new SourceListener() { + + public void sourceCreated(Source source) { + newSourceEvents[0] = newSourceEvents[0] + 1; + newSource[0] = source; + } + + public void sourceTaggedAs(Source source, SourceTag tag) { + newTagEvents[0] = newTagEvents[0] + 1; + taggedSource[0] = source; + newTag[0] = tag; + } + }); + + // New source has a default tag applied. + // Get one event for the new source, another one when it gets tagged + final Source source = Source.fromText("testSource", "testSource"); + assertEquals(newSourceEvents[0], 1); + assertEquals(newSource[0], source); + assertEquals(newTagEvents[0], 1); + assertEquals(taggedSource[0], source); + assertEquals(newTag[0], Source.Tags.FROM_LITERAL); + + // reset + newSource[0] = null; + taggedSource[0] = null; + newTag[0] = null; + + // Add a tag; only get one event (the new tag) + source.tagAs(testTag); + assertEquals(newSourceEvents[0], 1); + assertEquals(newSource[0], null); + assertEquals(newTagEvents[0], 2); + assertEquals(taggedSource[0], source); + assertEquals(newTag[0], testTag); + + // Add the same tag; no events, and nothing changes. + source.tagAs(testTag); + assertEquals(newSourceEvents[0], 1); + assertEquals(newSource[0], null); + assertEquals(newTagEvents[0], 2); + assertEquals(taggedSource[0], source); + assertEquals(newTag[0], testTag); + + } +} diff -r 838f005f9aec -r ae601ad0b023 graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/source/SourceTest.java --- a/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/source/SourceTest.java Sun May 31 14:30:13 2015 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,162 +0,0 @@ -/* - * Copyright (c) 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.source; - -import static org.junit.Assert.*; - -import org.junit.*; - -import com.oracle.truffle.api.source.*; - -public class SourceTest { - - @Test - public void sourceTagTest() { - - // Private tag - final SourceTag testTag = new SourceTag() { - - public String name() { - return null; - } - - public String getDescription() { - return null; - } - }; - - // No sources exist with the private tag - assertEquals(Source.findSourcesTaggedAs(testTag).size(), 0); - - // Create a new source - final Source source = Source.fromText("test1 source", "test1 source"); - - // Initially has only the default tag - assertEquals(source.getSourceTags().size(), 1); - - assertTrue(source.getSourceTags().contains(Source.Tags.FROM_LITERAL)); - assertTrue(source.isTaggedAs(Source.Tags.FROM_LITERAL)); - assertTrue(Source.findSourcesTaggedAs(Source.Tags.FROM_LITERAL).contains(source)); - - assertFalse(source.isTaggedAs(testTag)); - assertEquals(Source.findSourcesTaggedAs(testTag).size(), 0); - - // Add a private tag - source.tagAs(testTag); - - // Now there are exactly two tags - assertEquals(source.getSourceTags().size(), 2); - - assertTrue(source.getSourceTags().contains(Source.Tags.FROM_LITERAL)); - assertTrue(source.isTaggedAs(Source.Tags.FROM_LITERAL)); - assertTrue(Source.findSourcesTaggedAs(Source.Tags.FROM_LITERAL).contains(source)); - - assertTrue(source.getSourceTags().contains(testTag)); - assertTrue(source.isTaggedAs(testTag)); - assertEquals(Source.findSourcesTaggedAs(testTag).size(), 1); - assertTrue(Source.findSourcesTaggedAs(testTag).contains(source)); - - // Add the private tag again - source.tagAs(testTag); - - // Nothing has changed - assertEquals(source.getSourceTags().size(), 2); - - assertTrue(source.getSourceTags().contains(Source.Tags.FROM_LITERAL)); - assertTrue(source.isTaggedAs(Source.Tags.FROM_LITERAL)); - assertTrue(Source.findSourcesTaggedAs(Source.Tags.FROM_LITERAL).contains(source)); - - assertTrue(source.getSourceTags().contains(testTag)); - assertTrue(source.isTaggedAs(testTag)); - assertEquals(Source.findSourcesTaggedAs(testTag).size(), 1); - assertTrue(Source.findSourcesTaggedAs(testTag).contains(source)); - } - - @Test - public void sourceListenerTest() { - - // Private tag - final SourceTag testTag = new SourceTag() { - - public String name() { - return null; - } - - public String getDescription() { - return null; - } - }; - - final int[] newSourceEvents = {0}; - final Source[] newSource = {null}; - - final int[] newTagEvents = {0}; - final Source[] taggedSource = {null}; - final SourceTag[] newTag = {null}; - - Source.addSourceListener(new SourceListener() { - - public void sourceCreated(Source source) { - newSourceEvents[0] = newSourceEvents[0] + 1; - newSource[0] = source; - } - - public void sourceTaggedAs(Source source, SourceTag tag) { - newTagEvents[0] = newTagEvents[0] + 1; - taggedSource[0] = source; - newTag[0] = tag; - } - }); - - // New source has a default tag applied. - // Get one event for the new source, another one when it gets tagged - final Source source = Source.fromText("testSource", "testSource"); - assertEquals(newSourceEvents[0], 1); - assertEquals(newSource[0], source); - assertEquals(newTagEvents[0], 1); - assertEquals(taggedSource[0], source); - assertEquals(newTag[0], Source.Tags.FROM_LITERAL); - - // reset - newSource[0] = null; - taggedSource[0] = null; - newTag[0] = null; - - // Add a tag; only get one event (the new tag) - source.tagAs(testTag); - assertEquals(newSourceEvents[0], 1); - assertEquals(newSource[0], null); - assertEquals(newTagEvents[0], 2); - assertEquals(taggedSource[0], source); - assertEquals(newTag[0], testTag); - - // Add the same tag; no events, and nothing changes. - source.tagAs(testTag); - assertEquals(newSourceEvents[0], 1); - assertEquals(newSource[0], null); - assertEquals(newTagEvents[0], 2); - assertEquals(taggedSource[0], source); - assertEquals(newTag[0], testTag); - - } -}