package org.nibor.autolink;

import static org.junit.Assert.assertEquals;

public abstract class AutolinkTestCase {

    protected void assertLinked(String input, String expected, LinkType expectedLinkType) {
        String result = renderExtractedLinks(input, "|", expectedLinkType);
        assertEquals(expected, result);

        result = renderExtractedSpans(input, "|", expectedLinkType);
        assertEquals(expected, result);

    }

    protected void assertNotLinked(String input) {
        String result = renderExtractedLinks(input, "|", null);
        assertEquals(input, result);

        result = renderExtractedSpans(input, "|", null);
        assertEquals(input, result);
    }

    protected abstract LinkExtractor getLinkExtractor();

    protected String renderExtractedLinks(String input, final String marker, final LinkType expectedLinkType) {
        Iterable<LinkSpan> links = getLinkExtractor().extractLinks(input);
        return Autolink.renderLinks(input, links, new LinkRenderer() {
            @Override
            public void render(LinkSpan link, CharSequence text, StringBuilder sb) {
                if (expectedLinkType != null) {
                    assertEquals(expectedLinkType, link.getType());
                }
                sb.append(marker);
                sb.append(text, link.getBeginIndex(), link.getEndIndex());
                sb.append(marker);
            }
        });
    }

    protected String renderExtractedSpans(String input, final String marker, final LinkType expectedLinkType) {
        Iterable<Span> spans = getLinkExtractor().extractSpans(input);
        StringBuilder sb = new StringBuilder();
        for (Span span : spans) {
            if (span instanceof LinkSpan) {
                LinkType type = ((LinkSpan) span).getType();
                if (expectedLinkType != null) {
                    assertEquals(expectedLinkType, type);
                }
                sb.append(marker);
                sb.append(input, span.getBeginIndex(), span.getEndIndex());
                sb.append(marker);
            } else {
                sb.append(input, span.getBeginIndex(), span.getEndIndex());
            }
        }
        return sb.toString();
    }

}

@@ -872,34 +872,72 @@
     
-Iterable%3CLinkSpan%3E links =
+StringBuilder sb = new StringBuilder();%0A        for (Span span :
  get
@@ -963,20 +963,20 @@
 ract
-Link
+Span
 s(input)
 ;%0A  
@@ -963,33 +963,35 @@
 ractSpans(input)
-;
+) %7B
 %0A        return 
@@ -987,177 +987,92 @@
     
-return Autolink.renderLinks(input, links, new LinkRenderer() %7B%0A            @Override%0A            public void render(LinkSpan link, CharSequence text, StringBuilder sb) %7B
+    if (span instanceof LinkSpan) %7B%0A                LinkSpan link = (LinkSpan) span;
 %0A   
@@ -1267,11 +1267,12 @@
 end(
-tex
+inpu
 t, l
@@ -1353,32 +1353,39 @@
 );%0A            %7D
+ else %7B
 %0A        %7D);%0A   
@@ -1377,17 +1377,135 @@
         
-%7D
+        sb.append(input, span.getBeginIndex(), span.getEndIndex());%0A            %7D%0A        %7D%0A        return sb.toString(
 );%0A    %7D

Use non-deprecated API for tests