Skip to content

Non-null assertion cleanup PART 1 - #706

Open
growabeard wants to merge 8 commits into
scribe-org:mainfrom
growabeard:non-null-assertion-cleanup
Open

growabeard wants to merge 8 commits into
scribe-org:mainfrom
growabeard:non-null-assertion-cleanup

Conversation

@growabeard

@growabeard growabeard commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

Contributor checklist


Description

This PR is fixing non-null assertions in a few classes, with tests accompanying the changes.

This is only the first PR in a series of work.

Related issue

@andrewtavis andrewtavis added the no-changelog No changelog entry is needed for this pull request label Sep 25, 2026
@andrewtavis

Copy link
Copy Markdown
Member

Thanks for the PR, @growabeard! Would you be able to fix the Ktlint issues?

@growabeard

growabeard commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator Author

Egg on my face. I ran lintKotlin but ignored the output.. 👻

@andrewtavis

Copy link
Copy Markdown
Member

No stress, @growabeard! @prince-0408 and/or @Roniscend: Would either of you be able to do an initial review?

How many parts do you think you'll do for the PRs, @growabeard? Just wondering if you have an impression :)

@growabeard

growabeard commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator Author

@andrewtavis

How many parts do you think you'll do for the PRs, @growabeard? Just wondering if you have an impression :)

I think just one more actually. I should have the second one ready this week.

@andrewtavis

Copy link
Copy Markdown
Member

Sounds great, @growabeard!

Comment thread app/src/keyboards/java/be/scri/helpers/RecentEmojiHelper.kt Outdated
@growabeard

Copy link
Copy Markdown
Collaborator Author

@prince-0408 can you recheck now?

@growabeard
growabeard force-pushed the non-null-assertion-cleanup branch from 63bc9cb to 1ba26c4 Compare October 2, 2026 19:37
@andrewtavis
andrewtavis self-requested a review October 2, 2026 22:02
if (emojiEditorList != null) {
emojiEditorList!!.add(emoji)
if (emojiEditorList.isNotEmpty()) {
emojiEditorList.add(emoji)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Since emojiEditorList is now a non-null MutableList<String>, both branches perform the exact same operation (appending emoji). You can simplify this to:

Suggested change
emojiEditorList.add(emoji)
emojiEditorList.add(emoji)

}
}
emojiEditorList = null
emojiEditorList = emptyList<String>().toMutableList()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

emojiEditorList.clear() (or emojiEditorList = mutableListOf()) is more idiomatic than emptyList<String>().toMutableList() and avoids allocating intermediate collections.

Also, inside commitEmojiEditorList(), the emojiEditorList.let { ... } block is redundant now that emojiEditorList is non-null and can be simplified.


private const val PREFS_NAME = "recent_emojis"
private const val KEY_RECENT = "recent_emoji_list"
const val PREFS_NAME = "recent_emojis"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Consider making PREFS_NAME and KEY_RECENT internal const val instead of public const val so they don't leak into the public API outside the keyboard module, while remaining accessible to RecentEmojiHelperTest.


recordRecentEmoji(context, "emoji1")

verify(exactly = 1) { mockEditor.putString(any(), any()) }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We can verify the exact key and emoji value stored instead of using any() to ensure the emoji is properly persisted when preferences are initially null:

Suggested change
verify(exactly = 1) { mockEditor.putString(any(), any()) }
verify { mockEditor.putString(KEY_RECENT, "emoji1") }

import org.junit.jupiter.api.Test

class AutocompletionHandlerTest {
private lateinit var looper: Looper

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

looper is declared and mocked in setUp(), but never used anywhere in this test class. It can be safely removed.


val result = AutocompletionHandler.buildCompletions(typedWord, completions)

assert(result.isEmpty())

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

For consistency with the other test classes in this PR and to avoid depending on the -ea JVM runtime flag for Kotlin's assert(...), please use JUnit 5 assertions here (e.g. assertTrue(result.isEmpty()) or assertEquals(emptyList<String>(), result)).

Comment thread app/build.gradle.kts
// Unit Testing
// ==========================
testImplementation("org.junit.jupiter:junit-jupiter-api:$junit5Version")
testImplementation("org.junit.jupiter:junit-jupiter-params:${junit5Version}")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: Can omit the curly braces ($junit5Version) to match the surrounding dependency lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog No changelog entry is needed for this pull request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants