<commit_before>package com.garpr.android.features.favoritePlayers

import com.garpr.android.BaseTest
import com.garpr.android.data.models.AbsPlayer
import com.garpr.android.data.models.Endpoint
import com.garpr.android.data.models.LitePlayer
import com.garpr.android.data.models.Region
import com.garpr.android.features.favoritePlayers.FavoritePlayersViewModel.ListItem
import com.garpr.android.misc.Schedulers
import com.garpr.android.misc.ThreadUtils
import com.garpr.android.repositories.FavoritePlayersRepository
import com.garpr.android.repositories.IdentityRepository
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.koin.test.inject
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class FavoritePlayersViewModelTest : BaseTest() {

    private lateinit var viewModel: FavoritePlayersViewModel

    protected val favoritePlayersRepository: FavoritePlayersRepository by inject()
    protected val identityRepository: IdentityRepository by inject()
    protected val schedulers: Schedulers by inject()
    protected val threadUtils: ThreadUtils by inject()

    companion object {
        private val CHARLEZARD: AbsPlayer = LitePlayer(
                id = "587a951dd2994e15c7dea9fe",
                name = "Charlezard"
        )

        private val IMYT: AbsPlayer = LitePlayer(
                id = "5877eb55d2994e15c7dea98b",
                name = "Imyt"
        )

        private val MIKKUZ: AbsPlayer = LitePlayer(
                id = "583a4a15d2994e0577b05c74",
                name = "mikkuz"
        )

        private val SNAP: AbsPlayer = LitePlayer(
                id = "59213f1ad2994e1d79144956",
                name = "Snap"
        )

        private val TWO_SAINT: AbsPlayer = LitePlayer(
                id = "56d093064d7521611048599e",
                name = "2Saint"
        )

        private val NEW_JERSEY = Region(
                displayName = "<NAME>",
                id = "norcal",
                endpoint = Endpoint.NOT_GAR_PR
        )

        private val NORCAL = Region(
                displayName = "Norcal",
                id = "norcal",
                endpoint = Endpoint.GAR_PR
        )
    }

    @Before
    override fun setUp() {
        super.setUp()

        viewModel = FavoritePlayersViewModel(favoritePlayersRepository, identityRepository,
                schedulers, threadUtils)
    }

    @Test
    fun testAddFavoritePlayerCausesUpdate() {
        var state: FavoritePlayersViewModel.State? = null

        viewModel.stateLiveData.observeForever {
            state = it
        }

        favoritePlayersRepository.addPlayer(MIKKUZ, NORCAL)
        assertEquals(1, state?.list?.size)

        val player = state?.list?.get(0) as ListItem.FavoritePlayer
        assertFalse(player.isIdentity)
        assertEquals(MIKKUZ, player.player)
    }

    @Test
    fun testAddAndRemoveFavoritePlayersCausesUpdates() {
        var state: FavoritePlayersViewModel.State? = null

        viewModel.stateLiveData.observeForever {
            state = it
        }

        favoritePlayersRepository.addPlayer(IMYT, NORCAL)
        assertEquals(1, state?.list?.size)

        var player = state?.list?.get(0) as ListItem.FavoritePlayer
        assertFalse(player.isIdentity)
        assertEquals(IMYT, player.player)

        favoritePlayersRepository.addPlayer(SNAP, NORCAL)
        assertEquals(2, state?.list?.size)

        player = state?.list?.get(0) as ListItem.FavoritePlayer
        assertEquals(IMYT, player.player)

        player = state?.list?.get(1) as ListItem.FavoritePlayer
        assertEquals(SNAP, player.player)

        favoritePlayersRepository.removePlayer(IMYT)
        assertEquals(1, state?.list?.size)

        player = state?.list?.get(0) as ListItem.FavoritePlayer
        assertEquals(SNAP, player.player)
    }

    @Test
    fun testSearch() {
        favoritePlayersRepository.addPlayer(CHARLEZARD, NORCAL)
        favoritePlayersRepository.addPlayer(IMYT, NORCAL)
        favoritePlayersRepository.addPlayer(MIKKUZ, NORCAL)
        favoritePlayersRepository.addPlayer(SNAP, NORCAL)
        favoritePlayersRepository.addPlayer(TWO_SAINT, NEW_JERSEY)

        var state: FavoritePlayersViewModel.State? = null

        viewModel.stateLiveData.observeForever {
            state = it
        }

        viewModel.search("i")
        assertEquals(3, state?.searchResults?.size)

        var player = state?.searchResults?.get(0) as ListItem.FavoritePlayer
        assertEquals(IMYT, player.player)

        player = state?.searchResults?.get(1) as ListItem.FavoritePlayer
        assertEquals(MIKKUZ, player.player)

        player = state?.searchResults?.get(2) as ListItem.FavoritePlayer
        assertEquals(TWO_SAINT, player.player)
    }

    @Test
    fun testSearchWithBlankStringAndNoFavorites() {
        var state: FavoritePlayersViewModel.State? = null

        viewModel.stateLiveData.observeForever {
            state = it
        }

        viewModel.search(" ")
        assertNull(state?.searchResults)
    }

    @Test
    fun testSearchWithEmptyStringAndNoFavorites() {
        var state: FavoritePlayersViewModel.State? = null

        viewModel.stateLiveData.observeForever {
            state = it
        }

        viewModel.search("")
        assertNull(state?.searchResults)
    }

    @Test
    fun testSearchWithNullStringAndNoFavorites() {
        var state: FavoritePlayersViewModel.State? = null

        viewModel.stateLiveData.observeForever {
            state = it
        }

        viewModel.search(null)
        assertNull(state?.searchResults)
    }

}
<commit_msg>added testing of FavoritePlayersViewModel#init<commit_after>package com.garpr.android.features.favoritePlayers

import com.garpr.android.BaseTest
import com.garpr.android.data.models.AbsPlayer
import com.garpr.android.data.models.Endpoint
import com.garpr.android.data.models.LitePlayer
import com.garpr.android.data.models.Region
import com.garpr.android.features.favoritePlayers.FavoritePlayersViewModel.ListItem
import com.garpr.android.misc.Schedulers
import com.garpr.android.misc.ThreadUtils
import com.garpr.android.repositories.FavoritePlayersRepository
import com.garpr.android.repositories.IdentityRepository
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.koin.test.inject
import org.robolectric.RobolectricTestRunner

@RunWith(RobolectricTestRunner::class)
class FavoritePlayersViewModelTest : BaseTest() {

    private lateinit var viewModel: FavoritePlayersViewModel

    protected val favoritePlayersRepository: FavoritePlayersRepository by inject()
    protected val identityRepository: IdentityRepository by inject()
    protected val schedulers: Schedulers by inject()
    protected val threadUtils: ThreadUtils by inject()

    companion object {
        private val CHARLEZARD: AbsPlayer = LitePlayer(
                id = "587a951dd2994e15c7dea9fe",
                name = "Charlezard"
        )

        private val IMYT: AbsPlayer = LitePlayer(
                id = "5877eb55d2994e15c7dea98b",
                name = "Imyt"
        )

        private val MIKKUZ: AbsPlayer = LitePlayer(
                id = "583a4a15d2994e0577b05c74",
                name = "mikkuz"
        )

        private val SNAP: AbsPlayer = LitePlayer(
                id = "59213f1ad2994e1d79144956",
                name = "Snap"
        )

        private val TWO_SAINT: AbsPlayer = LitePlayer(
                id = "56d093064d7521611048599e",
                name = "2Saint"
        )

        private val NEW_JERSEY = Region(
                displayName = "New Jersey",
                id = "norcal",
                endpoint = Endpoint.NOT_GAR_PR
        )

        private val NORCAL = Region(
                displayName = "Norcal",
                id = "norcal",
                endpoint = Endpoint.GAR_PR
        )
    }

    @Before
    override fun setUp() {
        super.setUp()

        viewModel = FavoritePlayersViewModel(favoritePlayersRepository, identityRepository,
                schedulers, threadUtils)
    }

    @Test
    fun testAddFavoritePlayerCausesUpdate() {
        var state: FavoritePlayersViewModel.State? = null

        viewModel.stateLiveData.observeForever {
            state = it
        }

        favoritePlayersRepository.addPlayer(MIKKUZ, NORCAL)
        assertEquals(1, state?.list?.size)

        val player = state?.list?.get(0) as ListItem.FavoritePlayer
        assertFalse(player.isIdentity)
        assertEquals(MIKKUZ, player.player)
    }

