<commit_before>package com.uxxu.konashi.lib;

import android.support.test.runner.AndroidJUnit4;

import com.uxxu.konashi.lib.stores.KonashiAnalogStore;

import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.internal.util.reflection.Whitebox;

import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;


/**
 * Created by izumin on 7/31/15.
 */
@RunWith(Enclosed.class)
public class KonashiManagerTest {
    public static final String TAG = KonashiManagerTest.class.getSimpleName();

    abstract public static class BaseTest {
        @Spy private KonashiManager mManager;

        private ArgumentCaptor<UUID> mWriteMessageUuidCaptor;
        private ArgumentCaptor<byte[]> mWriteMessageValueCaptor;
        private ArgumentCaptor<KonashiErrorReason> mNotifyKonashiErrorCaptor;

        @Before
        public void setUp() throws Exception {
            MockitoAnnotations.initMocks(this);

            mWriteMessageUuidCaptor = ArgumentCaptor.forClass(UUID.class);
            mWriteMessageValueCaptor = ArgumentCaptor.forClass(byte[].class);
            Mockito.doNothing().when(mManager)
                    .addWriteMessage(mWriteMessageUuidCaptor.capture(), mWriteMessageValueCaptor.capture());

            mNotifyKonashiErrorCaptor = ArgumentCaptor.forClass(KonashiErrorReason.class);
            Mockito.doNothing().when(mManager).notifyKonashiError(mNotifyKonashiErrorCaptor.capture());
        }

        protected KonashiManager getManager() {
            return mManager;
        }

        protected void stubIsEnableAccessKonashi(boolean isEnable) {
            Mockito.doReturn(isEnable).when(mManager).isEnableAccessKonashi();
        }

        protected void stubIsEnableI2c(boolean isEnable) {
            Mockito.doReturn(isEnable).when(mManager).isEnableI2c();
        }

        public UUID getCapturedWrittenUuid() {
            return mWriteMessageUuidCaptor.getValue();
        }

        public byte[] getCapturedWrittenValue() {
            return mWriteMessageValueCaptor.getValue();
        }

        public KonashiErrorReason getCapturedError() {
            return mNotifyKonashiErrorCaptor.getValue();
        }
    }

    @RunWith(Enclosed.class)
    public static class PioTest {
        @RunWith(AndroidJUnit4.class)
        public static class PinModeTest extends BaseTest {
            @Test
            public void whenKonashiIsNotEnable() {
                stubIsEnableAccessKonashi(false);
                getManager().pinMode(9999, 9999);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.NOT_READY);
            }

            @Test
            public void withInvalidPin() {
                stubIsEnableAccessKonashi(true);
                getManager().pinMode(9999, Konashi.OUTPUT);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.INVALID_PARAMETER);
            }

