package net.ranides.assira.grammar;

import net.ranides.assira.collection.query.CQuery;
import net.ranides.assira.io.ResourceUtils;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

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

public class InspectionTest {


    @Test
    public void stream() throws IOException {
        G4Parser parser = G4Parser.builder().resource("Calculator.g4").build();

        G4Node tree = parser.parse("expression", "(1 + 2) * 3 + 4^2");

        List<String> list1 = CQuery.from().tree().dfs(() -> tree, v -> v.children())
            .map(v -> v.name())
            .sort()
            .list();
        List<String> list2 = CQuery.from().tree().dfs(() -> tree, v -> v.children())
            .map(v -> v.name())
            .distinct()
            .sort()
            .list();
        List<String> list3 = CQuery.from().tree().dfs(() -> tree, v -> v.children(), v -> v.shortname().equals("number"))
            .map(v -> v.name())
            .distinct()
            .sort()
            .list();

        assertTrue(list1.containsAll(list2));
        assertTrue(list2.containsAll(list3));

        assertEquals(
            Arrays.asList(
                "SYMBOL_ADD", "SYMBOL_LB", "SYMBOL_MUL", "SYMBOL_POW", "SYMBOL_RB",
                "expression#expr_1", "expression#expr_2", "expression#expr_3",
                "expression#expr_bracket", "expression#expr_number",
                "number$1",
                "operator1$1", "operator2#opmul", "operator3$1",
                "operatorAdd$1", "operatorMul$1", "operatorPow$1"
            ),
            list3
        );
    }

    @Test
    public void printTree() throws IOException {
        G4Parser parser = G4Parser.builder().resource("Calculator.g4").build();

        G4Node tree1 = parser.parse("expression", "(4 + 2) * 3 ");

        assertEquals(ResourceUtils.text("Calculator.tree.txt"), G4Support.printTree(tree1));
    }

    @Test
    public void printPaths() throws IOException {
        G4Parser g4 = G4Parser.builder().resource("Calculator.g4").build();

        G4Node tree = g4.parse("expression", "(4 + 2) * 3 ");

        assertEquals(ResourceUtils.text("Calculator.paths.txt"), G4Support.printPath(tree));
    }

}
