<commit_before>package com.github.ajalt.flexadapter

import android.view.View
import android.widget.Space
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.verify
import com.nhaarman.mockito_kotlin.verifyNoMoreInteractions
import org.assertj.core.api.Assertions.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config

private class C : FlexAdapterExtensionItem(0) {
    override fun bindItemView(itemView: View, position: Int) = Unit
}

abstract class I
open class O : I()
class O2 : O()

@RunWith(RobolectricTestRunner::class)
@Config(manifest = Config.NONE)
class FlexAdapterTest {
    @Rule @JvmField
    val exception: ExpectedException = ExpectedException.none()

    @Test
    fun `registered items can be added`() {
        val adapter = FlexAdapter<Any>()
        adapter.register<Int>(123) { value, view, i -> }
        adapter.register<String>(456) { value, view, i -> }

        adapter.items.addAll(arrayOf("asd", 1))
        adapter.getItemViewType(0)
        adapter.getItemViewType(1)
    }

    @Test
    fun `registered subclasses can be added`() {
        val adapter = FlexAdapter<I>()
        adapter.register<I>(123) { value, view, i -> }

        adapter.items.addAll(listOf(O2(), O()))
        adapter.getItemViewType(0)
        adapter.getItemViewType(1)
    }

    @Test
    fun `unregistered items cannot be added`() {
        val adapter = FlexAdapter<Any>()
        exception.expect(IllegalArgumentException::class.java)
        adapter.items.add(1)
    }

    @Test
    fun `registering FlexAdapterItems raises an exception`() {
        val adapter = FlexAdapter<C>()
        exception.expect(IllegalArgumentException::class.java)
        adapter.register<C>(0) { it, v, i -> }
        adapter.items.addAll(arrayOf(C(), C()))
    }

    @Test
    fun `multiple base class registration updated subclasses`() {
        val adapter = FlexAdapter<I>()
        val cb1: Runnable = mock()
        val cb2: Runnable = mock()
        adapter.register<I>(0) { it, v, i -> cb1.run() }

        adapter.items.addAll(listOf(O2(), O()))
        bindViewAt(adapter, 0)
        verify(cb1).run()

        adapter.register<I>(0) { it, v, i -> cb2.run() }

        bindViewAt(adapter, 0)
        verifyNoMoreInteractions(cb1)
        verify(cb2).run()
    }

    private fun bindViewAt(adapter: FlexAdapter<*>, i: Int) {
        val viewHolder = FlexAdapterExtensionItem.ViewHolder(Space(RuntimeEnvironment.application))
        adapter.bindViewHolder(viewHolder, i)
    }

    @Test
    fun `custom viewTypes are returned`() {
        val adapter = FlexAdapter<String>()
        adapter.register<String>(456, viewType = 123) { value, view, i -> }
        adapter.items.add("x")
        assertThat(adapter.getItemViewType(0)).isEqualTo(123)
    }

    @Test
    fun `multiple registration changes viewTypes`() {
        val adapter = FlexAdapter<String>()
        adapter.register<String>(456, viewType = 123) { value, view, i -> }
        adapter.items.add("x")
        adapter.register<String>(456, viewType = 789) { value, view, i -> }
        assertThat(adapter.getItemViewType(0)).isEqualTo(789)
    }

    @Test
    fun `multiple base class registration changes viewTypes`() {
        val adapter = FlexAdapter<I>()
        adapter.register<I>(456, viewType = 123) { value, view, i -> }
        adapter.items.add(O())
        adapter.register<I>(456, viewType = 789) { value, view, i -> }
        assertThat(adapter.getItemViewType(0)).isEqualTo(789)
    }
}


<commit_msg>Add more tests<commit_after>package com.github.ajalt.flexadapter

import android.R
import android.view.View
import android.widget.FrameLayout
import com.nhaarman.mockito_kotlin.mock
import com.nhaarman.mockito_kotlin.verify
import com.nhaarman.mockito_kotlin.verifyNoMoreInteractions
import org.assertj.core.api.Assertions.assertThat
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment
import org.robolectric.annotation.Config

private class C : FlexAdapterExtensionItem(0) {
    override fun bindItemView(itemView: View, position: Int) = Unit
}

