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.

MessageBuildersTest.kt 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package com.dmdirc.ktirc.messages
  2. import com.dmdirc.ktirc.IrcClient
  3. import com.dmdirc.ktirc.model.MessageTag
  4. import com.nhaarman.mockitokotlin2.mock
  5. import com.nhaarman.mockitokotlin2.verify
  6. import org.junit.jupiter.api.Assertions.assertEquals
  7. import org.junit.jupiter.api.Test
  8. internal class MessageBuildersTest {
  9. private val mockClient = mock<IrcClient>()
  10. @Test
  11. fun `sendCapabilityRequest sends CAP REQ message with single argument`() {
  12. mockClient.sendCapabilityRequest(listOf("a"))
  13. verify(mockClient).send("CAP REQ :a")
  14. }
  15. @Test
  16. fun `sendCapabilityRequest sends CAP REQ message with multiple args`() {
  17. mockClient.sendCapabilityRequest(listOf("a b c"))
  18. verify(mockClient).send("CAP REQ :a b c")
  19. }
  20. @Test
  21. fun `sendJoin sends correct JOIN message`() {
  22. mockClient.sendJoin("#TheGibson")
  23. verify(mockClient).send("JOIN :#TheGibson")
  24. }
  25. @Test
  26. fun `sendModeRequest sends correct MODE message`() {
  27. mockClient.sendModeRequest("#TheGibson")
  28. verify(mockClient).send("MODE :#TheGibson")
  29. }
  30. @Test
  31. fun `sendNickChange sends correct NICK message`() {
  32. mockClient.sendNickChange("AcidBurn")
  33. verify(mockClient).send("NICK :AcidBurn")
  34. }
  35. @Test
  36. fun `sendPassword sends correct PASS message`() {
  37. mockClient.sendPassword("hacktheplanet")
  38. verify(mockClient).send("PASS :hacktheplanet")
  39. }
  40. @Test
  41. fun `sendPong sends correct PONG message`() {
  42. mockClient.sendPong("abcdef".toByteArray())
  43. verify(mockClient).send("PONG :abcdef")
  44. }
  45. @Test
  46. fun `sendMessage sends correct PRIVMSG message`() {
  47. mockClient.sendMessage("acidBurn", "Hack the planet!")
  48. verify(mockClient).send("PRIVMSG acidBurn :Hack the planet!")
  49. }
  50. @Test
  51. fun `sendMessage sends correct PRIVMSG message with reply to tag`() {
  52. mockClient.sendMessage("acidBurn", "Hack the planet!", "abc123")
  53. verify(mockClient).send("@+draft/reply=abc123 PRIVMSG acidBurn :Hack the planet!")
  54. }
  55. @Test
  56. fun `sendCtcp sends correct CTCP message with no arguments`() {
  57. mockClient.sendCtcp("acidBurn", "ping")
  58. verify(mockClient).send("PRIVMSG acidBurn :\u0001PING\u0001")
  59. }
  60. @Test
  61. fun `sendCtcp sends correct CTCP message with arguments`() {
  62. mockClient.sendCtcp("acidBurn", "ping", "12345")
  63. verify(mockClient).send("PRIVMSG acidBurn :\u0001PING 12345\u0001")
  64. }
  65. @Test
  66. fun `sendAction sends correct action`() {
  67. mockClient.sendAction("acidBurn", "hacks the planet")
  68. verify(mockClient).send("PRIVMSG acidBurn :\u0001ACTION hacks the planet\u0001")
  69. }
  70. @Test
  71. fun `sendUser sends correct USER message`() {
  72. mockClient.sendUser("AcidBurn","Kate")
  73. verify(mockClient).send("USER AcidBurn 0 * :Kate")
  74. }
  75. @Test
  76. fun `sendUser sends correct AUTHENTICATE message`() {
  77. mockClient.sendAuthenticationMessage("SCRAM-MD5")
  78. verify(mockClient).send("AUTHENTICATE SCRAM-MD5")
  79. }
  80. @Test
  81. fun `sendUser sends correct blank AUTHENTICATE message`() {
  82. mockClient.sendAuthenticationMessage()
  83. verify(mockClient).send("AUTHENTICATE +")
  84. }
  85. @Test
  86. fun `sendWithTag sends message without tags`() {
  87. mockClient.sendWithTags(emptyMap(), "PING")
  88. verify(mockClient).send("PING")
  89. }
  90. @Test
  91. fun `sendWithTag sends message with single tag`() {
  92. mockClient.sendWithTags(mapOf(MessageTag.MessageId to "abc"), "PING")
  93. verify(mockClient).send("@draft/msgid=abc PING")
  94. }
  95. @Test
  96. fun `sendWithTag sends message with multiple tag`() {
  97. mockClient.sendWithTags(mapOf(MessageTag.MessageId to "abc", MessageTag.AccountName to "foo"), "PING")
  98. verify(mockClient).send("@draft/msgid=abc;account=foo PING")
  99. }
  100. @Test
  101. fun `sendWithTag ignores tags with null values`() {
  102. mockClient.sendWithTags(mapOf(MessageTag.MessageId to null, MessageTag.AccountName to "foo"), "PING")
  103. verify(mockClient).send("@account=foo PING")
  104. }
  105. @Test
  106. fun `sendTagMessage sends tags`() {
  107. mockClient.sendTagMessage("#thegibson", mapOf(MessageTag.MessageId to "id", MessageTag.AccountName to "foo"))
  108. verify(mockClient).send("@draft/msgid=id;account=foo TAGMSG #thegibson")
  109. }
  110. @Test
  111. fun `sendTagMessage sends tags with reply ID`() {
  112. mockClient.sendTagMessage("#thegibson", mapOf(MessageTag.MessageId to "id", MessageTag.AccountName to "foo"), "otherid")
  113. verify(mockClient).send("@draft/msgid=id;account=foo;+draft/reply=otherid TAGMSG #thegibson")
  114. }
  115. @Test
  116. fun `escapes tag values`() {
  117. assertEquals("\\\\hack\\sthe\\r\\nplanet\\:", "\\hack the\r\nplanet;".escapeTagValue())
  118. }
  119. }