package com.mebigfatguy.fbcontrib.utils;

import static org.testng.Assert.assertEquals;

import org.apache.bcel.Constants;
import org.apache.bcel.classfile.Constant;
import org.apache.bcel.classfile.ConstantPool;
import org.apache.bcel.classfile.ConstantUtf8;
import org.apache.bcel.classfile.Method;
import org.testng.annotations.Test;

public class RegisterUtilsTest {

    @Test
    public void shouldGetStaticParameterRegisters() {
        int[] expected = { 0, 2, 3 };
        Method method = new Method();
        method.setAccessFlags(Constants.ACC_PUBLIC | Constants.ACC_STATIC);

        Constant[] cnst = new Constant[] { new ConstantUtf8("(JLjava/lang/String;[[I)V") };
        ConstantPool cp = new ConstantPool(cnst) {

            @Override
            protected Object clone() throws CloneNotSupportedException {
                throw new CloneNotSupportedException();
            }

        };
        method.setConstantPool(cp);
        method.setSignatureIndex(0);

        int[] regs = RegisterUtils.getParameterRegisters(method);

        assertEquals(regs, expected);
    }

    @Test
    public void shouldGetInstanceParameterRegisters() {
        int[] expected = { 1, 3, 5, 6 };
        Method method = new Method();
        method.setAccessFlags(Constants.ACC_PUBLIC);

        Constant[] cnst = new Constant[] { new ConstantUtf8("(DJ[D[J)V") };
        ConstantPool cp = new ConstantPool(cnst) {

            @Override
            protected Object clone() throws CloneNotSupportedException {
                throw new CloneNotSupportedException();
            }

        };
        method.setConstantPool(cp);
        method.setSignatureIndex(0);

        int[] regs = RegisterUtils.getParameterRegisters(method);

        assertEquals(regs, expected);

    }
}

@@ -294,16 +294,60 @@
 Method;%0A
+import org.testng.annotations.DataProvider;%0A
 import o
@@ -418,529 +418,539 @@
    @
-Test%0A    public void shouldGetStaticParameterRegisters() %7B%0A        int%5B%5D expected = %7B 0, 2, 3 %7D;%0A        Method method = new Method();%0A        method.setAccessFlags(Constants.ACC_PUBLIC %7C Constants.ACC_STATIC);%0A%0A        Constant%5B%5D cnst = new Constant%5B%5D %7B new ConstantUtf8(%22(JLjava/lang/String;%5B%5BI)V%22) %7D;%0A        ConstantPool cp = new ConstantPool(cnst) %7B%0A%0A            @Override%0A            protected Object clone() throws CloneNotSupportedException %7B%0A                throw new CloneNotSupportedException();%0A            %7D%0A
+DataProvider(name = %22parameterRegisters%22)%0A    Object%5B%5D%5B%5D parameterRegisters() %7B%0A        return new Object%5B%5D%5B%5D %7B%0A            // access flags, signature, expected registers%0A            %7BConstants.ACC_PUBLIC %7C Constants.ACC_STATIC, %22(JLjava/lang/String;%5B%5BI)V%22, new int%5B%5D %7B 0, 2, 3 %7D%7D,%0A            %7BConstants.ACC_STATIC, %22(Ljava/lang/Object;)Z%22, new int%5B%5D %7B 0 %7D%7D,%0A            %7BConstants.ACC_PUBLIC, %22(DJ%5BD%5BJ)V%22, new int%5B%5D %7B 1, 3, 5, 6 %7D%7D,%0A            %7B0, %22(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;%22, new int%5B%5D %7B 1, 2 %7D%7D,
 %0A   
@@ -965,199 +965,57 @@
     
+%7D%0A%0A
     
-method.setConstantPool(cp);%0A        method.setSignatureIndex(0);%0A%0A        int%5B%5D regs = RegisterUtils.getParameterRegisters(method);%0A%0A        assertEquals(regs, expected);%0A    %7D%0A%0A    @Test
+@Test(dataProvider = %22parameterRegisters%22)
 %0A   
@@ -1040,16 +1040,8 @@
 dGet
-Instance
 Para
@@ -1059,52 +1059,60 @@
 ers(
-) %7B%0A        int%5B%5D expected = %7B 1, 3, 5, 6 %7D;
+int accessFlags, String signature, int%5B%5D expected) %7B
 %0A   
@@ -1176,36 +1176,27 @@
 ssFlags(
-Constants.ACC_PUBLIC
+accessFlags
 );%0A%0A    
@@ -1251,27 +1251,25 @@
 antUtf8(
-%22(DJ%5BD%5BJ)V%22
+signature
 ) %7D;%0A   
@@ -1673,17 +1673,17 @@
 ected);%0A
-%0A
     %7D%0A
+%0A
 %7D%0A

combine tests and use DataProvider