Kotlinlearncs.online LogoJava
Return to List

Test Writing: Count Letters with Map

Created By: Geoffrey Challen
/ Version: 2020.10.0

Create a method countLetters. countLetters accepts an array of Strings and returns a Map from Strings to Ints.

The map should contain counts of the passed Strings based on their first letter. For example, provided the array {"test", "me", "testing"} your Map should be {"t": 2, "m": 1}. You should ignore empty Strings and not include any zero counts.

As a reminder, you can retrieve the first character of a String as a char using index notation: example[0]. You may find substring more helpful. You may also want to utilize the Elvis operator ?: for retrieving a default value from a map when the requested key does not exist.

Test Design Challenge

You're challenge is to write tests for this problem described above.

  • Provide a public class named TestCountLetters with a single non-private class method named test that accepts no arguments and does not return a value.
  • If the implementation of the class described above is incorrect, your test method should throw an exception.
  • If it is correct, do not throw an exception.
  • You may want to use Kotlin's assert or check methods