aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/anarres/cpp/ErrorTest.java
blob: 42240d4c4a28cf22770f95d6a89738028f749fdf (plain)
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
package org.anarres.cpp;

import java.io.IOException;
import org.junit.Test;
import static org.anarres.cpp.Token.*;
import static org.junit.Assert.*;

public class ErrorTest {

    private boolean testError(Preprocessor p)
            throws LexerException,
            IOException {
        for (;;) {
            Token tok = p.token();
            if (tok.getType() == EOF)
                break;
            if (tok.getType() == INVALID)
                return true;
        }
        return false;
    }

    private void testError(String input) throws Exception {
        StringLexerSource sl;
        DefaultPreprocessorListener pl;
        Preprocessor p;

        /* Without a PreprocessorListener, throws an exception. */
        sl = new StringLexerSource(input, true);
        p = new Preprocessor();
        p.addFeature(Feature.CSYNTAX);
        p.addInput(sl);
        try {
            assertTrue(testError(p));
            fail("Lexing unexpectedly succeeded without listener.");
        } catch (LexerException e) {
            /* required */
        }

        /* With a PreprocessorListener, records the error. */
        sl = new StringLexerSource(input, true);
        p = new Preprocessor();
        p.addFeature(Feature.CSYNTAX);
        p.addInput(sl);
        pl = new DefaultPreprocessorListener();
        p.setListener(pl);
        assertNotNull("CPP has listener", p.getListener());
        assertTrue(testError(p));
        assertTrue("Listener has errors", pl.getErrors() > 0);

        /* Without CSYNTAX, works happily. */
        sl = new StringLexerSource(input, true);
        p = new Preprocessor();
        p.addInput(sl);
        assertTrue(testError(p));
    }

    @Test
    public void testErrors() throws Exception {
        testError("\"");
        testError("'");
        // testError("''");
    }

}