package net.kenro.ji.jin.purescript.purescript_examples;


import java.io.File;
import java.io.IOException;

import com.intellij.openapi.util.io.FileUtil;
import com.intellij.psi.impl.DebugUtil;
import com.intellij.testFramework.PsiTestCase;
import com.intellij.util.Processor;

import net.kenro.ji.jin.purescript.file.PSFile;
import org.jetbrains.annotations.NotNull;

public class PurescriptExamplesTest extends PsiTestCase {

    private Processor<File> processor() {
        return file -> {
            if (file.isFile() && file.getName().endsWith(".purs")) {
                try {
                    testExample(file);
                } catch (Exception e) {
                    e.printStackTrace();
                    fail("Failed to read file " + file.getAbsolutePath());
                }
            }
            return true;
        };
    }

    public void testExamples() {
        String testDataPath = "src/test/resources/purescript_examples";
        FileUtil.processFilesRecursively(new File(testDataPath + "/passing"), processor());
        FileUtil.processFilesRecursively(new File(testDataPath + "/failing"), processor());
        FileUtil.processFilesRecursively(new File(testDataPath + "/warning"), processor());
        FileUtil.processFilesRecursively(new File(testDataPath + "/docs"), processor());

        String additionalTests = "src/test/resources/additional";
        FileUtil.processFilesRecursively(new File(additionalTests + "/passing"), processor());
//        FileUtil.processFilesRecursively(new File(additionalTests + "/perf"), processor(true));
    }

//    public void testPerformance() throws Exception {
//        String perfDir = "src/test/resources/additional/perf/";
//        double emptyFile = perfExample(new File(perfDir + "Empty.purs"));
//        double longFile = perfExample(new File(perfDir + "LongFile.purs"));
//        assertTrue(longFile < emptyFile * 10);
//    }

    public static String readFile(File file) throws IOException {
        return FileUtil.loadFile(file.getCanonicalFile(), "utf-8", true);
    }

    private double perfExample(@NotNull File fileName) throws Exception {
        int N = 100;
        String text = readFile(fileName);
        createFile(fileName.getName(), text);
        long start = System.nanoTime();
        for (int i = 0; i < N; i++) {
            createFile(fileName.getName(), text);
        }
        long end = System.nanoTime();
        double v = (end - start) * 1e-6 / N;
        System.out.printf("Parsing %s :%.3fms\n", fileName, v);
        return v;
    }

    private void testExample(@NotNull File fileName) throws Exception {
        PSFile file = (PSFile) createFile(fileName.getName(), readFile(fileName));

        String psiTree = DebugUtil.psiToString(file, false);
        File expectedFile = new File(fileName.getAbsolutePath() + ".psi");
        if (expectedFile.isFile()) {
            String expectedTree = readFile(expectedFile);
            assertEquals(fileName.getName() + " failed.", expectedTree, psiTree);
        } else {
//            assert false;  // Only manually.
            FileUtil.writeToFile(new File(fileName.getAbsolutePath() + ".psi"), psiTree);
        }

        assertEquals(fileName.getName() + " failed.", true, !psiTree.contains("PsiErrorElement"));
    }
}


@@ -56,58 +56,8 @@
 ;%0A%0A%0A
-import java.io.File;%0Aimport java.io.IOException;%0A%0A
 impo
@@ -172,19 +172,37 @@
 amework.
-Psi
+fixtures.BasePlatform
 TestCase
@@ -239,16 +239,62 @@
 cessor;%0A
+import net.kenro.ji.jin.purescript.PSLanguage;
 %0Aimport 
@@ -377,16 +377,66 @@
 tNull;%0A%0A
+import java.io.File;%0Aimport java.io.IOException;%0A%0A
 public c
@@ -471,19 +471,28 @@
 extends 
-Psi
+BasePlatform
 TestCase
@@ -2132,32 +2132,33 @@
 , true);%0A    %7D%0A%0A
+%0A
     private doub
@@ -2157,19 +2157,17 @@
 ate 
-double perf
+void test
 Exam
@@ -2225,205 +2225,42 @@
     
-int N = 100;%0A        String text = readFile(fileName);%0A        createFile(fileName.getName(), text);%0A        long start = System.nanoTime();%0A        for (int i = 0; i %3C N; i++) %7B%0A           
+PSFile file = (PSFile)
  create
+Light
 File
@@ -2283,331 +2283,53 @@
 e(),
- text);%0A        %7D%0A        long end = System.nanoTime();%0A        double v = (end - start) * 1e-6 / N;%0A        System.out.printf(%22Parsing %25s :%25.3fms%5Cn%22, fileName, v);%0A        return v;%0A    %7D%0A%0A    private void testExample(@NotNull File fileName) throws Exception %7B%0A        PSFile file = (PSFile) createFile(fileName.getName(),
+%0A            PSLanguage.INSTANCE,%0A           
  rea

Remove use of deprecated superclass