/*
 * See LICENSE file in distribution for copyright and licensing information.
 */
package org.yaml.snakeyaml.types;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @see http://yaml.org/type/str.html
 */
public class StrTagTest extends AbstractTest {
    private String getData(String data, String key) {
        return (String) getMapValue(data, key);
    }

    public void testString() throws IOException {
        assertEquals("abcd", getData("string: abcd", "string"));
    }

    public void testStringShorthand() throws IOException {
        assertEquals("abcd", getData("string: !!str abcd", "string"));
    }

    public void testStringTag() throws IOException {
        assertEquals("abcd", getData("string: !<tag:yaml.org,2002:str> abcd", "string"));
    }

    public void testUnicode() throws IOException {
        // escaped 8-bit unicode character (u-umlaut):
        assertEquals("\u00fc", load("\"\\xfc\""));
        assertEquals("\\xfc", load("\\xfc"));

        // 2 escaped 8-bit unicode characters (u-umlaut following by e-grave):
        assertEquals("\u00fc\u00e8", load("\"\\xfc\\xe8\""));

        // escaped 16-bit unicode character (em dash):
        assertEquals("\u2014", load("\"\\u2014\""));

        // UTF-32 encoding is explicitly not supported
        // TODO assertEquals("\"\\U2014AAAA\"", load("\"\\U2014AAAA\""));

        // (and I don't have a surrogate pair handy at the moment)
        // raw unicode characters in the stream (em dash)
        assertEquals("\u2014", load("\u2014"));
    }

    /**
     * @see http://code.google.com/p/jvyamlb/issues/detail?id=6
     */
    @SuppressWarnings("unchecked")
    public void testIssueId6() {
        Map<String, String> map = (Map<String, String>) load("---\ntest: |-\n \"Test\r\r (* error here)\"");
        assertEquals("\"Test\n\n(* error here)\"", map.get("test"));
    }

    public void testStrDump() {
        assertEquals("abc\n", dump("abc"));
    }

    public void testUnicodeDump() {
        assertEquals("\"\\xfc\"\n", dump("\"\u00fc\""));// double quoted must be
        // escaped
        assertEquals("\\u263a\n", dump("\\u263a"));
        assertEquals("The leading zero must be preserved.", "\\u063a\n", dump("\\u063a"));
    }

    public void testStringIntOut() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("number", "1");
        String output = dump(map);
        assertTrue(output, output.contains("number: !!str 1"));
    }

    public void testStringFloatOut() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("number", "1.1");
        String output = dump(map);
        assertTrue(output, output.contains("number: !!str 1.1"));
    }

    public void testStringBoolOut() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("number", "True");
        String output = dump(map);
        assertTrue(output, output.contains("number: !!str True"));
    }

    public void testEmitLongString() throws IOException {
        String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        String output = dump(str);
        assertEquals(str + "\n", output);
    }

    public void testEmitLongStringWithCR() throws IOException {
        String str = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n\n";
        String output = dump(str);
        assertEquals("'" + str + "\n  '\n", output);
    }

    public void testEmitDoubleQuoted() throws IOException {
        String str = "\"xx\"";
        String output = dump(str);
        assertEquals("'" + str + "'\n", output);
    }

    public void testEmitEndOfLine() throws IOException {
        String str = "xxxxxxx\n";
        String output = dump(str);
        assertEquals("'" + str + "\n  '\n", output);
    }
}

@@ -136,69 +136,237 @@
 .io.
-IOException;%0D%0Aimport java.util.HashMap;%0D%0Aimport java.util.Map
+ByteArrayOutputStream;%0D%0Aimport java.io.IOException;%0D%0Aimport java.io.OutputStreamWriter;%0D%0Aimport java.io.Writer;%0D%0Aimport java.nio.charset.Charset;%0D%0Aimport java.util.HashMap;%0D%0Aimport java.util.Map;%0D%0A%0D%0Aimport org.yaml.snakeyaml.Yaml
 ;%0D%0A%0D
@@ -2728,23 +2728,19 @@
 number: 
-!!str 1
+'1'
 %22));%0D%0A  
@@ -2979,17 +2979,13 @@
 er: 
-!!str 
+'
 1.1
+'
 %22));
@@ -3228,18 +3228,14 @@
 er: 
-!!str 
+'
 True
+'
 %22));
@@ -4149,32 +4149,32 @@
 t = dump(str);%0D%0A
-
         assertEq
@@ -4217,12 +4217,600 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    public void testDumpUtf16() throws IOException %7B%0D%0A        String str = %22xxx%22;%0D%0A        assertEquals(3, str.toString().length());%0D%0A        Yaml yaml = new Yaml();%0D%0A        Charset charset = Charset.forName(%22UTF-16%22);%0D%0A        ByteArrayOutputStream stream = new ByteArrayOutputStream();%0D%0A        Writer writer = new OutputStreamWriter(stream, charset);%0D%0A        yaml.dump(str, writer);%0D%0A        assertEquals(str + %22%5Cn%22, stream.toString(%22UTF-16%22));%0D%0A        assertEquals(%22Must include BOM: %22 + stream.toString(), (1 + 3 + 1) * 2, stream.toString()%0D%0A                .length());%0D%0A    %7D%0D
 %0A%7D%0D%0A

fix dump tests