package diffutils;

import difflib.DiffUtils;
import difflib.Patch;
import junit.framework.TestCase;

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

public class GenerateUnifiedDiffTest extends TestCase {
    static final String FS = File.separator;

    public List<String> fileToLines(String filename) {
        List<String> lines = new LinkedList<String>();
        String line = "";
        try {
            BufferedReader in = new BufferedReader(new FileReader(filename));
            while ((line = in.readLine()) != null) {
                lines.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lines;
    }

    public void testGenerateUnified() throws Exception {
        List<String> origLines = fileToLines("test" + FS + "mocks" + FS + "original.txt");
        List<String> revLines = fileToLines("test" + FS + "mocks" + FS + "revised.txt");

        verify(origLines, revLines);
    }

    public void testGenerateUnifiedWithOneDelta() throws Exception {
        List<String> origLines = fileToLines("test" + FS + "mocks" + FS + "one_delta_test_original.txt");
        List<String> revLines = fileToLines("test" + FS + "mocks" + FS + "one_delta_test_revised.txt");

        verify(origLines, revLines);
    }

    public void testDiff_Issue10() throws Exception {
        final List<String> baseLines = fileToLines("test" + FS + "mocks" + FS + "issue10_base.txt");
        final List<String> patchLines = fileToLines("test" + FS + "mocks" + FS + "issue10_patch.txt");
        final Patch p = DiffUtils.parseUnifiedDiff(patchLines);
        DiffUtils.patch(baseLines, p);
    }

    public void testDiff_Issue11() throws Exception {
        final List<String> lines1 = fileToLines("test" + FS + "mocks" + FS + "issue11_1.txt");
        final List<String> lines2 = fileToLines("test" + FS + "mocks" + FS + "issue11_2.txt");
        verify(lines1, lines2);
    }

    public void testDiff5() throws Exception {
        final List<String> lines1 = fileToLines("test" + FS + "mocks" + FS + "5A.txt");
        final List<String> lines2 = fileToLines("test" + FS + "mocks" + FS + "5B.txt");
        verify(lines1, lines2);
    }

    private void verify(List<String> origLines, List<String> revLines) throws Exception {
        Patch p = DiffUtils.diff(origLines, revLines);
        List<String> unifiedDiff = DiffUtils.generateUnifiedDiff(
                "test" + FS + "mocks" + FS + "original.txt", "test" + FS + "mocks" + FS + "revised.txt", origLines, p, 10);

        Patch fromUnifiedPatch = DiffUtils.parseUnifiedDiff(unifiedDiff);
        List<String> patchedLines = (List<String>) fromUnifiedPatch.applyTo(origLines);

        assertTrue(revLines.size() == patchedLines.size());
        for (int i = 0; i < revLines.size(); i++) {
            String l1 = revLines.get(i);
            String l2 = patchedLines.get(i);
            if (!l1.equals(l2)) {
                fail("Line " + (i + 1) + " of the patched file did not match the revised original");
            }
        }
    }
}

@@ -202,16 +202,41 @@
 eption;%0A
+import java.util.Arrays;%0A
 import j
@@ -1414,32 +1414,285 @@
 vLines);%0A    %7D%0A%0A
+    public void testGenerateUnifiedDiffWithoutAnyDeltas() throws Exception %7B%0A        List%3CString%3E test = Arrays.asList(%22abc%22);%0A        Patch patch = DiffUtils.diff(test, test);%0A        DiffUtils.generateUnifiedDiff(%22abc%22, %22abc%22, test, patch, 0);%0A    %7D%0A%0A
     public void 

add unit tests for generating uniffied diff with empty deltas case