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

import net.ranides.assira.generic.TypeToken;
import net.ranides.test.mockup.reflection.MTFunction;
import net.ranides.test.mockup.reflection.MTFunction.MTRecord;
import org.junit.Test;

import static net.ranides.assira.junit.NewAssert.assertEquals;

/**
 *
 * @author Ranides Atterwim {@literal <ranides@gmail.com>}
 */
@SuppressWarnings("ALL")
public class IMethodTest {

    @SuppressWarnings({"raw", "unchecked"})
    @Test
    public void testToFunctor() throws Throwable {
        
        Object that = new MTRecord(300);
        IMethod m1 = IClass.typefor(that).method("sum").get();
        IMethod m2 = IClass.typefor(that).method("mul").get();
        
        MTFunction<MTRecord> f11 = m1.handle(MTFunction.GENERIC);
        MTFunction<MTRecord> f12 = m1.handle(MTFunction.RAW);
        
        assertEquals(309, f11.calculate(new MTRecord(300), 3, 6));
        assertEquals(410, f12.calculate(new MTRecord(400), 3, 7));
        
        MTFunction<MTRecord> f21 = m2.handle(MTFunction.GENERIC);
        MTFunction<MTRecord> f22 = m2.handle(MTFunction.RAW);
        
        assertEquals(318, f21.calculate(new MTRecord(300), 3, 6));
        assertEquals(421, f22.calculate(new MTRecord(400), 3, 7));
    }
    
    @Test
    public void testToFunctorConstruct() {
        IConstructor<?> c1 = IClass.typeinfo(MTRecord.class)
            .constructors()
            .first()
            .get();
        
        MTFunction.Generator g1 = c1.handle(new TypeToken<MTFunction.Generator>(){});
        MTFunction.Generator g2 = c1.handle(MTFunction.Generator.class);

        assertEquals(503, g1.build(500).sum(1, 2));
        assertEquals(603, g2.build(600).sum(1, 2));
    }

}
