/*
 * Copyright 2015-2016 the original author or authors.
 *
 * 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 io.koara;

import static org.junit.Assert.assertEquals;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

import io.koara.ast.Document;

@RunWith(Parameterized.class)
public class ComplianceTest {

    private String module;
    private String testcase;

    public ComplianceTest(String module, String testcase) {
        this.module = module;
        this.testcase = testcase;
    }

    @Parameters(name = "{0}: {1}")
    public static Iterable<Object[]> data() {
        List<Object[]> modules = new ArrayList<Object[]>();
        for (File module : new File("testsuite/input").listFiles()) {
        	if(!module.getName().startsWith("end2end") && !module.getName().startsWith(".")) {
        		for (File testcase : module.listFiles()) {
        			modules.add(new Object[] { module.getName(),
                          testcase.getName().substring(0, testcase.getName().length() - 3) });
        		}
        		
        	}
        }
        return modules;
    }
    
    @Test
    public void testKoaraToKoara() throws Exception {
    	String input = readFile("testsuite/input/" + module + "/" + testcase + ".kd");
        String expected = readFile("testsuite/output/koara/" + module + "/" + testcase + ".kd");
        String expectedHtml = readFile("testsuite/output/html5/" + module + "/" + testcase + ".htm");

        Parser parser = new Parser();
        Document document = parser.parse(input);
        KoaraRenderer renderer = new KoaraRenderer();
        document.accept(renderer);
        assertEquals(expected, renderer.getOutput());

        // Make sure the koara output correctly parses back to html
        Html5Renderer html5Renderer = new Html5Renderer();
        document = parser.parse(renderer.getOutput());
        document.accept(html5Renderer);
        assertEquals(expectedHtml, html5Renderer.getOutput());
    }

    @Test
    public void testKoaraToHtml5() throws Exception {
    	String kd = readFile("testsuite/input/" + module + "/" + testcase + ".kd");
        String html = readFile("testsuite/output/html5/" + module + "/" + testcase + ".htm");

        Parser parser = new Parser();
        Document document = parser.parse(kd);
        Html5Renderer renderer = new Html5Renderer();
        document.accept(renderer);
        assertEquals(html, renderer.getOutput());
    }
    
    private String readFile(String path) throws IOException {
        BufferedReader reader = null;
        try {
            StringBuffer fileData = new StringBuffer();
            reader = new BufferedReader(new FileReader(path));
            char[] buf = new char[1024];
            int numRead = 0;
            while ((numRead = reader.read(buf)) != -1) {
                String readData = String.valueOf(buf, 0, numRead);
                fileData.append(readData);
            }
            return fileData.toString();
        } finally {
            reader.close();
        }
    }

}
@@ -1848,24 +1848,26 @@
 %0A    %7D%0A    %0A
+//
     @Test%0A  
@@ -1856,32 +1856,34 @@
    %0A//    @Test%0A
+//
     public void 
@@ -1912,32 +1912,34 @@
 ows Exception %7B%0A
+//
     %09String inpu
@@ -1998,32 +1998,34 @@
 tcase + %22.kd%22);%0A
+//
         String e
@@ -2097,32 +2097,34 @@
 tcase + %22.kd%22);%0A
+//
         String e
@@ -2201,33 +2201,37 @@
 case + %22.htm%22);%0A
-%0A
+//%0A//
         Parser p
@@ -2244,32 +2244,34 @@
 = new Parser();%0A
+//
         Document
@@ -2303,16 +2303,18 @@
 input);%0A
+//
         
@@ -2351,32 +2351,34 @@
 oaraRenderer();%0A
+//
         document
@@ -2388,32 +2388,34 @@
 cept(renderer);%0A
+//
         assertEq
@@ -2452,17 +2452,21 @@
 put());%0A
-%0A
+//%0A//
         
@@ -2525,16 +2525,18 @@
 to html%0A
+//
         
@@ -2578,32 +2578,34 @@
 tml5Renderer();%0A
+//
         document
@@ -2639,24 +2639,26 @@
 tOutput());%0A
+//
         docu
@@ -2677,32 +2677,34 @@
 html5Renderer);%0A
+//
         assertEq
@@ -2746,24 +2746,26 @@
 tOutput());%0A
+//
     %7D%0A%0A    @

fix build