package net.ranides.assira.lexer;

import org.junit.Test;

import java.util.regex.Pattern;

import static net.ranides.assira.junit.NewAssert.assertThrows;
import static org.junit.Assert.assertEquals;

public class StringTokenizerTest {

    @Test
    public void predicates() {
        StringTokenizer streamer = new StringTokenizer("hello world");
        streamer.skip(4);

        assertThrows(IllegalStateException.class, () -> {
            streamer.skip(9);
        });

        streamer.skip(7);

        assertThrows(IllegalStateException.class, () -> {
            streamer.skip();
        });
    }

    @Test
    public void tail() {
        StringTokenizer streamer = new StringTokenizer("hello world");
        streamer.skip(4);

        assertEquals("o world", streamer.tail());
    }

    @Test
    public void patterns() {
        StringTokenizer streamer = new StringTokenizer("hello 1990 world");

        assertEquals("hello", streamer.peek(Pattern.compile("[a-z]+")));
        assertEquals("hello ", streamer.peek(Pattern.compile("[a-z ]+")));
        assertEquals("hello 1990", streamer.peek(Pattern.compile("[a-z]+ [0-9]+")));

        assertEquals("hello", streamer.get(Pattern.compile("[a-z]+")));
        assertEquals(" ", streamer.get(Pattern.compile(" +")));
        assertEquals("1990", streamer.get(Pattern.compile("[0-9]+")));

        assertEquals(1, streamer.skip(Pattern.compile(" +")));
        assertEquals("world", streamer.peek(Pattern.compile("[^0-9]+")));
        assertEquals("world", streamer.get(Pattern.compile("[^0-9]+")));
    }


    @Test
    public void usage() {
        StringTokenizer streamer = new StringTokenizer("hello world  text ");

        assertEquals("hello", streamer.get(c -> c!=' '));
        assertEquals(" ", streamer.get(c -> c==' '));
        assertEquals("", streamer.get(c -> c==' '));
        assertEquals("world", streamer.get(c -> c!=' '));
        assertEquals("  ", streamer.get(c -> c==' '));

        assertEquals('t', streamer.peek());
        assertEquals("text", streamer.peek(c -> c!=' '));
    }

    @Test
    public void peek() {
        StringTokenizer streamer = new StringTokenizer("hello world!");

        assertEquals("hello", streamer.peek(c -> c!=' '));
        assertEquals("hello world", streamer.peek(c -> c!='!'));
        assertEquals("hello world!", streamer.peek(c -> c!='?'));
    }

    @Test
    public void next() {
        StringTokenizer streamer1 = new StringTokenizer("hello world!");
        StringTokenizer streamer2 = new StringTokenizer("hello world!");
        StringTokenizer streamer3 = new StringTokenizer("hello world!");

        assertEquals("hello", streamer1.get(c -> c!=' '));
        assertEquals("hello world", streamer2.get(c -> c!='!'));
        assertEquals("hello world!", streamer3.get(c -> c!='?'));

    }

}