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

Tests.Channels.cs 6.8 kB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using Discord.Rest;
  2. using System;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Xunit;
  6. namespace Discord
  7. {
  8. public partial class Tests
  9. {
  10. internal static async Task Migration_CreateTextChannels(DiscordRestClient client, RestGuild guild)
  11. {
  12. var text1 = await guild.GetDefaultChannelAsync();
  13. var text2 = await guild.CreateTextChannelAsync("text2");
  14. var text3 = await guild.CreateTextChannelAsync("text3");
  15. var text4 = await guild.CreateTextChannelAsync("text4");
  16. var text5 = await guild.CreateTextChannelAsync("text5");
  17. //Modify #general
  18. await text1.ModifyAsync(x =>
  19. {
  20. x.Name = "text1";
  21. x.Position = 1;
  22. x.Topic = "Topic1";
  23. });
  24. await text2.ModifyAsync(x =>
  25. {
  26. x.Position = 2;
  27. });
  28. await text3.ModifyAsync(x =>
  29. {
  30. x.Topic = "Topic2";
  31. });
  32. await text4.ModifyAsync(x =>
  33. {
  34. x.Position = 3;
  35. x.Topic = "Topic2";
  36. });
  37. await text5.ModifyAsync(x =>
  38. {
  39. });
  40. CheckTextChannels(guild, text1, text2, text3, text4, text5);
  41. }
  42. [Fact]
  43. public async Task TestTextChannels()
  44. {
  45. CheckTextChannels(_guild, (await _guild.GetTextChannelsAsync()).ToArray());
  46. }
  47. private static void CheckTextChannels(RestGuild guild, params RestTextChannel[] textChannels)
  48. {
  49. Assert.Equal(textChannels.Length, 5);
  50. Assert.All(textChannels, x =>
  51. {
  52. Assert.NotNull(x);
  53. Assert.NotEqual(x.Id, 0UL);
  54. Assert.True(x.Position >= 0);
  55. });
  56. var text1 = textChannels.Where(x => x.Name == "text1").FirstOrDefault();
  57. var text2 = textChannels.Where(x => x.Name == "text2").FirstOrDefault();
  58. var text3 = textChannels.Where(x => x.Name == "text3").FirstOrDefault();
  59. var text4 = textChannels.Where(x => x.Name == "text4").FirstOrDefault();
  60. var text5 = textChannels.Where(x => x.Name == "text5").FirstOrDefault();
  61. Assert.NotNull(text1);
  62. //Assert.True(text1.Id == guild.DefaultChannelId);
  63. Assert.Equal(text1.Position, 1);
  64. Assert.Equal(text1.Topic, "Topic1");
  65. Assert.NotNull(text2);
  66. Assert.Equal(text2.Position, 2);
  67. Assert.Null(text2.Topic);
  68. Assert.NotNull(text3);
  69. Assert.Equal(text3.Topic, "Topic2");
  70. Assert.NotNull(text4);
  71. Assert.Equal(text4.Position, 3);
  72. Assert.Equal(text4.Topic, "Topic2");
  73. Assert.NotNull(text5);
  74. Assert.Null(text5.Topic);
  75. }
  76. internal static async Task Migration_CreateVoiceChannels(DiscordRestClient client, RestGuild guild)
  77. {
  78. var voice1 = await guild.CreateVoiceChannelAsync("voice1");
  79. var voice2 = await guild.CreateVoiceChannelAsync("voice2");
  80. var voice3 = await guild.CreateVoiceChannelAsync("voice3");
  81. await voice1.ModifyAsync(x =>
  82. {
  83. x.Bitrate = 96000;
  84. x.Position = 1;
  85. });
  86. await voice2.ModifyAsync(x =>
  87. {
  88. x.UserLimit = null;
  89. });
  90. await voice3.ModifyAsync(x =>
  91. {
  92. x.Bitrate = 8000;
  93. x.Position = 1;
  94. x.UserLimit = 16;
  95. });
  96. CheckVoiceChannels(voice1, voice2, voice3);
  97. }
  98. [Fact]
  99. public async Task TestVoiceChannels()
  100. {
  101. CheckVoiceChannels((await _guild.GetVoiceChannelsAsync()).ToArray());
  102. }
  103. private static void CheckVoiceChannels(params RestVoiceChannel[] voiceChannels)
  104. {
  105. Assert.Equal(voiceChannels.Length, 3);
  106. Assert.All(voiceChannels, x =>
  107. {
  108. Assert.NotNull(x);
  109. Assert.NotEqual(x.Id, 0UL);
  110. Assert.NotEqual(x.UserLimit, 0);
  111. Assert.True(x.Bitrate > 0);
  112. Assert.True(x.Position >= 0);
  113. });
  114. var voice1 = voiceChannels.Where(x => x.Name == "voice1").FirstOrDefault();
  115. var voice2 = voiceChannels.Where(x => x.Name == "voice2").FirstOrDefault();
  116. var voice3 = voiceChannels.Where(x => x.Name == "voice3").FirstOrDefault();
  117. Assert.NotNull(voice1);
  118. Assert.Equal(voice1.Bitrate, 96000);
  119. Assert.Equal(voice1.Position, 1);
  120. Assert.NotNull(voice2);
  121. Assert.Equal(voice2.UserLimit, null);
  122. Assert.NotNull(voice3);
  123. Assert.Equal(voice3.Bitrate, 8000);
  124. Assert.Equal(voice3.Position, 1);
  125. Assert.Equal(voice3.UserLimit, 16);
  126. }
  127. [Fact]
  128. public Task TestChannelCategories()
  129. {
  130. CheckChannelCategories(_client, _guild);
  131. return Task.CompletedTask;
  132. }
  133. private async static void CheckChannelCategories(DiscordRestClient client, RestGuild guild)
  134. {
  135. // create some channel categories
  136. var cat1 = await guild.CreateCategoryChannelAsync("Cat1");
  137. var cat2 = await guild.CreateCategoryChannelAsync("Cat2");
  138. // check that both CategoryID and GetCategoryID throw NotSupportedException
  139. // because Categories cannot be nested
  140. Assert.Throws<NotSupportedException>(() =>
  141. {
  142. var x = cat1.CategoryId;
  143. });
  144. Assert.Throws<NotSupportedException>(() =>
  145. {
  146. var x = cat2.GetCategoryAsync();
  147. });
  148. var text1 = await guild.CreateTextChannelAsync("nestedText1");
  149. var voice1 = await guild.CreateVoiceChannelAsync("nestedVoice1");
  150. // set the text channel parent to Cat 1
  151. await text1.ModifyAsync(x =>
  152. {
  153. x.CategoryId = cat1.Id;
  154. });
  155. await voice1.ModifyAsync(x =>
  156. {
  157. x.CategoryId = cat2.Id;
  158. });
  159. // these shouldn't throw because they are not channel categories
  160. // assert that CategoryId works for text channels
  161. Assert.Equal(text1.CategoryId, cat1.Id);
  162. Assert.Equal((await text1.GetCategoryAsync()).Id, cat1.Id);
  163. // and for voice channels
  164. Assert.Equal(voice1.CategoryId, cat2.Id);
  165. Assert.Equal((await voice1.GetCategoryAsync()).Id, cat2.Id);
  166. // incomplete test, could use more coverage of other methods
  167. }
  168. }
  169. }