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

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

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class LexicalCastTest {

    @Test
    public void testCast_String() {
        assertEquals((Long)13L, LexicalCast.cast("13", long.class));
        assertEquals((Integer)13, LexicalCast.cast("13", int.class));
        assertEquals(asShort(13), LexicalCast.cast("13", short.class));
        assertEquals(asByte(13), LexicalCast.cast("13", byte.class));
        assertEquals(3.14f, LexicalCast.cast("3.14", float.class), 0.001f);
        assertEquals(3.14, LexicalCast.cast("3.14", double.class), 0.001);
        assertTrue(LexicalCast.cast("true", boolean.class));
        
        assertEquals((Long)13L, LexicalCast.cast("13", Long.class));
        assertEquals((Integer)13, LexicalCast.cast("13", Integer.class));
        assertEquals(asShort(13), LexicalCast.cast("13", Short.class));
        assertEquals(asByte(13), LexicalCast.cast("13", Byte.class));
        assertEquals(3.14f, LexicalCast.cast("3.14", Float.class), 0.001f);
        assertEquals(3.14, LexicalCast.cast("3.14", Double.class), 0.001);
        assertTrue(LexicalCast.cast("true", Boolean.class));
        
        assertThrows(ClassCastException.class, () -> {
            LexicalCast.cast("yes", boolean.class);
        });
        
    }
    
    @Test
    public void testCast_Number() {
        assertEquals((Long)13L, LexicalCast.cast(13, long.class));
        assertEquals((Integer)13, LexicalCast.cast(13L, int.class));
        assertEquals(asShort(13), LexicalCast.cast(13, short.class));
        assertEquals(asByte(13), LexicalCast.cast(13, byte.class));
        assertEquals(3.14f, LexicalCast.cast(3.14, float.class), 0.001f);
        assertEquals(3.14, LexicalCast.cast(3.14, double.class), 0.001);
        
        assertEquals((Long)13L, LexicalCast.cast(13, Long.class));
        assertEquals((Integer)13, LexicalCast.cast(13L, Integer.class));
        assertEquals(asShort(13), LexicalCast.cast(13, Short.class));
        assertEquals(asByte(13), LexicalCast.cast(13, Byte.class));
        assertEquals(3.14f, LexicalCast.cast(3.14, Float.class), 0.001f);
        assertEquals(3.14, LexicalCast.cast(3.14f, Double.class), 0.001);
    }
    
    @Test
    public void testStringConstructor() {
        assertEquals("[ranides]", LexicalCast.cast("ranides", Name.class).name());
    }
    
    private static Short asShort(int value) {
        return (short)value;
    }
    
    private static Byte asByte(int value) {
        return (byte)value;
    }
    
    private static final class Name {
        
        private final String mName;

        public Name(String iname) {
            this.mName = "[" + iname + "]";
        }

        public String name() {
            return mName;
        }
        
    }
}
