package net.ranides.assira.generic;

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

public class RefTest {

    @Test
    public void testException() {
        Ref<Object> opt = Ref
            .ofNullable(null)
            .withMessage(() -> new RuntimeException("Unknown name"));

        NewAssert.assertThrows(e -> "Unknown name".equals(e.getMessage()), () -> {
            opt.get();
        });

        CQuery<Object> query1 = CQuery.from().optional(opt);

        NewAssert.assertThrows(e -> "Unknown name".equals(e.getMessage()), () -> {
            query1.first().get();
        });

        NewAssert.assertThrows(e -> "Unknown name".equals(e.getMessage()), () -> {
            query1.filter(v -> false).first().get();
        });

        NewAssert.assertThrows(e -> "Unknown name".equals(e.getMessage()), () -> {
            query1.prefetch().first().get();
        });

        NewAssert.assertThrows(e -> "Unknown name".equals(e.getMessage()), () -> {
            query1.map(v -> "x" + v).first().get();
        });

        NewAssert.assertThrows(e -> "Unknown name".equals(e.getMessage()), () -> {
            query1.reverse().first().get();
        });

        NewAssert.assertThrows(e -> "Unknown name".equals(e.getMessage()), () -> {
            query1.reduce((a, b) -> a);
        });

    }
}