<commit_before>package com.github.michaelbull.result

import com.natpryce.hamkrest.Matcher
import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import com.natpryce.hamkrest.sameInstance
import org.junit.jupiter.api.Test

internal class MapTest {
    private sealed class MapError(val reason: String) {
        object HelloError : MapError("hello")
        object WorldError : MapError("world")
        class CustomError(reason: String) : MapError(reason)
    }

    private fun sameError(error: MapError): Matcher<MapError> {
        return sameInstance(error)
    }

    @Test
    internal fun `map should return the transformed result value if ok`() {
        val test: Result<Int, Nothing> = Ok(500).map { 50 }

        val value = Ok(10).map { it + 20 }.get()
        assertThat(value, equalTo(30))
    }

    @Test
    internal fun `map should return the result error if not ok`() {
        val result = Err(MapError.HelloError).map { "hello $it" }

        result as Err

        assertThat(result.error, sameError(MapError.HelloError))
    }

    @Test
    internal fun `mapError should return the result value if ok`() {
        val value = Ok(55).map { it + 15 }.mapError { MapError.WorldError }.get()
        assertThat(value, equalTo(70))
    }

    @Test
    internal fun `mapError should return the transformed result error if not ok`() {
        val result: Result<String, MapError> = Ok("let")
            .map { "$it me" }
            .andThen {
                when (it) {
                    "let me" -> Err(MapError.CustomError("$it $it"))
                    else -> Ok("$it get")
                }
            }
            .mapError { MapError.CustomError("${it.reason} get what i want") }

        result as Err

        assertThat(result.error.reason, equalTo("let me let me get what i want"))
    }

    @Test
    internal fun `mapBoth should return the transformed result value if ok`() {
        val value = Ok("there is").mapBoth(
            success = { "$it a light" },
            failure = { "$it that never" }
        )

        assertThat(value, equalTo("there is a light"))
    }

    @Test
    internal fun `mapBoth should return the transformed result error if not ok`() {
        val error = Err(MapError.CustomError("this")).mapBoth(
            success = { "$it charming" },
            failure = { "${it.reason} man" }
        )

        assertThat(error, equalTo("this man"))
    }

    @Test
    internal fun `mapEither should return the transformed result value if ok`() {
        val result = Ok(500).mapEither(
            success = { it + 500 },
            failure = { MapError.CustomError("$it") }
        )

        result as Ok

        assertThat(result.value, equalTo(1000))
    }

    @Test
    internal fun `mapEither should return the transformed result error if not ok`() {
        val result = Err("the reckless").mapEither(
            success = { "the wild youth" },
            failure = { MapError.CustomError("the truth") }
        )

        result as Err

        assertThat(result.error.reason, equalTo("the truth"))
    }
}<commit_msg>Suppress unreachable code warnings in MapTest<commit_after>package com.github.michaelbull.result

import com.natpryce.hamkrest.Matcher
import com.natpryce.hamkrest.assertion.assertThat
import com.natpryce.hamkrest.equalTo
import com.natpryce.hamkrest.sameInstance
import org.junit.jupiter.api.Test

internal class MapTest {
    private sealed class MapError(val reason: String) {
        object HelloError : MapError("hello")
        object WorldError : MapError("world")
        class CustomError(reason: String) : MapError(reason)
    }

    private fun sameError(error: MapError): Matcher<MapError> {
        return sameInstance(error)
    }

    @Test
    internal fun `map should return the transformed result value if ok`() {
        val value = Ok(10).map { it + 20 }.get()
        assertThat(value, equalTo(30))
    }

    @Test
    @Suppress("UNREACHABLE_CODE")
    internal fun `map should return the result error if not ok`() {
        val result = Err(MapError.HelloError).map { "hello $it" }

        result as Err

        assertThat(result.error, sameError(MapError.HelloError))
    }

    @Test
    internal fun `mapError should return the result value if ok`() {
        val value = Ok(55).map { it + 15 }.mapError { MapError.WorldError }.get()
        assertThat(value, equalTo(70))
    }

    @Test
    internal fun `mapError should return the transformed result error if not ok`() {
        val result: Result<String, MapError> = Ok("let")
            .map { "$it me" }
            .andThen {
                when (it) {
                    "let me" -> Err(MapError.CustomError("$it $it"))
                    else -> Ok("$it get")
                }
            }
            .mapError { MapError.CustomError("${it.reason} get what i want") }

        result as Err

        assertThat(result.error.reason, equalTo("let me let me get what i want"))
    }

    @Test
    @Suppress("UNREACHABLE_CODE")
    internal fun `mapBoth should return the transformed result value if ok`() {
        val value = Ok("there is").mapBoth(
            success = { "$it a light" },
            failure = { "$it that never" }
        )

        assertThat(value, equalTo("there is a light"))
    }

    @Test
    @Suppress("UNREACHABLE_CODE")
    internal fun `mapBoth should return the transformed result error if not ok`() {
        val error = Err(MapError.CustomError("this")).mapBoth(
            success = { "$it charming" },
            failure = { "${it.reason} man" }
        )

        assertThat(error, equalTo("this man"))
    }

    @Test
    @Suppress("UNREACHABLE_CODE")
    internal fun `mapEither should return the transformed result value if ok`() {
        val result = Ok(500).mapEither(
            success = { it + 500 },
            failure = { MapError.CustomError("$it") }
        )

        result as Ok

        assertThat(result.value, equalTo(1000))
    }

    @Test
    internal fun `mapEither should return the transformed result error if not ok`() {
        val result = Err("the reckless").mapEither(
            success = { "the wild youth" },
            failure = { MapError.CustomError("the truth") }
        )

        result as Err

        assertThat(result.error.reason, equalTo("the truth"))
    }
}
{'fraction_non_alphanumeric': 0.1128703558551497, 'fraction_numerical': 0.007681454773475467, 'mean_word_length': 2.2435180477885104, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 12, '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': '4040657', 'n_tokens_mistral': 1886, 'n_tokens_neox': 1802, 'n_words': 533}