// (c) 2012 B Smith-Mannschott -- Distributed under the Eclipse Public License
package bpsm.edn.printer;

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

import java.io.StringReader;
import java.io.StringWriter;
import java.util.ArrayList;

import org.junit.Test;

import bpsm.edn.parser.Parser;
import bpsm.edn.parser.Parsers;

public class PrinterTest {

    @Test
    public void testSingleValues() {
        assertRoundTrip("nil");
        assertRoundTrip("a");
        assertRoundTrip("a/b");
        assertRoundTrip("/");
        assertRoundTrip("true");
        assertRoundTrip("false");
        assertRoundTrip(":a");
        assertRoundTrip(":a/b");
        assertRoundTrip("1");
        assertRoundTrip("1N");
        assertRoundTrip("3.14159");
        assertRoundTrip("123456789101112131415.1617181920M");
        assertRoundTrip("\\space");
        assertRoundTrip("\\a");
        assertRoundTrip("\"ab\\ncd\"");
        assertRoundTrip("[]");
        assertRoundTrip("()");
        assertRoundTrip("{}");
        assertRoundTrip("#{}");
        assertRoundTrip("{#{},()}");
        assertRoundTrip("#uuid \"f81d4fae-7dec-11d0-a765-00a0c91e6bf6\"");
        assertRoundTrip("\"\\\\\\\"\\'\\b\\t\\n\\r\\f\"");
    }

    @Test
    public void testComplexValue() {
        assertRoundTrip("{:foo [1 2.0 19023847928034709821374012938749N 91821234112347634.128937467E-3M]\n" + 
        		" :bar/baz #{true false nil}\n" + 
        		" / (\"abc\\tdef\\n\" #uuid \"f81d4fae-7dec-11d0-a765-00a0c91e6bf6\")\n" + 
        		" \\formfeed [#inst \"2010\", #inst \"2010-11\", #inst \"2010-11-12T09:08:07.123+02:00\"]\n" + 
        		" :omega [a b c d \\a\\b\\c#{}]}");
    }

    
    @Test 
    public void testDefaultPrinter() {
    	StringWriter sw=new StringWriter();
    	Printer p=Printers.newPrinter(sw);
    	
    	ArrayList<Object> al=new ArrayList<Object>();
    	al.add(1);
    	al.add(2);
    	try {
			p.printValue(al);
		} catch (IOException e) {
			fail("Unexpected IO Exception");
		}
    	assertEquals("[1 2]", sw.toString());
    }
    
    void assertRoundTrip(String ednText) {
        Parser parser;
        parser = Parsers.newParser(Parsers.defaultConfiguration(), new StringReader(ednText));
        Object originalParsedValue = parser.nextValue();

        StringWriter sw = new StringWriter();
        Printer ew = Printers.newPrinter(Printers.defaultPrinterConfig(), sw);
        ew.printValue(originalParsedValue);
        ew.close();

        parser = Parsers.newParser(Parsers.defaultConfiguration(), new StringReader(sw.toString()));
        Object secondGenerationParsedValue = parser.nextValue();
        assertEquals("'" + ednText + "' => '" + sw.toString()
                + "' did not round-trip.", originalParsedValue,
                secondGenerationParsedValue);
        assertEquals(Parser.END_OF_INPUT, parser.nextValue());
        
    }

}

@@ -147,45 +147,8 @@
 als;
-%0Aimport static org.junit.Assert.fail;
 %0A%0Aim
@@ -1682,18 +1682,17 @@
    @Test
- 
 %0A
+
     publ
@@ -1726,17 +1726,20 @@
 ) %7B%0A    
-%09
+    
 StringWr
@@ -1745,17 +1745,19 @@
 riter sw
-=
+ = 
 new Stri
@@ -1772,17 +1772,20 @@
 ();%0A    
+ 
-%09
+   
 Printer 
@@ -1785,17 +1785,19 @@
 rinter p
-=
+ = 
 Printers
@@ -1821,15 +1821,21 @@
     
+ 
-%09
+   
 %0A    
-%09
+    
 Arra
@@ -1850,17 +1850,19 @@
 ject%3E al
-=
+ = 
 new Arra
@@ -1878,25 +1878,28 @@
 ect%3E();%0A    
+   
-%09
+ 
 al.add(1);%0A 
@@ -1901,17 +1901,20 @@
 1);%0A    
-%09
+    
 al.add(2
@@ -1924,18 +1924,12 @@
     
-%09try %7B%0A%09%09%09
+    
 p.pr
@@ -1946,81 +1946,16 @@
 l);%0A
-%09%09%7D catch (IOException e) %7B%0A%09%09%09fail(%22Unexpected IO Exception%22);%0A%09%09%7D%0A
+    
     
-%09
 asse

fix up PrinterTest