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.4 kB

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