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

import static net.ranides.assira.junit.NewAssert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public final class StringTester {
    
    private StringTester() {
        /* utility class */
    }
    
    public static void assertString(String expected, CharSequence value) {
        
        assertEquals(expected, value.toString());
        
        assertTrue(expected.contentEquals(value));
        
        cmp(expected, value);
                
        assertThrows(IndexOutOfBoundsException.class, ()->{
            value.charAt(-1);
        });
        assertThrows(IndexOutOfBoundsException.class, ()->{
            value.charAt(value.length());
        });
        assertThrows(IndexOutOfBoundsException.class, ()->{
            value.charAt(value.length()+1);
        });
        assertThrows(IndexOutOfBoundsException.class, ()->{
            value.subSequence(1, value.length()+2);
        });
        assertThrows(IndexOutOfBoundsException.class, ()->{
            value.subSequence(-1, 2);
        });
        assertThrows(IllegalArgumentException.class, ()->{
            value.subSequence(2, 1);
        });
    }
    
    private static void cmp(CharSequence expected, CharSequence value) {
        String message = "'" + expected + "' != '" + value + "'";
        assertEquals(message, expected.length(), value.length());
        for(int i=0, n=expected.length(); i<n; i++) {
            assertEquals("At ["+i+"] " + message, expected.charAt(i), value.charAt(i));
        }
    }
}