    @Test
    fun testAddAndRemoveFavoritePlayersCausesUpdates() {
        var state: FavoritePlayersViewModel.State? = null

        viewModel.stateLiveData.observeForever {
            state = it
        }

        favoritePlayersRepository.addPlayer(IMYT, NORCAL)
        assertEquals(1, state?.list?.size)

        var player = state?.list?.get(0) as ListItem.FavoritePlayer
        assertFalse(player.isIdentity)
        assertEquals(IMYT, player.player)

        favoritePlayersRepository.addPlayer(SNAP, NORCAL)
        assertEquals(2, state?.list?.size)

        player = state?.list?.get(0) as ListItem.FavoritePlayer
        assertEquals(IMYT, player.player)

        player = state?.list?.get(1) as ListItem.FavoritePlayer
        assertEquals(SNAP, player.player)

        favoritePlayersRepository.removePlayer(IMYT)
        assertEquals(1, state?.list?.size)

        player = state?.list?.get(0) as ListItem.FavoritePlayer
        assertEquals(SNAP, player.player)
    }

    @Test
    fun testInit() {
        var state: FavoritePlayersViewModel.State? = null
        val isFetchingStates = mutableListOf<Boolean>()

        viewModel.stateLiveData.observeForever {
            state = it
            isFetchingStates.add(it.isFetching)
        }

        assertEquals(true, state?.isEmpty)
        assertEquals(false, state?.isFetching)
        assertTrue(state?.list.isNullOrEmpty())
        assertTrue(state?.searchResults.isNullOrEmpty())
        assertEquals(1, isFetchingStates.size)
        assertFalse(isFetchingStates[0])

        favoritePlayersRepository.addPlayer(SNAP, NORCAL)
        assertEquals(false, state?.isEmpty)
        assertEquals(false, state?.isFetching)
        assertEquals(1, state?.list?.size)
        assertTrue(state?.searchResults.isNullOrEmpty())
        assertEquals(3, isFetchingStates.size)
        assertFalse(isFetchingStates[0])
        assertTrue(isFetchingStates[1])
        assertFalse(isFetchingStates[2])
    }

    @Test
    fun testSearch() {
        favoritePlayersRepository.addPlayer(CHARLEZARD, NORCAL)
        favoritePlayersRepository.addPlayer(IMYT, NORCAL)
        favoritePlayersRepository.addPlayer(MIKKUZ, NORCAL)
        favoritePlayersRepository.addPlayer(SNAP, NORCAL)
        favoritePlayersRepository.addPlayer(TWO_SAINT, NEW_JERSEY)

        var state: FavoritePlayersViewModel.State? = null

        viewModel.stateLiveData.observeForever {
            state = it
        }

        viewModel.search("i")
        assertEquals(3, state?.searchResults?.size)

        var player = state?.searchResults?.get(0) as ListItem.FavoritePlayer
        assertEquals(IMYT, player.player)

        player = state?.searchResults?.get(1) as ListItem.FavoritePlayer
        assertEquals(MIKKUZ, player.player)

        player = state?.searchResults?.get(2) as ListItem.FavoritePlayer
        assertEquals(TWO_SAINT, player.player)
    }

    @Test
    fun testSearchWithBlankStringAndNoFavorites() {
        var state: FavoritePlayersViewModel.State? = null

        viewModel.stateLiveData.observeForever {
            state = it
        }

        viewModel.search(" ")
        assertNull(state?.searchResults)
    }

    @Test
    fun testSearchWithEmptyStringAndNoFavorites() {
        var state: FavoritePlayersViewModel.State? = null

        viewModel.stateLiveData.observeForever {
            state = it
        }

        viewModel.search("")
        assertNull(state?.searchResults)
    }

    @Test
    fun testSearchWithNullStringAndNoFavorites() {
        var state: FavoritePlayersViewModel.State? = null

        viewModel.stateLiveData.observeForever {
            state = it
        }

        viewModel.search(null)
        assertNull(state?.searchResults)
    }

}

{'fraction_non_alphanumeric': 0.0843784727734561, 'fraction_numerical': 0.016589934910303222, 'mean_word_length': 2.610028653295129, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '265651', 'n_tokens_mistral': 3816, 'n_tokens_neox': 3536, 'n_words': 633}