types_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. package tgbotapi
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestUserStringWith(t *testing.T) {
  7. user := User{
  8. ID: 0,
  9. FirstName: "Test",
  10. LastName: "Test",
  11. UserName: "",
  12. LanguageCode: "en",
  13. IsBot: false,
  14. }
  15. if user.String() != "Test Test" {
  16. t.Fail()
  17. }
  18. }
  19. func TestUserStringWithUserName(t *testing.T) {
  20. user := User{
  21. ID: 0,
  22. FirstName: "Test",
  23. LastName: "Test",
  24. UserName: "@test",
  25. LanguageCode: "en",
  26. }
  27. if user.String() != "@test" {
  28. t.Fail()
  29. }
  30. }
  31. func TestMessageTime(t *testing.T) {
  32. message := Message{Date: 0}
  33. date := time.Unix(0, 0)
  34. if message.Time() != date {
  35. t.Fail()
  36. }
  37. }
  38. func TestMessageIsCommandWithCommand(t *testing.T) {
  39. message := Message{Text: "/command"}
  40. message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
  41. if !message.IsCommand() {
  42. t.Fail()
  43. }
  44. }
  45. func TestIsCommandWithText(t *testing.T) {
  46. message := Message{Text: "some text"}
  47. if message.IsCommand() {
  48. t.Fail()
  49. }
  50. }
  51. func TestIsCommandWithEmptyText(t *testing.T) {
  52. message := Message{Text: ""}
  53. if message.IsCommand() {
  54. t.Fail()
  55. }
  56. }
  57. func TestCommandWithCommand(t *testing.T) {
  58. message := Message{Text: "/command"}
  59. message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
  60. if message.Command() != "command" {
  61. t.Fail()
  62. }
  63. }
  64. func TestCommandWithEmptyText(t *testing.T) {
  65. message := Message{Text: ""}
  66. if message.Command() != "" {
  67. t.Fail()
  68. }
  69. }
  70. func TestCommandWithNonCommand(t *testing.T) {
  71. message := Message{Text: "test text"}
  72. if message.Command() != "" {
  73. t.Fail()
  74. }
  75. }
  76. func TestCommandWithBotName(t *testing.T) {
  77. message := Message{Text: "/command@testbot"}
  78. message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 16}}
  79. if message.Command() != "command" {
  80. t.Fail()
  81. }
  82. }
  83. func TestCommandWithAtWithBotName(t *testing.T) {
  84. message := Message{Text: "/command@testbot"}
  85. message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 16}}
  86. if message.CommandWithAt() != "command@testbot" {
  87. t.Fail()
  88. }
  89. }
  90. func TestMessageCommandArgumentsWithArguments(t *testing.T) {
  91. message := Message{Text: "/command with arguments"}
  92. message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
  93. if message.CommandArguments() != "with arguments" {
  94. t.Fail()
  95. }
  96. }
  97. func TestMessageCommandArgumentsWithMalformedArguments(t *testing.T) {
  98. message := Message{Text: "/command-without argument space"}
  99. message.Entities = []MessageEntity{{Type: "bot_command", Offset: 0, Length: 8}}
  100. if message.CommandArguments() != "without argument space" {
  101. t.Fail()
  102. }
  103. }
  104. func TestMessageCommandArgumentsWithoutArguments(t *testing.T) {
  105. message := Message{Text: "/command"}
  106. if message.CommandArguments() != "" {
  107. t.Fail()
  108. }
  109. }
  110. func TestMessageCommandArgumentsForNonCommand(t *testing.T) {
  111. message := Message{Text: "test text"}
  112. if message.CommandArguments() != "" {
  113. t.Fail()
  114. }
  115. }
  116. func TestMessageEntityParseURLGood(t *testing.T) {
  117. entity := MessageEntity{URL: "https://www.google.com"}
  118. if _, err := entity.ParseURL(); err != nil {
  119. t.Fail()
  120. }
  121. }
  122. func TestMessageEntityParseURLBad(t *testing.T) {
  123. entity := MessageEntity{URL: ""}
  124. if _, err := entity.ParseURL(); err == nil {
  125. t.Fail()
  126. }
  127. }
  128. func TestChatIsPrivate(t *testing.T) {
  129. chat := Chat{ID: 10, Type: "private"}
  130. if !chat.IsPrivate() {
  131. t.Fail()
  132. }
  133. }
  134. func TestChatIsGroup(t *testing.T) {
  135. chat := Chat{ID: 10, Type: "group"}
  136. if !chat.IsGroup() {
  137. t.Fail()
  138. }
  139. }
  140. func TestChatIsChannel(t *testing.T) {
  141. chat := Chat{ID: 10, Type: "channel"}
  142. if !chat.IsChannel() {
  143. t.Fail()
  144. }
  145. }
  146. func TestChatIsSuperGroup(t *testing.T) {
  147. chat := Chat{ID: 10, Type: "supergroup"}
  148. if !chat.IsSuperGroup() {
  149. t.Fail()
  150. }
  151. }
  152. func TestMessageEntityIsMention(t *testing.T) {
  153. entity := MessageEntity{Type: "mention"}
  154. if !entity.IsMention() {
  155. t.Fail()
  156. }
  157. }
  158. func TestMessageEntityIsHashtag(t *testing.T) {
  159. entity := MessageEntity{Type: "hashtag"}
  160. if !entity.IsHashtag() {
  161. t.Fail()
  162. }
  163. }
  164. func TestMessageEntityIsBotCommand(t *testing.T) {
  165. entity := MessageEntity{Type: "bot_command"}
  166. if !entity.IsCommand() {
  167. t.Fail()
  168. }
  169. }
  170. func TestMessageEntityIsUrl(t *testing.T) {
  171. entity := MessageEntity{Type: "url"}
  172. if !entity.IsURL() {
  173. t.Fail()
  174. }
  175. }
  176. func TestMessageEntityIsEmail(t *testing.T) {
  177. entity := MessageEntity{Type: "email"}
  178. if !entity.IsEmail() {
  179. t.Fail()
  180. }
  181. }
  182. func TestMessageEntityIsBold(t *testing.T) {
  183. entity := MessageEntity{Type: "bold"}
  184. if !entity.IsBold() {
  185. t.Fail()
  186. }
  187. }
  188. func TestMessageEntityIsItalic(t *testing.T) {
  189. entity := MessageEntity{Type: "italic"}
  190. if !entity.IsItalic() {
  191. t.Fail()
  192. }
  193. }
  194. func TestMessageEntityIsCode(t *testing.T) {
  195. entity := MessageEntity{Type: "code"}
  196. if !entity.IsCode() {
  197. t.Fail()
  198. }
  199. }
  200. func TestMessageEntityIsPre(t *testing.T) {
  201. entity := MessageEntity{Type: "pre"}
  202. if !entity.IsPre() {
  203. t.Fail()
  204. }
  205. }
  206. func TestMessageEntityIsTextLink(t *testing.T) {
  207. entity := MessageEntity{Type: "text_link"}
  208. if !entity.IsTextLink() {
  209. t.Fail()
  210. }
  211. }
  212. func TestFileLink(t *testing.T) {
  213. file := File{FilePath: "test/test.txt"}
  214. if file.Link("token") != "https://api.telegram.org/file/bottoken/test/test.txt" {
  215. t.Fail()
  216. }
  217. }
  218. // Ensure all configs are sendable
  219. var (
  220. _ Chattable = AnimationConfig{}
  221. _ Chattable = AnswerWebAppQueryConfig{}
  222. _ Chattable = AudioConfig{}
  223. _ Chattable = BanChatMemberConfig{}
  224. _ Chattable = BanChatSenderChatConfig{}
  225. _ Chattable = CallbackConfig{}
  226. _ Chattable = ChatActionConfig{}
  227. _ Chattable = ChatAdministratorsConfig{}
  228. _ Chattable = ChatInfoConfig{}
  229. _ Chattable = ChatInviteLinkConfig{}
  230. _ Chattable = CloseConfig{}
  231. _ Chattable = ContactConfig{}
  232. _ Chattable = CopyMessageConfig{}
  233. _ Chattable = CreateChatInviteLinkConfig{}
  234. _ Chattable = DeleteChatPhotoConfig{}
  235. _ Chattable = DeleteChatStickerSetConfig{}
  236. _ Chattable = DeleteMessageConfig{}
  237. _ Chattable = DeleteMyCommandsConfig{}
  238. _ Chattable = DeleteWebhookConfig{}
  239. _ Chattable = DocumentConfig{}
  240. _ Chattable = EditChatInviteLinkConfig{}
  241. _ Chattable = EditMessageCaptionConfig{}
  242. _ Chattable = EditMessageLiveLocationConfig{}
  243. _ Chattable = EditMessageMediaConfig{}
  244. _ Chattable = EditMessageReplyMarkupConfig{}
  245. _ Chattable = EditMessageTextConfig{}
  246. _ Chattable = FileConfig{}
  247. _ Chattable = ForwardConfig{}
  248. _ Chattable = GameConfig{}
  249. _ Chattable = GetChatMemberConfig{}
  250. _ Chattable = GetChatMenuButtonConfig{}
  251. _ Chattable = GetGameHighScoresConfig{}
  252. _ Chattable = GetMyDefaultAdministratorRightsConfig{}
  253. _ Chattable = InlineConfig{}
  254. _ Chattable = InvoiceConfig{}
  255. _ Chattable = KickChatMemberConfig{}
  256. _ Chattable = LeaveChatConfig{}
  257. _ Chattable = LocationConfig{}
  258. _ Chattable = LogOutConfig{}
  259. _ Chattable = MediaGroupConfig{}
  260. _ Chattable = MessageConfig{}
  261. _ Chattable = PhotoConfig{}
  262. _ Chattable = PinChatMessageConfig{}
  263. _ Chattable = PreCheckoutConfig{}
  264. _ Chattable = PromoteChatMemberConfig{}
  265. _ Chattable = RestrictChatMemberConfig{}
  266. _ Chattable = RevokeChatInviteLinkConfig{}
  267. _ Chattable = SendPollConfig{}
  268. _ Chattable = SetChatDescriptionConfig{}
  269. _ Chattable = SetChatMenuButtonConfig{}
  270. _ Chattable = SetChatPhotoConfig{}
  271. _ Chattable = SetChatTitleConfig{}
  272. _ Chattable = SetGameScoreConfig{}
  273. _ Chattable = SetMyDefaultAdministratorRightsConfig{}
  274. _ Chattable = ShippingConfig{}
  275. _ Chattable = StickerConfig{}
  276. _ Chattable = StopMessageLiveLocationConfig{}
  277. _ Chattable = StopPollConfig{}
  278. _ Chattable = UnbanChatMemberConfig{}
  279. _ Chattable = UnbanChatSenderChatConfig{}
  280. _ Chattable = UnpinChatMessageConfig{}
  281. _ Chattable = UpdateConfig{}
  282. _ Chattable = UserProfilePhotosConfig{}
  283. _ Chattable = VenueConfig{}
  284. _ Chattable = VideoConfig{}
  285. _ Chattable = VideoNoteConfig{}
  286. _ Chattable = VoiceConfig{}
  287. _ Chattable = WebhookConfig{}
  288. )
  289. // Ensure all Fileable types are correct.
  290. var (
  291. _ Fileable = (*PhotoConfig)(nil)
  292. _ Fileable = (*AudioConfig)(nil)
  293. _ Fileable = (*DocumentConfig)(nil)
  294. _ Fileable = (*StickerConfig)(nil)
  295. _ Fileable = (*VideoConfig)(nil)
  296. _ Fileable = (*AnimationConfig)(nil)
  297. _ Fileable = (*VideoNoteConfig)(nil)
  298. _ Fileable = (*VoiceConfig)(nil)
  299. _ Fileable = (*SetChatPhotoConfig)(nil)
  300. _ Fileable = (*EditMessageMediaConfig)(nil)
  301. _ Fileable = (*SetChatPhotoConfig)(nil)
  302. _ Fileable = (*UploadStickerConfig)(nil)
  303. _ Fileable = (*NewStickerSetConfig)(nil)
  304. _ Fileable = (*AddStickerConfig)(nil)
  305. _ Fileable = (*MediaGroupConfig)(nil)
  306. _ Fileable = (*WebhookConfig)(nil)
  307. _ Fileable = (*SetStickerSetThumbConfig)(nil)
  308. )
  309. // Ensure all RequestFileData types are correct.
  310. var (
  311. _ RequestFileData = (*FilePath)(nil)
  312. _ RequestFileData = (*FileBytes)(nil)
  313. _ RequestFileData = (*FileReader)(nil)
  314. _ RequestFileData = (*FileURL)(nil)
  315. _ RequestFileData = (*FileID)(nil)
  316. _ RequestFileData = (*fileAttach)(nil)
  317. )