package com.alibaba.fastjson2.reader;

import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONFactory;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.annotation.JSONField;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class FieldReaderInt8FieldTest {
    @Test
    public void test() {
        Bean bean = new Bean();
        ObjectReader<Bean> objectReader = JSONFactory.defaultObjectReaderProvider.getObjectReader(Bean.class);
        FieldReader fieldReader = objectReader.getFieldReader("value");
        fieldReader.accept(bean, "123");
        assertEquals((byte) 123, bean.value);

        fieldReader.accept(bean, 65);
        assertEquals((byte) 65, bean.value);

        fieldReader.accept(bean, (short) 66);
        assertEquals((byte) 66, bean.value);

        assertThrows(JSONException.class, () -> fieldReader.accept(bean, new Object()));
        assertEquals((byte) 123, objectReader.readObject(JSONReader.of("{\"value\":123}")).value);
    }

    private static class Bean {
        public Byte value;
    }

    @Test
    public void test2() {
        ObjectReader<Bean2> objectReader = JSONFactory.defaultObjectReaderProvider.getObjectReader(Bean2.class);
        assertEquals(
                (byte) 123,
                objectReader.readObject(
                        JSONReader.of("{\"id\":101, \"value\":123}")
                ).value
        );
    }

    private static class Bean2 {
        public Byte value;
        public final int id;

        public Bean2(@JSONField(name = "id") int id) {
            this.id = id;
        }
    }
}
