001: /*
002: $Id: SourceBufferTest.java 2250 2005-06-04 09:39:18Z jez $
003:
004: Copyright 2005 (C) Jeremy Rayner. All Rights Reserved.
005:
006: Redistribution and use of this software and associated documentation
007: ("Software"), with or without modification, are permitted provided
008: that the following conditions are met:
009:
010: 1. Redistributions of source code must retain copyright
011: statements and notices. Redistributions must also contain a
012: copy of this document.
013:
014: 2. Redistributions in binary form must reproduce the
015: above copyright notice, this list of conditions and the
016: following disclaimer in the documentation and/or other
017: materials provided with the distribution.
018:
019: 3. The name "groovy" must not be used to endorse or promote
020: products derived from this Software without prior written
021: permission of The Codehaus. For written permission,
022: please contact info@codehaus.org.
023:
024: 4. Products derived from this Software may not be called "groovy"
025: nor may "groovy" appear in their names without prior written
026: permission of The Codehaus. "groovy" is a registered
027: trademark of The Codehaus.
028:
029: 5. Due credit should be given to The Codehaus -
030: http://groovy.codehaus.org/
031:
032: THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033: ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034: NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036: THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043: OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: */
046:
047: package org.codehaus.groovy.antlr;
048:
049: import groovy.util.GroovyTestCase;
050:
051: import java.io.Reader;
052: import java.io.StringReader;
053:
054: /**
055: *
056: * @author <a href="mailto:groovy@ross-rayner.com">Jeremy Rayner</a>
057: * @version $Revision: 2250 $
058: */
059: public class SourceBufferTest extends GroovyTestCase {
060:
061: public void testEmptyBuffer() throws Exception {
062: SourceBuffer buffer = getSourceBuffer("");
063: assertNull(buffer.getSnippet(new LineColumn(1, 1),
064: new LineColumn(1, 1)));
065: }
066:
067: public void testSimpleUsage() throws Exception {
068: SourceBuffer buffer = getSourceBuffer("println 'hello world'");
069: assertEquals("hello", buffer.getSnippet(new LineColumn(1, 10),
070: new LineColumn(1, 15)));
071: }
072:
073: public void testUnixLineUsage() throws Exception {
074: String endOfLine = "\n";
075: StringBuffer src = new StringBuffer();
076: src.append("println 'hello world'").append(endOfLine);
077: src.append("println 'oh not, not that again'")
078: .append(endOfLine);
079: SourceBuffer buffer = getSourceBuffer(src.toString());
080: assertEquals("hello", buffer.getSnippet(new LineColumn(1, 10),
081: new LineColumn(1, 15)));
082: assertEquals("world'" + endOfLine + "print", buffer.getSnippet(
083: new LineColumn(1, 16), new LineColumn(2, 6)));
084: assertEquals(endOfLine, buffer.getSnippet(
085: new LineColumn(1, 22), new LineColumn(1, 23)));
086: assertEquals(endOfLine, buffer.getSnippet(
087: new LineColumn(2, 33), new LineColumn(2, 34)));
088: }
089:
090: public void testDOSLineUsage() throws Exception {
091: String endOfLine = "\r\n";
092: StringBuffer src = new StringBuffer();
093: src.append("println 'hello world'").append(endOfLine);
094: src.append("println 'oh not, not that again'")
095: .append(endOfLine);
096: SourceBuffer buffer = getSourceBuffer(src.toString());
097: assertEquals("hello", buffer.getSnippet(new LineColumn(1, 10),
098: new LineColumn(1, 15)));
099: assertEquals("oh not", buffer.getSnippet(new LineColumn(2, 10),
100: new LineColumn(2, 16)));
101: assertEquals("world'" + endOfLine + "print", buffer.getSnippet(
102: new LineColumn(1, 16), new LineColumn(2, 6)));
103: assertEquals(endOfLine, buffer.getSnippet(
104: new LineColumn(1, 22), new LineColumn(1, 24)));
105: assertEquals(endOfLine, buffer.getSnippet(
106: new LineColumn(2, 33), new LineColumn(2, 35)));
107: }
108:
109: public void testOutOfBounds() throws Exception {
110: String endOfLine = "\n";
111: StringBuffer src = new StringBuffer();
112: src.append("println 'hello world'").append(endOfLine);
113: src.append("println 'oh not, not that again'")
114: .append(endOfLine);
115: SourceBuffer buffer = getSourceBuffer(src.toString());
116: assertEquals("println", buffer.getSnippet(new LineColumn(0, 0),
117: new LineColumn(1, 8)));
118: assertEquals("println", buffer.getSnippet(new LineColumn(-10,
119: -1), new LineColumn(1, 8)));
120: assertEquals(endOfLine, buffer.getSnippet(
121: new LineColumn(2, 33), new LineColumn(2, 40)));
122: assertEquals("", buffer.getSnippet(new LineColumn(3, 33),
123: new LineColumn(6, 40)));
124: }
125:
126: private SourceBuffer getSourceBuffer(String text) throws Exception {
127: SourceBuffer buffer = new SourceBuffer();
128: Reader reader = new UnicodeEscapingReader(
129: new StringReader(text), buffer);
130:
131: while (reader.read() != -1) {
132: // empty loop
133: // - read all characters till the end of the reader
134: // UnicodeEscapingReader has side effects of
135: // filling the buffer
136: }
137: return buffer;
138: }
139: }
|