/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.asm;

import net.ranides.assira.generic.BiFunction;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author ranides
 */
public class ProxyDelegatorTest {
    
    private static final BiFunction<Object, Object> WRAPPER = new BiFunction<Object, Object>() {
        @Override
        public Object apply(Object value) {
            if(value instanceof Number) {
                return ((Number)value).doubleValue() * 100;
            } else {
                return value;
            }
        }
        @Override
        public Object inverse(Object value) {
            if(value instanceof Number) {
                return ((Number)value).doubleValue() / 100;
            } else {
                return value;
            }
        }
    };
    
    private interface JMath {
        void width(Number value);
        void height(Number value);
        double width();
        double height();
        double mul(double a, double b);
    }
    
    private static class CJMath implements JMath {
        
        private double w;
        private double h;
        
        public CJMath() {
            w = 1.0;
            h = 2.0;
        }
        @Override
        public void width(Number value) {
            this.w = value.doubleValue();
        }
        @Override
        public void height(Number value) {
            this.h = value.doubleValue();
        }
        @Override
        public double width() {
            return w;
        }
        @Override
        public double height() {
            return h;
        }
        @Override
        public double mul(double a, double b) {
            return a * b;
        }
    }
    
    
    
    public ProxyDelegatorTest() {
        
    }

    @Test
    public void testCreate() {
        final JMath jmath = new CJMath();
        final JMath wmath = ProxyDelegator.paramWrapper(jmath, JMath.class, WRAPPER);
        
        assertEquals(1.0, (Double)jmath.width(), 0.1);
        assertEquals(2.0, (Double)jmath.height(), 0.1);
        assertEquals(100*(Double)jmath.width(), (Double)wmath.width(), 0.1);
        assertEquals(100*(Double)jmath.height(), (Double)wmath.height(), 0.1);
        
        wmath.width(1900);
        jmath.height(7);
        
        assertEquals(19.0, (Double)jmath.width(), 0.1);
        assertEquals(7.0, (Double)jmath.height(), 0.1);
        assertEquals(100*(Double)jmath.width(), (Double)wmath.width(), 0.1);
        assertEquals(100*(Double)jmath.height(), (Double)wmath.height(), 0.1);
    }
}
