package se.ejp.traci.lang.parser;

import static org.junit.Assert.assertEquals;

import java.io.IOException;

import org.antlr.runtime.NoViableAltException;
import org.antlr.runtime.Token;
import org.junit.Test;

public class TraciLexerTest extends TraciLexerBase
{
    @Test
    public void testLexer()
    {
        runLexer("17+\n23;");
        assertEquals(0, lexerErrors.size());
        assertEquals(6, tokens.size());
        assertToken(0, TraciLexer.INT,          "17", Token.DEFAULT_CHANNEL, 1, 0);
        assertToken(1, TraciLexer.PLUS_OP,       "+", Token.DEFAULT_CHANNEL, 1, 2);
        assertToken(2, TraciLexer.WS,           "\n", Token.HIDDEN_CHANNEL,  1, 3);
        assertToken(3, TraciLexer.INT,          "23", Token.DEFAULT_CHANNEL, 2, 0);
        assertToken(4, TraciLexer.SEMICOLON,     ";", Token.DEFAULT_CHANNEL, 2, 2);
        assertToken(5, TraciLexer.EOF,       "<EOF>", Token.DEFAULT_CHANNEL, 2, 3);
    }

    @Test
    public void testNoViableAltException()
    {
        runLexer("17%23;");
        assertEquals(1, lexerErrors.size());
        assertEquals(NoViableAltException.class, lexerErrors.get(0).e.getClass());
        assertEquals('%', lexerErrors.get(0).e.c);
    }

    @Test
    public void testLexerFile() throws IOException
    {
        runLexerFile("testcode/fibonacci.traci");
        assertEquals(0, lexerErrors.size());
        assertToken(0, TraciLexer.DEF, "def", Token.DEFAULT_CHANNEL, 1, 0);
        assertToken(1, TraciLexer.WS,    " ", Token.HIDDEN_CHANNEL,  1, 3);
        assertToken(2, TraciLexer.ID,  "fib", Token.DEFAULT_CHANNEL, 1, 4);
        assertToken(3, TraciLexer.LPAR,  "(", Token.DEFAULT_CHANNEL, 1, 7);
        assertToken(4, TraciLexer.ID,    "n", Token.DEFAULT_CHANNEL, 1, 8);
        assertToken(5, TraciLexer.RPAR,  ")", Token.DEFAULT_CHANNEL, 1, 9);
    }

    @Test
    public void testLexerWithPreprocessor()
    {
        runLexerPreprocessedFile("testcode/fibonacci.traci");
        assertEquals(0, lexerErrors.size());
        assertToken(0, TraciLexer.PPLINE, null, Token.HIDDEN_CHANNEL, 1, 0);
        assertToken(1, TraciLexer.WS,     "\n", Token.HIDDEN_CHANNEL);
        assertToken(2, TraciLexer.DEF,   "def", Token.DEFAULT_CHANNEL, 1, 0);
        assertToken(3, TraciLexer.WS,      " ", Token.HIDDEN_CHANNEL,  1, 3);
        assertToken(4, TraciLexer.ID,    "fib", Token.DEFAULT_CHANNEL, 1, 4);
        assertToken(5, TraciLexer.LPAR,    "(", Token.DEFAULT_CHANNEL, 1, 7);
        assertToken(6, TraciLexer.ID,      "n", Token.DEFAULT_CHANNEL, 1, 8);
        assertToken(7, TraciLexer.RPAR,    ")", Token.DEFAULT_CHANNEL, 1, 9);
    }
}

@@ -103,16 +103,61 @@
 ption;%0A%0A
+import org.antlr.runtime.EarlyExitException;%0A
 import o
@@ -1239,32 +1239,1012 @@
 0).e.c);%0A    %7D%0A%0A
+    @Test%0A    public void testFloat()%0A    %7B%0A        runLexer(%22.23%22);%0A        assertEquals(0, lexerErrors.size());%0A        assertToken(0, TraciLexer.FLOAT, %22.23%22, Token.DEFAULT_CHANNEL, 1, 0);%0A%0A        runLexer(%222.23.23%22);%0A        assertEquals(0, lexerErrors.size());%0A        assertToken(0, TraciLexer.FLOAT, %222.23%22, Token.DEFAULT_CHANNEL, 1, 0);%0A%0A        runLexer(%222.23e-10-5%22);%0A        assertEquals(0, lexerErrors.size());%0A        assertToken(0, TraciLexer.FLOAT, %222.23e-10%22, Token.DEFAULT_CHANNEL, 1, 0);%0A%0A        runLexer(%22.23E+13.2%22);%0A        assertEquals(0, lexerErrors.size());%0A        assertToken(0, TraciLexer.FLOAT, %22.23E+13%22, Token.DEFAULT_CHANNEL, 1, 0);%0A%0A        runLexer(%2223E13%22);%0A        assertEquals(0, lexerErrors.size());%0A        assertToken(0, TraciLexer.FLOAT, %2223E13%22, Token.DEFAULT_CHANNEL, 1, 0);%0A%0A        runLexer(%221.2e+%22);%0A        assertEquals(1, lexerErrors.size());%0A        assertEquals(EarlyExitException.class, lexerErrors.get(0).e.getClass());%0A    %7D%0A%0A
     @Test%0A    pu

Test lexer float.