package net.ranides.assira.observable;

import lombok.extern.slf4j.Slf4j;
import net.ranides.assira.events.EventDispatcher;
import net.ranides.assira.junit.ManualTestUtils;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;

@Slf4j
public class ObservableBeanFactoryTest {

    @Test
    public void generate() {
        List<String> events = new ArrayList<>();
        EventDispatcher dispatcher = EventDispatcher.newInstance();
        dispatcher.addEventListener(BeanEvent.Change.class, e -> {
            events.add(e.property() + "=" + e.current());
            ManualTestUtils.trace(e);
        });

        SimpleModel model = ObservableBeanFactory.getSystem().newObservable(SimpleModel.class, dispatcher);
        model.setAge(24);
        model.setName("John");
        model.setAge(27);

        assertEquals(Arrays.asList("age=24","name=John","age=27"), events);
        SimpleModel expected = new SimpleModel() {};
        expected.setAge(27);
        expected.setName("John");
        assertEquals(expected, model);
    }

}