2

Trying to ensure consistency concisely across my application. How do I check the bundle has the same keys as a set? - Or how do I construct a keySet succinctly for comparison?

import android.os.Bundle


fun main() {
    val expect = setOf("alpha", "beta")
    val bundle = Bundle()
    bundle.putCharSequence("alpha", "gamma")
    bundle.putCharSequence("beta", "zeta")
    assert(bundle.keySet() == expect)
    // Also tried `bundle.keySet().equals(expect)`
}

Output of println("Expected $expect got $keys"):

Expected [alpha, beta] got android.util.MapCollections$KeySet@48d26086
A T
  • 13,008
  • 21
  • 97
  • 158
  • Are you doing this as a unit test? – Tenfour04 Jan 30 '21 at 18:42
  • No, just in my code. Haven't built tests yet. Yeah I know asserts need to be enabled with flag – A T Jan 31 '21 at 01:46
  • I tried your code by just sticking it in an Activity's `onCreate` and changing it to `require` and it passes fine, as expected. Haven't tried it with a unit test. – Tenfour04 Jan 31 '21 at 02:05

0 Answers0