package com.fasterxml.jackson.core.json.async;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.async.AsyncTestBase;
import com.fasterxml.jackson.core.testsupport.AsyncReaderWrapper;

import java.io.IOException;

public class AsyncNonStandardNumberParsingTest extends AsyncTestBase
{
    private final JsonFactory DEFAULT_F = new JsonFactory();

    public void testHexadecimal() throws Exception
    {
        final String JSON = "[ 0xc0ffee ]";

        // without enabling, should get an exception
        AsyncReaderWrapper p = createParser(DEFAULT_F, JSON, 1);
        assertToken(JsonToken.START_ARRAY, p.nextToken());
        try {
            p.nextToken();
            fail("Expected exception");
        } catch (Exception e) {
            verifyException(e, "Unexpected character ('x'");
        } finally {
            p.close();
        }
    }

    public void testHexadecimalBigX() throws Exception
    {
        final String JSON = "[ 0XC0FFEE ]";

        // without enabling, should get an exception
        AsyncReaderWrapper p = createParser(DEFAULT_F, JSON, 1);
        assertToken(JsonToken.START_ARRAY, p.nextToken());
        try {
            p.nextToken();
            fail("Expected exception");
        } catch (Exception e) {
            verifyException(e, "Unexpected character ('X'");
        } finally {
            p.close();
        }
    }

    public void testNegativeHexadecimal() throws Exception
    {
        final String JSON = "[ -0xc0ffee ]";

        // without enabling, should get an exception
        AsyncReaderWrapper p = createParser(DEFAULT_F, JSON, 1);
        assertToken(JsonToken.START_ARRAY, p.nextToken());
        try {
            p.nextToken();
            fail("Expected exception");
        } catch (Exception e) {
            verifyException(e, "Unexpected character ('x'");
        } finally {
            p.close();
        }
    }

    //next 2 tests do not work as expected
    /*
    public void testFloatMarker() throws Exception
    {
        final String JSON = "[ -0.123f ]";

        // without enabling, should get an exception
        AsyncReaderWrapper p = createParser(DEFAULT_F, JSON, 1);
        assertToken(JsonToken.START_ARRAY, p.nextToken());
        try {
            p.nextToken();
            fail("Expected exception");
        } catch (Exception e) {
            verifyException(e, "Unexpected character ('f'");
        } finally {
            p.close();
        }
    }

    public void testDoubleMarker() throws Exception
    {
        final String JSON = "[ -0.123d ]";

        // without enabling, should get an exception
        AsyncReaderWrapper p = createParser(DEFAULT_F, JSON, 1);
        assertToken(JsonToken.START_ARRAY, p.nextToken());
        try {
            p.nextToken();
            fail("Expected exception");
        } catch (Exception e) {
            verifyException(e, "Unexpected character ('d'");
        } finally {
            p.close();
        }
    }
    */

    private AsyncReaderWrapper createParser(JsonFactory f, String doc, int readBytes) throws IOException
    {
        return asyncForBytes(f, readBytes, _jsonDoc(doc), 1);
    }
}

@@ -3051,16 +3051,1997 @@
     */%0A%0A
+    /**%0A     * The format %22.NNN%22 (as opposed to %220.NNN%22) is not valid JSON, so this should fail%0A     */%0A    public void testLeadingDotInDecimal() throws Exception %7B%0A        final String JSON = %22%5B .123 %5D%22;%0A%0A        // without enabling, should get an exception%0A        AsyncReaderWrapper p = createParser(DEFAULT_F, JSON, 1);%0A        assertToken(JsonToken.START_ARRAY, p.nextToken());%0A        try %7B%0A            p.nextToken();%0A            fail(%22Expected exception%22);%0A        %7D catch (Exception e) %7B%0A            verifyException(e, %22Unexpected character ('.'%22);%0A        %7D finally %7B%0A            p.close();%0A        %7D%0A    %7D%0A%0A    /*%0A     * The format %22+NNN%22 (as opposed to %22NNN%22) is not valid JSON, so this should fail%0A     */%0A    public void testLeadingPlusSignInDecimalDefaultFail() throws Exception %7B%0A        final String JSON = %22%5B +123 %5D%22;%0A%0A        // without enabling, should get an exception%0A        AsyncReaderWrapper p = createParser(DEFAULT_F, JSON, 1);%0A        assertToken(JsonToken.START_ARRAY, p.nextToken());%0A        try %7B%0A            p.nextToken();%0A            fail(%22Expected exception%22);%0A        %7D catch (Exception e) %7B%0A            //the message does not match non-async parsers%0A            verifyException(e, %22Unrecognized token '+123'%22);%0A        %7D finally %7B%0A            p.close();%0A        %7D%0A    %7D%0A%0A    /**%0A     * The format %22NNN.%22 (as opposed to %22NNN%22) is not valid JSON, so this should fail%0A     */%0A    public void testTrailingDotInDecimal() throws Exception %7B%0A        final String JSON = %22%5B 123. %5D%22;%0A%0A        // without enabling, should get an exception%0A        AsyncReaderWrapper p = createParser(DEFAULT_F, JSON, 1);%0A        assertToken(JsonToken.START_ARRAY, p.nextToken());%0A        try %7B%0A            p.nextToken();%0A            fail(%22Expected exception%22);%0A        %7D catch (Exception e) %7B%0A            //the message does not match non-async parsers%0A            verifyException(e, %22Unexpected character (' '%22);%0A        %7D finally %7B%0A            p.close();%0A        %7D%0A    %7D%0A%0A
     priv

Update AsyncNonStandardNumberParsingTest.java (#789)