/*
 * Copyright 2019, Robert Dyer
 *                 and Bowling Green State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package boa.test.functions;

import static boa.functions.BoaAstIntrinsics.prettyprint;
import static org.junit.Assert.assertEquals;
import static boa.functions.BoaAstIntrinsics.parse;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
 * Test prettyprint().
 *
 * @author rdyer
 */
@RunWith(Parameterized.class)
public class TestPrettyprint {
	private final static String CLASS_START = "class c {\n";
	private final static String CLASS_END = "}\n";
	private final static String STATEMENT_START = CLASS_START + "\tvoid m()\n\t{";
	private final static String STATEMENT_END = "\n\t}\n" + CLASS_END;

	@Parameters
	public static Collection<String[]> code() {
		return Arrays.asList(new String[][] {
				/* classes */
				{ "class c {\n}\n" }, { "public class c {\n}\n" }, { "class c extends d {\n}\n" },
				{ "class c implements i1 {\n}\n" }, { "class c implements i1, i2, i3 {\n}\n" },
				{ "abstract static final private class c extends d implements i1, i2, i3 {\n}\n" },

				/* methods */
				{ CLASS_START + "\tvoid m()\n\t{\n\t}\n" + CLASS_END },
				{ CLASS_START + "\tint m()\n\t{\n\t\treturn 1;\n\t}\n" + CLASS_END },

				/* statements */
				{ STATEMENT_START + "\n\t\tswitch (f1) {" 
						+ "\n\t\t\tcase 1:" 
						+ "\n\t\t\tf1 = 2;" 
						+ "\n\t\t\tdefault:"
						+ "\n\t\t\tbreak;\n\t\t}" + STATEMENT_END }, // SWITCH
				
				/* expressions */
				{ STATEMENT_START + "\n\t\tList<String> list = new ArrayList<String>();" + STATEMENT_END} // NEW
		});
	}

	private String code;

	public TestPrettyprint(final String code) {
		this.code = code;
	}

	@Test()
	public void testPrettyprint() throws Exception {
		String expected = prettyprint(parse(code));
		System.out.println(expected);
		assertEquals(code, expected);
	}
}

@@ -1326,16 +1326,22 @@
 m()%5Cn%5Ct%7B
+%5Cn%5Ct%5Ct
 %22;%0A%09priv
@@ -1546,16 +1546,21 @@
 %7D%5Cn%22 %7D, 
+%0A%09%09%09%09
 %7B %22publi
@@ -1572,32 +1572,37 @@
 ss c %7B%5Cn%7D%5Cn%22 %7D, 
+%0A%09%09%09%09
 %7B %22class c exten
@@ -1657,16 +1657,21 @@
 %7D%5Cn%22 %7D, 
+%0A%09%09%09%09
 %7B %22class
@@ -1994,22 +1994,16 @@
 TART + %22
-%5Cn%5Ct%5Ct
 switch (
@@ -2018,25 +2018,29 @@
 %09%09%09%09%09%09+ 
-%22%5Cn%5Ct%5Ct%5Ct
+indent(3) + %22
 case 1:%22
@@ -2049,25 +2049,29 @@
 %09%09%09%09%09%09+ 
-%22%5Cn%5Ct%5Ct%5Ct
+indent(3) + %22
 f1 = 2;%22
@@ -2080,25 +2080,29 @@
 %09%09%09%09%09%09+ 
-%22%5Cn%5Ct%5Ct%5Ct
+indent(3) + %22
 default:
@@ -2115,60 +2115,166 @@
 %09%09+ 
-%22%5Cn%5Ct%5Ct%5Ctbreak;%5Cn%5Ct%5Ct%7D%22 + STATEMENT_END %7D, // SWITCH
+indent(3) + %22break;%22%0A%09%09%09%09%09%09+ indent(2) + %22%7D%22 + STATEMENT_END %7D, // SWITCH%0A%09%09%09%09%7B STATEMENT_START + %22throw new RuntimeException(e);%22 + STATEMENT_END %7D, // THROW
 %0A%09%09%09
@@ -2322,22 +2322,16 @@
 TART + %22
-%5Cn%5Ct%5Ct
 List%3CStr
@@ -2402,16 +2402,135 @@
 %7D);%0A%09%7D%0A%0A
+%09private static String indent(int num) %7B%0A%09%09String str = %22%5Cn%22;%0A%09%09while (num-- %3E 0) %0A%09%09%09str += %22%5Ct%22; %0A%09%09return str;%0A%09%7D%0A%09%0A
 %09private
@@ -2611,16 +2611,18 @@
 code;%0A%09%7D
+%0A%09
 %0A%0A%09@Test

add throw statement test case for prettyprint