aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/javacc/FastCharStreamTestCase.java
blob: 90f0df121a18c03108cce4b068c7b8525c98223e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.javacc;

import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * @author Simon Thoresen Hult
 */
public class FastCharStreamTestCase {

    @Test
    public void requireThatInputCanBeRead() throws IOException {
        FastCharStream input = new FastCharStream("foo");
        assertEquals('f', input.readChar());
        assertEquals('o', input.readChar());
        assertEquals('o', input.readChar());
        try {
            input.readChar();
            fail();
        } catch (IOException e) {

        }
    }

    @Test
    public void requireThatColumnIsTracked() throws IOException {
        FastCharStream input = new FastCharStream("foo");
        assertEquals(1, input.getEndColumn());
        input.readChar();
        assertEquals(2, input.getEndColumn());
        input.readChar();
        assertEquals(3, input.getEndColumn());
        input.readChar();
        assertEquals(4, input.getEndColumn());
    }

    @Test
    public void requireThatLineIsNotTracked() throws IOException {
        FastCharStream input = new FastCharStream("f\no");
        assertEquals(-1, input.getEndLine());
        assertEquals(-1, input.getBeginLine());
        input.readChar();
        assertEquals(-1, input.getBeginLine());
        input.readChar();
        assertEquals(-1, input.getBeginLine());
        input.readChar();
        assertEquals(-1, input.getBeginLine());
        assertEquals(-1, input.getEndLine());
    }


    @Test
    public void requireThatBackupIsSupported() throws IOException {
        FastCharStream input = new FastCharStream("foo");
        assertEquals('f', input.readChar());
        input.backup(1);
        assertEquals('f', input.readChar());
        assertEquals('o', input.readChar());
        input.backup(2);
        assertEquals('f', input.readChar());
        assertEquals('o', input.readChar());
        assertEquals('o', input.readChar());
        input.backup(3);
        assertEquals('f', input.readChar());
        assertEquals('o', input.readChar());
        assertEquals('o', input.readChar());
        input.backup(2);
        assertEquals('o', input.readChar());
        assertEquals('o', input.readChar());
        input.backup(1);
        assertEquals('o', input.readChar());
    }

    @Test
    public void requireThatSuffixIsNotSupported() {
        try {
            new FastCharStream("foo").getSuffix(0);
            fail();
        } catch (UnsupportedOperationException e) {

        }
    }

    @Test
    public void requireThatDoneDoesNotThrowException() {
        FastCharStream input = new FastCharStream("foo");
        input.done();
    }

    @Test
    public void requireThatTokensCanBeRetrieved() throws IOException {
        FastCharStream input = new FastCharStream("foo bar baz");
        input.readChar();
        input.readChar();
        input.readChar();
        input.readChar();
        assertEquals('b', input.beginToken());
        assertEquals(5, input.getBeginColumn());
        assertEquals(-1, input.getBeginLine());
        assertEquals(6, input.getEndColumn());
        assertEquals(-1, input.getEndLine());
        assertEquals('a', input.readChar());
        assertEquals('r', input.readChar());
        assertEquals(8, input.getEndColumn());
        assertEquals(-1, input.getEndLine());
        assertEquals("bar", input.getImage());
    }

    @Test
    public void requireThatExceptionsDetectLineNumber() {
        FastCharStream input = new FastCharStream("foo\nbar");
        assertEquals("line 2, column 1\n" +
                     "At position:\n" +
                     "bar\n" +
                     "^",
                     input.formatException("line -1, column 5"));
        assertEquals("line 2, column 2\n" +
                     "At position:\n" +
                     "bar\n" +
                     " ^",
                     input.formatException("line -1, column 6"));
        assertEquals("line 2, column 3\n" +
                     "At position:\n" +
                     "bar\n" +
                     "  ^",
                     input.formatException("line -1, column 7"));
        assertEquals("line 2, column 4\n" +
                     "At position:\n" +
                     "bar\n" +
                     "   ^",
                     input.formatException("line -1, column 8"));
        assertEquals("foo line 2, column 2\n" +
                     "At position:\n" +
                     "bar\n" +
                     " ^",
                     input.formatException("foo line -1, column 6"));
        assertEquals("foo line 2, column 2 bar\n" +
                     "At position:\n" +
                     "bar\n" +
                     " ^",
                     input.formatException("foo line -1, column 6 bar"));
        assertEquals("line 2, column 2 bar\n" +
                     "At position:\n" +
                     "bar\n" +
                     " ^",
                     input.formatException("line -1, column 6 bar"));
    }

    @Test
    public void requireErrorMsgExceptionAtEOF() {
        FastCharStream input = new FastCharStream("\n");
        assertEquals("line 1, column 1\n" +
                     "At position:\n" +
                     "EOF\n" +
                     "^",
                     input.formatException("line -1, column 1"));
    }
    
    @Test
    public void requireThatUnknownExceptionFormatIsIgnored() {
        FastCharStream input = new FastCharStream("foo\nbar");
        assertEquals("",
                     input.formatException(""));
        assertEquals("foo",
                     input.formatException("foo"));
        assertEquals("foo line -1, column ",
                     input.formatException("foo line -1, column "));
        assertEquals("foo line -1, column bar",
                     input.formatException("foo line -1, column bar"));
    }

    @Test
    public void requireThatIllegalExceptionColumnIsIgnored() {
        FastCharStream input = new FastCharStream("foo\nbar");
        assertEquals("line -1, column 9",
                     input.formatException("line -1, column 9"));
    }
}