Methods as sources

Report a typo

There is a utility method that evaluates password strength. A password is considered strong if its length is at least 8 characters and it contains at least one uppercase character, one lowercase character, one digit, and one special character. A medium-strength password is also at least 8 characters and contains alphabetic characters and at least one digit. Any other password is considered to be weak:

enum class PasswordStrength {
    STRONG, MEDIUM, WEAK
}

class PasswordUtils {
    fun getStrength(password: String): PasswordStrength {
        val length = ".{8,}".toRegex() // at least 8 chars long
        val lowercase = "(?=.*[a-z])".toRegex() // at least one lowercase char
        val uppercase = "(?=.*[A-Z])".toRegex() // at least one uppercase char
        val digit = "(?=.*\\d)".toRegex() // at least one digit
        val special = "(?=.*[ !@#$%^&*])".toRegex() // at least one of these special chars
        return if (password.matches(lowercase + uppercase + digit + special + length)) {
            PasswordStrength.STRONG
        } else if (password.matches(lowercase + digit + length)) {
            PasswordStrength.MEDIUM
        } else if (password.matches(uppercase + digit + length)) {
            PasswordStrength.MEDIUM
        } else {
            PasswordStrength.WEAK
        }
    }
}

Some team member wrote a few unit tests for it using @MethodSource to generate arguments for the method in test:

internal class SnippetTests {
    
    val passwordUtils = PasswordUtils()
    
    @ParameterizedTest
    @MethodSource("provideStrongPasswords")
    fun testStrongPasswords(password: String) {
        assertEquals(PasswordStrength.STRONG, passwordUtils.getStrength(password))
    }

    @ParameterizedTest
    @MethodSource("provideWeakPasswords")
    fun testMediumPasswords(password: String) {
        assertEquals(PasswordStrength.MEDIUM, passwordUtils.getStrength(password))
    }

    @ParameterizedTest
    @MethodSource("provideMediumPasswords")
    fun testWeakPasswords(password: String) {
        assertEquals(PasswordStrength.WEAK, passwordUtils.getStrength(password))
    }
    fun provideStrongPasswords(): List<String> {
        return listOf("aN we2aM", "*****Jj0", "Ux134!&")
    }

    fun provideMediumPasswords(): List<String> {
        return listOf("QWERTY2", "Admin01", "2418^ax00")
    }

    fun provideWeakPasswords(): List<String> {
        return listOf("password", "f8HdA*", "Y20220101")
    }
}

Look carefully at these test methods and argument sources and select all the correct statements about them.

Select one or more options from the list
___

Create a free account to access the full topic