package net.ranides.assira.lexer;

import org.junit.Test;

import java.util.*;

import static org.junit.Assert.*;

public class StringStreamerTest {

    @Test
    public void testSplit() {
        assertEquals(Arrays.asList("hello"), split("hello"));
        assertEquals(Arrays.asList("hello", "world"), split("hello&world"));
        assertEquals(Arrays.asList("hello", "world", "here"), split("hello&world&here"));


        assertEquals(Arrays.asList("hello<component>"), split("hello<component>"));
        assertEquals(Arrays.asList("hello<component<nest>>"), split("hello<component<nest>>"));
        assertEquals(Arrays.asList("hello<n>", "world"), split("hello<n>&world"));


        assertEquals(Arrays.asList("hello", "world<data>", "here"), split("hello&world<data>&here"));
        assertEquals(Arrays.asList("hello", "world<data<list>>", "here"), split("hello&world<data<list>>&here"));
        assertEquals(Arrays.asList("hello", "world<data<list>>", "here", "last"), split("hello&world<data<list>>&here&last"));
        assertEquals(Arrays.asList("hello", "world<data<list>>", "here", "last<array>"), split("hello&world<data<list>>&here&last<array>"));
    }


    public List<String> split(String name) {
        List<String> list = new ArrayList<>();

        StringStreamer<Void> is = new StringStreamer<>((i,s) -> list.add(s));

        is.put(name);
        int level = 0;
        while(!is.end()) {
            char c = is.get();
            if('<' == c) {
                level++;
            }
            if('>' == c) {
                level--;
            }
            if('&' == c && (level ==0)) {
                is.send(null, 0, 1);
            }
        }
        is.send(null);
        return list;
    }

}