|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using Microsoft.CodeAnalysis.Testing; |
| 5 | +using Microsoft.DurableTask.Analyzers.Orchestration; |
| 6 | + |
| 7 | +using VerifyCS = Microsoft.DurableTask.Analyzers.Tests.Verifiers.CSharpAnalyzerVerifier<Microsoft.DurableTask.Analyzers.Orchestration.GetInputOrchestrationAnalyzer>; |
| 8 | + |
| 9 | +namespace Microsoft.DurableTask.Analyzers.Tests.Orchestration; |
| 10 | + |
| 11 | +public class GetInputOrchestrationAnalyzerTests |
| 12 | +{ |
| 13 | + [Fact] |
| 14 | + public async Task EmptyCodeWithNoSymbolsAvailableHasNoDiag() |
| 15 | + { |
| 16 | + string code = @""; |
| 17 | + |
| 18 | + // checks that empty code with no assembly references of Durable Functions has no diagnostics. |
| 19 | + // this guarantees that if someone adds our analyzer to a project that doesn't use Durable Functions, |
| 20 | + // the analyzer won't crash/they won't get any diagnostics |
| 21 | + await VerifyCS.VerifyAnalyzerAsync(code); |
| 22 | + } |
| 23 | + |
| 24 | + [Fact] |
| 25 | + public async Task EmptyCodeWithSymbolsAvailableHasNoDiag() |
| 26 | + { |
| 27 | + string code = @""; |
| 28 | + |
| 29 | + // checks that empty code with access to assembly references of Durable Functions has no diagnostics |
| 30 | + await VerifyCS.VerifyDurableTaskAnalyzerAsync(code); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public async Task NonOrchestrationHasNoDiag() |
| 35 | + { |
| 36 | + string code = Wrapper.WrapDurableFunctionOrchestration(@" |
| 37 | +void Method(){ |
| 38 | + // This is not an orchestration method, so no diagnostic |
| 39 | +} |
| 40 | +"); |
| 41 | + |
| 42 | + await VerifyCS.VerifyDurableTaskAnalyzerAsync(code); |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public async Task DurableFunctionOrchestrationUsingGetInputHasInfoDiag() |
| 47 | + { |
| 48 | + string code = Wrapper.WrapDurableFunctionOrchestration(@" |
| 49 | +[Function(""Run"")] |
| 50 | +int Run([OrchestrationTrigger] TaskOrchestrationContext context) |
| 51 | +{ |
| 52 | + int input = {|#0:context.GetInput<int>()|}; |
| 53 | + return input; |
| 54 | +} |
| 55 | +"); |
| 56 | + |
| 57 | + DiagnosticResult expected = BuildDiagnostic().WithLocation(0).WithArguments("Run"); |
| 58 | + |
| 59 | + await VerifyCS.VerifyDurableTaskAnalyzerAsync(code, expected); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public async Task DurableFunctionOrchestrationWithInputParameterHasNoDiag() |
| 64 | + { |
| 65 | + string code = Wrapper.WrapDurableFunctionOrchestration(@" |
| 66 | +[Function(""Run"")] |
| 67 | +int Run([OrchestrationTrigger] TaskOrchestrationContext context, int input) |
| 68 | +{ |
| 69 | + // Using input parameter is the recommended approach |
| 70 | + return input; |
| 71 | +} |
| 72 | +"); |
| 73 | + |
| 74 | + await VerifyCS.VerifyDurableTaskAnalyzerAsync(code); |
| 75 | + } |
| 76 | + |
| 77 | + [Fact] |
| 78 | + public async Task TaskOrchestratorWithInputParameterHasNoDiag() |
| 79 | + { |
| 80 | + string code = Wrapper.WrapTaskOrchestrator(@" |
| 81 | +public class MyOrchestrator : TaskOrchestrator<int, int> |
| 82 | +{ |
| 83 | + public override Task<int> RunAsync(TaskOrchestrationContext context, int input) |
| 84 | + { |
| 85 | + // Using input parameter is the recommended approach |
| 86 | + return Task.FromResult(input); |
| 87 | + } |
| 88 | +} |
| 89 | +"); |
| 90 | + |
| 91 | + await VerifyCS.VerifyDurableTaskAnalyzerAsync(code); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public async Task TaskOrchestratorUsingGetInputHasInfoDiag() |
| 96 | + { |
| 97 | + string code = Wrapper.WrapTaskOrchestrator(@" |
| 98 | +public class MyOrchestrator : TaskOrchestrator<int, int> |
| 99 | +{ |
| 100 | + public override Task<int> RunAsync(TaskOrchestrationContext context, int input) |
| 101 | + { |
| 102 | + // Even though input parameter exists, GetInput is still flagged as not recommended |
| 103 | + int value = {|#0:context.GetInput<int>()|}; |
| 104 | + return Task.FromResult(value); |
| 105 | + } |
| 106 | +} |
| 107 | +"); |
| 108 | + |
| 109 | + DiagnosticResult expected = BuildDiagnostic().WithLocation(0).WithArguments("MyOrchestrator"); |
| 110 | + |
| 111 | + await VerifyCS.VerifyDurableTaskAnalyzerAsync(code, expected); |
| 112 | + } |
| 113 | + |
| 114 | + [Fact] |
| 115 | + public async Task OrchestratorFuncUsingGetInputHasInfoDiag() |
| 116 | + { |
| 117 | + string code = Wrapper.WrapFuncOrchestrator(@" |
| 118 | +tasks.AddOrchestratorFunc(""MyOrchestration"", (TaskOrchestrationContext context) => |
| 119 | +{ |
| 120 | + int input = {|#0:context.GetInput<int>()|}; |
| 121 | + return Task.FromResult(input); |
| 122 | +}); |
| 123 | +"); |
| 124 | + |
| 125 | + DiagnosticResult expected = BuildDiagnostic().WithLocation(0).WithArguments("MyOrchestration"); |
| 126 | + |
| 127 | + await VerifyCS.VerifyDurableTaskAnalyzerAsync(code, expected); |
| 128 | + } |
| 129 | + |
| 130 | + [Fact] |
| 131 | + public async Task NestedMethodCallWithGetInputHasInfoDiag() |
| 132 | + { |
| 133 | + string code = Wrapper.WrapDurableFunctionOrchestration(@" |
| 134 | +[Function(""Run"")] |
| 135 | +int Run([OrchestrationTrigger] TaskOrchestrationContext context) |
| 136 | +{ |
| 137 | + return HelperMethod(context); |
| 138 | +} |
| 139 | +
|
| 140 | +int HelperMethod(TaskOrchestrationContext context) |
| 141 | +{ |
| 142 | + int input = {|#0:context.GetInput<int>()|}; |
| 143 | + return input; |
| 144 | +} |
| 145 | +"); |
| 146 | + |
| 147 | + DiagnosticResult expected = BuildDiagnostic().WithLocation(0).WithArguments("Run"); |
| 148 | + |
| 149 | + await VerifyCS.VerifyDurableTaskAnalyzerAsync(code, expected); |
| 150 | + } |
| 151 | + |
| 152 | + [Fact] |
| 153 | + public async Task MultipleGetInputCallsHaveMultipleDiags() |
| 154 | + { |
| 155 | + string code = Wrapper.WrapDurableFunctionOrchestration(@" |
| 156 | +[Function(""Run"")] |
| 157 | +int Run([OrchestrationTrigger] TaskOrchestrationContext context) |
| 158 | +{ |
| 159 | + int input1 = {|#0:context.GetInput<int>()|}; |
| 160 | + int input2 = {|#1:context.GetInput<int>()|}; |
| 161 | + return input1 + input2; |
| 162 | +} |
| 163 | +"); |
| 164 | + |
| 165 | + DiagnosticResult expected1 = BuildDiagnostic().WithLocation(0).WithArguments("Run"); |
| 166 | + DiagnosticResult expected2 = BuildDiagnostic().WithLocation(1).WithArguments("Run"); |
| 167 | + |
| 168 | + await VerifyCS.VerifyDurableTaskAnalyzerAsync(code, expected1, expected2); |
| 169 | + } |
| 170 | + |
| 171 | + static DiagnosticResult BuildDiagnostic() |
| 172 | + { |
| 173 | + return VerifyCS.Diagnostic(GetInputOrchestrationAnalyzer.DiagnosticId); |
| 174 | + } |
| 175 | +} |
0 commit comments