package net.ranides.assira.functional;

import org.junit.Test;

import java.util.NoSuchElementException;

import static org.junit.Assert.*;

public class BranchTest {

    @Test
    public void basic() {
        String value1 = Branch.first(17)
            .forValue(5, "five")
            .forValue(17, "seventeen")
            .forValue(9, "nine")
            .get();

        assertEquals("seventeen", value1);

        String value2 = Branch.first(17)
            .forValue(5, "five")
            .forValue(17, "seventeen")
            .forValue(9, "nine")
            .forValue(17, "invalid")
            .get();
        assertEquals("seventeen", value2);
    }

    @Test(expected = NoSuchElementException.class)
    public void noElement() {
        Branch.single(17)
            .forValue(5, "five")
            .forValue(17, "seventeen")
            .forValue(9, "nine")
            .forValue(17, "invalid")
            .get();
    }

    @Test
    public void type() {
        String value1 = Branch.single(17L).forType(String.class, "text").forType(Long.class, "long").get();
        assertEquals("long", value1);

        String value2 = Branch.single(17L).forType(String.class, "text").forType(long.class, "long").get();
        assertEquals("long", value2);

        String value3 = Branch.single(17L).forType(String.class, "text").forType(Number.class, "number").get();
        assertEquals("number", value3);

        String value4 = Branch.single(null).forType(String.class, "text").forType(Number.class, "number").orElse("?");
        assertEquals("?", value4);
    }
}