/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.io;

import java.io.IOException;
import java.nio.charset.UnsupportedCharsetException;
import net.ranides.assira.text.Charsets;
import org.junit.Test;
import static net.ranides.assira.junit.NewAssert.*;
import net.ranides.assira.text.IOStrings;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class StringInputTest {
    
    private static final String TEXT = "Pchnąć w tę łódź jeża lub ośm skrzyń fig";

    @Test
    public void testUTF8() throws IOException {
        StringInput si1 = new StringInput(TEXT, Charsets.UTF8);
        assertEquals(TEXT, IOStrings.read(si1, si1.charset()));
        assertNotEquals(TEXT, IOStrings.read(si1, Charsets.PL_DOS));
        assertEquals(Charsets.UTF8, si1.charset());
        
        StringInput si2 = new StringInput(TEXT, Charsets.UTF8.name());
        assertEquals(TEXT, IOStrings.read(si2, si2.charset()));
        assertNotEquals(TEXT, IOStrings.read(si2, Charsets.PL_DOS));
        assertEquals(Charsets.UTF8, si2.charset());
        
        StringInput si3 = new StringInput(TEXT);
        assertEquals(TEXT, IOStrings.read(si3, si3.charset()));
        assertNotEquals(TEXT, IOStrings.read(si3, Charsets.PL_DOS));
        assertEquals(Charsets.UTF8, si3.charset());
        
        assertThrows(UnsupportedCharsetException.class, ()->{
            new StringInput(TEXT, "unknown-cs");
        });
    }
    
    @Test
    public void testPL() throws IOException {
        StringInput si1 = new StringInput(TEXT, Charsets.PL_WINDOWS);
        assertEquals(TEXT, IOStrings.read(si1, si1.charset()));
        assertNotEquals(TEXT, IOStrings.read(si1, Charsets.PL_DOS));
        assertEquals(Charsets.PL_WINDOWS, si1.charset());
        
        StringInput si2 = new StringInput(TEXT, Charsets.PL_WINDOWS.name());
        assertEquals(TEXT, IOStrings.read(si2, si2.charset()));
        assertNotEquals(TEXT, IOStrings.read(si2, Charsets.PL_DOS));
        assertEquals(Charsets.PL_WINDOWS, si2.charset());
        
        assertThrows(UnsupportedCharsetException.class, ()->{
            new StringInput(TEXT, "unknown-cs");
        });
    }
    
}
