package me.teridax.jcash.decode;

import me.teridax.jcash.lang.Locales;
import org.junit.Test;

import static junit.framework.TestCase.assertEquals;
import static me.teridax.jcash.decode.StringDecoder.*;

public class DecodeTests {
    @Test
    public void testDecodeSuccessfulFunctions() {
        // make sure a comma separated integer from fractions
        Locales.setDefaultLocale("de", "DE");

        assertEquals(decodePercent("1,003"), 100.3d, 1e-3d);

        decodeUniqueIdentificationNumber("9578647895");
        decodeUniqueIdentificationNumber("    927856347   ");
        decodeUniqueIdentificationNumber("0");
        decodeUniqueIdentificationNumber("'9578647895");

        decodeName("Adolf");
        decodeName("Günther");
        decodeName("Saßkia");

        decodeStreet("Bahnhofstraße 40/1");
        decodeStreet("Gülleweg 9");
        decodeStreet("Echsengaße 67 / 4");

        assertEquals(decodePercent("1,4%"), 1.4, 1e-3d);
        assertEquals(decodePercent("99"), 9900.0d);
        assertEquals(decodePercent("1,003%"), 1.003, 1e-5d);
        assertEquals(decodePercent("1,003"), 100.3, 1e-5d);
        assertEquals(decodePercent("'1,003"), 100.3, 1e-5d);

        assertEquals(decodeCurrency("1,3€"), 1.3d);
        assertEquals(decodeCurrency("‘0567€"), 567d);
        assertEquals(decodeCurrency("145,34"), 145, 34d);
        assertEquals(decodeCurrency("0,45   €"), 0.45d);
    }

    @Test
    public void decodeLocaleDependent() {
        Locales.setDefaultLocale("de", "DE");
        assertEquals(decodeCurrency("13.00,45€"), 1300.45);

        Locales.setDefaultLocale("en", "EN");
        assertEquals(decodeCurrency("13,00.45€"), 1300.45);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testDecodeInvalidFunctions() {
        decodeUniqueIdentificationNumber("q0948tvb6q047t 740   t74z0tz    784");
        decodeUniqueIdentificationNumber("-39867.8475");
        decodeUniqueIdentificationNumber("-398678475");
        decodeUniqueIdentificationNumber(" ß9qu908t76q34798t6q734vb9843");

        decodeName("John Doe");
        decodeName("3490qt67v 0b34");
        decodeName("Alexander9");
        decodeName("-ga76re78g6$§");

        decodeStreet("Bahnhofstraße -40/1");
        decodeStreet("Gülleweg 9//567");
        decodeStreet("23Echsengaße 67 / 4 Hofwetg 9");
    }
}