abstract class I
open class O : I()
class O2 : O()

@RunWith(RobolectricTestRunner::class)
@Config(manifest = Config.NONE)
class FlexAdapterTest {
    private val layout = R.layout.test_list_item

    @Rule @JvmField
    val exception: ExpectedException = ExpectedException.none()

    private fun bindViewAt(adapter: FlexAdapter<*>, i: Int) {
        val viewHolder = adapter.onCreateViewHolder(
                FrameLayout(RuntimeEnvironment.application),
                adapter.getItemViewType(i))
        assertThat(viewHolder).isNotNull()
        adapter.bindViewHolder(viewHolder, i)
    }

    @Test
    fun `registered items can be added`() {
        val adapter = FlexAdapter<Any>()
        adapter.register<Int>(layout) { value, view, i -> }
        adapter.register<String>(layout) { value, view, i -> }

        adapter.items.addAll(arrayOf("asd", 1))
        bindViewAt(adapter, 0)
        bindViewAt(adapter, 1)
    }

    @Test
    fun `multiply registered classes can be added`() {
        val adapter = FlexAdapter<O>()
        adapter.register<O>(layout) { value, view, i -> }
        adapter.register<O>(layout) { value, view, i -> }

        adapter.items.addAll(listOf(O2(), O()))
        bindViewAt(adapter, 0)
        bindViewAt(adapter, 1)
    }

    @Test
    fun `registered subclasses can be added`() {
        val adapter = FlexAdapter<I>()
        adapter.register<I>(layout) { value, view, i -> }

        adapter.items.addAll(listOf(O2(), O()))
        bindViewAt(adapter, 0)
        bindViewAt(adapter, 1)
    }

    @Test
    fun `unregistered items cannot be added`() {
        val adapter = FlexAdapter<Any>()
        exception.expect(IllegalArgumentException::class.java)
        adapter.items.add(1)
    }

    @Test
    fun `registering FlexAdapterItems raises an exception`() {
        val adapter = FlexAdapter<C>()
        exception.expect(IllegalArgumentException::class.java)
        adapter.register<C>(layout) { it, v, i -> }
        adapter.items.addAll(arrayOf(C(), C()))
    }

    @Test
    fun `multiple base class registration updates subclasses`() {
        val adapter = FlexAdapter<I>()
        val cb1: Runnable = mock()
        val cb2: Runnable = mock()
        adapter.register<I>(layout) { it, v, i -> cb1.run() }

        adapter.items.addAll(listOf(O2(), O()))
        bindViewAt(adapter, 0)
        verify(cb1).run()

        adapter.register<I>(layout) { it, v, i -> cb2.run() }

        bindViewAt(adapter, 0)
        verifyNoMoreInteractions(cb1)
        verify(cb2).run()
    }

    @Test
    fun `custom viewTypes are returned`() {
        val adapter = FlexAdapter<String>()
        adapter.register<String>(layout, viewType = 123) { value, view, i -> }
        adapter.items.add("x")
        assertThat(adapter.getItemViewType(0)).isEqualTo(123)
    }

    @Test
    fun `multiple registration changes viewTypes`() {
        val adapter = FlexAdapter<String>()
        adapter.register<String>(layout, viewType = 123) { value, view, i -> }
        adapter.items.add("x")
        adapter.register<String>(layout, viewType = 789) { value, view, i -> }
        assertThat(adapter.getItemViewType(0)).isEqualTo(789)
    }

    @Test
    fun `multiple base class registration changes viewTypes`() {
        val adapter = FlexAdapter<I>()
        adapter.register<I>(layout, viewType = 123) { value, view, i -> }
        adapter.items.add(O())
        adapter.register<I>(layout, viewType = 789) { value, view, i -> }
        assertThat(adapter.getItemViewType(0)).isEqualTo(789)
    }
}



{'fraction_non_alphanumeric': 0.12572145545796737, 'fraction_numerical': 0.015307402760351318, 'mean_word_length': 2.995488721804511, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 70, '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': '6818610', 'n_tokens_mistral': 2527, 'n_tokens_neox': 2390, 'n_words': 528}