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.

GuildPermissionsTests.cs 10 kB

test: Split Unit and Integration tests into separate projects (#1290) * Squashed commit of test rewrite changes fix missing priority speaker flag rewrite the TestChannelPermissionModify test add test for GuildPermission modify separate unit and integration tests, start writing channel and guild permission tests copy over the color tests copy over the emote tests copy the token utils tests make the mocked entities sealed classes copy the TypeReaderTests class properly dispose the CommandService in the TypeReaderTests start writing tests for EmbedBuilder and related classes test that properties throw ArgumentException when invalid add tests for the embed length property add withFooter tests finish adding tests to EmbedBuilder fix bug in value validation of EmbedFieldBuilder hey, these tests actually found a bug! add tests for the MentionUtils class add tests for the Format util class remove all of the old tests add analyzer tests (copied from old tests) add tests for the SnowflakeUtils class add integration tests these get around the issue of state persisting between tests by creating and deleting a guild for each set of tests. these shouldn't be run excessively because of the rate limits, but should be fine every now and then remove unnecessary launchSettings.json update outdated string don't create a new guild each time, as that can result in errors this can happen if a bot creates too many guilds without properly deleting them add some tests that show that guild can be modified await async assert add more measures that created channels are deleted when done remove "Test" prefix from test method names I think that this prefix when already displayed under a class with a suffix of "Tests" is redundant Remove mention of old test project fix an issue from forgetting to await Assert.ThrowsAsync explicitly disable parallelization on integration tests add test for GuildPermission modify separate unit and integration tests, start writing channel and guild permission tests copy over the color tests copy over the emote tests make the mocked entities sealed classes properly dispose the CommandService in the TypeReaderTests fix bug in value validation of EmbedFieldBuilder hey, these tests actually found a bug! add tests for the MentionUtils class add tests for the Format util class remove all of the old tests add analyzer tests (copied from old tests) add tests for the SnowflakeUtils class add integration tests these get around the issue of state persisting between tests by creating and deleting a guild for each set of tests. these shouldn't be run excessively because of the rate limits, but should be fine every now and then remove unnecessary launchSettings.json update outdated string don't create a new guild each time, as that can result in errors this can happen if a bot creates too many guilds without properly deleting them add more measures that created channels are deleted when done remove "Test" prefix from test method names I think that this prefix when already displayed under a class with a suffix of "Tests" is redundant Remove mention of old test project fix an issue from forgetting to await Assert.ThrowsAsync explicitly disable parallelization on integration tests update the azure CI build script separate execution of test projects so that if one fails the other will not pass one of the unit tests failed, but the analzyer tests passed fix test that would break in different timezones enable the integration tests (only on dev branch) * Squashed commit of test rewrite changes fix missing priority speaker flag rewrite the TestChannelPermissionModify test add test for GuildPermission modify separate unit and integration tests, start writing channel and guild permission tests copy over the color tests copy over the emote tests copy the token utils tests make the mocked entities sealed classes copy the TypeReaderTests class properly dispose the CommandService in the TypeReaderTests start writing tests for EmbedBuilder and related classes test that properties throw ArgumentException when invalid add tests for the embed length property add withFooter tests finish adding tests to EmbedBuilder fix bug in value validation of EmbedFieldBuilder hey, these tests actually found a bug! add tests for the MentionUtils class add tests for the Format util class remove all of the old tests add analyzer tests (copied from old tests) add tests for the SnowflakeUtils class add integration tests these get around the issue of state persisting between tests by creating and deleting a guild for each set of tests. these shouldn't be run excessively because of the rate limits, but should be fine every now and then remove unnecessary launchSettings.json update outdated string don't create a new guild each time, as that can result in errors this can happen if a bot creates too many guilds without properly deleting them add some tests that show that guild can be modified await async assert add more measures that created channels are deleted when done remove "Test" prefix from test method names I think that this prefix when already displayed under a class with a suffix of "Tests" is redundant Remove mention of old test project fix an issue from forgetting to await Assert.ThrowsAsync explicitly disable parallelization on integration tests add test for GuildPermission modify separate unit and integration tests, start writing channel and guild permission tests copy over the color tests copy over the emote tests make the mocked entities sealed classes properly dispose the CommandService in the TypeReaderTests fix bug in value validation of EmbedFieldBuilder hey, these tests actually found a bug! add tests for the MentionUtils class add tests for the Format util class remove all of the old tests add analyzer tests (copied from old tests) add tests for the SnowflakeUtils class add integration tests these get around the issue of state persisting between tests by creating and deleting a guild for each set of tests. these shouldn't be run excessively because of the rate limits, but should be fine every now and then remove unnecessary launchSettings.json update outdated string don't create a new guild each time, as that can result in errors this can happen if a bot creates too many guilds without properly deleting them add more measures that created channels are deleted when done remove "Test" prefix from test method names I think that this prefix when already displayed under a class with a suffix of "Tests" is redundant Remove mention of old test project fix an issue from forgetting to await Assert.ThrowsAsync explicitly disable parallelization on integration tests update the azure CI build script separate execution of test projects so that if one fails the other will not pass one of the unit tests failed, but the analzyer tests passed fix test that would break in different timezones enable the integration tests (only on dev branch) * Update mocked channels for changed SendFileAsync signature * comment out the integration tests from the build script no bot token is provided to this script, and use of integration tests in CI is questionable here * force rebuild because Azure linux build broke
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using Xunit;
  5. namespace Discord
  6. {
  7. /// <summary>
  8. /// Tests the behavior of the <see cref="Discord.GuildPermissions"/> type and related functions.
  9. /// </summary>
  10. public class GuildPermissionsTests
  11. {
  12. /// <summary>
  13. /// Tests the default value of the <see cref="Discord.GuildPermissions"/> constructor.
  14. /// </summary>
  15. [Fact]
  16. public void DefaultConstructor()
  17. {
  18. var p = new GuildPermissions();
  19. Assert.Equal((ulong)0, p.RawValue);
  20. Assert.Equal(GuildPermissions.None.RawValue, p.RawValue);
  21. }
  22. /// <summary>
  23. /// Tests the behavior of the <see cref="Discord.GuildPermissions"/> raw value constructor.
  24. /// </summary>
  25. [Fact]
  26. public void RawValueConstructor()
  27. {
  28. // returns all of the values that will be tested
  29. // a Theory cannot be used here, because these values are not all constants
  30. IEnumerable<ulong> GetTestValues()
  31. {
  32. yield return 0;
  33. yield return GuildPermissions.None.RawValue;
  34. yield return GuildPermissions.All.RawValue;
  35. yield return GuildPermissions.Webhook.RawValue;
  36. };
  37. foreach (var rawValue in GetTestValues())
  38. {
  39. var p = new GuildPermissions(rawValue);
  40. Assert.Equal(rawValue, p.RawValue);
  41. }
  42. }
  43. /// <summary>
  44. /// Tests the behavior of the <see cref="Discord.GuildPermissions"/> constructor for each
  45. /// of it's flags.
  46. /// </summary>
  47. [Fact]
  48. public void FlagsConstructor()
  49. {
  50. // util method for asserting that the constructor sets the given flag
  51. void AssertFlag(Func<GuildPermissions> cstr, GuildPermission flag)
  52. {
  53. var p = cstr();
  54. // ensure flag set to true
  55. Assert.True(p.Has(flag));
  56. // ensure only this flag is set
  57. Assert.Equal((ulong)flag, p.RawValue);
  58. }
  59. AssertFlag(() => new GuildPermissions(createInstantInvite: true), GuildPermission.CreateInstantInvite);
  60. AssertFlag(() => new GuildPermissions(kickMembers: true), GuildPermission.KickMembers);
  61. AssertFlag(() => new GuildPermissions(banMembers: true), GuildPermission.BanMembers);
  62. AssertFlag(() => new GuildPermissions(administrator: true), GuildPermission.Administrator);
  63. AssertFlag(() => new GuildPermissions(manageChannels: true), GuildPermission.ManageChannels);
  64. AssertFlag(() => new GuildPermissions(manageGuild: true), GuildPermission.ManageGuild);
  65. AssertFlag(() => new GuildPermissions(addReactions: true), GuildPermission.AddReactions);
  66. AssertFlag(() => new GuildPermissions(viewAuditLog: true), GuildPermission.ViewAuditLog);
  67. AssertFlag(() => new GuildPermissions(viewChannel: true), GuildPermission.ViewChannel);
  68. AssertFlag(() => new GuildPermissions(sendMessages: true), GuildPermission.SendMessages);
  69. AssertFlag(() => new GuildPermissions(sendTTSMessages: true), GuildPermission.SendTTSMessages);
  70. AssertFlag(() => new GuildPermissions(manageMessages: true), GuildPermission.ManageMessages);
  71. AssertFlag(() => new GuildPermissions(embedLinks: true), GuildPermission.EmbedLinks);
  72. AssertFlag(() => new GuildPermissions(attachFiles: true), GuildPermission.AttachFiles);
  73. AssertFlag(() => new GuildPermissions(readMessageHistory: true), GuildPermission.ReadMessageHistory);
  74. AssertFlag(() => new GuildPermissions(mentionEveryone: true), GuildPermission.MentionEveryone);
  75. AssertFlag(() => new GuildPermissions(useExternalEmojis: true), GuildPermission.UseExternalEmojis);
  76. AssertFlag(() => new GuildPermissions(connect: true), GuildPermission.Connect);
  77. AssertFlag(() => new GuildPermissions(speak: true), GuildPermission.Speak);
  78. AssertFlag(() => new GuildPermissions(muteMembers: true), GuildPermission.MuteMembers);
  79. AssertFlag(() => new GuildPermissions(deafenMembers: true), GuildPermission.DeafenMembers);
  80. AssertFlag(() => new GuildPermissions(moveMembers: true), GuildPermission.MoveMembers);
  81. AssertFlag(() => new GuildPermissions(useVoiceActivation: true), GuildPermission.UseVAD);
  82. AssertFlag(() => new GuildPermissions(prioritySpeaker: true), GuildPermission.PrioritySpeaker);
  83. AssertFlag(() => new GuildPermissions(changeNickname: true), GuildPermission.ChangeNickname);
  84. AssertFlag(() => new GuildPermissions(manageNicknames: true), GuildPermission.ManageNicknames);
  85. AssertFlag(() => new GuildPermissions(manageRoles: true), GuildPermission.ManageRoles);
  86. AssertFlag(() => new GuildPermissions(manageWebhooks: true), GuildPermission.ManageWebhooks);
  87. AssertFlag(() => new GuildPermissions(manageEmojis: true), GuildPermission.ManageEmojis);
  88. }
  89. /// <summary>
  90. /// Tests the behavior of <see cref="Discord.GuildPermissions.Modify(bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?)"/>
  91. /// with each of the parameters.
  92. /// </summary>
  93. [Fact]
  94. public void Modify()
  95. {
  96. // asserts that flag values can be checked
  97. // and that flag values can be toggled on and off
  98. // and that the behavior of ToList works as expected
  99. void AssertUtil(GuildPermission permission,
  100. Func<GuildPermissions, bool> has,
  101. Func<GuildPermissions, bool, GuildPermissions> modify)
  102. {
  103. var perm = new GuildPermissions();
  104. // ensure permission initially false
  105. // use both the function and Has to ensure that the GetPermission
  106. // function is working
  107. Assert.False(has(perm));
  108. Assert.False(perm.Has(permission));
  109. // enable it, and ensure that it gets set
  110. perm = modify(perm, true);
  111. Assert.True(has(perm));
  112. Assert.True(perm.Has(permission));
  113. // check ToList behavior
  114. var list = perm.ToList();
  115. Assert.Contains(permission, list);
  116. Assert.Single(list);
  117. // set it false again
  118. perm = modify(perm, false);
  119. Assert.False(has(perm));
  120. Assert.False(perm.Has(permission));
  121. // ensure that no perms are set now
  122. Assert.Equal(GuildPermissions.None.RawValue, perm.RawValue);
  123. }
  124. AssertUtil(GuildPermission.CreateInstantInvite, x => x.CreateInstantInvite, (p, enable) => p.Modify(createInstantInvite: enable));
  125. AssertUtil(GuildPermission.KickMembers, x => x.KickMembers, (p, enable) => p.Modify(kickMembers: enable));
  126. AssertUtil(GuildPermission.BanMembers, x => x.BanMembers, (p, enable) => p.Modify(banMembers: enable));
  127. AssertUtil(GuildPermission.Administrator, x => x.Administrator, (p, enable) => p.Modify(administrator: enable));
  128. AssertUtil(GuildPermission.ManageChannels, x => x.ManageChannels, (p, enable) => p.Modify(manageChannels: enable));
  129. AssertUtil(GuildPermission.ManageGuild, x => x.ManageGuild, (p, enable) => p.Modify(manageGuild: enable));
  130. AssertUtil(GuildPermission.AddReactions, x => x.AddReactions, (p, enable) => p.Modify(addReactions: enable));
  131. AssertUtil(GuildPermission.ViewAuditLog, x => x.ViewAuditLog, (p, enable) => p.Modify(viewAuditLog: enable));
  132. AssertUtil(GuildPermission.ViewChannel, x => x.ViewChannel, (p, enable) => p.Modify(viewChannel: enable));
  133. AssertUtil(GuildPermission.SendMessages, x => x.SendMessages, (p, enable) => p.Modify(sendMessages: enable));
  134. AssertUtil(GuildPermission.SendTTSMessages, x => x.SendTTSMessages, (p, enable) => p.Modify(sendTTSMessages: enable));
  135. AssertUtil(GuildPermission.ManageMessages, x => x.ManageMessages, (p, enable) => p.Modify(manageMessages: enable));
  136. AssertUtil(GuildPermission.EmbedLinks, x => x.EmbedLinks, (p, enable) => p.Modify(embedLinks: enable));
  137. AssertUtil(GuildPermission.AttachFiles, x => x.AttachFiles, (p, enable) => p.Modify(attachFiles: enable));
  138. AssertUtil(GuildPermission.ReadMessageHistory, x => x.ReadMessageHistory, (p, enable) => p.Modify(readMessageHistory: enable));
  139. AssertUtil(GuildPermission.MentionEveryone, x => x.MentionEveryone, (p, enable) => p.Modify(mentionEveryone: enable));
  140. AssertUtil(GuildPermission.UseExternalEmojis, x => x.UseExternalEmojis, (p, enable) => p.Modify(useExternalEmojis: enable));
  141. AssertUtil(GuildPermission.Connect, x => x.Connect, (p, enable) => p.Modify(connect: enable));
  142. AssertUtil(GuildPermission.Speak, x => x.Speak, (p, enable) => p.Modify(speak: enable));
  143. AssertUtil(GuildPermission.MuteMembers, x => x.MuteMembers, (p, enable) => p.Modify(muteMembers: enable));
  144. AssertUtil(GuildPermission.MoveMembers, x => x.MoveMembers, (p, enable) => p.Modify(moveMembers: enable));
  145. AssertUtil(GuildPermission.UseVAD, x => x.UseVAD, (p, enable) => p.Modify(useVoiceActivation: enable));
  146. AssertUtil(GuildPermission.ChangeNickname, x => x.ChangeNickname, (p, enable) => p.Modify(changeNickname: enable));
  147. AssertUtil(GuildPermission.ManageNicknames, x => x.ManageNicknames, (p, enable) => p.Modify(manageNicknames: enable));
  148. AssertUtil(GuildPermission.ManageRoles, x => x.ManageRoles, (p, enable) => p.Modify(manageRoles: enable));
  149. AssertUtil(GuildPermission.ManageWebhooks, x => x.ManageWebhooks, (p, enable) => p.Modify(manageWebhooks: enable));
  150. AssertUtil(GuildPermission.ManageEmojis, x => x.ManageEmojis, (p, enable) => p.Modify(manageEmojis: enable));
  151. }
  152. }
  153. }