package org.deft.extension.test.append;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.deft.extension.append.AppendInformation;
import org.deft.extension.append.AppendTemplate;
import org.deft.extension.repository.ExtensionRepository;
import org.deft.extension.test.testutil.TestLoader;
import org.deft.extension.test.testutil.TestUtil;
import org.deft.repository.ast.TreeNode;
import org.deft.repository.ast.annotation.FormattingConfiguration;
import org.deft.repository.ast.annotation.NodeInformation;
import org.junit.Test;

public class AppendTemplateManuelTest {
	FormattingConfiguration appendCF;
	String testRoot = "resources/test/AppendTemplateTest/";

	@Test
	public void testManual() throws Exception {

		TestLoader tests = new TestLoader(testRoot + "manual.testcase");
		for (org.deft.extension.test.testutil.Test test : tests) {
			String input = test.getParamterAsString("input");
			String expected = test.getParamterAsString("expected").trim()
					.replace("\n", "\r\n");
			String xpath = test.getParamterAsString("xpath");
			String xpath2 = test.getParamterAsString("xpath2");

			appendCF = new FormattingConfiguration();
			appendCF.addTemplate(new AppendTemplate());
			TreeNode ast = org.deft.extension.test.testutil.TestUtil
					.readFileInAST(input);

			ExtensionRepository.getInstance().clearRepository();
			addInformations(ast, xpath);
			addInformations(ast, xpath2);

			String actual = TestUtil.astToXMLString(ast, appendCF);

			assertEquals(test.getName() + " failed", expected, actual);
		}

	}

	private void addInformations(TreeNode ast, String xpath) {
		if (xpath == null)
			return;

		List<TreeNode> targets = ast.executeXPathQuery(xpath);

		for (TreeNode tn : targets) {
			AppendInformation ai = new AppendInformation();
			tn.addInformation(ai);
		}
	}
}
