hatepoint / phrases-hyperskill-tests

0 stars 1 forks source link

findViewByString already does null checks internally, it is redundant to do assertNotNull for the return value #6

Closed RedJocker closed 1 year ago

RedJocker commented 1 year ago

this is the implementation for findViewByString

inline fun <reified T> View.findViewByString(idString: String): T {
        val id = this.resources.getIdentifier(idString, "id", context.packageName)
        val view: View? = this.findViewById(id)

        val idNotFoundMessage = "View with id \"$idString\" was not found"
        val wrongClassMessage = "View with id \"$idString\" is not from expected class. " +
                "Expected ${T::class.java.simpleName} found ${view?.javaClass?.simpleName}"

        assertNotNull(idNotFoundMessage, view)
        assertTrue(wrongClassMessage, view is T)

        return view as T
    }

it makes sure the widget that is looked for is not null and that the class is the expected class before returning the value