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.

GuildAccessTests.cs 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using Discord.Analyzers;
  2. using Microsoft.CodeAnalysis;
  3. using Microsoft.CodeAnalysis.Diagnostics;
  4. using System;
  5. using TestHelper;
  6. using Xunit;
  7. namespace Discord
  8. {
  9. public partial class AnalyserTests
  10. {
  11. public class GuildAccessTests : DiagnosticVerifier
  12. {
  13. [Fact]
  14. public void VerifyDiagnosticWhenLackingRequireContext()
  15. {
  16. string source = @"using System;
  17. using System.Threading.Tasks;
  18. using Discord.Commands;
  19. namespace Test
  20. {
  21. public class TestModule : ModuleBase<ICommandContext>
  22. {
  23. [Command(""test"")]
  24. public Task TestCmd() => ReplyAsync(Context.Guild.Name);
  25. }
  26. }";
  27. var expected = new DiagnosticResult()
  28. {
  29. Id = "DNET0001",
  30. Locations = new[] { new DiagnosticResultLocation("Test0.cs", line: 10, column: 45) },
  31. Message = "Command method 'TestCmd' is accessing 'Context.Guild' but is not restricted to Guild contexts.",
  32. Severity = DiagnosticSeverity.Warning
  33. };
  34. VerifyCSharpDiagnostic(source, expected);
  35. }
  36. [Fact]
  37. public void VerifyDiagnosticWhenWrongRequireContext()
  38. {
  39. string source = @"using System;
  40. using System.Threading.Tasks;
  41. using Discord.Commands;
  42. namespace Test
  43. {
  44. public class TestModule : ModuleBase<ICommandContext>
  45. {
  46. [Command(""test""), RequireContext(ContextType.Group)]
  47. public Task TestCmd() => ReplyAsync(Context.Guild.Name);
  48. }
  49. }";
  50. var expected = new DiagnosticResult()
  51. {
  52. Id = "DNET0001",
  53. Locations = new[] { new DiagnosticResultLocation("Test0.cs", line: 10, column: 45) },
  54. Message = "Command method 'TestCmd' is accessing 'Context.Guild' but is not restricted to Guild contexts.",
  55. Severity = DiagnosticSeverity.Warning
  56. };
  57. VerifyCSharpDiagnostic(source, expected);
  58. }
  59. [Fact]
  60. public void VerifyNoDiagnosticWhenRequireContextOnMethod()
  61. {
  62. string source = @"using System;
  63. using System.Threading.Tasks;
  64. using Discord.Commands;
  65. namespace Test
  66. {
  67. public class TestModule : ModuleBase<ICommandContext>
  68. {
  69. [Command(""test""), RequireContext(ContextType.Guild)]
  70. public Task TestCmd() => ReplyAsync(Context.Guild.Name);
  71. }
  72. }";
  73. VerifyCSharpDiagnostic(source, Array.Empty<DiagnosticResult>());
  74. }
  75. [Fact]
  76. public void VerifyNoDiagnosticWhenRequireContextOnClass()
  77. {
  78. string source = @"using System;
  79. using System.Threading.Tasks;
  80. using Discord.Commands;
  81. namespace Test
  82. {
  83. [RequireContext(ContextType.Guild)]
  84. public class TestModule : ModuleBase<ICommandContext>
  85. {
  86. [Command(""test"")]
  87. public Task TestCmd() => ReplyAsync(Context.Guild.Name);
  88. }
  89. }";
  90. VerifyCSharpDiagnostic(source, Array.Empty<DiagnosticResult>());
  91. }
  92. protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer()
  93. => new GuildAccessAnalyzer();
  94. }
  95. }
  96. }