package kalang.test;

import java.util.List;
import kalang.compiler.CompilationUnit;
import kalang.compiler.KalangCompiler;
import kalang.util.ParseTreeNavigator;
import kalang.util.TokenNavigator;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTree;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Kason Yang <i@kasonyang.com>
 */
public class KalangCompilerTest {
    
    public KalangCompilerTest() {
    }
    
    @Test
    public void test(){
        KalangCompiler kc = new KalangCompiler();
        kc.addSource("Test", "class{  }");
        kc.compile();
        CompilationUnit unit = kc.getCompilationUnit("Test");
        assert unit != null;
        CommonTokenStream ts = unit.getTokenStream();
        List<Token> tokens = ts.getTokens();
        testTokenNavigator(tokens.toArray(new Token[0]),unit.getAstBuilder().getParseTree());
    }
    
    private void testTokenNavigator(Token[] tokens,ParseTree tree){
        TokenNavigator tokenNav = new TokenNavigator(tokens);
        ParseTreeNavigator treeNav = new ParseTreeNavigator(tree);
        assertEquals(0,tokenNav.getCurrentTokenIndex());
        assertEquals("class", treeNav.getParseTree(tokenNav.getCurrentToken()).getText());
        
        tokenNav.move(5);
        assertEquals("{", tokenNav.getCurrentToken().getText());
        assertEquals(1,tokenNav.getCurrentTokenIndex());
        tokenNav.move(8);
        assertEquals("}",tokenNav.getCurrentToken().getText());
        
        assertEquals("}", treeNav.getParseTree(tokenNav.getCurrentToken()).getText());
        
    }
}

@@ -816,42 +816,131 @@
     
-List%3CToken%3E tokens = ts.getTokens(
+//the tokens contains tokens in all channels%0A        List%3CToken%3E tokens = ts.getTokens();%0A        assertEquals(5, ts.size()
 );%0A 
@@ -1723,32 +1723,90 @@
 ()).getText());%0A
+        assertEquals(3, tokenNav.getCurrentTokenIndex());%0A
         %0A    %7D%0A%7D

test modified