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

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