            @Test
            public void withInvalidMode() {
                stubIsEnableAccessKonashi(true);
                getManager().pinMode(Konashi.PIO1, 9999);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.INVALID_PARAMETER);
            }

            @Test
            public void whenPioModeHasNotSet() {
                stubIsEnableAccessKonashi(true);
                getManager().pinMode(Konashi.PIO1, Konashi.OUTPUT);
                assertThat(getCapturedWrittenUuid()).isEqualTo(KonashiUUID.PIO_SETTING_UUID);
                assertThat(getCapturedWrittenValue()).isEqualTo(new byte[] {0x02});
            }

            @Test
            public void whenPioModeHasAlreadySet() {
                stubIsEnableAccessKonashi(true);
                Whitebox.setInternalState(getManager(), "mPioModeSetting", (byte) 0x17);
                getManager().pinMode(Konashi.PIO1, Konashi.INPUT);
                assertThat(getCapturedWrittenUuid()).isEqualTo(KonashiUUID.PIO_SETTING_UUID);
                assertThat(getCapturedWrittenValue()).isEqualTo(new byte[] {0x15});
            }
        }

        @RunWith(AndroidJUnit4.class)
        public static class PinModeAllTest extends BaseTest {
            @Test
            public void whenKonashiIsNotEnable() {
                stubIsEnableAccessKonashi(false);
                getManager().pinModeAll(9999);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.NOT_READY);
            }

            @Test
            public void withInvalidMode() {
                stubIsEnableAccessKonashi(true);
                getManager().pinModeAll(9999);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.INVALID_PARAMETER);
            }

            @Test
            public void whenPioModeAll() {
                stubIsEnableAccessKonashi(true);
                getManager().pinModeAll(0x17);
                assertThat(getCapturedWrittenUuid()).isEqualTo(KonashiUUID.PIO_SETTING_UUID);
                assertThat(getCapturedWrittenValue()).isEqualTo(new byte[] {0x17});
            }
        }

        @RunWith(AndroidJUnit4.class)
        public static class PinPullupTest extends BaseTest {
            @Test
            public void whenKonashiIsNotEnable() {
                stubIsEnableAccessKonashi(false);
                getManager().pinPullup(9999, 9999);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.NOT_READY);
            }

            @Test
            public void withInvalidPin() {
                stubIsEnableAccessKonashi(true);
                getManager().pinPullup(9999, Konashi.PULLUP);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.INVALID_PARAMETER);
            }

            @Test
            public void withInvalidMode() {
                stubIsEnableAccessKonashi(true);
                getManager().pinPullup(Konashi.PIO1, 9999);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.INVALID_PARAMETER);
            }

            @Test
            public void whenPioPullupHasNotSet() {
                stubIsEnableAccessKonashi(true);
                getManager().pinPullup(Konashi.PIO1, Konashi.PULLUP);
                assertThat(getCapturedWrittenUuid()).isEqualTo(KonashiUUID.PIO_PULLUP_UUID);
                assertThat(getCapturedWrittenValue()).isEqualTo(new byte[] {0x02});
            }

            @Test
            public void whenPioPullupHasAlreadySet() {
                stubIsEnableAccessKonashi(true);
                Whitebox.setInternalState(getManager(), "mPioPullup", (byte) 0x17);
                getManager().pinPullup(Konashi.PIO1, Konashi.NO_PULLS);
                assertThat(getCapturedWrittenUuid()).isEqualTo(KonashiUUID.PIO_PULLUP_UUID);
                assertThat(getCapturedWrittenValue()).isEqualTo(new byte[] {0x15});
            }
        }
    }

    @RunWith(Enclosed.class)
    public static class AnalogTest {
        @RunWith(AndroidJUnit4.class)
        public static class AnalogReadTest extends BaseTest {
            @Spy private KonashiAnalogStore mAnalogStore;

            @Before
            public void setUp() throws Exception {
                super.setUp();
                Whitebox.setInternalState(getManager(), "mAnalogStore", mAnalogStore);
            }

            @Test<commit_msg>Add test for KonashiManager#pinPullupAll()<commit_after>package com.uxxu.konashi.lib;

import android.support.test.runner.AndroidJUnit4;

import com.uxxu.konashi.lib.stores.KonashiAnalogStore;

import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.mockito.internal.util.reflection.Whitebox;

import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;


/**
 * Created by izumin on 7/31/15.
 */
@RunWith(Enclosed.class)
public class KonashiManagerTest {
    public static final String TAG = KonashiManagerTest.class.getSimpleName();

    abstract public static class BaseTest {
        @Spy private KonashiManager mManager;

        private ArgumentCaptor<UUID> mWriteMessageUuidCaptor;
        private ArgumentCaptor<byte[]> mWriteMessageValueCaptor;
        private ArgumentCaptor<KonashiErrorReason> mNotifyKonashiErrorCaptor;

        @Before
        public void setUp() throws Exception {
            MockitoAnnotations.initMocks(this);

            mWriteMessageUuidCaptor = ArgumentCaptor.forClass(UUID.class);
            mWriteMessageValueCaptor = ArgumentCaptor.forClass(byte[].class);
            Mockito.doNothing().when(mManager)
                    .addWriteMessage(mWriteMessageUuidCaptor.capture(), mWriteMessageValueCaptor.capture());

            mNotifyKonashiErrorCaptor = ArgumentCaptor.forClass(KonashiErrorReason.class);
            Mockito.doNothing().when(mManager).notifyKonashiError(mNotifyKonashiErrorCaptor.capture());
        }

        protected KonashiManager getManager() {
            return mManager;
        }

        protected void stubIsEnableAccessKonashi(boolean isEnable) {
            Mockito.doReturn(isEnable).when(mManager).isEnableAccessKonashi();
        }

        protected void stubIsEnableI2c(boolean isEnable) {
            Mockito.doReturn(isEnable).when(mManager).isEnableI2c();
        }

        public UUID getCapturedWrittenUuid() {
            return mWriteMessageUuidCaptor.getValue();
        }

        public byte[] getCapturedWrittenValue() {
            return mWriteMessageValueCaptor.getValue();
        }

        public KonashiErrorReason getCapturedError() {
            return mNotifyKonashiErrorCaptor.getValue();
        }
    }

    @RunWith(Enclosed.class)
    public static class PioTest {
        @RunWith(AndroidJUnit4.class)
        public static class PinModeTest extends BaseTest {
            @Test
            public void whenKonashiIsNotEnable() {
                stubIsEnableAccessKonashi(false);
                getManager().pinMode(9999, 9999);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.NOT_READY);
            }

            @Test
            public void withInvalidPin() {
                stubIsEnableAccessKonashi(true);
                getManager().pinMode(9999, Konashi.OUTPUT);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.INVALID_PARAMETER);
            }

            @Test
            public void withInvalidMode() {
                stubIsEnableAccessKonashi(true);
                getManager().pinMode(Konashi.PIO1, 9999);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.INVALID_PARAMETER);
            }

            @Test
            public void whenPioModeHasNotSet() {
                stubIsEnableAccessKonashi(true);
                getManager().pinMode(Konashi.PIO1, Konashi.OUTPUT);
                assertThat(getCapturedWrittenUuid()).isEqualTo(KonashiUUID.PIO_SETTING_UUID);
                assertThat(getCapturedWrittenValue()).isEqualTo(new byte[] {0x02});
            }

            @Test
            public void whenPioModeHasAlreadySet() {
                stubIsEnableAccessKonashi(true);
                Whitebox.setInternalState(getManager(), "mPioModeSetting", (byte) 0x17);
                getManager().pinMode(Konashi.PIO1, Konashi.INPUT);
                assertThat(getCapturedWrittenUuid()).isEqualTo(KonashiUUID.PIO_SETTING_UUID);
                assertThat(getCapturedWrittenValue()).isEqualTo(new byte[] {0x15});
            }
        }

        @RunWith(AndroidJUnit4.class)
        public static class PinModeAllTest extends BaseTest {
            @Test
            public void whenKonashiIsNotEnable() {
                stubIsEnableAccessKonashi(false);
                getManager().pinModeAll(9999);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.NOT_READY);
            }

            @Test
            public void withInvalidMode() {
                stubIsEnableAccessKonashi(true);
                getManager().pinModeAll(9999);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.INVALID_PARAMETER);
            }

            @Test
            public void whenPioModeAll() {
                stubIsEnableAccessKonashi(true);
                getManager().pinModeAll(0x17);
                assertThat(getCapturedWrittenUuid()).isEqualTo(KonashiUUID.PIO_SETTING_UUID);
                assertThat(getCapturedWrittenValue()).isEqualTo(new byte[] {0x17});
            }
        }

        @RunWith(AndroidJUnit4.class)
        public static class PinPullupTest extends BaseTest {
            @Test
            public void whenKonashiIsNotEnable() {
                stubIsEnableAccessKonashi(false);
                getManager().pinPullup(9999, 9999);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.NOT_READY);
            }

            @Test
            public void withInvalidPin() {
                stubIsEnableAccessKonashi(true);
                getManager().pinPullup(9999, Konashi.PULLUP);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.INVALID_PARAMETER);
            }

            @Test
            public void withInvalidMode() {
                stubIsEnableAccessKonashi(true);
                getManager().pinPullup(Konashi.PIO1, 9999);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.INVALID_PARAMETER);
            }

            @Test
            public void whenPioPullupHasNotSet() {
                stubIsEnableAccessKonashi(true);
                getManager().pinPullup(Konashi.PIO1, Konashi.PULLUP);
                assertThat(getCapturedWrittenUuid()).isEqualTo(KonashiUUID.PIO_PULLUP_UUID);
                assertThat(getCapturedWrittenValue()).isEqualTo(new byte[] {0x02});
            }

            @Test
            public void whenPioPullupHasAlreadySet() {
                stubIsEnableAccessKonashi(true);
                Whitebox.setInternalState(getManager(), "mPioPullup", (byte) 0x17);
                getManager().pinPullup(Konashi.PIO1, Konashi.NO_PULLS);
                assertThat(getCapturedWrittenUuid()).isEqualTo(KonashiUUID.PIO_PULLUP_UUID);
                assertThat(getCapturedWrittenValue()).isEqualTo(new byte[] {0x15});
            }
        }

        @RunWith(AndroidJUnit4.class)
        public static class PinPullupAllTest extends BaseTest {
            @Test
            public void whenKonashiIsNotEnable() {
                stubIsEnableAccessKonashi(false);
                getManager().pinPullupAll(9999);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.NOT_READY);
            }

            @Test
            public void withInvalidMode() {
                stubIsEnableAccessKonashi(true);
                getManager().pinPullupAll(9999);
                assertThat(getCapturedError()).isEqualTo(KonashiErrorReason.INVALID_PARAMETER);
            }

            @Test
            public void whenPioModeAll() {
                stubIsEnableAccessKonashi(true);
                getManager().pinPullupAll(0x17);
                assertThat(getCapturedWrittenUuid()).isEqualTo(KonashiUUID.PIO_PULLUP_UUID);
                assertThat(getCapturedWrittenValue()).isEqualTo(new byte[] {0x17});
            }
        }
    }

    @RunWith(Enclosed.class)
    public static class AnalogTest {
        @RunWith(AndroidJUnit4.class)
        public static class AnalogReadTest extends BaseTest {
            @Spy private KonashiAnalogStore mAnalogStore;

            @Before
            public void setUp() throws Exception {
                super.setUp();
                Whitebox.setInternalState(getManager(), "mAnalogStore", mAnalogStore);
            }

            @Test
{'fraction_non_alphanumeric': 0.08961735011840957, 'fraction_numerical': 0.011155428144085754, 'mean_word_length': 2.2998149290561383, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2773904', 'n_tokens_mistral': 4549, 'n_tokens_neox': 4410, 'n_words': 605}