package net.ranides.assira.observable;

import net.ranides.assira.collection.prototype.GenericMap;
import net.ranides.assira.collection.prototype.OpenGenericMap;
import net.ranides.assira.events.EventDispatcher;
import net.ranides.assira.events.EventRouter;
import net.ranides.assira.math.MutableNumber;
import net.ranides.assira.math.MutableNumber.MutableInteger;
import net.ranides.test.contracts.collection.maps.GenericMapTester;
import org.junit.Test;

import static org.junit.Assert.*;

public class ObservableGenericMapTest {

    @Test
    public void basic() {
        MutableInteger counter = new MutableInteger(0);
        GenericMapTester.basic(() -> newMap(counter));
        assertEquals(6, counter.intValue());
    }

    @Test
    public void contains() {
        MutableInteger counter = new MutableInteger(0);
        GenericMapTester.contains(() -> newMap(counter));
        assertEquals(4, counter.intValue());
    }

    @Test
    public void entries() {
        MutableInteger counter = new MutableInteger(0);
        GenericMapTester.entries(() -> newMap(counter));
        assertEquals(4, counter.intValue());
    }

    @Test
    public void views() {
        MutableInteger counter = new MutableInteger(0);
        GenericMapTester.views(() -> newMap(counter));
        assertEquals(4, counter.intValue());
    }

    @Test
    public void setEntry() {
        MutableInteger counter = new MutableInteger(0);
        GenericMapTester.setEntry(() -> newMap(counter));
        assertEquals(4, counter.intValue());
    }

    private <T> GenericMap<T> newMap(MutableInteger counter) {
        EventDispatcher dispatcher = EventDispatcher.newInstance();
        ObservableGenericMap<T> map = new ObservableGenericMap<>(dispatcher);
        map.addEventListener(GenericMapEvent.class, v -> {
            counter.increment();
        });
        return map;
    }

}