package targetast;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Method;

import static org.junit.jupiter.api.Assertions.assertEquals;


public class OLTest {
	private static Class<?> classToTest;
	private static Class<?> classToTest1;
	private static Object instanceOfClass;
	private static Object instanceOfClass1;
	
	@BeforeAll
	public static void setUpBeforeClass() throws Exception {
		var classFiles = TestCodegen.generateClassFiles(TestCodegen.createClassLoader(), "OL.jav");
		classToTest = classFiles.get("OL");
		instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
		classToTest1 = classFiles.get("OLMain");
		instanceOfClass1 = classToTest1.getDeclaredConstructor().newInstance();
	}
	
	@Test
	public void testmInt() throws Exception {
		Method m = classToTest.getDeclaredMethod("m", Integer.class);
		Integer result = (Integer) m.invoke(instanceOfClass, 5);
		assertEquals(new Integer(10), result);
	}
	
	@Test
	public void testmDouble() throws Exception {
		Method m = classToTest.getDeclaredMethod("m", Double.class);
		Double result = (Double) m.invoke(instanceOfClass, 5.0);
		assertEquals(new Double(10.0), result);
	}
	
	@Test
	public void testmString() throws Exception {
		Method m = classToTest.getDeclaredMethod("m", String.class);
		String result = (String) m.invoke(instanceOfClass, "xxx");
		assertEquals("xxxxxx", result);
	}
	
	@Test
	public void testOLMainClassName() {
		assertEquals("OLMain", classToTest1.getName());
	}
	
	@Test
	public void testmainInt() throws Exception {
		Method main = classToTest1.getDeclaredMethod("main", Integer.class);
		Integer result = (Integer) main.invoke(instanceOfClass1, 5);
		assertEquals(Integer.valueOf(10), result);
	}
	
	@Test
	public void testmainDouble() throws Exception {
		Method main = classToTest1.getDeclaredMethod("main", Double.class);
		Double result = (Double) main.invoke(instanceOfClass1, 5.0);
		assertEquals(Double.valueOf(10.0), result);
	}
	
	@Test
	public void testmainString() throws Exception {
		Method main = classToTest1.getDeclaredMethod("main", String.class);
		String result = (String) main.invoke(instanceOfClass1, "xxx");
		assertEquals("xxxxxx", result);
	}
}
