package nl.hsac.fitnesse.slim.converter;

import fitnesse.slim.converters.ConverterRegistry;
import org.junit.Test;

import java.util.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class NumberedListConverterTest {
    private final NumberedListConverter converter = new NumberedListConverter();

    @Test
    public void testToStringNull() {
        assertNull(converter.toString(null));
    }

    @Test
    public void testToStringEmpty() {
        assertEquals("<ol></ol>", converter.toString(new ArrayList<Object>()));
    }

    @Test
    public void testToStringFilled() {
        ArrayList<Object> list = new ArrayList<Object>(Arrays.asList("b", "c", "d", null));
        assertEquals("<ol><li>b</li><li>c</li><li>d</li><li>null</li></ol>", converter.toString(list));
    }

    @Test
    public void testFromStringEmpty() {
        assertEquals(new ArrayList<Object>(), converter.fromString("<ol> </ol>"));
    }

    @Test
    public void testFromStringFilled() {
        List<String> expected = Arrays.asList("b", "c", "d");
        assertEquals(expected, converter.fromString("<ol> <li>   b </li> <li>   c </li><li>d</li></ol>"));
    }

    @Test
    public void testFromStringStandardList() {
        List<String> expected = Arrays.asList("b", "c", "d");
        assertEquals(expected, converter.fromString(expected.toString()));
    }

    @Test
    public void testViaRegistry() {
        NumberedListConverter.register();
        ConverterRegistry.addConverter(Map.class, new MapConverter());

        Map<String, List<String>> map = new HashMap<String, List<String>>();
        map.put("test", new LinkedList<String>(Arrays.asList("b", "c", null)));
        assertEquals(
                "<table class=\"hash_table\">\n" +
                "\t<tr class=\"hash_row\">\n" +
                "\t\t<td class=\"hash_key\">test</td>\n" +
                "\t\t<td class=\"hash_value\"><ol><li>b</li><li>c</li><li>null</li></ol></td>\n" +
                "\t</tr>\n" +
                "</table>",
                ConverterRegistry.getConverterForClass(Map.class).toString(map));

    Map<String, List<Integer>> map2 = new HashMap<String, List<Integer>>();
    map2.put("test", new LinkedList<Integer>(Arrays.asList(1, null, 3)));
    assertEquals(
            "<table class=\"hash_table\">\n" +
                    "\t<tr class=\"hash_row\">\n" +
                    "\t\t<td class=\"hash_key\">test</td>\n" +
                    "\t\t<td class=\"hash_value\"><ol><li>1</li><li>null</li><li>3</li></ol></td>\n" +
                    "\t</tr>\n" +
                    "</table>",
            ConverterRegistry.getConverterForClass(Map.class).toString(map2));

    Map<String, ArrayList<Object>> map3 = new HashMap<String, ArrayList<Object>>();
    map3.put("test", new ArrayList<Object>(Arrays.asList(1, null, "test")));
    assertEquals(
            "<table class=\"hash_table\">\n" +
                    "\t<tr class=\"hash_row\">\n" +
                    "\t\t<td class=\"hash_key\">test</td>\n" +
                    "\t\t<td class=\"hash_value\"><ol><li>1</li><li>null</li><li>test</li></ol></td>\n" +
                    "\t</tr>\n" +
                    "</table>",
            ConverterRegistry.getConverterForClass(Map.class).toString(map3));
    }
}

@@ -2131,20 +2131,42 @@
 ing(map)
-);%0A%0A
+.replace(%22%5Cr%22, %22%22));%0A%0A    
     Map%3C
@@ -2229,24 +2229,28 @@
 nteger%3E%3E();%0A
+    
     map2.put
@@ -2307,24 +2307,28 @@
 null, 3)));%0A
+    
     assertEq
@@ -2325,32 +2325,36 @@
   assertEquals(%0A
+    
             %22%3Cta
@@ -2396,32 +2396,36 @@
                 
+    
 %22%5Ct%3Ctr class=%5C%22h
@@ -2432,32 +2432,36 @@
 ash_row%5C%22%3E%5Cn%22 +%0A
+    
                 
@@ -2519,32 +2519,36 @@
                 
+ 
+   
 %22%5Ct%5Ct%3Ctd class=%5C
@@ -2626,32 +2626,36 @@
                 
+    
 %22%5Ct%3C/tr%3E%5Cn%22 +%0A  
@@ -2664,32 +2664,36 @@
                 
+ 
+   
 %22%3C/table%3E%22,%0A    
@@ -2680,32 +2680,36 @@
     %22%3C/table%3E%22,%0A
+    
             Conv
@@ -2768,20 +2768,42 @@
 ng(map2)
-);%0A%0A
+.replace(%22%5Cr%22, %22%22));%0A%0A    
     Map%3C
@@ -2874,24 +2874,28 @@
 Object%3E%3E();%0A
+    
     map3.put
@@ -2955,24 +2955,28 @@
  %22test%22)));%0A
+    
     assertEq
@@ -2973,32 +2973,36 @@
   assertEquals(%0A
+    
             %22%3Cta
@@ -3044,32 +3044,36 @@
                 
+ 
+   
 %22%5Ct%3Ctr class=%5C%22h
@@ -3080,32 +3080,36 @@
 ash_row%5C%22%3E%5Cn%22 +%0A
+    
                 
@@ -3167,32 +3167,36 @@
                 
+ 
+   
 %22%5Ct%5Ct%3Ctd class=%5C
@@ -3277,32 +3277,36 @@
                 
+    
 %22%5Ct%3C/tr%3E%5Cn%22 +%0A  
@@ -3315,32 +3315,36 @@
                 
+  
+  
 %22%3C/table%3E%22,%0A    
@@ -3331,32 +3331,36 @@
     %22%3C/table%3E%22,%0A
+    
             Conv
@@ -3419,16 +3419,34 @@
 ng(map3)
+.replace(%22%5Cr%22, %22%22)
 );%0A    %7D

Allow test to pass on windows