You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MapsTest.kt 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package com.dmdirc.ktirc.model
  2. import com.dmdirc.ktirc.io.CaseMapping
  3. import org.junit.jupiter.api.Assertions.*
  4. import org.junit.jupiter.api.Test
  5. import org.junit.jupiter.api.assertThrows
  6. internal class CaseInsensitiveMapTest {
  7. private data class Wrapper(val name: String)
  8. private var caseMapping = CaseMapping.Rfc
  9. private val map = object : CaseInsensitiveMap<Wrapper>({ caseMapping }, { it -> it.name }) {}
  10. @Test
  11. fun `CaseInsensitiveMap stores values`() {
  12. val value = Wrapper("acidBurn")
  13. map += value
  14. assertSame(value, map["acidBurn"])
  15. }
  16. @Test
  17. fun `CaseInsensitiveMap disallows the same value twice`() {
  18. val value = Wrapper("acidBurn")
  19. map += value
  20. assertThrows<IllegalArgumentException> {
  21. map += value
  22. }
  23. }
  24. @Test
  25. fun `CaseInsensitiveMap retrieves values using differently cased keys`() {
  26. val value = Wrapper("[acidBurn]")
  27. map += value
  28. assertSame(value, map["{ACIDBURN}"])
  29. }
  30. @Test
  31. fun `CaseInsensitiveMap retrieves values if the casemapping changes`() {
  32. val value = Wrapper("[acidBurn]")
  33. map += value
  34. caseMapping = CaseMapping.Ascii
  35. assertSame(value, map["[ACIDBURN]"])
  36. assertNull(map["{acidburn}"])
  37. }
  38. @Test
  39. fun `CaseInsensitiveMap retrieves null if value not found`() {
  40. val value = Wrapper("[acidBurn]")
  41. map += value
  42. assertNull(map["acidBurn"])
  43. assertNull(map["thePlague"])
  44. }
  45. @Test
  46. fun `CaseInsensitiveMap removes values`() {
  47. map += Wrapper("acidBurn")
  48. map -= "ACIDburn"
  49. assertNull(map["acidBurn"])
  50. }
  51. @Test
  52. fun `CaseInsensitiveMap can be iterated`() {
  53. map += Wrapper("acidBurn")
  54. map += Wrapper("zeroCool")
  55. val names = map.map { it.name }.toList()
  56. assertEquals(2, names.size)
  57. assertTrue(names.contains("acidBurn"))
  58. assertTrue(names.contains("zeroCool"))
  59. }
  60. @Test
  61. fun `ChannelInsensitiveMap indicates if it contains a value or not`() {
  62. map += Wrapper("acidBurn")
  63. assertTrue("acidBurn" in map)
  64. assertFalse("thePlague" in map)
  65. }
  66. @Test
  67. fun `ChannelInsensitiveMap can be cleared`() {
  68. map += Wrapper("acidBurn")
  69. map += Wrapper("zeroCool")
  70. map.clear()
  71. assertFalse("acidBurn" in map)
  72. assertFalse("zeroCool" in map)
  73. assertEquals(0, map.count())
  74. }
  75. }
  76. internal class ChannelStateMapTest {
  77. @Test
  78. fun `ChannelStateMap maps channels on name`() {
  79. val channelUserMap = ChannelStateMap { CaseMapping.Rfc }
  80. channelUserMap += ChannelState("#dumpsterDiving") { CaseMapping.Rfc }
  81. assertTrue("#dumpsterDiving" in channelUserMap)
  82. assertTrue("#dumpsterdiving" in channelUserMap)
  83. assertFalse("#thegibson" in channelUserMap)
  84. }
  85. }
  86. internal class ChannelUserMapTest {
  87. @Test
  88. fun `ChannelUserMap maps users on nickname`() {
  89. val channelUserMap = ChannelUserMap { CaseMapping.Rfc }
  90. channelUserMap += ChannelUser("acidBurn")
  91. assertTrue("acidBurn" in channelUserMap)
  92. assertTrue("acidburn" in channelUserMap)
  93. assertFalse("zerocool" in channelUserMap)
  94. }
  95. }