Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix complex condition detekt errors #201

Merged
merged 2 commits into from
Oct 24, 2024

Conversation

albendz
Copy link
Contributor

@albendz albendz commented Oct 15, 2024

Contributor checklist


Description

Resolve complex condition errors for linting.

I tested this by running automated tests outside the project in my own scratch environment that made sure the before/after results of the boolean conditions were the same with and without my changes. I tried adding unit tests and started going really deep with mocking and hit some issues that didn't quite make sense when mocking in the MyKeyboardView constructor.

I'm not able to test the app on my device unfortunately. I've started a thread on Matrix for help.

Testing code

    fun compareConditionFour(
        label: CharSequence?,
        mKeyBoardShiftState: Int?
    ): Boolean {
            var newLabel: CharSequence? = label
            if (newLabel != null && newLabel.isNotEmpty() && mKeyBoardShiftState!! > 3 && newLabel.length < 3 && Character.isLowerCase(newLabel[0])) {
                newLabel = newLabel.toString().toUpperCase()
            }

        val before = newLabel

        val after = label?.takeIf { it.length in 1..2 }?.let {
            if (mKeyBoardShiftState?.let { state -> state > 3 } == true) {
                label.toString().uppercase()
            } else {
                label
            }
        } ?: label

        return before == after
    }

   @Test
    fun compareConditionFour() {
        assertTrue(ComplexConditions.compareConditionFour(null, 1))
        assertTrue(ComplexConditions.compareConditionFour(null, 3))
        assertTrue(ComplexConditions.compareConditionFour(null, 4))

        assertTrue(ComplexConditions.compareConditionFour("ab", 1))
        assertTrue(ComplexConditions.compareConditionFour("ab", 3))
        assertTrue(ComplexConditions.compareConditionFour("ab", 4))

        assertTrue(ComplexConditions.compareConditionFour("", 1))
        assertTrue(ComplexConditions.compareConditionFour("", 3))
        assertTrue(ComplexConditions.compareConditionFour("", 4))

        assertTrue(ComplexConditions.compareConditionFour("null", 1))
        assertTrue(ComplexConditions.compareConditionFour("null", 3))
        assertTrue(ComplexConditions.compareConditionFour("null", 4))

        assertTrue(ComplexConditions.compareConditionFour("nul", 1))
        assertTrue(ComplexConditions.compareConditionFour("nul", 3))
        assertTrue(ComplexConditions.compareConditionFour("nul", 4))
    }

    fun compareConditionFive(
        mBuffer: Pair<Int, Int>?,
        mKeyboardChanged: Boolean,
        width: Int,
        length: Int
    ): Boolean {
        val before = if (mBuffer == null || mKeyboardChanged) {
            mBuffer == null || mKeyboardChanged && (mBuffer!!.first != width || mBuffer!!.second != length)
        } else false

        val after = if (mBuffer == null || mKeyboardChanged) mBuffer?.let { buffer -> buffer.first != width || buffer.second != length } != false else false

        return before == after
    }

    @Test
    fun testComplexConditionFive() {
        assertTrue(ComplexConditions.compareConditionFive(null, false, 0, 0))
        assertTrue(ComplexConditions.compareConditionFive((0 to 0), false, 0, 0))
        assertTrue(ComplexConditions.compareConditionFive((0 to 0), true, 0, 0))
        assertTrue(ComplexConditions.compareConditionFive(null, true, 0, 0))
        assertTrue(ComplexConditions.compareConditionFive((1 to 0), false, 0, 0))
        assertTrue(ComplexConditions.compareConditionFive((1 to 0), true, 0, 0))
        assertTrue(ComplexConditions.compareConditionFive((0 to 1), false, 0, 0))
        assertTrue(ComplexConditions.compareConditionFive((0 to 1), true, 0, 0))
    }

    fun compareConditionSix(
        code: Int
    ): Boolean {
        val before = code == 1 || code == 2 || code == 3 || code == 4 || code == 5
        val after = code in listOf(1, 2, 3, 4, 5)

        return before == after
    }

    @Test
    fun testComplexConditionSix() {
        assertTrue(ComplexConditions.compareConditionSix(0))
        assertTrue(ComplexConditions.compareConditionSix(1))
        assertTrue(ComplexConditions.compareConditionSix(2))
        assertTrue(ComplexConditions.compareConditionSix(3))
        assertTrue(ComplexConditions.compareConditionSix(4))
        assertTrue(ComplexConditions.compareConditionSix(5))
        assertTrue(ComplexConditions.compareConditionSix(6))
    }

Related issue

Copy link

github-actions bot commented Oct 15, 2024

Thank you for the pull request!

The Scribe team will do our best to address your contribution as soon as we can. The following is a checklist for maintainers to make sure this process goes as well as possible. Feel free to address the points below yourself in further commits if you realize that actions are needed :)

If you're not already a member of our public Matrix community, please consider joining! We'd suggest using Element as your Matrix client, and definitely join the General and Android rooms once you're in. Also consider joining our bi-weekly Saturday dev syncs. It'd be great to have you!

Maintainer checklist

  • The linting and formatting workflows within the PR checks do not indicate new errors in the files changed

  • The CHANGELOG has been updated with a description of the changes for the upcoming release and the corresponding issue (if necessary)

@andrewtavis andrewtavis requested review from angrezichatterbox and andrewtavis and removed request for angrezichatterbox October 15, 2024 16:48
@andrewtavis andrewtavis added the hacktoberfest-accepted Accepted as a part of Hacktoberfest label Oct 15, 2024
Copy link
Member

@angrezichatterbox angrezichatterbox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great PR @albendz. The changes look good to me. Thanks :)

@andrewtavis
Copy link
Member

Could we get the merge conflicts resolved and then I'll bring this in? Thanks so much to you both!

@albendz
Copy link
Contributor Author

albendz commented Oct 23, 2024

I'll take a look at the merge conflicts

Copy link
Member

@andrewtavis andrewtavis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciate the help with the merge conflicts, @albendz! Thanks for all your support to the project 😊

@andrewtavis andrewtavis merged commit 5541547 into scribe-org:main Oct 24, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest-accepted Accepted as a part of Hacktoberfest
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants