<commit_before>/*
 * Copyright (c) 2021 toastkidjp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
 */

package jp.toastkid.yobidashi.browser.bookmark

import android.content.Context
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.impl.annotations.MockK
import io.mockk.mockkConstructor
import io.mockk.unmockkAll
import jp.toastkid.yobidashi.browser.bookmark.model.BookmarkRepository
import jp.toastkid.yobidashi.libs.db.AppDatabase
import jp.toastkid.yobidashi.libs.db.DatabaseFinder
import kotlinx.coroutines.test.TestCoroutineDispatcher
import org.junit.After
import org.junit.Before
import org.junit.Test

/**
 * @author toastkidjp
 */
class BookmarkInsertionTest {

    private lateinit var bookmarkInsertion: BookmarkInsertion

    @MockK
    private lateinit var context: Context

    @MockK
    private lateinit var appDatabase: AppDatabase

    @MockK
    private lateinit var bookmarkRepository: BookmarkRepository

    @Before
    fun setUp() {
        MockKAnnotations.init(this)

        mockkConstructor(DatabaseFinder::class)
        coEvery { anyConstructed<DatabaseFinder>().invoke(any()) }.returns(appDatabase)
        coEvery { appDatabase.bookmarkRepository() }.returns(bookmarkRepository)
        coEvery { bookmarkRepository.add(any()) }.returns(Unit)
    }

    @After
    fun tearDown() {
        unmockkAll()
    }

    @Test
    fun testEmptyTitleCase() {
        bookmarkInsertion = BookmarkInsertion(context, "", "https://www.yahoo.co.jp")

        bookmarkInsertion.insert()

        coVerify(exactly = 0) { anyConstructed<DatabaseFinder>().invoke(any()) }
        coVerify(exactly = 0) { appDatabase.bookmarkRepository() }
        coVerify(exactly = 0) { bookmarkRepository.add(any()) }
    }

    @Test
    fun test() {
        bookmarkInsertion = BookmarkInsertion(
            context,
            "Yahoo! JAPAN",
            "https://www.yahoo.co.jp",
            dispatcher = TestCoroutineDispatcher()
        )

        bookmarkInsertion.insert()

        coVerify(exactly = 1) { anyConstructed<DatabaseFinder>().invoke(any()) }
        coVerify(exactly = 1) { appDatabase.bookmarkRepository() }
        coVerify(exactly = 1) { bookmarkRepository.add(any()) }
    }

}<commit_msg>refactor #485 Rename test function.<commit_after>/*
 * Copyright (c) 2021 toastkidjp.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompany this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html.
 */

package jp.toastkid.yobidashi.browser.bookmark

import android.content.Context
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.impl.annotations.MockK
import io.mockk.mockkConstructor
import io.mockk.unmockkAll
import jp.toastkid.yobidashi.browser.bookmark.model.BookmarkRepository
import jp.toastkid.yobidashi.libs.db.AppDatabase
import jp.toastkid.yobidashi.libs.db.DatabaseFinder
import kotlinx.coroutines.test.TestCoroutineDispatcher
import org.junit.After
import org.junit.Before
import org.junit.Test

/**
 * @author toastkidjp
 */
class BookmarkInsertionTest {

    private lateinit var bookmarkInsertion: BookmarkInsertion

    @MockK
    private lateinit var context: Context

    @MockK
    private lateinit var appDatabase: AppDatabase

    @MockK
    private lateinit var bookmarkRepository: BookmarkRepository

    @Before
    fun setUp() {
        MockKAnnotations.init(this)

        mockkConstructor(DatabaseFinder::class)
        coEvery { anyConstructed<DatabaseFinder>().invoke(any()) }.returns(appDatabase)
        coEvery { appDatabase.bookmarkRepository() }.returns(bookmarkRepository)
        coEvery { bookmarkRepository.add(any()) }.returns(Unit)
    }

    @After
    fun tearDown() {
        unmockkAll()
    }

    @Test
    fun testEmptyTitleCase() {
        bookmarkInsertion = BookmarkInsertion(context, "", "https://www.yahoo.co.jp")

        bookmarkInsertion.insert()

        coVerify(exactly = 0) { anyConstructed<DatabaseFinder>().invoke(any()) }
        coVerify(exactly = 0) { appDatabase.bookmarkRepository() }
        coVerify(exactly = 0) { bookmarkRepository.add(any()) }
    }

    @Test
    fun testCorrectCase() {
        bookmarkInsertion = BookmarkInsertion(
            context,
            "Yahoo! JAPAN",
            "https://www.yahoo.co.jp",
            dispatcher = TestCoroutineDispatcher()
        )

        bookmarkInsertion.insert()

        coVerify(exactly = 1) { anyConstructed<DatabaseFinder>().invoke(any()) }
        coVerify(exactly = 1) { appDatabase.bookmarkRepository() }
        coVerify(exactly = 1) { bookmarkRepository.add(any()) }
    }

}
{'fraction_non_alphanumeric': 0.10221601117987622, 'fraction_numerical': 0.0061888600519065685, 'mean_word_length': 3.4024604569420034, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 6, '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': '6964328', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1504, 'n_words': 304}